Added first push buffer binding using reflection

This commit is contained in:
2025-08-30 16:07:14 -07:00
parent a917f70f62
commit eb3f88ff90
8 changed files with 154 additions and 14 deletions

View File

@@ -78,6 +78,7 @@ add_source_and_header(KRStreamerThread)
add_source_and_header(KRSwapchain) add_source_and_header(KRSwapchain)
add_source_and_header(KRContextObject) add_source_and_header(KRContextObject)
add_source_and_header(KRHelpers) add_source_and_header(KRHelpers)
add_source_and_header(KRModelView)
add_source_and_header(KROctree) add_source_and_header(KROctree)
add_source_and_header(KROctreeNode) add_source_and_header(KROctreeNode)
add_source_and_header(KRPresentationThread) add_source_and_header(KRPresentationThread)

67
kraken/KRModelView.cpp Normal file
View File

@@ -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;
}

57
kraken/KRModelView.h Normal file
View File

@@ -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;
};

View File

@@ -37,6 +37,7 @@
#include "nodes/KRPointLight.h" #include "nodes/KRPointLight.h"
#include "KRContext.h" #include "KRContext.h"
#include "KRRenderPass.h" #include "KRRenderPass.h"
#include "KRModelView.h"
using namespace hydra; using namespace hydra;
@@ -499,6 +500,25 @@ bool KRPipeline::hasPushConstant(ShaderValue location) const
return false; return false;
} }
void KRPipeline::setPushConstants(const std::vector<const KRReflectedObject*> objects)
{
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
for (int i = 0; i < kPushConstantCount; i++) {
size_t size = pushConstants.size[static_cast<size_t>(i)];
if (size != 0) {
void* constant = (pushConstants.buffer + pushConstants.offset[static_cast<size_t>(i)]);
for (const KRReflectedObject* object : objects) {
if (object->getShaderValue(static_cast<ShaderValue>(i), constant, size)) {
break;
}
}
}
}
}
}
void KRPipeline::setPushConstant(ShaderValue location, float value) void KRPipeline::setPushConstant(ShaderValue location, float value)
{ {
for (StageInfo& stageInfo : m_stages) { for (StageInfo& stageInfo : m_stages) {
@@ -582,6 +602,11 @@ void KRPipeline::updateDescriptorBinding()
void KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matModel) void KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matModel)
{ {
KRModelView modelView(ri.viewport, matModel);
std::vector<const KRReflectedObject*> objects = { &modelView };
setPushConstants(objects);
setPushConstant(ShaderValue::absolute_time, getContext().getAbsoluteTime()); setPushConstant(ShaderValue::absolute_time, getContext().getAbsoluteTime());
int light_directional_count = 0; int light_directional_count = 0;
@@ -643,17 +668,6 @@ void KRPipeline::updatePushConstants(KRNode::RenderInfo& ri, const Matrix4& matM
//light_spot_count = spot_lights.size(); //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)) { if (hasPushConstant(ShaderValue::mvp) || hasPushConstant(ShaderValue::invmvp)) {
// Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram // Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram
Matrix4 mvpMatrix = matModel * ri.viewport->getViewProjectionMatrix(); Matrix4 mvpMatrix = matModel * ri.viewport->getViewProjectionMatrix();

View File

@@ -222,6 +222,7 @@ public:
static const size_t kPushConstantCount = static_cast<size_t>(ShaderValue::NUM_PUSH_CONSTANTS); static const size_t kPushConstantCount = static_cast<size_t>(ShaderValue::NUM_PUSH_CONSTANTS);
void setPushConstants(const std::vector<const KRReflectedObject*> objects);
bool hasPushConstant(ShaderValue location) const; bool hasPushConstant(ShaderValue location) const;
void setPushConstant(ShaderValue location, float value); void setPushConstant(ShaderValue location, float value);
void setPushConstant(ShaderValue location, int value); void setPushConstant(ShaderValue location, int value);

View File

@@ -111,5 +111,5 @@ const char* SHADER_VALUE_NAMES[];
class KRReflectedObject class KRReflectedObject
{ {
public: public:
virtual bool GetShaderValue(ShaderValue value, void* buffer, size_t size) = 0; virtual bool getShaderValue(ShaderValue value, void* buffer, size_t size) const = 0;
}; };

View File

@@ -55,7 +55,7 @@ KRViewport::KRViewport(const Vector2& size, const Matrix4& matView, const Matrix
calculateDerivedValues(); calculateDerivedValues();
} }
bool KRViewport::GetShaderValue(ShaderValue value, void* buffer, size_t size) bool KRViewport::getShaderValue(ShaderValue value, void* buffer, size_t size) const
{ {
return false; return false;
} }

View File

@@ -47,7 +47,7 @@ public:
KRViewport(const hydra::Vector2& size, const hydra::Matrix4& matView, const hydra::Matrix4& matProjection); KRViewport(const hydra::Vector2& size, const hydra::Matrix4& matView, const hydra::Matrix4& matProjection);
~KRViewport(); ~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::Vector2& getSize() const;
const hydra::Matrix4& getViewMatrix() const; const hydra::Matrix4& getViewMatrix() const;