Skip to content

Commit

Permalink
Allow specialization constant overrides in config (#46)
Browse files Browse the repository at this point in the history
* Allow specialization constant overrides in config

Only simple types (bool, int, float) for now.

Example:
```ini
reshade = reshade-shaders/Pixelate.fx

# type  = slider
# min   = 2
# max   = 48
# Cell Size
#cell_size = 4
cell_size = 2

# type  = slider
# min   = 0.0
# max   = 1.0
# Smoothness
#avg_amount = 0.333
avg_amount = 1.0
```

* Move temporary variables initializations into loop

* Use smallCamelCase for variables

I want a at least somewhat consistent naming scheme

Co-authored-by: DadSchoorse <[email protected]>
  • Loading branch information
pchome and DadSchoorse committed Jan 5, 2020
1 parent f2519c6 commit 7762519
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/effect_reshade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cassert>

#include <set>
#include <variant>

#include "image_view.hpp"
#include "descriptor_set.hpp"
Expand Down Expand Up @@ -378,14 +379,72 @@ namespace vkBasalt

//pipeline

// Configure effect
std::vector<VkSpecializationMapEntry> specMapEntrys;
std::vector<char> specData;

for (uint32_t specId = 0, offset = 0; auto& opt : module.spec_constants)
{
if (!opt.name.empty())
{
auto val = pConfig->getOption(opt.name);
if (!val.empty())
{
std::variant<int32_t,uint32_t,float> convertedValue;
offset = static_cast<uint32_t>(specData.size());
switch(opt.type.base)
{
case reshadefx::type::t_bool:
convertedValue = (val == "true" || val == "1") ? 1 : 0;
specData.resize(offset + sizeof(VkBool32));
std::memcpy(specData.data() + offset, &convertedValue, sizeof(VkBool32));
specMapEntrys.push_back({specId, offset, sizeof(VkBool32)});
break;
case reshadefx::type::t_int:
convertedValue = std::stoi(val);
specData.resize(offset + sizeof(int32_t));
std::memcpy(specData.data() + offset, &convertedValue, sizeof(int32_t));
specMapEntrys.push_back({specId, offset, sizeof(int32_t)});
break;
case reshadefx::type::t_uint:
convertedValue = static_cast<uint32_t>(std::stoul(val));
specData.resize(offset + sizeof(uint32_t));
std::memcpy(specData.data() + offset, &convertedValue, sizeof(uint32_t));
specMapEntrys.push_back({specId, offset, sizeof(uint32_t)});
break;
case reshadefx::type::t_float:
convertedValue = std::stof(val);
specData.resize(offset + sizeof(float));
std::memcpy(specData.data() + offset, &convertedValue, sizeof(float));
specMapEntrys.push_back({specId, offset, sizeof(float)});
break;
default:
// do nothing
break;
}
}
}
specId++;
}

VkSpecializationInfo specializationInfo;
if (specMapEntrys.size() > 0) {
specializationInfo = {
.mapEntryCount = static_cast<uint32_t>(specMapEntrys.size()),
.pMapEntries = specMapEntrys.data(),
.dataSize = specData.size(),
.pData = specData.data()
};
}

VkPipelineShaderStageCreateInfo shaderStageCreateInfoVert;
shaderStageCreateInfoVert.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStageCreateInfoVert.pNext = nullptr;
shaderStageCreateInfoVert.flags = 0;
shaderStageCreateInfoVert.stage = VK_SHADER_STAGE_VERTEX_BIT;
shaderStageCreateInfoVert.module = shaderModule;
shaderStageCreateInfoVert.pName = pass.vs_entry_point.c_str();
shaderStageCreateInfoVert.pSpecializationInfo = nullptr;
shaderStageCreateInfoVert.pSpecializationInfo = (specMapEntrys.size() > 0) ? &specializationInfo : nullptr;

VkPipelineShaderStageCreateInfo shaderStageCreateInfoFrag;
shaderStageCreateInfoFrag.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Expand All @@ -394,7 +453,7 @@ namespace vkBasalt
shaderStageCreateInfoFrag.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
shaderStageCreateInfoFrag.module = shaderModule;
shaderStageCreateInfoFrag.pName = pass.ps_entry_point.c_str();
shaderStageCreateInfoFrag.pSpecializationInfo = nullptr;
shaderStageCreateInfoFrag.pSpecializationInfo = (specMapEntrys.size() > 0) ? &specializationInfo : nullptr;

VkPipelineShaderStageCreateInfo shaderStages[] = {shaderStageCreateInfoVert,shaderStageCreateInfoFrag};

Expand Down

0 comments on commit 7762519

Please sign in to comment.