2020-08-06 00:54:43 +02:00
|
|
|
#include "BasePass.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
2021-01-19 15:30:00 +01:00
|
|
|
#include "Window/Window.h"
|
2020-10-03 11:00:10 +02:00
|
|
|
#include "Scene/Components/CameraComponent.h"
|
|
|
|
|
#include "Scene/Actor/CameraActor.h"
|
2021-01-19 15:30:00 +01:00
|
|
|
#include "Math/Vector.h"
|
2021-05-06 17:02:10 +02:00
|
|
|
#include "RenderGraph.h"
|
2020-08-06 00:54:43 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
BasePassMeshProcessor::BasePassMeshProcessor(const PScene scene, Gfx::PViewport viewport, Gfx::PGraphics graphics, uint8 translucentBasePass)
|
2020-08-06 00:54:43 +02:00
|
|
|
: MeshProcessor(scene, graphics)
|
2020-10-03 11:00:10 +02:00
|
|
|
, target(viewport)
|
2020-08-06 00:54:43 +02:00
|
|
|
, translucentBasePass(translucentBasePass)
|
2020-10-14 01:49:43 +02:00
|
|
|
, cachedPrimitiveIndex(0)
|
2020-08-06 00:54:43 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BasePassMeshProcessor::~BasePassMeshProcessor()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasePassMeshProcessor::addMeshBatch(
|
|
|
|
|
const MeshBatch& batch,
|
2020-10-03 11:00:10 +02:00
|
|
|
// const PPrimitiveComponent primitiveComponent,
|
2020-08-06 00:54:43 +02:00
|
|
|
const Gfx::PRenderPass renderPass,
|
2020-10-03 11:00:10 +02:00
|
|
|
Gfx::PPipelineLayout pipelineLayout,
|
|
|
|
|
Gfx::PDescriptorLayout primitiveLayout,
|
2020-10-14 01:49:43 +02:00
|
|
|
Array<Gfx::PDescriptorSet>& descriptorSets,
|
2021-04-01 16:40:14 +02:00
|
|
|
int32 /*staticMeshId*/)
|
2020-08-06 00:54:43 +02:00
|
|
|
{
|
2020-10-03 11:00:10 +02:00
|
|
|
const PMaterialAsset material = batch.material;
|
2021-04-01 16:40:14 +02:00
|
|
|
//const Gfx::MaterialShadingModel shadingModel = material->getShadingModel();
|
2020-08-06 00:54:43 +02:00
|
|
|
|
|
|
|
|
const PVertexShaderInput vertexInput = batch.vertexInput;
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
const Gfx::ShaderCollection* collection = material->getRenderMaterial()->getShaders(Gfx::RenderPassType::BasePass, vertexInput->getType());
|
2020-09-19 14:36:50 +02:00
|
|
|
assert(collection != nullptr);
|
2020-10-03 11:00:10 +02:00
|
|
|
for(uint32 i = 0; i < batch.elements.size(); ++i)
|
|
|
|
|
{
|
2021-05-10 23:57:55 +02:00
|
|
|
Gfx::PDescriptorSet descriptorSet = primitiveLayout->allocateDescriptorSet();
|
2020-10-03 11:00:10 +02:00
|
|
|
descriptorSet->updateBuffer(0, batch.elements[i].uniformBuffer);
|
|
|
|
|
descriptorSet->writeChanges();
|
|
|
|
|
cachedPrimitiveSets.add(descriptorSet);
|
|
|
|
|
}
|
2021-04-13 23:09:16 +02:00
|
|
|
Gfx::PRenderCommand renderCommand = graphics->createRenderCommand();
|
2020-10-03 11:00:10 +02:00
|
|
|
renderCommand->setViewport(target);
|
|
|
|
|
for(uint32 i = 0; i < batch.elements.size(); ++i)
|
|
|
|
|
{
|
2021-05-06 17:02:10 +02:00
|
|
|
pipelineLayout->addDescriptorLayout(BasePass::INDEX_MATERIAL, material->getRenderMaterial()->getDescriptorLayout());
|
2020-10-03 11:00:10 +02:00
|
|
|
pipelineLayout->create();
|
2021-05-06 17:02:10 +02:00
|
|
|
descriptorSets[BasePass::INDEX_MATERIAL] = material->getDescriptor();
|
|
|
|
|
descriptorSets[BasePass::INDEX_SCENE_DATA] = cachedPrimitiveSets[cachedPrimitiveIndex++];
|
2020-10-03 11:00:10 +02:00
|
|
|
buildMeshDrawCommand(batch,
|
|
|
|
|
// primitiveComponent,
|
|
|
|
|
renderPass,
|
|
|
|
|
pipelineLayout,
|
|
|
|
|
renderCommand,
|
|
|
|
|
descriptorSets,
|
|
|
|
|
collection->vertexShader,
|
|
|
|
|
collection->controlShader,
|
|
|
|
|
collection->evalutionShader,
|
|
|
|
|
collection->geometryShader,
|
|
|
|
|
collection->fragmentShader,
|
|
|
|
|
false);
|
|
|
|
|
}
|
2020-08-06 00:54:43 +02:00
|
|
|
renderCommands.add(renderCommand);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array<Gfx::PRenderCommand> BasePassMeshProcessor::getRenderCommands()
|
|
|
|
|
{
|
|
|
|
|
return renderCommands;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasePassMeshProcessor::clearCommands()
|
|
|
|
|
{
|
|
|
|
|
renderCommands.clear();
|
2020-10-03 11:00:10 +02:00
|
|
|
cachedPrimitiveSets.clear();
|
2020-10-14 01:49:43 +02:00
|
|
|
cachedPrimitiveIndex = 0;
|
2020-08-06 00:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-06 17:02:10 +02:00
|
|
|
BasePass::BasePass(PRenderGraph renderGraph, const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source)
|
2021-09-23 10:10:39 +02:00
|
|
|
: RenderPass(renderGraph, graphics, viewport)
|
2021-05-06 17:02:10 +02:00
|
|
|
, processor(new BasePassMeshProcessor(scene, viewport, graphics, false))
|
2020-10-03 11:00:10 +02:00
|
|
|
, descriptorSets(4)
|
|
|
|
|
, source(source->getCameraComponent())
|
2020-08-06 00:54:43 +02:00
|
|
|
{
|
2020-11-03 01:18:30 +01:00
|
|
|
UniformBufferCreateInfo uniformInitializer;
|
2020-10-03 11:00:10 +02:00
|
|
|
basePassLayout = graphics->createPipelineLayout();
|
|
|
|
|
|
2021-05-06 17:02:10 +02:00
|
|
|
lightLayout = graphics->createDescriptorLayout("LightLayout");
|
2021-06-12 18:51:29 +02:00
|
|
|
|
|
|
|
|
// Directional Lights
|
|
|
|
|
lightLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
|
|
|
|
lightLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
|
|
|
|
// Point Lights
|
|
|
|
|
lightLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
|
|
|
|
lightLayout->addDescriptorBinding(3, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
|
|
|
|
// Light Index List
|
|
|
|
|
lightLayout->addDescriptorBinding(4, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
|
|
|
|
// Light Grid
|
|
|
|
|
lightLayout->addDescriptorBinding(5, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE);
|
2020-10-03 11:00:10 +02:00
|
|
|
lightLayout->create();
|
2021-05-06 17:02:10 +02:00
|
|
|
basePassLayout->addDescriptorLayout(INDEX_LIGHT_ENV, lightLayout);
|
2021-05-10 23:57:55 +02:00
|
|
|
descriptorSets[INDEX_LIGHT_ENV] = lightLayout->allocateDescriptorSet();
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-05-06 17:02:10 +02:00
|
|
|
viewLayout = graphics->createDescriptorLayout("ViewLayout");
|
2020-10-03 11:00:10 +02:00
|
|
|
viewLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
2020-11-03 01:18:30 +01:00
|
|
|
uniformInitializer.resourceData.size = sizeof(ViewParameter);
|
|
|
|
|
uniformInitializer.resourceData.data = (uint8*)&viewParams;
|
|
|
|
|
uniformInitializer.bDynamic = true;
|
2020-10-03 11:00:10 +02:00
|
|
|
viewParamBuffer = graphics->createUniformBuffer(uniformInitializer);
|
|
|
|
|
viewLayout->create();
|
2021-05-06 17:02:10 +02:00
|
|
|
basePassLayout->addDescriptorLayout(INDEX_VIEW_PARAMS, viewLayout);
|
2021-05-10 23:57:55 +02:00
|
|
|
descriptorSets[INDEX_VIEW_PARAMS] = viewLayout->allocateDescriptorSet();
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-05-06 17:02:10 +02:00
|
|
|
primitiveLayout = graphics->createDescriptorLayout("PrimitiveLayout");
|
2020-10-03 11:00:10 +02:00
|
|
|
primitiveLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
|
|
|
|
primitiveLayout->create();
|
2021-05-06 17:02:10 +02:00
|
|
|
basePassLayout->addDescriptorLayout(INDEX_SCENE_DATA, primitiveLayout);
|
2020-08-06 00:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
BasePass::~BasePass()
|
2020-08-06 00:54:43 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 22:12:56 +02:00
|
|
|
void BasePass::updateViewFrame(PViewFrame viewFrame)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
void BasePass::beginFrame()
|
|
|
|
|
{
|
|
|
|
|
processor->clearCommands();
|
|
|
|
|
primitiveLayout->reset();
|
|
|
|
|
BulkResourceData uniformUpdate;
|
|
|
|
|
|
|
|
|
|
viewParams.viewMatrix = source->getViewMatrix();
|
|
|
|
|
viewParams.projectionMatrix = source->getProjectionMatrix();
|
2021-01-19 15:30:00 +01:00
|
|
|
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()));
|
2020-10-03 11:00:10 +02:00
|
|
|
uniformUpdate.size = sizeof(ViewParameter);
|
|
|
|
|
uniformUpdate.data = (uint8*)&viewParams;
|
|
|
|
|
viewParamBuffer->updateContents(uniformUpdate);
|
2021-06-04 18:27:49 +02:00
|
|
|
viewLayout->reset();
|
|
|
|
|
lightLayout->reset();
|
|
|
|
|
descriptorSets[INDEX_LIGHT_ENV] = lightLayout->allocateDescriptorSet();
|
|
|
|
|
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();
|
2021-09-29 22:12:56 +02:00
|
|
|
/*for(auto &&meshBatch : scene->getStaticMeshes())
|
2021-04-25 23:43:40 +02:00
|
|
|
{
|
|
|
|
|
meshBatch.material->updateDescriptorData();
|
2021-09-29 22:12:56 +02:00
|
|
|
}*/
|
2020-10-03 11:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 00:54:43 +02:00
|
|
|
void BasePass::render()
|
|
|
|
|
{
|
2021-05-10 23:57:55 +02:00
|
|
|
|
|
|
|
|
oLightIndexList->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
|
|
|
|
oLightGrid->pipelineBarrier(
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
|
|
|
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
2021-06-04 18:27:49 +02:00
|
|
|
|
2021-06-12 18:51:29 +02:00
|
|
|
descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(0, directLightBuffer);
|
|
|
|
|
descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(1, numDirLightBuffer);
|
|
|
|
|
descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(2, pointLightBuffer);
|
|
|
|
|
descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(3, numPointLightBuffer);
|
|
|
|
|
descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(4, oLightIndexList);
|
|
|
|
|
descriptorSets[INDEX_LIGHT_ENV]->updateTexture(5, oLightGrid);
|
2021-05-10 23:57:55 +02:00
|
|
|
descriptorSets[INDEX_LIGHT_ENV]->writeChanges();
|
2020-08-06 00:54:43 +02:00
|
|
|
graphics->beginRenderPass(renderPass);
|
2021-09-29 22:12:56 +02:00
|
|
|
/*for (auto &&meshBatch : scene->getStaticMeshes())
|
2020-08-06 00:54:43 +02:00
|
|
|
{
|
2021-04-25 23:43:40 +02:00
|
|
|
processor->addMeshBatch(meshBatch, renderPass, basePassLayout, primitiveLayout, descriptorSets);
|
2021-09-29 22:12:56 +02:00
|
|
|
}*/
|
2020-08-06 00:54:43 +02:00
|
|
|
graphics->executeCommands(processor->getRenderCommands());
|
|
|
|
|
graphics->endRenderPass();
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
|
|
|
|
void BasePass::endFrame()
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-05-06 17:02:10 +02:00
|
|
|
|
|
|
|
|
void BasePass::publishOutputs()
|
|
|
|
|
{
|
2021-06-12 18:51:29 +02:00
|
|
|
colorAttachment = new Gfx::SwapchainAttachment(viewport->getOwner());
|
2021-05-06 17:02:10 +02:00
|
|
|
renderGraph->registerRenderPassOutput("BASEPASS_COLOR", colorAttachment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasePass::createRenderPass()
|
2021-06-12 18:51:29 +02:00
|
|
|
{
|
|
|
|
|
directLightBuffer = renderGraph->requestBuffer("DIRECTIONAL_LIGHTS");
|
|
|
|
|
pointLightBuffer = renderGraph->requestBuffer("POINT_LIGHTS");
|
|
|
|
|
numDirLightBuffer = renderGraph->requestUniform("NUM_DIRECTIONAL_LIGHTS");
|
|
|
|
|
numPointLightBuffer = renderGraph->requestUniform("NUM_POINT_LIGHTS");
|
2021-05-06 17:02:10 +02:00
|
|
|
Gfx::PRenderTargetAttachment depthAttachment = renderGraph->requestRenderTarget("DEPTHPREPASS_DEPTH");
|
|
|
|
|
depthAttachment->loadOp = Gfx::SE_ATTACHMENT_LOAD_OP_LOAD;
|
|
|
|
|
Gfx::PRenderTargetLayout layout = new Gfx::RenderTargetLayout(colorAttachment, depthAttachment);
|
2021-09-23 10:10:39 +02:00
|
|
|
renderPass = graphics->createRenderPass(layout, viewport);
|
2021-05-10 23:57:55 +02:00
|
|
|
oLightIndexList = renderGraph->requestBuffer("LIGHTCULLING_OLIGHTLIST");
|
|
|
|
|
oLightGrid = renderGraph->requestTexture("LIGHTCULLING_OLIGHTGRID");
|
2021-05-06 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasePass::modifyRenderPassMacros(Map<const char*, const char*>& defines)
|
|
|
|
|
{
|
|
|
|
|
defines["INDEX_LIGHT_ENV"] = "0";
|
|
|
|
|
defines["INDEX_VIEW_PARAMS"] = "1";
|
|
|
|
|
defines["INDEX_MATERIAL"] = "2";
|
|
|
|
|
defines["INDEX_SCENE_DATA"] = "3";
|
|
|
|
|
}
|