2020-08-06 00:54:43 +02:00
|
|
|
#include "BasePass.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
|
|
|
|
#include "Graphics/Window.h"
|
2020-10-03 11:00:10 +02:00
|
|
|
#include "Scene/Components/CameraComponent.h"
|
|
|
|
|
#include "Scene/Actor/CameraActor.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,
|
2020-08-06 00:54:43 +02:00
|
|
|
int32 staticMeshId)
|
|
|
|
|
{
|
2020-10-03 11:00:10 +02:00
|
|
|
const PMaterialAsset material = batch.material;
|
2020-08-06 00:54:43 +02:00
|
|
|
const Gfx::MaterialShadingModel shadingModel = material->getShadingModel();
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
Gfx::PDescriptorSet descriptorSet = primitiveLayout->allocatedDescriptorSet();
|
|
|
|
|
descriptorSet->updateBuffer(0, batch.elements[i].uniformBuffer);
|
|
|
|
|
descriptorSet->writeChanges();
|
|
|
|
|
cachedPrimitiveSets.add(descriptorSet);
|
|
|
|
|
}
|
2020-10-30 18:45:35 +01:00
|
|
|
Gfx::PRenderCommand renderCommand;
|
|
|
|
|
if (cachedCommandBuffers.size() > 0)
|
|
|
|
|
{
|
|
|
|
|
renderCommand = cachedCommandBuffers.back();
|
|
|
|
|
cachedCommandBuffers.pop();
|
|
|
|
|
renderCommand->begin();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
renderCommand = graphics->createRenderCommand();
|
|
|
|
|
renderCommand->begin();
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
renderCommand->setViewport(target);
|
|
|
|
|
for(uint32 i = 0; i < batch.elements.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
pipelineLayout->addDescriptorLayout(2, material->getRenderMaterial()->getDescriptorLayout());
|
|
|
|
|
pipelineLayout->create();
|
|
|
|
|
descriptorSets[2] = material->getDescriptor();
|
2020-10-14 01:49:43 +02:00
|
|
|
descriptorSets[3] = 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()
|
|
|
|
|
{
|
2020-10-30 18:45:35 +01:00
|
|
|
cachedCommandBuffers = renderCommands;
|
2020-08-06 00:54:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
BasePass::BasePass(const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source)
|
|
|
|
|
: processor(new BasePassMeshProcessor(scene, viewport, graphics, false))
|
2020-08-06 00:54:43 +02:00
|
|
|
, scene(scene)
|
|
|
|
|
, graphics(graphics)
|
2020-10-03 11:00:10 +02:00
|
|
|
, viewport(viewport)
|
|
|
|
|
, descriptorSets(4)
|
|
|
|
|
, source(source->getCameraComponent())
|
2020-08-06 00:54:43 +02:00
|
|
|
{
|
|
|
|
|
Gfx::PRenderTargetAttachment colorAttachment = new Gfx::SwapchainAttachment(viewport->getOwner());
|
|
|
|
|
TextureCreateInfo depthBufferInfo;
|
|
|
|
|
depthBufferInfo.width = viewport->getSizeX();
|
|
|
|
|
depthBufferInfo.height = viewport->getSizeY();
|
|
|
|
|
depthBufferInfo.format = Gfx::SE_FORMAT_D32_SFLOAT;
|
|
|
|
|
depthBufferInfo.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
|
|
|
|
|
depthBuffer = graphics->createTexture2D(depthBufferInfo);
|
|
|
|
|
Gfx::PRenderTargetAttachment depthAttachment =
|
2020-10-23 01:47:56 +02:00
|
|
|
new Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
|
|
|
|
depthAttachment->clear.depthStencil.depth = 1.0f;
|
2020-08-06 00:54:43 +02:00
|
|
|
Gfx::PRenderTargetLayout layout = new Gfx::RenderTargetLayout(colorAttachment, depthAttachment);
|
|
|
|
|
renderPass = graphics->createRenderPass(layout);
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2020-11-03 01:18:30 +01:00
|
|
|
UniformBufferCreateInfo uniformInitializer;
|
2020-10-03 11:00:10 +02:00
|
|
|
|
|
|
|
|
basePassLayout = graphics->createPipelineLayout();
|
|
|
|
|
|
|
|
|
|
lightLayout = graphics->createDescriptorLayout();
|
|
|
|
|
lightLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
2020-11-03 01:18:30 +01:00
|
|
|
uniformInitializer.resourceData.size = sizeof(LightEnv);
|
|
|
|
|
uniformInitializer.resourceData.data = (uint8*)&scene->getLightEnvironment();
|
|
|
|
|
uniformInitializer.bDynamic = true;
|
2020-10-03 11:00:10 +02:00
|
|
|
lightUniform = graphics->createUniformBuffer(uniformInitializer);
|
|
|
|
|
lightLayout->create();
|
|
|
|
|
basePassLayout->addDescriptorLayout(0, lightLayout);
|
|
|
|
|
descriptorSets[0] = lightLayout->allocatedDescriptorSet();
|
|
|
|
|
|
|
|
|
|
viewLayout = graphics->createDescriptorLayout();
|
|
|
|
|
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->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
2020-11-03 01:18:30 +01:00
|
|
|
uniformInitializer.resourceData.size = sizeof(ScreenToViewParameter);
|
|
|
|
|
uniformInitializer.resourceData.data = (uint8*)&screenToViewParams;
|
|
|
|
|
uniformInitializer.bDynamic = true;
|
2020-10-03 11:00:10 +02:00
|
|
|
screenToViewParamBuffer = graphics->createUniformBuffer(uniformInitializer);
|
|
|
|
|
viewLayout->create();
|
|
|
|
|
basePassLayout->addDescriptorLayout(1, viewLayout);
|
|
|
|
|
descriptorSets[1] = viewLayout->allocatedDescriptorSet();
|
|
|
|
|
|
|
|
|
|
primitiveLayout = graphics->createDescriptorLayout();
|
|
|
|
|
primitiveLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
|
|
|
|
primitiveLayout->create();
|
|
|
|
|
basePassLayout->addDescriptorLayout(3, 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
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
void BasePass::beginFrame()
|
|
|
|
|
{
|
|
|
|
|
processor->clearCommands();
|
|
|
|
|
primitiveLayout->reset();
|
|
|
|
|
BulkResourceData uniformUpdate;
|
|
|
|
|
uniformUpdate.size = sizeof(LightEnv);
|
|
|
|
|
uniformUpdate.data = (uint8*)&scene->getLightEnvironment();
|
|
|
|
|
lightUniform->updateContents(uniformUpdate);
|
|
|
|
|
descriptorSets[0]->updateBuffer(0, lightUniform);
|
|
|
|
|
descriptorSets[0]->writeChanges();
|
|
|
|
|
|
|
|
|
|
viewParams.viewMatrix = source->getViewMatrix();
|
|
|
|
|
viewParams.projectionMatrix = source->getProjectionMatrix();
|
|
|
|
|
viewParams.cameraPosition = Vector4(source->getTransform().getPosition(), 0);
|
|
|
|
|
screenToViewParams.inverseProjectionMatrix = glm::inverse(viewParams.projectionMatrix);
|
|
|
|
|
screenToViewParams.screenDimensions = Vector2(static_cast<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
|
|
|
|
|
uniformUpdate.size = sizeof(ViewParameter);
|
|
|
|
|
uniformUpdate.data = (uint8*)&viewParams;
|
|
|
|
|
viewParamBuffer->updateContents(uniformUpdate);
|
|
|
|
|
uniformUpdate.size = sizeof(ScreenToViewParameter);
|
|
|
|
|
uniformUpdate.data = (uint8*)&screenToViewParams;
|
|
|
|
|
screenToViewParamBuffer->updateContents(uniformUpdate);
|
|
|
|
|
descriptorSets[1]->updateBuffer(0, viewParamBuffer);
|
|
|
|
|
descriptorSets[1]->updateBuffer(1, screenToViewParamBuffer);
|
|
|
|
|
descriptorSets[1]->writeChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 00:54:43 +02:00
|
|
|
void BasePass::render()
|
|
|
|
|
{
|
|
|
|
|
graphics->beginRenderPass(renderPass);
|
2020-09-19 14:36:50 +02:00
|
|
|
for (auto &&primitive : scene->getStaticMeshes())
|
2020-08-06 00:54:43 +02:00
|
|
|
{
|
2020-10-03 11:00:10 +02:00
|
|
|
processor->addMeshBatch(primitive, renderPass, basePassLayout, primitiveLayout, descriptorSets);
|
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()
|
|
|
|
|
{
|
|
|
|
|
}
|