Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions common/output_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,15 @@ void SpvReflectToYaml::Write(std::ostream& os) {
os << t2 << "- *bv" << itor->second << " # " << SafeString(sm_.push_constant_blocks[i].name) << std::endl;
}

// uint32_t spec_constant_count;
os << t1 << "specialization_constant_count: " << sm_.spec_constant_count << ",\n";
// SpvReflectSpecializationConstant* spec_constants;
os << t1 << "specialization_constants:" << std::endl;
for (uint32_t i = 0; i < sm_.spec_constant_count; ++i) {
os << t3 << "spirv_id: " << sm_.spec_constants[i].spirv_id << std::endl;
os << t3 << "constant_id: " << sm_.spec_constants[i].constant_id << std::endl;
}

if (verbosity_ >= 2) {
// struct Internal {
os << t1 << "_internal:" << std::endl;
Expand Down
55 changes: 51 additions & 4 deletions spirv_reflect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,8 @@ static SpvReflectResult ParseNames(SpvReflectPrvParser* p_parser) {
return SPV_REFLECT_RESULT_SUCCESS;
}

static SpvReflectResult ParseDecorations(SpvReflectPrvParser* p_parser) {
static SpvReflectResult ParseDecorations(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {
uint32_t spec_constant_count = 0;
for (uint32_t i = 0; i < p_parser->node_count; ++i) {
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]);

Expand Down Expand Up @@ -1538,8 +1539,7 @@ static SpvReflectResult ParseDecorations(SpvReflectPrvParser* p_parser) {
} break;

case SpvDecorationSpecId: {
uint32_t word_offset = p_node->word_offset + member_offset + 3;
CHECKED_READU32(p_parser, word_offset, p_target_decorations->spec_id);
spec_constant_count++;
} break;

case SpvDecorationHlslCounterBufferGOOGLE: {
Expand All @@ -1563,6 +1563,27 @@ static SpvReflectResult ParseDecorations(SpvReflectPrvParser* p_parser) {
} break;
}
}

if (spec_constant_count > 0) {
p_module->spec_constants = (SpvReflectSpecializationConstant*)calloc(spec_constant_count, sizeof(*p_module->spec_constants));
if (IsNull(p_module->spec_constants)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
}
for (uint32_t i = 0; i < p_parser->node_count; ++i) {
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]);
if (p_node->op == SpvOpDecorate) {
uint32_t decoration = (uint32_t)INVALID_VALUE;
CHECKED_READU32(p_parser, p_node->word_offset + 2, decoration);
if (decoration == SpvDecorationSpecId) {
const uint32_t count = p_module->spec_constant_count;
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_module->spec_constants[count].constant_id);
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_module->spec_constants[count].spirv_id);
p_module->spec_constant_count++;
}
}
}

return SPV_REFLECT_RESULT_SUCCESS;
}

Expand Down Expand Up @@ -3861,7 +3882,7 @@ static SpvReflectResult CreateShaderModule(uint32_t flags, size_t size, const vo
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS);
}
if (result == SPV_REFLECT_RESULT_SUCCESS) {
result = ParseDecorations(&parser);
result = ParseDecorations(&parser, p_module);
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS);
}

Expand Down Expand Up @@ -4050,6 +4071,7 @@ void spvReflectDestroyShaderModule(SpvReflectShaderModule* p_module) {
}
SafeFree(p_module->capabilities);
SafeFree(p_module->entry_points);
SafeFree(p_module->spec_constants);

// Push constants
for (size_t i = 0; i < p_module->push_constant_block_count; ++i) {
Expand Down Expand Up @@ -4455,6 +4477,31 @@ SpvReflectResult spvReflectEnumerateEntryPointPushConstantBlocks(const SpvReflec
return SPV_REFLECT_RESULT_SUCCESS;
}

SpvReflectResult spvReflectEnumerateSpecializationConstants(const SpvReflectShaderModule* p_module, uint32_t* p_count,
SpvReflectSpecializationConstant** pp_constants) {
if (IsNull(p_module)) {
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
}
if (IsNull(p_count)) {
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
}

if (IsNotNull(pp_constants)) {
if (*p_count != p_module->spec_constant_count) {
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
}

for (uint32_t index = 0; index < *p_count; ++index) {
SpvReflectSpecializationConstant* p_constant = (SpvReflectSpecializationConstant*)&p_module->spec_constants[index];
pp_constants[index] = p_constant;
}
} else {
*p_count = p_module->spec_constant_count;
}

return SPV_REFLECT_RESULT_SUCCESS;
}

const SpvReflectDescriptorBinding* spvReflectGetDescriptorBinding(const SpvReflectShaderModule* p_module, uint32_t binding_number,
uint32_t set_number, SpvReflectResult* p_result) {
const SpvReflectDescriptorBinding* p_descriptor = NULL;
Expand Down
49 changes: 49 additions & 0 deletions spirv_reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,15 @@ typedef struct SpvReflectCapability {
uint32_t word_offset;
} SpvReflectCapability;


/*! @struct SpvReflectSpecId

*/
typedef struct SpvReflectSpecializationConstant {
uint32_t spirv_id;
uint32_t constant_id;
} SpvReflectSpecializationConstant;

/*! @struct SpvReflectShaderModule

*/
Expand Down Expand Up @@ -548,6 +557,8 @@ typedef struct SpvReflectShaderModule {
SpvReflectInterfaceVariable* interface_variables; // Uses value(s) from first entry point
uint32_t push_constant_block_count; // Uses value(s) from first entry point
SpvReflectBlockVariable* push_constant_blocks; // Uses value(s) from first entry point
uint32_t spec_constant_count; // Uses value(s) from first entry point
SpvReflectSpecializationConstant* spec_constants; // Uses value(s) from first entry point

struct Internal {
SpvReflectModuleFlags module_flags;
Expand Down Expand Up @@ -959,6 +970,25 @@ SpvReflectResult spvReflectEnumerateEntryPointPushConstantBlocks(
);


/*! @fn spvReflectEnumerateSpecializationConstants
@param p_module Pointer to an instance of SpvReflectShaderModule.
@param p_count If pp_blocks is NULL, the module's specialization constant
count will be stored here. If pp_blocks is not NULL, *p_count
must contain the module's specialization constant count.
@param pp_constants If NULL, the module's specialization constant count
will be written to *p_count. If non-NULL, pp_blocks must
point to an array with *p_count entries, where pointers to
the module's specialization constant blocks will be written.
The caller must not free the variables written to this array.
@return If successful, returns SPV_REFLECT_RESULT_SUCCESS.
Otherwise, the error code indicates the cause of the failure.
*/
SpvReflectResult spvReflectEnumerateSpecializationConstants(
const SpvReflectShaderModule* p_module,
uint32_t* p_count,
SpvReflectSpecializationConstant** pp_constants
);

/*! @fn spvReflectGetDescriptorBinding

@param p_module Pointer to an instance of SpvReflectShaderModule.
Expand Down Expand Up @@ -1549,6 +1579,7 @@ class ShaderModule {
SpvReflectResult EnumeratePushConstants(uint32_t* p_count, SpvReflectBlockVariable** pp_blocks) const {
return EnumeratePushConstantBlocks(p_count, pp_blocks);
}
SpvReflectResult EnumerateSpecializationConstants(uint32_t* p_count, SpvReflectSpecializationConstant** pp_constants) const;

const SpvReflectDescriptorBinding* GetDescriptorBinding(uint32_t binding_number, uint32_t set_number, SpvReflectResult* p_result = nullptr) const;
const SpvReflectDescriptorBinding* GetEntryPointDescriptorBinding(const char* entry_point, uint32_t binding_number, uint32_t set_number, SpvReflectResult* p_result = nullptr) const;
Expand Down Expand Up @@ -1996,6 +2027,24 @@ inline SpvReflectResult ShaderModule::EnumeratePushConstantBlocks(
return m_result;
}

/*! @fn EnumerateSpecializationConstants
@param p_count
@param pp_constants
@return
*/
inline SpvReflectResult ShaderModule::EnumerateSpecializationConstants(
uint32_t* p_count,
SpvReflectSpecializationConstant** pp_constants
) const
{
m_result = spvReflectEnumerateSpecializationConstants(
&m_module,
p_count,
pp_constants
);
return m_result;
}

/*! @fn EnumerateEntryPointPushConstantBlocks

@param entry_point
Expand Down
2 changes: 2 additions & 0 deletions tests/16bit/vert_in_out_16.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,6 @@ module:
- *iv11 # "_f"
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/access_chains/array_length_from_access_chain.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/cbuffer_unused/cbuffer_unused_001.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3393,4 +3393,6 @@ module:
- *iv1 #
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/entry_exec_mode/comp_local_size.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/entry_exec_mode/geom_inv_out_vert.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,6 @@ module:
- *iv9 # ""
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/execution_mode/local_size_id.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/execution_mode/local_size_id_spec.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_0.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1299,4 +1299,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_1.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_2.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -820,4 +820,6 @@ module:
push_constant_count: 1,
push_constants:
- *bv8 # "pc"
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_3.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_4.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,6 @@ module:
push_constant_count: 1,
push_constants:
- *bv5 # "params"
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_5.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,6 @@ module:
push_constant_count: 1,
push_constants:
- *bv3 # "params"
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_6.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,6 @@ module:
push_constant_count: 1,
push_constants:
- *bv4 # "g_GlobalsBDAPushConstant_0"
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_7.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,6 @@ module:
push_constant_count: 1,
push_constants:
- *bv4 # "params"
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_handle_8.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/buffer_pointer.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,6 @@ module:
push_constant_count: 1,
push_constants:
- *bv4 # "push"
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/built_in_format.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,6 @@ module:
- *iv6 # "texcoord"
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/fn_struct_param.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,6 @@ module:
- *iv0 # "outColor"
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/frag_array_input.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -755,4 +755,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/frag_barycentric.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,6 @@ module:
- *iv3 # "value"
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/input_attachment.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,6 @@ module:
- *iv0 # "oColor"
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/io_vars_vs.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,6 @@ module:
- *iv8 # "normals"
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/matrix_major_order_glsl.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1048,4 +1048,6 @@ module:
- *iv1 # "my_output"
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/non_writable_image.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/runtime_array_of_array_of_struct.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/storage_buffer.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,6 @@ module:
output_variables:
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/glsl/texel_buffer.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,6 @@ module:
- *iv5 # ""
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
2 changes: 2 additions & 0 deletions tests/hlsl/append_consume.spv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,6 @@ module:
- *iv1 # "out.var.SV_TARGET"
push_constant_count: 0,
push_constants:
specialization_constant_count: 0,
specialization_constants:
...
Loading