diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index 0474b71..973f430 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -150,7 +150,7 @@ KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KR VkVertexInputBindingDescription bindingDescription{}; bindingDescription.binding = 0; - bindingDescription.stride = KRMesh::VertexSizeForAttributes(vertexAttributes); + bindingDescription.stride = (uint32_t)KRMesh::VertexSizeForAttributes(vertexAttributes); bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; uint32_t vertexAttributeCount = 0; @@ -164,7 +164,7 @@ KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KR desc.binding = 0; desc.location = attribute_locations[location_attrib] - 1; desc.format = KRMesh::AttributeVulkanFormat(mesh_attrib); - desc.offset = KRMesh::AttributeOffset(mesh_attrib, vertexAttributes); + desc.offset = (uint32_t)KRMesh::AttributeOffset(mesh_attrib, vertexAttributes); } } @@ -533,8 +533,9 @@ bool KRPipeline::hasPushConstant(ShaderValue location) const } -void KRPipeline::setPushConstants(const std::vector objects) +bool KRPipeline::setPushConstants(const std::vector objects) { + bool success = true; for (StageInfo& stageInfo : m_stages) { PushConstantInfo& pushConstants = stageInfo.pushConstants; for (int i = 0; i < kPushConstantCount; i++) { @@ -549,11 +550,13 @@ void KRPipeline::setPushConstants(const std::vector ob } } if(!found) { + success = false; KRContext::Log(KRContext::LOG_LEVEL_ERROR, "Push constant not found: %s", getShaderValueName(i)); } } } } + return success; } void KRPipeline::setPushConstant(ShaderValue location, float value) @@ -637,7 +640,7 @@ void KRPipeline::updateDescriptorBinding() // Vulkan Refactoring } -void KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matModel) +bool KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matModel) { KRDirectionalLight* directionalLight = nullptr; if (ri.renderPass->getType() != RenderPassType::RENDER_PASS_DEFERRED_LIGHTS && ri.renderPass->getType() != RenderPassType::RENDER_PASS_DEFERRED_GBUFFER && ri.renderPass->getType() != RenderPassType::RENDER_PASS_DEFERRED_OPAQUE && ri.renderPass->getType() != RenderPassType::RENDER_PASS_SHADOWMAP) { @@ -653,7 +656,7 @@ void KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matM ri.reflectedObjects.push_back(ri.viewport); ri.reflectedObjects.push_back(&ri.camera->settings); - setPushConstants(ri.reflectedObjects); + bool success = setPushConstants(ri.reflectedObjects); ri.reflectedObjects.pop_back(); ri.reflectedObjects.pop_back(); @@ -661,12 +664,18 @@ void KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matM setPushConstant(ShaderValue::absolute_time, getContext().getAbsoluteTime()); + if (!success) { + return false; + } + for (StageInfo& stageInfo : m_stages) { PushConstantInfo& pushConstants = stageInfo.pushConstants; if (pushConstants.buffer) { vkCmdPushConstants(ri.commandBuffer, pushConstants.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, pushConstants.bufferSize, pushConstants.buffer); } } + + return true; } bool KRPipeline::bind(KRNode::RenderInfo& ri, const Matrix4& matModel) @@ -674,7 +683,9 @@ bool KRPipeline::bind(KRNode::RenderInfo& ri, const Matrix4& matModel) updateDescriptorBinding(); updateDescriptorSets(); bindDescriptorSets(ri.commandBuffer); - updatePushConstants(ri, matModel); + if (!updatePushConstants(ri, matModel)) { + return false; + } if (ri.pipeline != this) { vkCmdBindPipeline(ri.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphicsPipeline); diff --git a/kraken/KRPipeline.h b/kraken/KRPipeline.h index 793d500..0bc50cb 100644 --- a/kraken/KRPipeline.h +++ b/kraken/KRPipeline.h @@ -221,7 +221,7 @@ public: static const size_t kPushConstantCount = static_cast(ShaderValue::NUM_SHADER_VALUES); - void setPushConstants(const std::vector objects); + bool setPushConstants(const std::vector objects); bool hasPushConstant(ShaderValue location) const; void setPushConstant(ShaderValue location, float value); void setPushConstant(ShaderValue location, int value); @@ -239,7 +239,7 @@ public: private: void updateDescriptorBinding(); void updateDescriptorSets(); - void updatePushConstants(KRNode::RenderInfo& ri, const hydra::Matrix4& matModel); + bool updatePushConstants(KRNode::RenderInfo& ri, const hydra::Matrix4& matModel); struct PushConstantInfo { diff --git a/kraken/nodes/KRNode.cpp b/kraken/nodes/KRNode.cpp index d0f9096..2d8d6de 100755 --- a/kraken/nodes/KRNode.cpp +++ b/kraken/nodes/KRNode.cpp @@ -166,7 +166,9 @@ void KRNode::makeOrphan() KRNode::~KRNode() { while (m_firstChildNode != nullptr) { + KRNode* nextChild = m_firstChildNode->m_nextNode; delete m_firstChildNode; + m_firstChildNode = nextChild; } makeOrphan(); diff --git a/kraken/resources/material/KRMaterial.cpp b/kraken/resources/material/KRMaterial.cpp index c0e7024..07ca38d 100755 --- a/kraken/resources/material/KRMaterial.cpp +++ b/kraken/resources/material/KRMaterial.cpp @@ -743,14 +743,13 @@ kraken_stream_level KRMaterial::getStreamLevel() return stream_level; } -void KRMaterial::bind(KRNode::RenderInfo& ri, ModelFormat modelFormat, __uint32_t vertexAttributes, CullMode cullMode, const std::vector& bones, const std::vector& bind_poses, const Matrix4& matModel, KRTexture* pLightMap, float lod_coverage) +bool KRMaterial::bind(KRNode::RenderInfo& ri, ModelFormat modelFormat, __uint32_t vertexAttributes, CullMode cullMode, const std::vector& bones, const std::vector& bind_poses, const Matrix4& matModel, KRTexture* pLightMap, float lod_coverage) { bool bLightMap = pLightMap && ri.camera->settings.bEnableLightMap; Vector2 default_scale = Vector2::One(); Vector2 default_offset = Vector2::Zero(); - bool bHasReflection = m_roughnessFactor > 0.f; bool bDiffuseMap = m_baseColorMap.texture.isBound() && ri.camera->settings.bEnableDiffuseMap; bool bNormalMap = m_normalMap.texture.isBound() && ri.camera->settings.bEnableNormalMap; bool bSpecMap = false; @@ -845,9 +844,13 @@ void KRMaterial::bind(KRNode::RenderInfo& ri, ModelFormat modelFormat, __uint32_ // pShader->setImageBinding("reflectionTexture", m_reflection.texture.get(), getContext().getSamplerManager()->DEFAULT_CLAMPED_SAMPLER); } + bool success = true; ri.reflectedObjects.push_back(this); - pShader->bind(ri, matModel); + if (!pShader->bind(ri, matModel)) { + success = false; + } ri.reflectedObjects.pop_back(); + return success; } bool KRMaterial::getShaderValue(ShaderValue value, float* output) const diff --git a/kraken/resources/material/KRMaterial.h b/kraken/resources/material/KRMaterial.h index 72e4050..aebb1e9 100755 --- a/kraken/resources/material/KRMaterial.h +++ b/kraken/resources/material/KRMaterial.h @@ -130,7 +130,7 @@ public: bool isTransparent(); - void bind(KRNode::RenderInfo& ri, ModelFormat modelFormat, __uint32_t vertexAttributes, CullMode cullMode, const std::vector& bones, const std::vector& bind_poses, const hydra::Matrix4& matModel, KRTexture* pLightMap, float lod_coverage = 0.0f); + bool bind(KRNode::RenderInfo& ri, ModelFormat modelFormat, __uint32_t vertexAttributes, CullMode cullMode, const std::vector& bones, const std::vector& bind_poses, const hydra::Matrix4& matModel, KRTexture* pLightMap, float lod_coverage = 0.0f); bool needsVertexTangents(); diff --git a/kraken/resources/mesh/KRMesh.cpp b/kraken/resources/mesh/KRMesh.cpp index 1611d2f..768f93c 100755 --- a/kraken/resources/mesh/KRMesh.cpp +++ b/kraken/resources/mesh/KRMesh.cpp @@ -266,21 +266,27 @@ void KRMesh::render(KRNode::RenderInfo& ri, const std::string& object_name, cons switch (pMaterial->getAlphaMode()) { case KRMaterial::KRMATERIAL_ALPHA_MODE_OPAQUE: // Non-transparent materials case KRMaterial::KRMATERIAL_ALPHA_MODE_TEST: // Alpha in diffuse texture is interpreted as punch-through when < 0.5 - pMaterial->bind(ri, getModelFormat(), getVertexAttributes(), CullMode::kCullBack, bones, bone_bind_poses, matModel, pLightMap, lod_coverage); - renderSubmesh(ri.commandBuffer, iSubmesh, ri.renderPass, object_name, pMaterial->getName(), lod_coverage); + if (pMaterial->bind(ri, getModelFormat(), getVertexAttributes(), CullMode::kCullBack, bones, bone_bind_poses, matModel, pLightMap, lod_coverage)) + { + renderSubmesh(ri.commandBuffer, iSubmesh, ri.renderPass, object_name, pMaterial->getName(), lod_coverage); + } break; case KRMaterial::KRMATERIAL_ALPHA_MODE_BLEND: // Blended Alpha if (pMaterial->m_doubleSided) { // Blended alpha rendered in two passes. First pass renders backfaces; second pass renders frontfaces. // // Render back faces before front faces - pMaterial->bind(ri, getModelFormat(), getVertexAttributes(), CullMode::kCullFront, bones, bone_bind_poses, matModel, pLightMap, lod_coverage); - renderSubmesh(ri.commandBuffer, iSubmesh, ri.renderPass, object_name, pMaterial->getName(), lod_coverage); + if (pMaterial->bind(ri, getModelFormat(), getVertexAttributes(), CullMode::kCullFront, bones, bone_bind_poses, matModel, pLightMap, lod_coverage)) + { + renderSubmesh(ri.commandBuffer, iSubmesh, ri.renderPass, object_name, pMaterial->getName(), lod_coverage); + } } // Render front faces - pMaterial->bind(ri, getModelFormat(), getVertexAttributes(), CullMode::kCullBack, bones, bone_bind_poses, matModel, pLightMap, lod_coverage); - renderSubmesh(ri.commandBuffer, iSubmesh, ri.renderPass, object_name, pMaterial->getName(), lod_coverage); + if (pMaterial->bind(ri, getModelFormat(), getVertexAttributes(), CullMode::kCullBack, bones, bone_bind_poses, matModel, pLightMap, lod_coverage)) + { + renderSubmesh(ri.commandBuffer, iSubmesh, ri.renderPass, object_name, pMaterial->getName(), lod_coverage); + } break; } } diff --git a/kraken/resources/shader/KRShaderManager.cpp b/kraken/resources/shader/KRShaderManager.cpp index 93edc6c..c216b3f 100644 --- a/kraken/resources/shader/KRShaderManager.cpp +++ b/kraken/resources/shader/KRShaderManager.cpp @@ -298,7 +298,7 @@ bool KRShaderManager::compileAll(KRBundle* outputBundle, KRUnknown* logResource) } sourceName = source->getName() + "." + source->getExtension(); sourceText[0] = (char*)source->getData()->getStart(); - sourceLen[0] = source->getData()->getSize(); + sourceLen[0] = (int)source->getData()->getSize(); sourceNameStr[0] = sourceName.c_str(); shader.setStringsWithLengthsAndNames(sourceText, sourceLen, sourceNameStr, 1); //shader.setStrings(&sourceStr, 1); diff --git a/kraken/resources/texture/KRTexture2D.cpp b/kraken/resources/texture/KRTexture2D.cpp index 94f953f..58d7d90 100755 --- a/kraken/resources/texture/KRTexture2D.cpp +++ b/kraken/resources/texture/KRTexture2D.cpp @@ -57,7 +57,7 @@ bool KRTexture2D::createGPUTexture(int targetLod) void* buffer = malloc(bufferSize); if (!getLodData(buffer, targetLod)) { - delete buffer; + free(buffer); return false; } @@ -130,7 +130,7 @@ bool KRTexture2D::createGPUTexture(int targetLod) device.streamUpload((void*)buffer, bufferSize, texture.image, regions.data(), regions.size()); } - delete buffer; + free(buffer); if (success) { m_new_lod = targetLod; diff --git a/kraken/resources/texture/KRTextureCube.cpp b/kraken/resources/texture/KRTextureCube.cpp index 1c8bf89..6ff30b8 100755 --- a/kraken/resources/texture/KRTextureCube.cpp +++ b/kraken/resources/texture/KRTextureCube.cpp @@ -151,9 +151,10 @@ bool KRTextureCube::createGPUTexture(int lod) } for (int i = 0; i < 6; i++) { - if (buffers[i]) { - delete buffers[i]; - } + if (buffers[i]) { + free(buffers[i]); + buffers[i] = nullptr; + } } return success; diff --git a/kraken/resources/texture/KRTextureTGA.cpp b/kraken/resources/texture/KRTextureTGA.cpp index 62fcb4c..2d09764 100755 --- a/kraken/resources/texture/KRTextureTGA.cpp +++ b/kraken/resources/texture/KRTextureTGA.cpp @@ -130,8 +130,6 @@ bool KRTextureTGA::getLodData(void* buffer, int lod) return false; // Mapped colors not supported } - Vector3i dimensions = { pHeader->width, pHeader->height, 1 }; - switch (pHeader->imagetype) { case 2: // rgb switch (pHeader->bitsperpixel) {