Files
Seele/src/Engine/Graphics/RenderPass/DepthPrepass.cpp
T

176 lines
6.8 KiB
C++
Raw Normal View History

2021-05-06 17:02:10 +02:00
#include "DepthPrepass.h"
#include "Graphics/Graphics.h"
#include "Window/Window.h"
#include "Scene/Components/CameraComponent.h"
#include "Scene/Actor/CameraActor.h"
#include "Math/Vector.h"
#include "RenderGraph.h"
using namespace Seele;
2021-10-01 19:55:04 +02:00
DepthPrepassMeshProcessor::DepthPrepassMeshProcessor(Gfx::PViewport viewport, Gfx::PGraphics graphics)
: MeshProcessor(graphics)
2021-05-06 17:02:10 +02:00
, target(viewport)
{
}
DepthPrepassMeshProcessor::~DepthPrepassMeshProcessor()
{
}
2022-01-12 14:40:26 +01:00
Job DepthPrepassMeshProcessor::processMeshBatch(
2021-05-06 17:02:10 +02:00
const MeshBatch& batch,
// const PPrimitiveComponent primitiveComponent,
2021-11-24 12:10:23 +01:00
const Gfx::PRenderPass& renderPass,
2022-01-12 14:40:26 +01:00
Gfx::PPipelineLayout baseLayout,
2021-05-06 17:02:10 +02:00
Gfx::PDescriptorLayout primitiveLayout,
2021-11-24 12:10:23 +01:00
Array<Gfx::PDescriptorSet> descriptorSets,
2021-05-06 17:02:10 +02:00
int32 /*staticMeshId*/)
{
2021-12-27 15:04:53 +01:00
//std::cout << "Depth job started" << std::endl;
2021-11-13 19:28:18 +01:00
PMaterialAsset material = batch.material;
2021-05-06 17:02:10 +02:00
//const Gfx::MaterialShadingModel shadingModel = material->getShadingModel();
const PVertexShaderInput vertexInput = batch.vertexInput;
2021-10-19 23:04:38 +02:00
const Gfx::ShaderCollection* collection = material->getShaders(Gfx::RenderPassType::DepthPrepass, vertexInput->getType());
2021-05-06 17:02:10 +02:00
assert(collection != nullptr);
2021-11-13 19:28:18 +01:00
Gfx::PRenderCommand renderCommand = graphics->createRenderCommand();
renderCommand->setViewport(target);
2022-01-12 14:40:26 +01:00
Gfx::PPipelineLayout pipelineLayout = graphics->createPipelineLayout(baseLayout);
2021-11-13 19:28:18 +01:00
pipelineLayout->addDescriptorLayout(DepthPrepass::INDEX_MATERIAL, material->getDescriptorLayout());
pipelineLayout->create();
Gfx::PDescriptorSet materialSet = material->createDescriptorSet();
descriptorSets[DepthPrepass::INDEX_MATERIAL] = materialSet;
2021-05-06 17:02:10 +02:00
for(uint32 i = 0; i < batch.elements.size(); ++i)
2021-11-13 19:28:18 +01:00
{
2021-05-10 23:57:55 +02:00
Gfx::PDescriptorSet descriptorSet = primitiveLayout->allocateDescriptorSet();
2021-05-06 17:02:10 +02:00
descriptorSet->updateBuffer(0, batch.elements[i].uniformBuffer);
descriptorSet->writeChanges();
2021-11-13 19:28:18 +01:00
descriptorSets[DepthPrepass::INDEX_SCENE_DATA] = descriptorSet;
2021-05-06 17:02:10 +02:00
buildMeshDrawCommand(batch,
// primitiveComponent,
renderPass,
pipelineLayout,
renderCommand,
descriptorSets,
collection->vertexShader,
collection->controlShader,
collection->evalutionShader,
collection->geometryShader,
collection->fragmentShader,
true);
}
2022-01-12 14:40:26 +01:00
std::scoped_lock lock(commandLock);
2021-05-06 17:02:10 +02:00
renderCommands.add(renderCommand);
2021-12-27 15:04:53 +01:00
//std::cout << "Finished depth job" << std::endl;
2022-01-12 14:40:26 +01:00
co_return;
2021-05-06 17:02:10 +02:00
}
2021-10-01 19:55:04 +02:00
DepthPrepass::DepthPrepass(Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source)
: RenderPass(graphics, viewport)
, processor(new DepthPrepassMeshProcessor(viewport, graphics))
2021-05-06 17:02:10 +02:00
, descriptorSets(3)
, source(source->getCameraComponent())
{
UniformBufferCreateInfo uniformInitializer;
depthPrepassLayout = graphics->createPipelineLayout();
viewLayout = graphics->createDescriptorLayout("ViewLayout");
viewLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uniformInitializer.resourceData.size = sizeof(ViewParameter);
uniformInitializer.resourceData.data = (uint8*)&viewParams;
uniformInitializer.bDynamic = true;
viewParamBuffer = graphics->createUniformBuffer(uniformInitializer);
viewLayout->create();
depthPrepassLayout->addDescriptorLayout(INDEX_VIEW_PARAMS, viewLayout);
primitiveLayout = graphics->createDescriptorLayout("PrimitiveLayout");
primitiveLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
primitiveLayout->create();
depthPrepassLayout->addDescriptorLayout(INDEX_SCENE_DATA, primitiveLayout);
}
DepthPrepass::~DepthPrepass()
{
}
2021-12-09 12:38:02 +01:00
MainJob DepthPrepass::beginFrame()
2021-05-06 17:02:10 +02:00
{
processor->clearCommands();
primitiveLayout->reset();
BulkResourceData uniformUpdate;
viewParams.viewMatrix = source->getViewMatrix();
viewParams.projectionMatrix = source->getProjectionMatrix();
viewParams.cameraPosition = Vector4(source->getCameraPosition(), 0);
2021-05-10 23:57:55 +02:00
viewParams.inverseProjectionMatrix = glm::inverse(viewParams.projectionMatrix);
viewParams.screenDimensions = Vector2(static_cast<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
2021-05-06 17:02:10 +02:00
uniformUpdate.size = sizeof(ViewParameter);
uniformUpdate.data = (uint8*)&viewParams;
viewParamBuffer->updateContents(uniformUpdate);
viewLayout->reset();
descriptorSets[INDEX_VIEW_PARAMS] = viewLayout->allocateDescriptorSet();
2021-05-06 17:02:10 +02:00
descriptorSets[INDEX_VIEW_PARAMS]->updateBuffer(0, viewParamBuffer);
descriptorSets[INDEX_VIEW_PARAMS]->writeChanges();
2022-02-24 22:38:26 +01:00
//std::cout << "DepthPrepass beginFrame()" << std::endl;
2021-12-09 12:38:02 +01:00
co_return;
2021-05-06 17:02:10 +02:00
}
2021-12-02 13:00:03 +01:00
MainJob DepthPrepass::render()
2021-05-06 17:02:10 +02:00
{
2021-05-10 23:57:55 +02:00
depthAttachment->getTexture()->pipelineBarrier(
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
depthAttachment->getTexture()->transferOwnership(Gfx::QueueType::GRAPHICS);
2021-05-06 17:02:10 +02:00
graphics->beginRenderPass(renderPass);
2021-11-24 12:10:23 +01:00
List<Job> jobs;
2021-10-15 23:12:29 +02:00
for (auto &&meshBatch : passData.staticDrawList)
2021-05-06 17:02:10 +02:00
{
2021-12-27 15:04:53 +01:00
//jobs.add(
2022-01-12 14:40:26 +01:00
co_await processor->processMeshBatch(meshBatch, renderPass, depthPrepassLayout, primitiveLayout, descriptorSets);
2021-11-24 12:10:23 +01:00
}
2021-12-27 15:04:53 +01:00
//co_await Job::all(jobs);
2021-05-06 17:02:10 +02:00
graphics->executeCommands(processor->getRenderCommands());
graphics->endRenderPass();
2022-02-24 22:38:26 +01:00
//std::cout << "DepthPrepass render()" << std::endl;
2021-12-22 11:42:07 +01:00
co_return;
2021-05-06 17:02:10 +02:00
}
2021-12-09 12:38:02 +01:00
MainJob DepthPrepass::endFrame()
2021-05-06 17:02:10 +02:00
{
2022-02-24 22:38:26 +01:00
//std::cout << "DepthPrepass endFrame()" << std::endl;
2021-12-09 12:38:02 +01:00
co_return;
2021-05-06 17:02:10 +02:00
}
void DepthPrepass::publishOutputs()
{
TextureCreateInfo depthBufferInfo;
2021-09-23 10:10:39 +02:00
// If we render to a part of an image, the depth buffer itself must
// still match the size of the whole image or their coordinate systems go out of sync
depthBufferInfo.width = viewport->getOwner()->getSizeX();
depthBufferInfo.height = viewport->getOwner()->getSizeY();
2021-05-06 17:02:10 +02:00
depthBufferInfo.format = Gfx::SE_FORMAT_D32_SFLOAT;
depthBufferInfo.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
depthBuffer = graphics->createTexture2D(depthBufferInfo);
depthAttachment =
new Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
depthAttachment->clear.depthStencil.depth = 1.0f;
2021-10-01 19:55:04 +02:00
resources->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment);
2021-05-06 17:02:10 +02:00
}
void DepthPrepass::createRenderPass()
{
Gfx::PRenderTargetLayout layout = new Gfx::RenderTargetLayout(depthAttachment);
2021-09-23 10:10:39 +02:00
renderPass = graphics->createRenderPass(layout, viewport);
2021-05-06 17:02:10 +02:00
}
void DepthPrepass::modifyRenderPassMacros(Map<const char*, const char*>& defines)
{
defines["INDEX_VIEW_PARAMS"] = "0";
defines["INDEX_MATERIAL"] = "1";
defines["INDEX_SCENE_DATA"] = "2";
}