2021-09-23 10:10:39 +02:00
|
|
|
#include "UIPass.h"
|
|
|
|
|
#include "RenderGraph.h"
|
|
|
|
|
#include "Graphics/Graphics.h"
|
2023-10-26 18:37:29 +02:00
|
|
|
#include "Graphics/RenderTarget.h"
|
2021-09-23 10:10:39 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
|
2023-10-26 18:37:29 +02:00
|
|
|
UIPass::UIPass(Gfx::PGraphics graphics, PScene scene)
|
|
|
|
|
: RenderPass(graphics, scene)
|
2021-09-23 10:10:39 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UIPass::~UIPass()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
void UIPass::beginFrame(const Component::Camera& cam)
|
2021-09-23 10:10:39 +02:00
|
|
|
{
|
2023-11-05 10:36:01 +01:00
|
|
|
RenderPass::beginFrame(cam);
|
2022-04-18 18:08:38 +02:00
|
|
|
VertexBufferCreateInfo info = {
|
2023-11-01 23:12:30 +01:00
|
|
|
.sourceData = {
|
2023-10-26 18:37:29 +02:00
|
|
|
.size = (uint32)(sizeof(UI::RenderElementStyle) * renderElements.size()),
|
|
|
|
|
.data = (uint8*)renderElements.data()
|
2022-04-18 18:08:38 +02:00
|
|
|
},
|
|
|
|
|
.vertexSize = sizeof(UI::RenderElementStyle),
|
2023-10-26 18:37:29 +02:00
|
|
|
.numVertices = (uint32)renderElements.size(),
|
2022-04-18 18:08:38 +02:00
|
|
|
};
|
|
|
|
|
elementBuffer = graphics->createVertexBuffer(info);
|
2023-10-26 18:37:29 +02:00
|
|
|
uint32 numTextures = static_cast<uint32>(usedTextures.size());
|
2022-04-18 18:08:38 +02:00
|
|
|
numTexturesBuffer->updateContents({
|
|
|
|
|
.size = sizeof(uint32),
|
|
|
|
|
.data = (uint8*)&numTextures,
|
|
|
|
|
});
|
|
|
|
|
descriptorSet->updateBuffer(2, numTexturesBuffer);
|
2023-10-26 18:37:29 +02:00
|
|
|
descriptorSet->updateTextureArray(3, usedTextures);
|
2022-04-18 18:08:38 +02:00
|
|
|
descriptorSet->writeChanges();
|
2022-04-15 23:45:44 +02:00
|
|
|
//co_return;
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
void UIPass::render()
|
2021-09-23 10:10:39 +02:00
|
|
|
{
|
|
|
|
|
graphics->beginRenderPass(renderPass);
|
|
|
|
|
Gfx::PRenderCommand command = graphics->createRenderCommand("UIPassCommand");
|
|
|
|
|
command->setViewport(viewport);
|
|
|
|
|
command->bindPipeline(pipeline);
|
2023-10-26 18:37:29 +02:00
|
|
|
command->bindVertexBuffer({elementBuffer});
|
2022-04-18 18:08:38 +02:00
|
|
|
command->bindDescriptor(descriptorSet);
|
2023-10-26 18:37:29 +02:00
|
|
|
command->draw(4, static_cast<uint32>(renderElements.size()), 0, 0);
|
2021-09-23 10:10:39 +02:00
|
|
|
graphics->executeCommands(Array<Gfx::PRenderCommand>({command}));
|
|
|
|
|
graphics->endRenderPass();
|
2023-10-26 18:37:29 +02:00
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
//co_return;
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:45:44 +02:00
|
|
|
void UIPass::endFrame()
|
2021-09-23 10:10:39 +02:00
|
|
|
{
|
2022-04-15 23:45:44 +02:00
|
|
|
//co_return;
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UIPass::publishOutputs()
|
|
|
|
|
{
|
2022-11-17 16:47:42 +01:00
|
|
|
TextureCreateInfo depthBufferInfo = {
|
2023-11-15 00:06:00 +01:00
|
|
|
.width = viewport->getWidth(),
|
|
|
|
|
.height = viewport->getHeight(),
|
2022-11-17 16:47:42 +01:00
|
|
|
.format = Gfx::SE_FORMAT_D32_SFLOAT,
|
|
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
|
|
|
|
};
|
2021-09-23 10:10:39 +02:00
|
|
|
|
|
|
|
|
depthBuffer = graphics->createTexture2D(depthBufferInfo);
|
|
|
|
|
depthAttachment =
|
|
|
|
|
new Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
2022-11-17 16:47:42 +01:00
|
|
|
depthAttachment->clear.depthStencil.depth = 1.0f;
|
2021-10-01 19:55:04 +02:00
|
|
|
resources->registerRenderPassOutput("UIPASS_DEPTH", depthAttachment);
|
2022-11-17 16:47:42 +01:00
|
|
|
|
|
|
|
|
TextureCreateInfo colorBufferInfo = {
|
2023-11-15 00:06:00 +01:00
|
|
|
.width = viewport->getWidth(),
|
|
|
|
|
.height = viewport->getHeight(),
|
2022-11-17 16:47:42 +01:00
|
|
|
.format = Gfx::SE_FORMAT_R16G16B16A16_SFLOAT,
|
|
|
|
|
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
|
|
|
|
};
|
|
|
|
|
colorBuffer = graphics->createTexture2D(colorBufferInfo);
|
|
|
|
|
renderTarget =
|
|
|
|
|
new Gfx::RenderTargetAttachment(colorBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
|
|
|
|
|
renderTarget->clear.color = { 0.0f, 0.0f, 0.0f, 1.0f };
|
|
|
|
|
resources->registerRenderPassOutput("UIPASS_COLOR", renderTarget);
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UIPass::createRenderPass()
|
|
|
|
|
{
|
|
|
|
|
ShaderCreateInfo createInfo;
|
2022-11-15 12:19:11 +01:00
|
|
|
createInfo.mainModule = "UIPass";
|
2021-09-23 10:10:39 +02:00
|
|
|
createInfo.name = "UIVertex";
|
|
|
|
|
createInfo.entryPoint = "vertexMain";
|
|
|
|
|
vertexShader = graphics->createVertexShader(createInfo);
|
|
|
|
|
|
|
|
|
|
createInfo.name = "UIFragment";
|
|
|
|
|
createInfo.entryPoint = "fragmentMain";
|
|
|
|
|
fragmentShader = graphics->createFragmentShader(createInfo);
|
2022-04-18 18:08:38 +02:00
|
|
|
Array<Gfx::VertexElement> decl;
|
|
|
|
|
decl.add({
|
2023-10-26 18:37:29 +02:00
|
|
|
.binding = 0,
|
2022-04-18 18:08:38 +02:00
|
|
|
.offset = offsetof(UI::RenderElementStyle, position),
|
|
|
|
|
.vertexFormat = Gfx::SE_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
.attributeIndex = 0,
|
|
|
|
|
.stride = sizeof(UI::RenderElementStyle),
|
2023-11-05 10:36:01 +01:00
|
|
|
.instanced = 1
|
2022-04-18 18:08:38 +02:00
|
|
|
});
|
|
|
|
|
decl.add({
|
2023-10-26 18:37:29 +02:00
|
|
|
.binding = 0,
|
2022-04-18 18:08:38 +02:00
|
|
|
.offset = offsetof(UI::RenderElementStyle, backgroundImageIndex),
|
|
|
|
|
.vertexFormat = Gfx::SE_FORMAT_R32_UINT,
|
|
|
|
|
.attributeIndex = 1,
|
|
|
|
|
.stride = sizeof(UI::RenderElementStyle),
|
2023-11-05 10:36:01 +01:00
|
|
|
.instanced = 1
|
2022-04-18 18:08:38 +02:00
|
|
|
});
|
|
|
|
|
decl.add({
|
2023-10-26 18:37:29 +02:00
|
|
|
.binding = 0,
|
2022-04-18 18:08:38 +02:00
|
|
|
.offset = offsetof(UI::RenderElementStyle, backgroundColor),
|
|
|
|
|
.vertexFormat = Gfx::SE_FORMAT_R32G32B32_SFLOAT,
|
|
|
|
|
.attributeIndex = 2,
|
|
|
|
|
.stride = sizeof(UI::RenderElementStyle),
|
2023-11-05 10:36:01 +01:00
|
|
|
.instanced = 1
|
2022-04-18 18:08:38 +02:00
|
|
|
});
|
|
|
|
|
decl.add({
|
2023-10-26 18:37:29 +02:00
|
|
|
.binding = 0,
|
2022-04-18 18:08:38 +02:00
|
|
|
.offset = offsetof(UI::RenderElementStyle, opacity),
|
|
|
|
|
.vertexFormat = Gfx::SE_FORMAT_R32_SFLOAT,
|
|
|
|
|
.attributeIndex = 3,
|
|
|
|
|
.stride = sizeof(UI::RenderElementStyle),
|
2023-11-05 10:36:01 +01:00
|
|
|
.instanced = 1
|
2022-04-18 18:08:38 +02:00
|
|
|
});
|
|
|
|
|
decl.add({
|
2023-10-26 18:37:29 +02:00
|
|
|
.binding = 0,
|
2022-04-18 18:08:38 +02:00
|
|
|
.offset = offsetof(UI::RenderElementStyle, dimensions),
|
|
|
|
|
.vertexFormat = Gfx::SE_FORMAT_R32G32_SFLOAT,
|
|
|
|
|
.attributeIndex = 4,
|
|
|
|
|
.stride = sizeof(UI::RenderElementStyle),
|
2023-11-05 10:36:01 +01:00
|
|
|
.instanced = 1
|
2022-04-18 18:08:38 +02:00
|
|
|
});
|
|
|
|
|
declaration = graphics->createVertexDeclaration(decl);
|
|
|
|
|
|
|
|
|
|
descriptorLayout = graphics->createDescriptorLayout("UIDescriptorLayout");
|
|
|
|
|
descriptorLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
|
|
|
|
descriptorLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_SAMPLER);
|
|
|
|
|
descriptorLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
|
|
|
|
|
descriptorLayout->addDescriptorBinding(3, Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256, Gfx::SE_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT | Gfx::SE_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT);
|
|
|
|
|
descriptorLayout->create();
|
|
|
|
|
|
2023-01-21 18:43:21 +01:00
|
|
|
Matrix4 projectionMatrix = glm::ortho(0, 1, 1, 0);
|
2022-04-18 18:08:38 +02:00
|
|
|
UniformBufferCreateInfo info = {
|
2023-11-01 23:12:30 +01:00
|
|
|
.sourceData = {
|
2023-01-21 18:43:21 +01:00
|
|
|
.size = sizeof(Matrix4),
|
2022-04-18 18:08:38 +02:00
|
|
|
.data = (uint8*)&projectionMatrix,
|
|
|
|
|
},
|
2023-11-05 10:36:01 +01:00
|
|
|
.dynamic = false,
|
2022-04-18 18:08:38 +02:00
|
|
|
};
|
2023-11-08 23:27:21 +01:00
|
|
|
Gfx::OUniformBuffer uniformBuffer = graphics->createUniformBuffer(info);
|
|
|
|
|
Gfx::OSamplerState backgroundSampler = graphics->createSamplerState({});
|
2022-04-18 18:08:38 +02:00
|
|
|
|
|
|
|
|
info = {
|
2023-11-01 23:12:30 +01:00
|
|
|
.sourceData = {
|
2022-04-18 18:08:38 +02:00
|
|
|
.size = sizeof(uint32),
|
|
|
|
|
.data = nullptr
|
|
|
|
|
},
|
2023-11-05 10:36:01 +01:00
|
|
|
.dynamic = true,
|
2022-04-18 18:08:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
numTexturesBuffer = graphics->createUniformBuffer(info);
|
|
|
|
|
|
|
|
|
|
descriptorSet = descriptorLayout->allocateDescriptorSet();
|
|
|
|
|
descriptorSet->updateBuffer(0, uniformBuffer);
|
|
|
|
|
descriptorSet->updateSampler(1, backgroundSampler);
|
|
|
|
|
descriptorSet->writeChanges();
|
|
|
|
|
|
2023-11-09 22:15:51 +01:00
|
|
|
Gfx::OPipelineLayout pipelineLayout = graphics->createPipelineLayout();
|
2022-04-18 18:08:38 +02:00
|
|
|
pipelineLayout->addDescriptorLayout(0, descriptorLayout);
|
2021-09-23 10:10:39 +02:00
|
|
|
pipelineLayout->create();
|
|
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
Gfx::ORenderTargetLayout layout = new Gfx::RenderTargetLayout(std::move(renderTarget), std::move(depthAttachment));
|
|
|
|
|
renderPass = graphics->createRenderPass(std::move(layout), viewport);
|
2021-09-23 10:10:39 +02:00
|
|
|
|
2023-10-26 18:37:29 +02:00
|
|
|
Gfx::LegacyPipelineCreateInfo pipelineInfo;
|
2021-09-23 10:10:39 +02:00
|
|
|
pipelineInfo.vertexDeclaration = declaration;
|
|
|
|
|
pipelineInfo.vertexShader = vertexShader;
|
|
|
|
|
pipelineInfo.fragmentShader = fragmentShader;
|
|
|
|
|
pipelineInfo.renderPass = renderPass;
|
2023-11-09 22:15:51 +01:00
|
|
|
pipelineInfo.pipelineLayout = std::move(pipelineLayout);
|
2021-09-23 10:10:39 +02:00
|
|
|
pipelineInfo.rasterizationState.cullMode = Gfx::SE_CULL_MODE_NONE;
|
|
|
|
|
pipelineInfo.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
|
|
|
|
|
|
2023-11-09 22:15:51 +01:00
|
|
|
pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
|
2021-09-23 10:10:39 +02:00
|
|
|
}
|