From c3853ffd243392a0bf5ee4ecf557e967d4f625ee Mon Sep 17 00:00:00 2001 From: Breno Cunha Queiroz Date: Sat, 9 Dec 2023 08:17:43 +0100 Subject: [PATCH] Feat: Calculate uniform buffer size --- .../graphics/apis/vulkan/descriptorPool.cpp | 2 +- src/atta/graphics/apis/vulkan/pipeline.cpp | 64 +++++----- src/atta/graphics/apis/vulkan/shader.cpp | 34 +---- src/atta/graphics/apis/vulkan/shader.h | 3 - src/atta/graphics/bufferLayout.cpp | 65 ++++++++-- src/atta/graphics/bufferLayout.h | 4 +- src/atta/graphics/compute/entityClick.cpp | 117 +++++++++--------- .../renderers/common/selectedPipeline.cpp | 90 +++++++------- src/atta/graphics/renderers/fastRenderer.cpp | 80 ++++++------ src/atta/graphics/shader.cpp | 3 +- src/atta/graphics/shader.h | 3 +- src/atta/ui/layers/uiLayer.cpp | 18 +-- 12 files changed, 243 insertions(+), 240 deletions(-) diff --git a/src/atta/graphics/apis/vulkan/descriptorPool.cpp b/src/atta/graphics/apis/vulkan/descriptorPool.cpp index c81faf07..20f58bac 100644 --- a/src/atta/graphics/apis/vulkan/descriptorPool.cpp +++ b/src/atta/graphics/apis/vulkan/descriptorPool.cpp @@ -13,7 +13,7 @@ DescriptorPool::DescriptorPool(std::vector descrip for (auto binding : descriptorBindings) poolSizes.push_back(VkDescriptorPoolSize{binding.type, static_cast(binding.descriptorCount * maxSets)}); - VkDescriptorPoolCreateInfo poolInfo = {}; + VkDescriptorPoolCreateInfo poolInfo{}; poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; poolInfo.poolSizeCount = static_cast(poolSizes.size()); poolInfo.pPoolSizes = poolSizes.data(); diff --git a/src/atta/graphics/apis/vulkan/pipeline.cpp b/src/atta/graphics/apis/vulkan/pipeline.cpp index e1a8b91b..78b43e6e 100644 --- a/src/atta/graphics/apis/vulkan/pipeline.cpp +++ b/src/atta/graphics/apis/vulkan/pipeline.cpp @@ -11,18 +11,12 @@ namespace atta::graphics::vk { -struct UniformBufferObject { - mat4 model; - mat4 view; - mat4 proj; -}; - Pipeline::Pipeline(const gfx::Pipeline::CreateInfo& info) : gfx::Pipeline(info), _device(common::getDevice()) { // Create framebuffer _framebuffers.push_back(std::dynamic_pointer_cast(_renderPass->getFramebuffer())); // Uniform buffers - _uniformBuffer = std::make_shared(sizeof(UniformBufferObject)); + _uniformBuffer = std::make_shared(_shader->getUniformBufferLayout().getStride()); // Vertex input BufferLayout layout = _shader->getVertexBufferLayout(); @@ -122,7 +116,7 @@ Pipeline::Pipeline(const gfx::Pipeline::CreateInfo& info) : gfx::Pipeline(info), // Descriptor set layout std::vector bindings; - bindings.push_back({0, 1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT}); + bindings.push_back({0, 1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT}); _descriptorSetLayout = std::make_shared(bindings); // Pipeline layout @@ -165,39 +159,39 @@ Pipeline::~Pipeline() { void Pipeline::begin() { // Bind - VkCommandBuffer commandBuffer = common::getCommandBuffers()->getCurrent(); - vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, _pipeline); - - // Viewport - VkViewport viewport{}; - viewport.x = 0.0f; - viewport.y = 0.0f; - viewport.width = _renderPass->getFramebuffer()->getWidth(); - viewport.height = _renderPass->getFramebuffer()->getHeight(); - viewport.minDepth = 0.0f; - viewport.maxDepth = 1.0f; - vkCmdSetViewport(commandBuffer, 0, 1, &viewport); - - // Scissor - VkRect2D scissor{}; - scissor.offset = {0, 0}; - scissor.extent = {(uint32_t)viewport.width, (uint32_t)viewport.height}; - vkCmdSetScissor(commandBuffer, 0, 1, &scissor); - - _descriptorSets->bind(0); + // VkCommandBuffer commandBuffer = common::getCommandBuffers()->getCurrent(); + // vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, _pipeline); + + //// Viewport + // VkViewport viewport{}; + // viewport.x = 0.0f; + // viewport.y = 0.0f; + // viewport.width = _renderPass->getFramebuffer()->getWidth(); + // viewport.height = _renderPass->getFramebuffer()->getHeight(); + // viewport.minDepth = 0.0f; + // viewport.maxDepth = 1.0f; + // vkCmdSetViewport(commandBuffer, 0, 1, &viewport); + + //// Scissor + // VkRect2D scissor{}; + // scissor.offset = {0, 0}; + // scissor.extent = {(uint32_t)viewport.width, (uint32_t)viewport.height}; + // vkCmdSetScissor(commandBuffer, 0, 1, &scissor); + + //_descriptorSets->bind(0); } void Pipeline::end() { - //---------- Update uniform buffers ----------// + //---------- TODO Update uniform buffers ----------// static auto startTime = std::chrono::high_resolution_clock::now(); auto currentTime = std::chrono::high_resolution_clock::now(); float time = std::chrono::duration(currentTime - startTime).count(); - UniformBufferObject ubo{}; - ubo.model = mat4(1.0f); - ubo.model.mat[3][0] = time * 0.1f; - ubo.view = mat4(1.0f); - ubo.proj = mat4(1.0f); - memcpy(_uniformBuffer->getMappedData(), &ubo, sizeof(ubo)); + // UniformBufferObject ubo{}; + // ubo.model = mat4(1.0f); + // ubo.model.mat[3][0] = time * 0.1f; + // ubo.view = mat4(1.0f); + // ubo.proj = mat4(1.0f); + // memcpy(_uniformBuffer->getMappedData(), &ubo, sizeof(ubo)); } void* Pipeline::getImGuiTexture() const { return nullptr; } diff --git a/src/atta/graphics/apis/vulkan/shader.cpp b/src/atta/graphics/apis/vulkan/shader.cpp index 4593516f..99ddfabb 100644 --- a/src/atta/graphics/apis/vulkan/shader.cpp +++ b/src/atta/graphics/apis/vulkan/shader.cpp @@ -39,7 +39,7 @@ void Shader::setCubemap(const char* name, std::shared_ptr image) {} std::vector Shader::getShaderStages() const { std::vector shaderStages; for (auto& [type, shader] : _shaders) { - VkPipelineShaderStageCreateInfo shaderStageInfo; + VkPipelineShaderStageCreateInfo shaderStageInfo{}; shaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; shaderStageInfo.module = shader; shaderStageInfo.pName = "main"; @@ -126,7 +126,6 @@ void Shader::compile() { if (file.is_open()) { file << shaderCode.apiCode; file.close(); - LOG_DEBUG("shader", "Saved $0 code", in); } else { LOG_ERROR("gfx::vk::Shader", "Failed to save shader code to [w]$0[] when compiling [w]$1[]", in, _file.string()); continue; @@ -134,7 +133,6 @@ void Shader::compile() { // Compile shader fs::path out = in.string() + ".spv"; - LOG_DEBUG("Shader", "in $0 out $1", in, out); if (!runCommand("glslc " + in.string() + " -o " + out.string())) return; @@ -155,36 +153,6 @@ void Shader::compile() { void Shader::bind() {} -VkShaderModule Shader::getHandle() const { - // TODO - return {}; -} - -VkPipelineShaderStageCreateInfo Shader::getShaderStage() const { - // VkShaderStageFlagBits stage = convertFileToShaderStage(_filepath); - - VkPipelineShaderStageCreateInfo createInfo = {}; - createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - // createInfo.stage = stage; - // createInfo.module = _shader; - createInfo.pName = "main"; - - return createInfo; -} - -VkShaderStageFlagBits Shader::convertFileToShaderStage(const fs::path& filepath) { - // std::string extension = filepath.extension().string(); - // if (extension == ".vert") - // return VK_SHADER_STAGE_VERTEX_BIT; - // if (extension == ".frag") - // return VK_SHADER_STAGE_FRAGMENT_BIT; - // if (extension == ".geom") - // return VK_SHADER_STAGE_GEOMETRY_BIT; - // ASSERT(false, "Unknown shader file format [w]$0[]. Instead of [*w]$1[], it should be [w].vert[], [w].frag[], or [w].geom[]", filepath.string(), - // extension); - return {}; -} - bool Shader::runCommand(std::string cmd) { std::array buffer; diff --git a/src/atta/graphics/apis/vulkan/shader.h b/src/atta/graphics/apis/vulkan/shader.h index 34dc929a..98f00841 100644 --- a/src/atta/graphics/apis/vulkan/shader.h +++ b/src/atta/graphics/apis/vulkan/shader.h @@ -18,9 +18,6 @@ class Shader final : public gfx::Shader { Shader(const fs::path& file); ~Shader(); - VkShaderModule getHandle() const; - VkPipelineShaderStageCreateInfo getShaderStage() const; - std::string generateApiCode(ShaderType type, std::string iCode) override; void compile() override; void bind() override; diff --git a/src/atta/graphics/bufferLayout.cpp b/src/atta/graphics/bufferLayout.cpp index 5c7cbb83..fbe36772 100644 --- a/src/atta/graphics/bufferLayout.cpp +++ b/src/atta/graphics/bufferLayout.cpp @@ -25,9 +25,6 @@ std::string BufferLayout::Element::typeToString(Type type) { return toStr[(int)t uint32_t BufferLayout::Element::sizeFromType(Type type) { switch (type) { - case Type::NONE: - LOG_WARN("gfx::BufferLayout::Element", "Trying to get size of unknown type"); - return 0; case Type::BOOL: return 4; case Type::INT: @@ -56,8 +53,35 @@ uint32_t BufferLayout::Element::sizeFromType(Type type) { return 4; case Type::SAMPLER_CUBE: return 4; + default: + LOG_WARN("gfx::BufferLayout::Element", "Trying to get size of unknown type"); + return 0; + } + return 0; +} + +uint32_t BufferLayout::Element::alignmentFromType(Type type) { + switch (type) { + case Type::BOOL: + case Type::SAMPLER_2D: + case Type::SAMPLER_CUBE: + return 1; + case Type::INT: + case Type::UINT: + case Type::FLOAT: + case Type::VEC2: + case Type::VEC3: + case Type::VEC4: + case Type::IVEC2: + case Type::IVEC3: + case Type::IVEC4: + case Type::MAT3: + case Type::MAT4: + return 4; + default: + LOG_WARN("gfx::BufferLayout::Element", "Trying to get alignment of unknown type"); + return 0; } - LOG_WARN("gfx::BufferLayout::Element", "Trying to get size of unknown type"); return 0; } @@ -93,12 +117,9 @@ uint32_t BufferLayout::Element::componentCountFromType(Type type) { //----------------------------------// //---------- BufferLayout ----------// //----------------------------------// -BufferLayout::BufferLayout(const std::initializer_list& elements) : _elements(elements) { - uint32_t offset = 0; - for (auto& element : _elements) { - element.offset = offset; - offset += element.size; - } +BufferLayout::BufferLayout(const std::initializer_list& elements) { + for (const auto& element : elements) + push(element.type, element.name); } void BufferLayout::push(Element::Type type, std::string name) { @@ -108,7 +129,16 @@ void BufferLayout::push(Element::Type type, std::string name) { e.name = name; e.type = type; e.size = Element::sizeFromType(type); - e.offset = getStride(); + + // Calculate offset in buffer + if (_elements.empty()) + e.offset = 0; + else { + uint32_t align = Element::alignmentFromType(type); + uint32_t offset = _elements.back().offset; + e.offset = (offset + align - 1) & ~(align - 1); + } + _elements.push_back(e); } @@ -124,9 +154,18 @@ bool BufferLayout::exists(std::string name) const { uint32_t BufferLayout::getElementCount() const { return static_cast(_elements.size()); } uint32_t BufferLayout::getStride() const { - uint32_t stride = 0; + if (_elements.empty()) + return 0; + + // Get buffer alignment (largest type alignment) + uint32_t bufferAlignment = 0; for (auto& element : _elements) - stride += element.size; + bufferAlignment = std::max(bufferAlignment, Element::alignmentFromType(element.type)); + + // Align buffer + uint32_t stride = _elements.back().offset + _elements.back().size; + stride = (stride + bufferAlignment - 1) & ~(bufferAlignment - 1); + return stride; } diff --git a/src/atta/graphics/bufferLayout.h b/src/atta/graphics/bufferLayout.h index 67e4d529..7be0bcfe 100644 --- a/src/atta/graphics/bufferLayout.h +++ b/src/atta/graphics/bufferLayout.h @@ -25,6 +25,8 @@ class BufferLayout final { static std::string typeToString(Type type); /// Type size in bytes static uint32_t sizeFromType(Type type); + /// Type alignment in bytes + static uint32_t alignmentFromType(Type type); /// Number of component in the type (FLOAT -> 1, VEC2 -> 2, MAT4 -> 16, ...) static uint32_t componentCountFromType(Type type); }; @@ -48,7 +50,7 @@ class BufferLayout final { bool exists(std::string name) const; /// Get number of elements uint32_t getElementCount() const; - /// Get buffer stride in bytes (sum of the size of all elements) + /// Get buffer stride in bytes (aligned sum of the size of all elements) uint32_t getStride() const; private: diff --git a/src/atta/graphics/compute/entityClick.cpp b/src/atta/graphics/compute/entityClick.cpp index b3a00206..ef2ad443 100644 --- a/src/atta/graphics/compute/entityClick.cpp +++ b/src/atta/graphics/compute/entityClick.cpp @@ -6,13 +6,13 @@ //-------------------------------------------------- #include -#include -#include -#include #include #include #include #include +#include +#include +#include namespace atta::graphics { @@ -44,61 +44,62 @@ EntityClick::EntityClick() : _width(500), _height(500) { } component::EntityId EntityClick::click(std::shared_ptr viewport, vec2i pos) { - component::EntityId eid; - component::EntityId maxEid = -1; - unsigned width = viewport->getWidth(); - unsigned height = viewport->getHeight(); - // Resize - if (width != _width || height != _height) { - _geometryPipeline->getRenderPass()->getFramebuffer()->resize(width, height); - _width = width; - _height = height; - } - - // Render entity ids - std::vector entities = component::getNoPrototypeView(); - - _geometryPipeline->begin(); - { - std::shared_ptr shader = _geometryPipeline->getShader(); - - mat4 m(1.0f); - glDisable(GL_DEPTH_TEST); - shader->setMat4("model", m); - shader->setMat4("projection", m); - shader->setMat4("view", m); - shader->setInt("entityId", -1); - graphics::getGraphicsAPI()->renderQuad3(); - glEnable(GL_DEPTH_TEST); - glClear(GL_DEPTH_BUFFER_BIT); // XXX Not sure why but it only works in the browser if clear the depth buffer - - // Render entities - shader->setMat4("projection", transpose(viewport->getCamera()->getProj())); - shader->setMat4("view", transpose(viewport->getCamera()->getView())); - - for (auto entity : entities) { - component::Mesh* mesh = component::getComponent(entity); - component::Transform* transform = component::getComponent(entity); - - if (mesh && transform) { - mat4 model = transpose(transform->getWorldTransformMatrix(entity)); - shader->setMat4("model", model); - - // component::EntityId - shader->setInt("entityId", entity); - maxEid = std::max((int)maxEid, entity); - - // Draw mesh - graphics::getGraphicsAPI()->renderMesh(mesh->sid); - } - } - - // Get pixel id - eid = _geometryPipeline->getRenderPass()->getFramebuffer()->readPixel(0, pos.x, pos.y); - } - _geometryPipeline->end(); - - return eid > maxEid ? -1 : eid; + return -1; + // component::EntityId eid; + // component::EntityId maxEid = -1; + // unsigned width = viewport->getWidth(); + // unsigned height = viewport->getHeight(); + //// Resize + // if (width != _width || height != _height) { + // _geometryPipeline->getRenderPass()->getFramebuffer()->resize(width, height); + // _width = width; + // _height = height; + // } + + //// Render entity ids + // std::vector entities = component::getNoPrototypeView(); + + //_geometryPipeline->begin(); + //{ + // std::shared_ptr shader = _geometryPipeline->getShader(); + + // mat4 m(1.0f); + // glDisable(GL_DEPTH_TEST); + // shader->setMat4("model", m); + // shader->setMat4("projection", m); + // shader->setMat4("view", m); + // shader->setInt("entityId", -1); + // graphics::getGraphicsAPI()->renderQuad3(); + // glEnable(GL_DEPTH_TEST); + // glClear(GL_DEPTH_BUFFER_BIT); // XXX Not sure why but it only works in the browser if clear the depth buffer + + // // Render entities + // shader->setMat4("projection", transpose(viewport->getCamera()->getProj())); + // shader->setMat4("view", transpose(viewport->getCamera()->getView())); + + // for (auto entity : entities) { + // component::Mesh* mesh = component::getComponent(entity); + // component::Transform* transform = component::getComponent(entity); + + // if (mesh && transform) { + // mat4 model = transpose(transform->getWorldTransformMatrix(entity)); + // shader->setMat4("model", model); + + // // component::EntityId + // shader->setInt("entityId", entity); + // maxEid = std::max((int)maxEid, entity); + + // // Draw mesh + // graphics::getGraphicsAPI()->renderMesh(mesh->sid); + // } + // } + + // // Get pixel id + // eid = _geometryPipeline->getRenderPass()->getFramebuffer()->readPixel(0, pos.x, pos.y); + //} + //_geometryPipeline->end(); + + // return eid > maxEid ? -1 : eid; } } // namespace atta::graphics diff --git a/src/atta/graphics/renderers/common/selectedPipeline.cpp b/src/atta/graphics/renderers/common/selectedPipeline.cpp index 6b401923..96c7b587 100644 --- a/src/atta/graphics/renderers/common/selectedPipeline.cpp +++ b/src/atta/graphics/renderers/common/selectedPipeline.cpp @@ -4,12 +4,12 @@ // Date: 2021-11-15 // By Breno Cunha Queiroz //-------------------------------------------------- -#include -#include -#include #include #include #include +#include +#include +#include namespace atta::graphics { @@ -23,56 +23,56 @@ SelectedPipeline::SelectedPipeline(std::shared_ptr renderPass) { } void SelectedPipeline::render(std::shared_ptr camera) { - _pipeline->begin(); - { - glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - glClear(GL_STENCIL_BUFFER_BIT); + //_pipeline->begin(); + //{ + // glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); + // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + // glEnable(GL_BLEND); + // glClear(GL_STENCIL_BUFFER_BIT); - std::shared_ptr shader = _pipeline->getShader(); - shader->setMat4("projection", transpose(camera->getProj())); - shader->setMat4("view", transpose(camera->getView())); + // std::shared_ptr shader = _pipeline->getShader(); + // shader->setMat4("projection", transpose(camera->getProj())); + // shader->setMat4("view", transpose(camera->getView())); - component::EntityId entity = component::getSelectedEntity(); - if (entity != -1) { - component::Mesh* mesh = component::getComponent(entity); - component::Transform* transform = component::getComponent(entity); + // component::EntityId entity = component::getSelectedEntity(); + // if (entity != -1) { + // component::Mesh* mesh = component::getComponent(entity); + // component::Transform* transform = component::getComponent(entity); - if (mesh && transform) { - // Draw mesh normal size - mat4 model = transform->getWorldTransformMatrix(entity); - vec3 pos, scale; - quat ori; - model.getPosOriScale(pos, ori, scale); - model.transpose(); + // if (mesh && transform) { + // // Draw mesh normal size + // mat4 model = transform->getWorldTransformMatrix(entity); + // vec3 pos, scale; + // quat ori; + // model.getPosOriScale(pos, ori, scale); + // model.transpose(); - shader->setMat4("model", model); - shader->setVec4("color", vec4(0, 0, 0, 0)); + // shader->setMat4("model", model); + // shader->setVec4("color", vec4(0, 0, 0, 0)); - glDisable(GL_DEPTH_TEST); - glStencilFunc(GL_ALWAYS, 1, 0xFF); - glStencilMask(0xFF); // Update stencil - graphics::getGraphicsAPI()->renderMesh(mesh->sid); + // glDisable(GL_DEPTH_TEST); + // glStencilFunc(GL_ALWAYS, 1, 0xFF); + // glStencilMask(0xFF); // Update stencil + // graphics::getGraphicsAPI()->renderMesh(mesh->sid); - // Draw scaled mesh - float distance = (camera->getPosition() - pos).length(); - model.setPosOriScale(pos, ori, scale + distance / 100.0f); - model.transpose(); - shader->setMat4("model", model); - shader->setVec4("color", vec4(0.22f, 0.63f, 0.27f, 1.0f)); + // // Draw scaled mesh + // float distance = (camera->getPosition() - pos).length(); + // model.setPosOriScale(pos, ori, scale + distance / 100.0f); + // model.transpose(); + // shader->setMat4("model", model); + // shader->setVec4("color", vec4(0.22f, 0.63f, 0.27f, 1.0f)); - glStencilFunc(GL_NOTEQUAL, 1, 0xFF); - glStencilMask(0x00); // Do not update stencil - graphics::getGraphicsAPI()->renderMesh(mesh->sid); + // glStencilFunc(GL_NOTEQUAL, 1, 0xFF); + // glStencilMask(0x00); // Do not update stencil + // graphics::getGraphicsAPI()->renderMesh(mesh->sid); - glEnable(GL_DEPTH_TEST); - glStencilMask(0xFF); - glStencilFunc(GL_ALWAYS, 1, 0xFF); - } - } - } - _pipeline->end(); + // glEnable(GL_DEPTH_TEST); + // glStencilMask(0xFF); + // glStencilFunc(GL_ALWAYS, 1, 0xFF); + // } + // } + //} + //_pipeline->end(); } } // namespace atta::graphics diff --git a/src/atta/graphics/renderers/fastRenderer.cpp b/src/atta/graphics/renderers/fastRenderer.cpp index a16a8484..ef4860fd 100644 --- a/src/atta/graphics/renderers/fastRenderer.cpp +++ b/src/atta/graphics/renderers/fastRenderer.cpp @@ -55,46 +55,46 @@ FastRenderer::FastRenderer() : Renderer("FastRenderer") { FastRenderer::~FastRenderer() {} void FastRenderer::render(std::shared_ptr camera) { - std::vector entities = component::getNoPrototypeView(); - _geometryPipeline->begin(); - { - std::shared_ptr shader = _geometryPipeline->getShader(); - - shader->setMat4("projection", transpose(camera->getProj())); - shader->setMat4("view", transpose(camera->getView())); - - for (auto entity : entities) { - component::Mesh* mesh = component::getComponent(entity); - component::Transform* transform = component::getComponent(entity); - component::Material* compMat = component::getComponent(entity); - resource::Material* material = compMat ? compMat->getResource() : nullptr; - - if (mesh && transform) { - mat4 model = transpose(transform->getWorldTransformMatrix(entity)); - shader->setMat4("model", model); - - if (material) { - if (material->colorIsImage()) { - shader->setImage("albedoTexture", material->colorImage); - shader->setVec3("albedo", {-1, -1, -1}); - } else - shader->setVec3("albedo", material->color); - } else { - resource::Material::CreateInfo defaultMaterial {}; - shader->setVec3("albedo", defaultMaterial.color); - } - - // Draw mesh - graphics::getGraphicsAPI()->renderMesh(mesh->sid); - } - } - } - _geometryPipeline->end(); - - if (_renderSelected) - _selectedPipeline->render(camera); - if (_renderDrawer) - _drawerPipeline->render(camera); + // std::vector entities = component::getNoPrototypeView(); + //_geometryPipeline->begin(); + //{ + // std::shared_ptr shader = _geometryPipeline->getShader(); + + // shader->setMat4("projection", transpose(camera->getProj())); + // shader->setMat4("view", transpose(camera->getView())); + + // for (auto entity : entities) { + // component::Mesh* mesh = component::getComponent(entity); + // component::Transform* transform = component::getComponent(entity); + // component::Material* compMat = component::getComponent(entity); + // resource::Material* material = compMat ? compMat->getResource() : nullptr; + + // if (mesh && transform) { + // mat4 model = transpose(transform->getWorldTransformMatrix(entity)); + // shader->setMat4("model", model); + + // if (material) { + // if (material->colorIsImage()) { + // shader->setImage("albedoTexture", material->colorImage); + // shader->setVec3("albedo", {-1, -1, -1}); + // } else + // shader->setVec3("albedo", material->color); + // } else { + // resource::Material::CreateInfo defaultMaterial {}; + // shader->setVec3("albedo", defaultMaterial.color); + // } + + // // Draw mesh + // graphics::getGraphicsAPI()->renderMesh(mesh->sid); + // } + // } + //} + //_geometryPipeline->end(); + + // if (_renderSelected) + // _selectedPipeline->render(camera); + // if (_renderDrawer) + // _drawerPipeline->render(camera); } void FastRenderer::resize(uint32_t width, uint32_t height) { diff --git a/src/atta/graphics/shader.cpp b/src/atta/graphics/shader.cpp index af17e40f..64f95e80 100644 --- a/src/atta/graphics/shader.cpp +++ b/src/atta/graphics/shader.cpp @@ -36,7 +36,8 @@ Shader::Shader(const fs::path& file) : _file(file) { fs::path Shader::getFile() const { return _file; } -BufferLayout Shader::getVertexBufferLayout() const { return _vertexLayout; } +const BufferLayout& Shader::getVertexBufferLayout() const { return _vertexLayout; } +const BufferLayout& Shader::getUniformBufferLayout() const { return _uniformLayout; } void Shader::processASL() { LOG_DEBUG("gfx::Shader", "Preprocess ASL: [w]$0", _file.string()); diff --git a/src/atta/graphics/shader.h b/src/atta/graphics/shader.h index ce08d735..0c3e21af 100644 --- a/src/atta/graphics/shader.h +++ b/src/atta/graphics/shader.h @@ -33,7 +33,8 @@ class Shader { virtual void setCubemap(const char* name, std::shared_ptr image) = 0; fs::path getFile() const; - BufferLayout getVertexBufferLayout() const; + const BufferLayout& getVertexBufferLayout() const; + const BufferLayout& getUniformBufferLayout() const; /** * @brief Shader type diff --git a/src/atta/ui/layers/uiLayer.cpp b/src/atta/ui/layers/uiLayer.cpp index aca27104..207c2fa9 100644 --- a/src/atta/ui/layers/uiLayer.cpp +++ b/src/atta/ui/layers/uiLayer.cpp @@ -86,15 +86,15 @@ void UILayer::begin() { void UILayer::end() { ImGui::Render(); - switch (gfx::getGraphicsAPI()->getType()) { - case gfx::GraphicsAPI::OPENGL: - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - break; - case gfx::GraphicsAPI::VULKAN: - ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), - std::dynamic_pointer_cast(gfx::getGraphicsAPI())->getCommandBuffers()->getCurrent()); - break; - } + // switch (gfx::getGraphicsAPI()->getType()) { + // case gfx::GraphicsAPI::OPENGL: + // ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + // break; + // case gfx::GraphicsAPI::VULKAN: + // ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), + // std::dynamic_pointer_cast(gfx::getGraphicsAPI())->getCommandBuffers()->getCurrent()); + // break; + // } ImGuiIO& io = ImGui::GetIO(); if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {