ui rendering works now

This commit is contained in:
Dynamitos
2022-04-18 18:08:38 +02:00
parent 796271f334
commit 14b816fc3c
25 changed files with 453 additions and 60 deletions
+3 -3
View File
@@ -210,12 +210,12 @@ StructuredBuffer::~StructuredBuffer()
bool StructuredBuffer::updateContents(const BulkResourceData& resourceData)
{
assert(contents.size() == resourceData.size);
if(std::memcmp(contents.data(), resourceData.data, contents.size()) == 0)
assert(contents.size() >= resourceData.size);
if(std::memcmp(contents.data(), resourceData.data, resourceData.size) == 0)
{
return false;
}
std::memcpy(contents.data(), resourceData.data, contents.size());
std::memcpy(contents.data(), resourceData.data, resourceData.size);
return true;
}
VertexBuffer::VertexBuffer(QueueFamilyMapping mapping, uint32 numVertices, uint32 vertexSize, QueueType startQueueType)
+2 -2
View File
@@ -362,12 +362,12 @@ class VertexBuffer : public Buffer
public:
VertexBuffer(QueueFamilyMapping mapping, uint32 numVertices, uint32 vertexSize, QueueType startQueueType);
virtual ~VertexBuffer();
inline uint32 getNumVertices()
constexpr uint32 getNumVertices() const
{
return numVertices;
}
// Size of one vertex in bytes
inline uint32 getVertexSize()
constexpr uint32 getVertexSize() const
{
return vertexSize;
}
+10 -7
View File
@@ -21,7 +21,7 @@ void TextPass::beginFrame()
for(TextRender& render : passData.texts)
{
FontData& fontData = getFontData(render.font);
TextResources& resources = textResources.add();
TextResources& resources = textResources[render.font].add();
Array<GlyphInstanceData> instanceData;
float x = render.position.x;
float y = render.position.y;
@@ -67,16 +67,19 @@ void TextPass::render()
{
graphics->beginRenderPass(renderPass);
Array<Gfx::PRenderCommand> commands;
for(TextResources& resources : textResources)
for(const auto& [fontAsset, resources] : textResources)
{
Gfx::PRenderCommand command = graphics->createRenderCommand("TextPassCommand");
command->setViewport(viewport);
command->bindPipeline(pipeline);
command->bindDescriptor({generalSet, resources.textureArraySet});
command->bindVertexBuffer({VertexInputStream(0, 0, resources.vertexBuffer)});
command->pushConstants(pipelineLayout, Gfx::SE_SHADER_STAGE_VERTEX_BIT | Gfx::SE_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(TextData), &resources.textData);
command->draw(4, static_cast<uint32>(resources.vertexBuffer->getNumVertices()), 0, 0);
for(const auto& resource : resources)
{
command->bindDescriptor({generalSet, resource.textureArraySet});
command->bindVertexBuffer({VertexInputStream(0, 0, resource.vertexBuffer)});
command->pushConstants(pipelineLayout, Gfx::SE_SHADER_STAGE_VERTEX_BIT | Gfx::SE_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(TextData), &resource.textData);
command->draw(4, static_cast<uint32>(resource.vertexBuffer->getNumVertices()), 0, 0);
}
commands.add(command);
}
graphics->executeCommands(commands);
+1 -1
View File
@@ -65,7 +65,7 @@ private:
Gfx::PDescriptorSet textureArraySet;
TextData textData;
};
Array<TextResources> textResources;
std::map<PFontAsset, Array<TextResources>> textResources;
Gfx::PRenderTargetAttachment renderTarget;
Gfx::PRenderTargetAttachment depthAttachment;
+97 -2
View File
@@ -17,6 +17,23 @@ UIPass::~UIPass()
void UIPass::beginFrame()
{
VertexBufferCreateInfo info = {
.resourceData = {
.size = (uint32)(sizeof(UI::RenderElementStyle) * passData.renderElements.size()),
.data = (uint8*)passData.renderElements.data()
},
.vertexSize = sizeof(UI::RenderElementStyle),
.numVertices = (uint32)passData.renderElements.size(),
};
elementBuffer = graphics->createVertexBuffer(info);
uint32 numTextures = passData.usedTextures.size();
numTexturesBuffer->updateContents({
.size = sizeof(uint32),
.data = (uint8*)&numTextures,
});
descriptorSet->updateBuffer(2, numTexturesBuffer);
descriptorSet->updateTextureArray(3, passData.usedTextures);
descriptorSet->writeChanges();
//co_return;
}
@@ -26,7 +43,9 @@ void UIPass::render()
Gfx::PRenderCommand command = graphics->createRenderCommand("UIPassCommand");
command->setViewport(viewport);
command->bindPipeline(pipeline);
command->draw(4, 1, 0, 0);
command->bindVertexBuffer({VertexInputStream(0, 0, elementBuffer)});
command->bindDescriptor(descriptorSet);
command->draw(4, passData.renderElements.size(), 0, 0);
graphics->executeCommands(Array<Gfx::PRenderCommand>({command}));
graphics->endRenderPass();
//co_return;
@@ -72,8 +91,84 @@ void UIPass::createRenderPass()
createInfo.name = "UIFragment";
createInfo.entryPoint = "fragmentMain";
fragmentShader = graphics->createFragmentShader(createInfo);
declaration = graphics->createVertexDeclaration({});
Array<Gfx::VertexElement> decl;
decl.add({
.streamIndex = 0,
.offset = offsetof(UI::RenderElementStyle, position),
.vertexFormat = Gfx::SE_FORMAT_R32G32B32_SFLOAT,
.attributeIndex = 0,
.stride = sizeof(UI::RenderElementStyle),
.bInstanced = 1
});
decl.add({
.streamIndex = 0,
.offset = offsetof(UI::RenderElementStyle, backgroundImageIndex),
.vertexFormat = Gfx::SE_FORMAT_R32_UINT,
.attributeIndex = 1,
.stride = sizeof(UI::RenderElementStyle),
.bInstanced = 1
});
decl.add({
.streamIndex = 0,
.offset = offsetof(UI::RenderElementStyle, backgroundColor),
.vertexFormat = Gfx::SE_FORMAT_R32G32B32_SFLOAT,
.attributeIndex = 2,
.stride = sizeof(UI::RenderElementStyle),
.bInstanced = 1
});
decl.add({
.streamIndex = 0,
.offset = offsetof(UI::RenderElementStyle, opacity),
.vertexFormat = Gfx::SE_FORMAT_R32_SFLOAT,
.attributeIndex = 3,
.stride = sizeof(UI::RenderElementStyle),
.bInstanced = 1
});
decl.add({
.streamIndex = 0,
.offset = offsetof(UI::RenderElementStyle, dimensions),
.vertexFormat = Gfx::SE_FORMAT_R32G32_SFLOAT,
.attributeIndex = 4,
.stride = sizeof(UI::RenderElementStyle),
.bInstanced = 1
});
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();
Matrix4 projectionMatrix = glm::ortho(0, 1, 1, 0);
UniformBufferCreateInfo info = {
.resourceData = {
.size = sizeof(Matrix4),
.data = (uint8*)&projectionMatrix,
},
.bDynamic = false,
};
Gfx::PUniformBuffer uniformBuffer = graphics->createUniformBuffer(info);
Gfx::PSamplerState backgroundSampler = graphics->createSamplerState({});
info = {
.resourceData = {
.size = sizeof(uint32),
.data = nullptr
},
.bDynamic = true,
};
numTexturesBuffer = graphics->createUniformBuffer(info);
descriptorSet = descriptorLayout->allocateDescriptorSet();
descriptorSet->updateBuffer(0, uniformBuffer);
descriptorSet->updateSampler(1, backgroundSampler);
descriptorSet->writeChanges();
pipelineLayout = graphics->createPipelineLayout();
pipelineLayout->addDescriptorLayout(0, descriptorLayout);
pipelineLayout->create();
Gfx::PRenderTargetLayout layout = new Gfx::RenderTargetLayout(renderTarget, depthAttachment);
+8 -1
View File
@@ -9,7 +9,8 @@ DECLARE_NAME_REF(Gfx, Texture2D)
DECLARE_NAME_REF(Gfx, RenderTargetAttachment)
struct UIPassData
{
const UI::RenderHierarchy hierarchy;
Array<UI::RenderElementStyle> renderElements;
Array<Gfx::PTexture> usedTextures;
};
class UIPass : public RenderPass<UIPassData>
{
@@ -26,6 +27,12 @@ private:
Gfx::PRenderTargetAttachment depthAttachment;
Gfx::PTexture2D depthBuffer;
Gfx::PDescriptorLayout descriptorLayout;
Gfx::PDescriptorSet descriptorSet;
Gfx::PUniformBuffer numTexturesBuffer;
Gfx::PVertexBuffer elementBuffer;
Gfx::PVertexDeclaration declaration;
Gfx::PVertexShader vertexShader;
Gfx::PFragmentShader fragmentShader;
@@ -128,7 +128,7 @@ void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBu
UniformBuffer* cachedBuffer = reinterpret_cast<UniformBuffer*>(cachedData[binding]);
if(vulkanBuffer->isDataEquals(cachedBuffer))
{
std::cout << "uniform data equal, skip" << std::endl;
//std::cout << "uniform data equal, skip" << std::endl;
return;
}
bufferInfos.add(init::DescriptorBufferInfo(vulkanBuffer->getHandle(), 0, vulkanBuffer->getSize()));