diff --git a/kraken/CMakeLists.txt b/kraken/CMakeLists.txt index 49d606b..ec2dcf4 100644 --- a/kraken/CMakeLists.txt +++ b/kraken/CMakeLists.txt @@ -78,6 +78,7 @@ add_source_and_header(KRStreamerThread) add_source_and_header(KRSwapchain) add_source_and_header(KRContextObject) add_source_and_header(KRHelpers) +add_source_and_header(KRModelView) add_source_and_header(KROctree) add_source_and_header(KROctreeNode) add_source_and_header(KRPresentationThread) diff --git a/kraken/KRModelView.cpp b/kraken/KRModelView.cpp new file mode 100644 index 0000000..0c9e4e5 --- /dev/null +++ b/kraken/KRModelView.cpp @@ -0,0 +1,67 @@ +// +// KRModelView.cpp +// Kraken Engine +// +// Copyright 2024 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KREngine-common.h" + +#include "KRModelView.h" +#include "KRViewport.h" + +using namespace hydra; + +KRModelView::KRModelView(KRViewport* viewport, const hydra::Matrix4& matModel) + : m_viewport(viewport) + , m_matModel(matModel) +{ + m_matModelInverse = matModel; + m_matModelInverse.invert(); +} + +KRModelView::~KRModelView() +{ + +} + +bool KRModelView::getShaderValue(ShaderValue value, void* buffer, size_t size) const +{ + if (size == sizeof(Vector3)) { + switch (value) { + case ShaderValue::camerapos_model_space: + { + // Transform location of camera to object space for calculation of specular halfVec + Vector3 cameraPosObject = Matrix4::Dot(m_matModelInverse, m_viewport->getCameraPosition()); + memcpy(buffer, &cameraPosObject, sizeof(Vector3)); + return true; + } + } + } + return false; +} + diff --git a/kraken/KRModelView.h b/kraken/KRModelView.h new file mode 100644 index 0000000..8b8a0ac --- /dev/null +++ b/kraken/KRModelView.h @@ -0,0 +1,57 @@ +// +// KRModelView.h +// Kraken Engine +// +// Copyright 2024 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#pragma once + +#include "KREngine-common.h" + +#include "aabb.h" + +#include "KRShaderReflection.h" + +class KRViewport; + +class KRModelView + : public KRReflectedObject +{ +public: + KRModelView(KRViewport* viewport, const hydra::Matrix4& matModel); + ~KRModelView(); + + bool getShaderValue(ShaderValue value, void* buffer, size_t size) const final; + +private: + KRViewport* m_viewport; + hydra::Matrix4 m_matModel; + + // Derived values + hydra::Matrix4 m_matModelInverse; +}; diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index b6ee44c..1ce5157 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -37,6 +37,7 @@ #include "nodes/KRPointLight.h" #include "KRContext.h" #include "KRRenderPass.h" +#include "KRModelView.h" using namespace hydra; @@ -499,6 +500,25 @@ bool KRPipeline::hasPushConstant(ShaderValue location) const return false; } + +void KRPipeline::setPushConstants(const std::vector objects) +{ + for (StageInfo& stageInfo : m_stages) { + PushConstantInfo& pushConstants = stageInfo.pushConstants; + for (int i = 0; i < kPushConstantCount; i++) { + size_t size = pushConstants.size[static_cast(i)]; + if (size != 0) { + void* constant = (pushConstants.buffer + pushConstants.offset[static_cast(i)]); + for (const KRReflectedObject* object : objects) { + if (object->getShaderValue(static_cast(i), constant, size)) { + break; + } + } + } + } + } +} + void KRPipeline::setPushConstant(ShaderValue location, float value) { for (StageInfo& stageInfo : m_stages) { @@ -582,6 +602,11 @@ void KRPipeline::updateDescriptorBinding() void KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matModel) { + KRModelView modelView(ri.viewport, matModel); + + std::vector objects = { &modelView }; + setPushConstants(objects); + setPushConstant(ShaderValue::absolute_time, getContext().getAbsoluteTime()); int light_directional_count = 0; @@ -643,17 +668,6 @@ void KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matM //light_spot_count = spot_lights.size(); } - if (hasPushConstant(ShaderValue::camerapos_model_space)) { - Matrix4 inverseModelMatrix = matModel; - inverseModelMatrix.invert(); - - if (hasPushConstant(ShaderValue::camerapos_model_space)) { - // Transform location of camera to object space for calculation of specular halfVec - Vector3 cameraPosObject = Matrix4::Dot(inverseModelMatrix, ri.viewport->getCameraPosition()); - setPushConstant(ShaderValue::camerapos_model_space, cameraPosObject); - } - } - if (hasPushConstant(ShaderValue::mvp) || hasPushConstant(ShaderValue::invmvp)) { // Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram Matrix4 mvpMatrix = matModel * ri.viewport->getViewProjectionMatrix(); diff --git a/kraken/KRPipeline.h b/kraken/KRPipeline.h index ab3af81..77b70f8 100644 --- a/kraken/KRPipeline.h +++ b/kraken/KRPipeline.h @@ -222,6 +222,7 @@ public: static const size_t kPushConstantCount = static_cast(ShaderValue::NUM_PUSH_CONSTANTS); + void setPushConstants(const std::vector objects); bool hasPushConstant(ShaderValue location) const; void setPushConstant(ShaderValue location, float value); void setPushConstant(ShaderValue location, int value); diff --git a/kraken/KRShaderReflection.h b/kraken/KRShaderReflection.h index 6cf48c0..80650ef 100644 --- a/kraken/KRShaderReflection.h +++ b/kraken/KRShaderReflection.h @@ -111,5 +111,5 @@ const char* SHADER_VALUE_NAMES[]; class KRReflectedObject { public: - virtual bool GetShaderValue(ShaderValue value, void* buffer, size_t size) = 0; + virtual bool getShaderValue(ShaderValue value, void* buffer, size_t size) const = 0; }; diff --git a/kraken/KRViewport.cpp b/kraken/KRViewport.cpp index d4526f4..9b80467 100755 --- a/kraken/KRViewport.cpp +++ b/kraken/KRViewport.cpp @@ -55,7 +55,7 @@ KRViewport::KRViewport(const Vector2& size, const Matrix4& matView, const Matrix calculateDerivedValues(); } -bool KRViewport::GetShaderValue(ShaderValue value, void* buffer, size_t size) +bool KRViewport::getShaderValue(ShaderValue value, void* buffer, size_t size) const { return false; } diff --git a/kraken/KRViewport.h b/kraken/KRViewport.h index 07b44aa..10f7ca4 100755 --- a/kraken/KRViewport.h +++ b/kraken/KRViewport.h @@ -47,7 +47,7 @@ public: KRViewport(const hydra::Vector2& size, const hydra::Matrix4& matView, const hydra::Matrix4& matProjection); ~KRViewport(); - bool GetShaderValue(ShaderValue value, void* buffer, size_t size) final; + bool getShaderValue(ShaderValue value, void* buffer, size_t size) const final; const hydra::Vector2& getSize() const; const hydra::Matrix4& getViewMatrix() const;