WIP Creating structures in KRPipeline to cache descriptor set binding reflection data.

Added stub function, KRPipeline::setImageBinding
This commit is contained in:
2022-09-15 20:33:41 -07:00
parent 183f7057e2
commit 8306202cf1
5 changed files with 103 additions and 40 deletions

View File

@@ -723,6 +723,9 @@ void KRCamera::renderPost(VkCommandBuffer& commandBuffer, KRSurface& surface)
}
}
KRTexture* fontTexture = m_pContext->getTextureManager()->getTexture("font");
fontTexture->resetPoolExpiry(0.0f, KRTexture::TEXTURE_USAGE_UI);
PipelineInfo info{};
std::string shader_name("debug_font");
info.shader_name = &shader_name;
@@ -733,10 +736,9 @@ void KRCamera::renderPost(VkCommandBuffer& commandBuffer, KRSurface& surface)
info.vertexAttributes = (1 << KRMesh::KRENGINE_ATTRIB_VERTEX) | (1 << KRMesh::KRENGINE_ATTRIB_TEXUVA);
info.modelFormat = ModelFormat::KRENGINE_MODEL_FORMAT_TRIANGLES;
KRPipeline* fontShader = m_pContext->getPipelineManager()->getPipeline(surface, info);
fontShader->setImageBinding("fontTexture", fontTexture, getContext().getSamplerManager()->DEFAULT_CLAMPED_SAMPLER);
fontShader->bind(commandBuffer, *this, m_viewport, Matrix4(), nullptr, nullptr, nullptr, KRNode::RENDER_PASS_FORWARD_TRANSPARENT);
m_pContext->getTextureManager()->selectTexture(0, m_pContext->getTextureManager()->getTexture("font"), 0.0f, KRTexture::TEXTURE_USAGE_UI);
m_debug_text_vbo_data.load(commandBuffer);
m_debug_text_vbo_data.bind(commandBuffer);

View File

@@ -48,6 +48,7 @@ using namespace kraken;
#include <set>
#include <list>
#include <map>
#include <variant>
#include <stack>

View File

@@ -113,7 +113,8 @@ const char* KRPipeline::KRENGINE_PUSH_CONSTANT_NAMES[] = {
KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const PipelineInfo& info, const char* szKey, const std::vector<KRShader*>& shaders, uint32_t vertexAttributes, ModelFormat modelFormat)
: KRContextObject(context)
{
for (PushConstantStageInfo& pushConstants : m_pushConstants) {
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
pushConstants.buffer = nullptr;
pushConstants.bufferSize = 0;
memset(pushConstants.size, 0, kPushConstantCount);
@@ -206,7 +207,9 @@ KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const PipelineInf
}
}
}
initPushConstantStage(shader->getShaderStage(), reflection);
initDescriptorSetStage(shader->getShaderStage(), reflection);
stageInfo.module = shaderModule;
stageInfo.pName = "main";
}
@@ -376,7 +379,8 @@ KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const PipelineInf
}
int iStage = 0;
for (PushConstantStageInfo& pushConstants : m_pushConstants) {
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.buffer) {
VkPipelineLayoutCreateInfo pushConstantsLayoutInfo{};
pushConstantsLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
@@ -474,25 +478,25 @@ KRPipeline::~KRPipeline()
if (m_descriptorSetLayout) {
// TODO: vkDestroyDescriptorSetLayout(device, m_descriptorSetLayout, nullptr);
}
for (PushConstantStageInfo& pushConstants : m_pushConstants) {
if (pushConstants.layout) {
// TODO: vkDestroyPipelineLayout(device, pushConstants.layout, nullptr);
}
}
if (getContext().getPipelineManager()->m_active_pipeline == this) {
getContext().getPipelineManager()->m_active_pipeline = NULL;
}
if (m_pushConstants[0].buffer) {
delete m_pushConstants[0].buffer;
m_pushConstants[0].buffer = nullptr;
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.layout) {
// TODO: vkDestroyPipelineLayout(device, pushConstants.layout, nullptr);
}
if (pushConstants.buffer) {
delete pushConstants.buffer;
pushConstants.buffer = nullptr;
}
}
}
void KRPipeline::initPushConstantStage(ShaderStage stage, const SpvReflectShaderModule* reflection)
{
PushConstantStageInfo& pushConstants = m_pushConstants[static_cast<int>(stage)];
PushConstantInfo& pushConstants = m_stages[static_cast<int>(stage)].pushConstants;
for (int i = 0; i < reflection->push_constant_block_count; i++) {
const SpvReflectBlockVariable& block = reflection->push_constant_blocks[i];
if (stricmp(block.name, "constants") == 0) {
@@ -516,10 +520,39 @@ void KRPipeline::initPushConstantStage(ShaderStage stage, const SpvReflectShader
}
}
void KRPipeline::initDescriptorSetStage(ShaderStage stage, const SpvReflectShaderModule* reflection)
{
std::vector<DescriptorSetInfo>& descriptorSets = m_stages[static_cast<int>(stage)].descriptorSets;
descriptorSets.reserve(reflection->descriptor_set_count);
for (int i = 0; i < reflection->descriptor_set_count; i++) {
SpvReflectDescriptorSet descriptorSet = reflection->descriptor_sets[i];
DescriptorSetInfo& descriptorSetInfo = descriptorSets.emplace_back();
descriptorSetInfo.query.reserve(descriptorSet.binding_count);
for (int j = 0; j < descriptorSet.binding_count; j++) {
SpvReflectDescriptorBinding& binding = *descriptorSet.bindings[j];
std::pair<VkDescriptorType, std::string>& descriptorQuery = descriptorSetInfo.query.emplace_back();
descriptorQuery.second = binding.name;
switch (binding.descriptor_type) {
case SPV_REFLECT_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
descriptorQuery.first = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
break;
case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
descriptorQuery.first = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
break;
default:
// Not supported
// TODO - Error handling
break;
}
}
}
}
bool KRPipeline::hasPushConstant(PushConstant location) const
{
for (const PushConstantStageInfo& stageConstants : m_pushConstants) {
if (stageConstants.size[static_cast<size_t>(location)]) {
for (const StageInfo& stageInfo : m_stages) {
const PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.size[static_cast<size_t>(location)]) {
return true;
}
}
@@ -528,9 +561,10 @@ bool KRPipeline::hasPushConstant(PushConstant location) const
void KRPipeline::setPushConstant(PushConstant location, float value)
{
for (PushConstantStageInfo& stageConstants : m_pushConstants) {
if (stageConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
float* constant = (float*)(m_pushConstants[0].buffer + stageConstants.offset[static_cast<size_t>(location)]);
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
float* constant = (float*)(pushConstants.buffer + pushConstants.offset[static_cast<size_t>(location)]);
*constant = value;
}
}
@@ -539,9 +573,10 @@ void KRPipeline::setPushConstant(PushConstant location, float value)
void KRPipeline::setPushConstant(PushConstant location, int value)
{
for (PushConstantStageInfo& stageConstants : m_pushConstants) {
if (stageConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
int* constant = (int*)(m_pushConstants[0].buffer + stageConstants.offset[static_cast<size_t>(location)]);
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
int* constant = (int*)(pushConstants.buffer + pushConstants.offset[static_cast<size_t>(location)]);
*constant = value;
}
}
@@ -549,18 +584,20 @@ void KRPipeline::setPushConstant(PushConstant location, int value)
void KRPipeline::setPushConstant(PushConstant location, const Vector2& value)
{
for (PushConstantStageInfo& stageConstants : m_pushConstants) {
if (stageConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
Vector2* constant = (Vector2*)(m_pushConstants[0].buffer + stageConstants.offset[static_cast<size_t>(location)]);
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
Vector2* constant = (Vector2*)(pushConstants.buffer + pushConstants.offset[static_cast<size_t>(location)]);
*constant = value;
}
}
}
void KRPipeline::setPushConstant(PushConstant location, const Vector3& value)
{
for (PushConstantStageInfo& stageConstants : m_pushConstants) {
if (stageConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
Vector3* constant = (Vector3*)(m_pushConstants[0].buffer + stageConstants.offset[static_cast<size_t>(location)]);
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
Vector3* constant = (Vector3*)(pushConstants.buffer + pushConstants.offset[static_cast<size_t>(location)]);
*constant = value;
}
}
@@ -568,9 +605,10 @@ void KRPipeline::setPushConstant(PushConstant location, const Vector3& value)
void KRPipeline::setPushConstant(PushConstant location, const Vector4& value)
{
for (PushConstantStageInfo& stageConstants : m_pushConstants) {
if (stageConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
Vector4* constant = (Vector4*)(m_pushConstants[0].buffer + stageConstants.offset[static_cast<size_t>(location)]);
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
Vector4* constant = (Vector4*)(pushConstants.buffer + pushConstants.offset[static_cast<size_t>(location)]);
*constant = value;
}
}
@@ -578,9 +616,10 @@ void KRPipeline::setPushConstant(PushConstant location, const Vector4& value)
void KRPipeline::setPushConstant(PushConstant location, const Matrix4& value)
{
for (PushConstantStageInfo& stageConstants : m_pushConstants) {
if (stageConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
Matrix4* constant = (Matrix4*)(m_pushConstants[0].buffer + m_pushConstants[0].offset[static_cast<size_t>(location)]);
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.size[static_cast<size_t>(location)] == sizeof(value)) {
Matrix4* constant = (Matrix4*)(pushConstants.buffer + pushConstants.offset[static_cast<size_t>(location)]);
*constant = value;
}
}
@@ -588,9 +627,10 @@ void KRPipeline::setPushConstant(PushConstant location, const Matrix4& value)
void KRPipeline::setPushConstant(PushConstant location, const Matrix4* value, const size_t count)
{
for (PushConstantStageInfo& stageConstants : m_pushConstants) {
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
// TODO - Vulkan refactoring
// GLDEBUG(glUniformMatrix4fv(pShader->m_pushConstants[0].offset[KRPipeline::PushConstant::bone_transforms], (GLsizei)bones.size(), GL_FALSE, bone_mats));
// GLDEBUG(glUniformMatrix4fv(pushConstants.offset[KRPipeline::PushConstant::bone_transforms], (GLsizei)bones.size(), GL_FALSE, bone_mats));
}
}
@@ -786,7 +826,8 @@ bool KRPipeline::bind(VkCommandBuffer& commandBuffer, KRCamera& camera, const KR
setPushConstant(PushConstant::render_frame, 1);
setPushConstant(PushConstant::volumetric_environment_frame, 2);
for (PushConstantStageInfo& pushConstants : m_pushConstants) {
for (StageInfo& stageInfo : m_stages) {
PushConstantInfo& pushConstants = stageInfo.pushConstants;
if (pushConstants.buffer) {
vkCmdPushConstants(commandBuffer, pushConstants.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, pushConstants.bufferSize, pushConstants.buffer);
}
@@ -806,3 +847,8 @@ VkPipeline& KRPipeline::getPipeline()
{
return m_graphicsPipeline;
}
void KRPipeline::setImageBinding(const std::string& name, KRTexture* texture, KRSampler* sampler)
{
// TODO - Implement
}

View File

@@ -39,6 +39,7 @@
#include "KRMesh.h"
#include "KRShader.h"
class KRSampler;
class KRShader;
class KRSurface;
class KRRenderPass;
@@ -304,6 +305,8 @@ public:
void setPushConstant(PushConstant location, const Matrix4& value);
void setPushConstant(PushConstant location, const Matrix4* value, const size_t count);
void setImageBinding(const std::string& name, KRTexture* texture, KRSampler* sampler);
VkPipeline& getPipeline();
private:
@@ -318,8 +321,19 @@ private:
VkPipelineLayout layout;
};
struct ImageDescriptorInfo
{
KRTexture* texture;
KRSampler* sampler;
};
struct UniformBufferDescriptorInfo
{
KRUniformBuffer* buffer;
};
typedef std::vector<std::pair<VkDescriptorType, std::string>> DescriptorSetQuery;
typedef std::vector<std::variant<KRTexture*, KRUniformBuffer*>> DescriptorSetBinding;
typedef std::vector<std::variant<ImageDescriptorInfo, UniformBufferDescriptorInfo>> DescriptorSetBinding;
struct DescriptorSetInfo
{

View File

@@ -32,11 +32,11 @@
#version 450
layout(location = 0) in mediump vec2 textureCoordinate;
layout(binding = 1) uniform sampler2D diffuseTexture;
layout(binding = 0) uniform sampler2D fontTexture;
layout(location = 0) out vec4 outColor;
void main()
{
vec4 font_color = texture(diffuseTexture, textureCoordinate);
vec4 font_color = texture(fontTexture, textureCoordinate);
outColor = vec4(font_color.r, font_color.g, font_color.b, font_color.r + 0.50);
}