| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "spirv_reflect.h" |
| 18 |
|
| 19 |
#include <assert.h> |
| 20 |
#include <stdbool.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#if defined(WIN32) |
| 24 |
#define _CRTDBG_MAP_ALLOC |
| 25 |
#include <crtdbg.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#else |
| 28 |
#include <stdlib.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 7) || defined(__APPLE_CC__) |
| 32 |
#define FALLTHROUGH __attribute__((fallthrough)) |
| 33 |
#else |
| 34 |
#define FALLTHROUGH |
| 35 |
#endif |
| 36 |
|
| 37 |
#if defined(SPIRV_REFLECT_ENABLE_ASSERTS) |
| 38 |
#define SPV_REFLECT_ASSERT(COND) assert(COND); |
| 39 |
#else |
| 40 |
#define SPV_REFLECT_ASSERT(COND) |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
enum { |
| 45 |
SPIRV_STARTING_WORD_INDEX = 5, |
| 46 |
SPIRV_WORD_SIZE = sizeof(uint32_t), |
| 47 |
SPIRV_BYTE_WIDTH = 8, |
| 48 |
SPIRV_MINIMUM_FILE_SIZE = SPIRV_STARTING_WORD_INDEX * SPIRV_WORD_SIZE, |
| 49 |
SPIRV_DATA_ALIGNMENT = 4 * SPIRV_WORD_SIZE, |
| 50 |
SPIRV_ACCESS_CHAIN_INDEX_OFFSET = 4, |
| 51 |
}; |
| 52 |
|
| 53 |
enum { |
| 54 |
INVALID_VALUE = 0xFFFFFFFF, |
| 55 |
}; |
| 56 |
|
| 57 |
enum { |
| 58 |
MAX_NODE_NAME_LENGTH = 1024, |
| 59 |
|
| 60 |
MAX_RECURSIVE_PHYSICAL_POINTER_CHECK = 128, |
| 61 |
}; |
| 62 |
|
| 63 |
enum { |
| 64 |
IMAGE_SAMPLED = 1, |
| 65 |
IMAGE_STORAGE = 2, |
| 66 |
}; |
| 67 |
|
| 68 |
typedef struct SpvReflectPrvArrayTraits { |
| 69 |
uint32_t element_type_id; |
| 70 |
uint32_t length_id; |
| 71 |
} SpvReflectPrvArrayTraits; |
| 72 |
|
| 73 |
typedef struct SpvReflectPrvImageTraits { |
| 74 |
uint32_t sampled_type_id; |
| 75 |
SpvDim dim; |
| 76 |
uint32_t depth; |
| 77 |
uint32_t arrayed; |
| 78 |
uint32_t ms; |
| 79 |
uint32_t sampled; |
| 80 |
SpvImageFormat image_format; |
| 81 |
} SpvReflectPrvImageTraits; |
| 82 |
|
| 83 |
typedef struct SpvReflectPrvNumberDecoration { |
| 84 |
uint32_t word_offset; |
| 85 |
uint32_t value; |
| 86 |
} SpvReflectPrvNumberDecoration; |
| 87 |
|
| 88 |
typedef struct SpvReflectPrvStringDecoration { |
| 89 |
uint32_t word_offset; |
| 90 |
const char* value; |
| 91 |
} SpvReflectPrvStringDecoration; |
| 92 |
|
| 93 |
typedef struct SpvReflectPrvDecorations { |
| 94 |
bool is_relaxed_precision; |
| 95 |
bool is_block; |
| 96 |
bool is_buffer_block; |
| 97 |
bool is_row_major; |
| 98 |
bool is_column_major; |
| 99 |
bool is_built_in; |
| 100 |
bool is_noperspective; |
| 101 |
bool is_flat; |
| 102 |
bool is_non_writable; |
| 103 |
bool is_non_readable; |
| 104 |
bool is_patch; |
| 105 |
bool is_per_vertex; |
| 106 |
bool is_per_task; |
| 107 |
bool is_weight_texture; |
| 108 |
bool is_block_match_texture; |
| 109 |
SpvReflectUserType user_type; |
| 110 |
SpvReflectPrvNumberDecoration set; |
| 111 |
SpvReflectPrvNumberDecoration binding; |
| 112 |
SpvReflectPrvNumberDecoration input_attachment_index; |
| 113 |
SpvReflectPrvNumberDecoration location; |
| 114 |
SpvReflectPrvNumberDecoration component; |
| 115 |
SpvReflectPrvNumberDecoration offset; |
| 116 |
SpvReflectPrvNumberDecoration uav_counter_buffer; |
| 117 |
SpvReflectPrvStringDecoration semantic; |
| 118 |
uint32_t array_stride; |
| 119 |
uint32_t matrix_stride; |
| 120 |
uint32_t spec_id; |
| 121 |
SpvBuiltIn built_in; |
| 122 |
} SpvReflectPrvDecorations; |
| 123 |
|
| 124 |
typedef struct SpvReflectPrvNode { |
| 125 |
uint32_t result_id; |
| 126 |
SpvOp op; |
| 127 |
uint32_t result_type_id; |
| 128 |
uint32_t type_id; |
| 129 |
SpvCapability capability; |
| 130 |
SpvStorageClass storage_class; |
| 131 |
uint32_t word_offset; |
| 132 |
uint32_t word_count; |
| 133 |
bool is_type; |
| 134 |
|
| 135 |
SpvReflectPrvArrayTraits array_traits; |
| 136 |
SpvReflectPrvImageTraits image_traits; |
| 137 |
uint32_t image_type_id; |
| 138 |
|
| 139 |
const char* name; |
| 140 |
SpvReflectPrvDecorations decorations; |
| 141 |
uint32_t member_count; |
| 142 |
const char** member_names; |
| 143 |
SpvReflectPrvDecorations* member_decorations; |
| 144 |
} SpvReflectPrvNode; |
| 145 |
|
| 146 |
typedef struct SpvReflectPrvString { |
| 147 |
uint32_t result_id; |
| 148 |
const char* string; |
| 149 |
} SpvReflectPrvString; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
typedef struct SpvReflectPrvAccessedVariable { |
| 160 |
SpvReflectPrvNode* p_node; |
| 161 |
uint32_t result_id; |
| 162 |
uint32_t variable_ptr; |
| 163 |
} SpvReflectPrvAccessedVariable; |
| 164 |
|
| 165 |
typedef struct SpvReflectPrvFunction { |
| 166 |
uint32_t id; |
| 167 |
uint32_t callee_count; |
| 168 |
uint32_t* callees; |
| 169 |
struct SpvReflectPrvFunction** callee_ptrs; |
| 170 |
uint32_t accessed_variable_count; |
| 171 |
SpvReflectPrvAccessedVariable* accessed_variables; |
| 172 |
} SpvReflectPrvFunction; |
| 173 |
|
| 174 |
typedef struct SpvReflectPrvAccessChain { |
| 175 |
uint32_t result_id; |
| 176 |
uint32_t result_type_id; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
uint32_t base_id; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
uint32_t index_count; |
| 187 |
uint32_t* indexes; |
| 188 |
|
| 189 |
|
| 190 |
SpvReflectBlockVariable* block_var; |
| 191 |
} SpvReflectPrvAccessChain; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
typedef struct SpvReflectPrvPhysicalPointerStruct { |
| 197 |
uint32_t struct_id; |
| 198 |
|
| 199 |
SpvReflectBlockVariable* p_var; |
| 200 |
} SpvReflectPrvPhysicalPointerStruct; |
| 201 |
|
| 202 |
typedef struct SpvReflectPrvParser { |
| 203 |
size_t spirv_word_count; |
| 204 |
uint32_t* spirv_code; |
| 205 |
uint32_t string_count; |
| 206 |
SpvReflectPrvString* strings; |
| 207 |
SpvSourceLanguage source_language; |
| 208 |
uint32_t source_language_version; |
| 209 |
uint32_t source_file_id; |
| 210 |
const char* source_embedded; |
| 211 |
size_t node_count; |
| 212 |
SpvReflectPrvNode* nodes; |
| 213 |
uint32_t entry_point_count; |
| 214 |
uint32_t capability_count; |
| 215 |
uint32_t function_count; |
| 216 |
SpvReflectPrvFunction* functions; |
| 217 |
uint32_t access_chain_count; |
| 218 |
SpvReflectPrvAccessChain* access_chains; |
| 219 |
|
| 220 |
uint32_t type_count; |
| 221 |
uint32_t descriptor_count; |
| 222 |
uint32_t push_constant_count; |
| 223 |
|
| 224 |
SpvReflectTypeDescription* physical_pointer_check[MAX_RECURSIVE_PHYSICAL_POINTER_CHECK]; |
| 225 |
uint32_t physical_pointer_count; |
| 226 |
|
| 227 |
SpvReflectPrvPhysicalPointerStruct* physical_pointer_structs; |
| 228 |
uint32_t physical_pointer_struct_count; |
| 229 |
} SpvReflectPrvParser; |
| 230 |
|
| 231 |
|
| 232 |
static uint32_t Max(uint32_t a, uint32_t b) { return a > b ? a : b; } |
| 233 |
static uint32_t Min(uint32_t a, uint32_t b) { return a < b ? a : b; } |
| 234 |
|
| 235 |
static uint32_t RoundUp(uint32_t value, uint32_t multiple) { |
| 236 |
assert(multiple && ((multiple & (multiple - 1)) == 0)); |
| 237 |
return (value + multiple - 1) & ~(multiple - 1); |
| 238 |
} |
| 239 |
|
| 240 |
#define IsNull(ptr) (ptr == NULL) |
| 241 |
|
| 242 |
#define IsNotNull(ptr) (ptr != NULL) |
| 243 |
|
| 244 |
#define SafeFree(ptr) \ |
| 245 |
{ \ |
| 246 |
free((void*)ptr); \ |
| 247 |
ptr = NULL; \ |
| 248 |
} |
| 249 |
|
| 250 |
static int SortCompareUint32(const void* a, const void* b) { |
| 251 |
const uint32_t* p_a = (const uint32_t*)a; |
| 252 |
const uint32_t* p_b = (const uint32_t*)b; |
| 253 |
|
| 254 |
return (int)*p_a - (int)*p_b; |
| 255 |
} |
| 256 |
|
| 257 |
static int SortCompareAccessedVariable(const void* a, const void* b) { |
| 258 |
const SpvReflectPrvAccessedVariable* p_a = (const SpvReflectPrvAccessedVariable*)a; |
| 259 |
const SpvReflectPrvAccessedVariable* p_b = (const SpvReflectPrvAccessedVariable*)b; |
| 260 |
|
| 261 |
return (int)p_a->variable_ptr - (int)p_b->variable_ptr; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
static size_t DedupSortedUint32(uint32_t* arr, size_t size) { |
| 272 |
if (size == 0) { |
| 273 |
return 0; |
| 274 |
} |
| 275 |
size_t dedup_idx = 0; |
| 276 |
for (size_t i = 0; i < size; ++i) { |
| 277 |
if (arr[dedup_idx] != arr[i]) { |
| 278 |
++dedup_idx; |
| 279 |
arr[dedup_idx] = arr[i]; |
| 280 |
} |
| 281 |
} |
| 282 |
return dedup_idx + 1; |
| 283 |
} |
| 284 |
|
| 285 |
static bool SearchSortedUint32(const uint32_t* arr, size_t size, uint32_t target) { |
| 286 |
size_t lo = 0; |
| 287 |
size_t hi = size; |
| 288 |
while (lo < hi) { |
| 289 |
size_t mid = (hi - lo) / 2 + lo; |
| 290 |
if (arr[mid] == target) { |
| 291 |
return true; |
| 292 |
} else if (arr[mid] < target) { |
| 293 |
lo = mid + 1; |
| 294 |
} else { |
| 295 |
hi = mid; |
| 296 |
} |
| 297 |
} |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
static SpvReflectResult IntersectSortedAccessedVariable(const SpvReflectPrvAccessedVariable* p_arr0, size_t arr0_size, |
| 302 |
const uint32_t* p_arr1, size_t arr1_size, uint32_t** pp_res, |
| 303 |
size_t* res_size) { |
| 304 |
*pp_res = NULL; |
| 305 |
*res_size = 0; |
| 306 |
if (IsNull(p_arr0) || IsNull(p_arr1)) { |
| 307 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 308 |
} |
| 309 |
|
| 310 |
const SpvReflectPrvAccessedVariable* p_arr0_end = p_arr0 + arr0_size; |
| 311 |
const uint32_t* p_arr1_end = p_arr1 + arr1_size; |
| 312 |
|
| 313 |
const SpvReflectPrvAccessedVariable* p_idx0 = p_arr0; |
| 314 |
const uint32_t* p_idx1 = p_arr1; |
| 315 |
while (p_idx0 != p_arr0_end && p_idx1 != p_arr1_end) { |
| 316 |
if (p_idx0->variable_ptr < *p_idx1) { |
| 317 |
++p_idx0; |
| 318 |
} else if (p_idx0->variable_ptr > *p_idx1) { |
| 319 |
++p_idx1; |
| 320 |
} else { |
| 321 |
++*res_size; |
| 322 |
++p_idx0; |
| 323 |
++p_idx1; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if (*res_size > 0) { |
| 328 |
*pp_res = (uint32_t*)calloc(*res_size, sizeof(**pp_res)); |
| 329 |
if (IsNull(*pp_res)) { |
| 330 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 331 |
} |
| 332 |
uint32_t* p_idxr = *pp_res; |
| 333 |
p_idx0 = p_arr0; |
| 334 |
p_idx1 = p_arr1; |
| 335 |
while (p_idx0 != p_arr0_end && p_idx1 != p_arr1_end) { |
| 336 |
if (p_idx0->variable_ptr < *p_idx1) { |
| 337 |
++p_idx0; |
| 338 |
} else if (p_idx0->variable_ptr > *p_idx1) { |
| 339 |
++p_idx1; |
| 340 |
} else { |
| 341 |
*(p_idxr++) = p_idx0->variable_ptr; |
| 342 |
++p_idx0; |
| 343 |
++p_idx1; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 348 |
} |
| 349 |
|
| 350 |
static bool InRange(const SpvReflectPrvParser* p_parser, uint32_t index) { |
| 351 |
bool in_range = false; |
| 352 |
if (IsNotNull(p_parser)) { |
| 353 |
in_range = (index < p_parser->spirv_word_count); |
| 354 |
} |
| 355 |
return in_range; |
| 356 |
} |
| 357 |
|
| 358 |
static SpvReflectResult ReadU32(SpvReflectPrvParser* p_parser, uint32_t word_offset, uint32_t* p_value) { |
| 359 |
assert(IsNotNull(p_parser)); |
| 360 |
assert(IsNotNull(p_parser->spirv_code)); |
| 361 |
assert(InRange(p_parser, word_offset)); |
| 362 |
SpvReflectResult result = SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF; |
| 363 |
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && InRange(p_parser, word_offset)) { |
| 364 |
*p_value = *(p_parser->spirv_code + word_offset); |
| 365 |
result = SPV_REFLECT_RESULT_SUCCESS; |
| 366 |
} |
| 367 |
return result; |
| 368 |
} |
| 369 |
|
| 370 |
#define UNCHECKED_READU32(parser, word_offset, value) \ |
| 371 |
{ (void)ReadU32(parser, word_offset, (uint32_t*)&(value)); } |
| 372 |
|
| 373 |
#define CHECKED_READU32(parser, word_offset, value) \ |
| 374 |
{ \ |
| 375 |
SpvReflectResult checked_readu32_result = ReadU32(parser, word_offset, (uint32_t*)&(value)); \ |
| 376 |
if (checked_readu32_result != SPV_REFLECT_RESULT_SUCCESS) { \ |
| 377 |
return checked_readu32_result; \ |
| 378 |
} \ |
| 379 |
} |
| 380 |
|
| 381 |
#define CHECKED_READU32_CAST(parser, word_offset, cast_to_type, value) \ |
| 382 |
{ \ |
| 383 |
uint32_t checked_readu32_cast_u32 = UINT32_MAX; \ |
| 384 |
SpvReflectResult checked_readu32_cast_result = ReadU32(parser, word_offset, (uint32_t*)&(checked_readu32_cast_u32)); \ |
| 385 |
if (checked_readu32_cast_result != SPV_REFLECT_RESULT_SUCCESS) { \ |
| 386 |
return checked_readu32_cast_result; \ |
| 387 |
} \ |
| 388 |
value = (cast_to_type)checked_readu32_cast_u32; \ |
| 389 |
} |
| 390 |
|
| 391 |
#define IF_READU32(result, parser, word_offset, value) \ |
| 392 |
if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \ |
| 393 |
result = ReadU32(parser, word_offset, (uint32_t*)&(value)); \ |
| 394 |
} |
| 395 |
|
| 396 |
#define IF_READU32_CAST(result, parser, word_offset, cast_to_type, value) \ |
| 397 |
if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \ |
| 398 |
uint32_t if_readu32_cast_u32 = UINT32_MAX; \ |
| 399 |
result = ReadU32(parser, word_offset, &if_readu32_cast_u32); \ |
| 400 |
if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \ |
| 401 |
value = (cast_to_type)if_readu32_cast_u32; \ |
| 402 |
} \ |
| 403 |
} |
| 404 |
|
| 405 |
static SpvReflectResult ReadStr(SpvReflectPrvParser* p_parser, uint32_t word_offset, uint32_t word_index, uint32_t word_count, |
| 406 |
uint32_t* p_buf_size, char* p_buf) { |
| 407 |
uint32_t limit = (word_offset + word_count); |
| 408 |
assert(IsNotNull(p_parser)); |
| 409 |
assert(IsNotNull(p_parser->spirv_code)); |
| 410 |
assert(InRange(p_parser, limit)); |
| 411 |
SpvReflectResult result = SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF; |
| 412 |
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && InRange(p_parser, limit)) { |
| 413 |
const char* c_str = (const char*)(p_parser->spirv_code + word_offset + word_index); |
| 414 |
uint32_t n = word_count * SPIRV_WORD_SIZE; |
| 415 |
uint32_t length_with_terminator = 0; |
| 416 |
for (uint32_t i = 0; i < n; ++i) { |
| 417 |
char c = *(c_str + i); |
| 418 |
if (c == 0) { |
| 419 |
length_with_terminator = i + 1; |
| 420 |
break; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
if (length_with_terminator > 0) { |
| 425 |
result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 426 |
if (IsNotNull(p_buf_size) && IsNotNull(p_buf)) { |
| 427 |
result = SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED; |
| 428 |
if (length_with_terminator <= *p_buf_size) { |
| 429 |
memset(p_buf, 0, *p_buf_size); |
| 430 |
memcpy(p_buf, c_str, length_with_terminator); |
| 431 |
result = SPV_REFLECT_RESULT_SUCCESS; |
| 432 |
} |
| 433 |
} else { |
| 434 |
if (IsNotNull(p_buf_size)) { |
| 435 |
*p_buf_size = length_with_terminator; |
| 436 |
result = SPV_REFLECT_RESULT_SUCCESS; |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
return result; |
| 442 |
} |
| 443 |
|
| 444 |
static SpvReflectDecorationFlags ApplyDecorations(const SpvReflectPrvDecorations* p_decoration_fields) { |
| 445 |
SpvReflectDecorationFlags decorations = SPV_REFLECT_DECORATION_NONE; |
| 446 |
if (p_decoration_fields->is_relaxed_precision) { |
| 447 |
decorations |= SPV_REFLECT_DECORATION_RELAXED_PRECISION; |
| 448 |
} |
| 449 |
if (p_decoration_fields->is_block) { |
| 450 |
decorations |= SPV_REFLECT_DECORATION_BLOCK; |
| 451 |
} |
| 452 |
if (p_decoration_fields->is_buffer_block) { |
| 453 |
decorations |= SPV_REFLECT_DECORATION_BUFFER_BLOCK; |
| 454 |
} |
| 455 |
if (p_decoration_fields->is_row_major) { |
| 456 |
decorations |= SPV_REFLECT_DECORATION_ROW_MAJOR; |
| 457 |
} |
| 458 |
if (p_decoration_fields->is_column_major) { |
| 459 |
decorations |= SPV_REFLECT_DECORATION_COLUMN_MAJOR; |
| 460 |
} |
| 461 |
if (p_decoration_fields->is_built_in) { |
| 462 |
decorations |= SPV_REFLECT_DECORATION_BUILT_IN; |
| 463 |
} |
| 464 |
if (p_decoration_fields->is_noperspective) { |
| 465 |
decorations |= SPV_REFLECT_DECORATION_NOPERSPECTIVE; |
| 466 |
} |
| 467 |
if (p_decoration_fields->is_flat) { |
| 468 |
decorations |= SPV_REFLECT_DECORATION_FLAT; |
| 469 |
} |
| 470 |
if (p_decoration_fields->is_non_writable) { |
| 471 |
decorations |= SPV_REFLECT_DECORATION_NON_WRITABLE; |
| 472 |
} |
| 473 |
if (p_decoration_fields->is_non_readable) { |
| 474 |
decorations |= SPV_REFLECT_DECORATION_NON_READABLE; |
| 475 |
} |
| 476 |
if (p_decoration_fields->is_patch) { |
| 477 |
decorations |= SPV_REFLECT_DECORATION_PATCH; |
| 478 |
} |
| 479 |
if (p_decoration_fields->is_per_vertex) { |
| 480 |
decorations |= SPV_REFLECT_DECORATION_PER_VERTEX; |
| 481 |
} |
| 482 |
if (p_decoration_fields->is_per_task) { |
| 483 |
decorations |= SPV_REFLECT_DECORATION_PER_TASK; |
| 484 |
} |
| 485 |
if (p_decoration_fields->is_weight_texture) { |
| 486 |
decorations |= SPV_REFLECT_DECORATION_WEIGHT_TEXTURE; |
| 487 |
} |
| 488 |
if (p_decoration_fields->is_block_match_texture) { |
| 489 |
decorations |= SPV_REFLECT_DECORATION_BLOCK_MATCH_TEXTURE; |
| 490 |
} |
| 491 |
return decorations; |
| 492 |
} |
| 493 |
|
| 494 |
static void ApplyNumericTraits(const SpvReflectTypeDescription* p_type, SpvReflectNumericTraits* p_numeric_traits) { |
| 495 |
memcpy(p_numeric_traits, &p_type->traits.numeric, sizeof(p_type->traits.numeric)); |
| 496 |
} |
| 497 |
|
| 498 |
static void ApplyArrayTraits(const SpvReflectTypeDescription* p_type, SpvReflectArrayTraits* p_array_traits) { |
| 499 |
memcpy(p_array_traits, &p_type->traits.array, sizeof(p_type->traits.array)); |
| 500 |
} |
| 501 |
|
| 502 |
static bool IsSpecConstant(const SpvReflectPrvNode* p_node) { |
| 503 |
return (p_node->op == SpvOpSpecConstant || p_node->op == SpvOpSpecConstantOp || p_node->op == SpvOpSpecConstantTrue || |
| 504 |
p_node->op == SpvOpSpecConstantFalse); |
| 505 |
} |
| 506 |
|
| 507 |
static SpvReflectPrvNode* FindNode(SpvReflectPrvParser* p_parser, uint32_t result_id) { |
| 508 |
SpvReflectPrvNode* p_node = NULL; |
| 509 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 510 |
SpvReflectPrvNode* p_elem = &(p_parser->nodes[i]); |
| 511 |
if (p_elem->result_id == result_id) { |
| 512 |
p_node = p_elem; |
| 513 |
break; |
| 514 |
} |
| 515 |
} |
| 516 |
return p_node; |
| 517 |
} |
| 518 |
|
| 519 |
static SpvReflectTypeDescription* FindType(SpvReflectShaderModule* p_module, uint32_t type_id) { |
| 520 |
SpvReflectTypeDescription* p_type = NULL; |
| 521 |
for (size_t i = 0; i < p_module->_internal->type_description_count; ++i) { |
| 522 |
SpvReflectTypeDescription* p_elem = &(p_module->_internal->type_descriptions[i]); |
| 523 |
if (p_elem->id == type_id) { |
| 524 |
p_type = p_elem; |
| 525 |
break; |
| 526 |
} |
| 527 |
} |
| 528 |
return p_type; |
| 529 |
} |
| 530 |
|
| 531 |
static SpvReflectPrvAccessChain* FindAccessChain(SpvReflectPrvParser* p_parser, uint32_t id) { |
| 532 |
const uint32_t ac_count = p_parser->access_chain_count; |
| 533 |
for (uint32_t i = 0; i < ac_count; i++) { |
| 534 |
if (p_parser->access_chains[i].result_id == id) { |
| 535 |
return &p_parser->access_chains[i]; |
| 536 |
} |
| 537 |
} |
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
static uint32_t FindAccessChainBaseVariable(SpvReflectPrvParser* p_parser, SpvReflectPrvAccessChain* p_access_chain) { |
| 544 |
uint32_t base_id = p_access_chain->base_id; |
| 545 |
SpvReflectPrvNode* base_node = FindNode(p_parser, base_id); |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
if (IsNull(base_node)) { |
| 550 |
return 0; |
| 551 |
} |
| 552 |
while (base_node->op != SpvOpVariable) { |
| 553 |
switch (base_node->op) { |
| 554 |
case SpvOpLoad: { |
| 555 |
UNCHECKED_READU32(p_parser, base_node->word_offset + 3, base_id); |
| 556 |
} break; |
| 557 |
case SpvOpFunctionParameter: { |
| 558 |
UNCHECKED_READU32(p_parser, base_node->word_offset + 2, base_id); |
| 559 |
} break; |
| 560 |
case SpvOpBitcast: |
| 561 |
|
| 562 |
|
| 563 |
return 0; |
| 564 |
default: { |
| 565 |
assert(false); |
| 566 |
} break; |
| 567 |
} |
| 568 |
|
| 569 |
SpvReflectPrvAccessChain* base_ac = FindAccessChain(p_parser, base_id); |
| 570 |
if (base_ac == 0) { |
| 571 |
return 0; |
| 572 |
} |
| 573 |
base_id = base_ac->base_id; |
| 574 |
base_node = FindNode(p_parser, base_id); |
| 575 |
if (IsNull(base_node)) { |
| 576 |
return 0; |
| 577 |
} |
| 578 |
} |
| 579 |
return base_id; |
| 580 |
} |
| 581 |
|
| 582 |
static SpvReflectBlockVariable* GetRefBlkVar(SpvReflectPrvParser* p_parser, SpvReflectPrvAccessChain* p_access_chain) { |
| 583 |
uint32_t base_id = p_access_chain->base_id; |
| 584 |
SpvReflectPrvNode* base_node = FindNode(p_parser, base_id); |
| 585 |
assert(base_node->op == SpvOpLoad); |
| 586 |
UNCHECKED_READU32(p_parser, base_node->word_offset + 3, base_id); |
| 587 |
SpvReflectPrvAccessChain* base_ac = FindAccessChain(p_parser, base_id); |
| 588 |
assert(base_ac != 0); |
| 589 |
SpvReflectBlockVariable* base_var = base_ac->block_var; |
| 590 |
assert(base_var != 0); |
| 591 |
return base_var; |
| 592 |
} |
| 593 |
|
| 594 |
bool IsPointerToPointer(SpvReflectPrvParser* p_parser, uint32_t type_id) { |
| 595 |
SpvReflectPrvNode* ptr_node = FindNode(p_parser, type_id); |
| 596 |
if (IsNull(ptr_node) || (ptr_node->op != SpvOpTypePointer)) { |
| 597 |
return false; |
| 598 |
} |
| 599 |
uint32_t pte_id = 0; |
| 600 |
UNCHECKED_READU32(p_parser, ptr_node->word_offset + 3, pte_id); |
| 601 |
SpvReflectPrvNode* pte_node = FindNode(p_parser, pte_id); |
| 602 |
if (IsNull(pte_node)) { |
| 603 |
return false; |
| 604 |
} |
| 605 |
return pte_node->op == SpvOpTypePointer; |
| 606 |
} |
| 607 |
|
| 608 |
static SpvReflectResult CreateParser(size_t size, void* p_code, SpvReflectPrvParser* p_parser) { |
| 609 |
if (p_code == NULL) { |
| 610 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 611 |
} |
| 612 |
|
| 613 |
if (size < SPIRV_MINIMUM_FILE_SIZE) { |
| 614 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_CODE_SIZE; |
| 615 |
} |
| 616 |
if ((size % 4) != 0) { |
| 617 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_CODE_SIZE; |
| 618 |
} |
| 619 |
|
| 620 |
p_parser->spirv_word_count = size / SPIRV_WORD_SIZE; |
| 621 |
p_parser->spirv_code = (uint32_t*)p_code; |
| 622 |
|
| 623 |
if (p_parser->spirv_code[0] != SpvMagicNumber) { |
| 624 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_MAGIC_NUMBER; |
| 625 |
} |
| 626 |
|
| 627 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 628 |
} |
| 629 |
|
| 630 |
static void DestroyParser(SpvReflectPrvParser* p_parser) { |
| 631 |
if (!IsNull(p_parser->nodes)) { |
| 632 |
|
| 633 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 634 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 635 |
if (IsNotNull(p_node->member_names)) { |
| 636 |
SafeFree(p_node->member_names); |
| 637 |
} |
| 638 |
if (IsNotNull(p_node->member_decorations)) { |
| 639 |
SafeFree(p_node->member_decorations); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
for (size_t i = 0; i < p_parser->function_count; ++i) { |
| 645 |
SafeFree(p_parser->functions[i].callees); |
| 646 |
SafeFree(p_parser->functions[i].callee_ptrs); |
| 647 |
SafeFree(p_parser->functions[i].accessed_variables); |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
for (uint32_t i = 0; i < p_parser->access_chain_count; ++i) { |
| 652 |
SafeFree(p_parser->access_chains[i].indexes); |
| 653 |
} |
| 654 |
|
| 655 |
SafeFree(p_parser->nodes); |
| 656 |
SafeFree(p_parser->strings); |
| 657 |
SafeFree(p_parser->source_embedded); |
| 658 |
SafeFree(p_parser->functions); |
| 659 |
SafeFree(p_parser->access_chains); |
| 660 |
|
| 661 |
if (IsNotNull(p_parser->physical_pointer_structs)) { |
| 662 |
SafeFree(p_parser->physical_pointer_structs); |
| 663 |
} |
| 664 |
p_parser->node_count = 0; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
static SpvReflectResult ParseNodes(SpvReflectPrvParser* p_parser) { |
| 669 |
assert(IsNotNull(p_parser)); |
| 670 |
assert(IsNotNull(p_parser->spirv_code)); |
| 671 |
|
| 672 |
uint32_t* p_spirv = p_parser->spirv_code; |
| 673 |
uint32_t spirv_word_index = SPIRV_STARTING_WORD_INDEX; |
| 674 |
|
| 675 |
|
| 676 |
uint32_t node_count = 0; |
| 677 |
while (spirv_word_index < p_parser->spirv_word_count) { |
| 678 |
uint32_t word = p_spirv[spirv_word_index]; |
| 679 |
SpvOp op = (SpvOp)(word & 0xFFFF); |
| 680 |
uint32_t node_word_count = (word >> 16) & 0xFFFF; |
| 681 |
if (node_word_count == 0) { |
| 682 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_INSTRUCTION; |
| 683 |
} |
| 684 |
if (op == SpvOpAccessChain) { |
| 685 |
++(p_parser->access_chain_count); |
| 686 |
} |
| 687 |
spirv_word_index += node_word_count; |
| 688 |
++node_count; |
| 689 |
} |
| 690 |
|
| 691 |
if (node_count == 0) { |
| 692 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
p_parser->node_count = node_count; |
| 697 |
p_parser->nodes = (SpvReflectPrvNode*)calloc(p_parser->node_count, sizeof(*(p_parser->nodes))); |
| 698 |
if (IsNull(p_parser->nodes)) { |
| 699 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 700 |
} |
| 701 |
|
| 702 |
for (uint32_t i = 0; i < node_count; ++i) { |
| 703 |
p_parser->nodes[i].op = (SpvOp)INVALID_VALUE; |
| 704 |
p_parser->nodes[i].storage_class = (SpvStorageClass)INVALID_VALUE; |
| 705 |
p_parser->nodes[i].decorations.set.value = (uint32_t)INVALID_VALUE; |
| 706 |
p_parser->nodes[i].decorations.binding.value = (uint32_t)INVALID_VALUE; |
| 707 |
p_parser->nodes[i].decorations.location.value = (uint32_t)INVALID_VALUE; |
| 708 |
p_parser->nodes[i].decorations.component.value = (uint32_t)INVALID_VALUE; |
| 709 |
p_parser->nodes[i].decorations.offset.value = (uint32_t)INVALID_VALUE; |
| 710 |
p_parser->nodes[i].decorations.uav_counter_buffer.value = (uint32_t)INVALID_VALUE; |
| 711 |
p_parser->nodes[i].decorations.spec_id = (uint32_t)INVALID_VALUE; |
| 712 |
p_parser->nodes[i].decorations.built_in = (SpvBuiltIn)INVALID_VALUE; |
| 713 |
} |
| 714 |
|
| 715 |
p_parser->source_file_id = (uint32_t)INVALID_VALUE; |
| 716 |
p_parser->source_embedded = NULL; |
| 717 |
|
| 718 |
|
| 719 |
uint32_t function_node = (uint32_t)INVALID_VALUE; |
| 720 |
|
| 721 |
|
| 722 |
if (p_parser->access_chain_count > 0) { |
| 723 |
p_parser->access_chains = (SpvReflectPrvAccessChain*)calloc(p_parser->access_chain_count, sizeof(*(p_parser->access_chains))); |
| 724 |
if (IsNull(p_parser->access_chains)) { |
| 725 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
uint32_t node_index = 0; |
| 731 |
uint32_t access_chain_index = 0; |
| 732 |
spirv_word_index = SPIRV_STARTING_WORD_INDEX; |
| 733 |
while (spirv_word_index < p_parser->spirv_word_count) { |
| 734 |
uint32_t word = p_spirv[spirv_word_index]; |
| 735 |
SpvOp op = (SpvOp)(word & 0xFFFF); |
| 736 |
uint32_t node_word_count = (word >> 16) & 0xFFFF; |
| 737 |
|
| 738 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[node_index]); |
| 739 |
p_node->op = op; |
| 740 |
p_node->word_offset = spirv_word_index; |
| 741 |
p_node->word_count = node_word_count; |
| 742 |
|
| 743 |
switch (p_node->op) { |
| 744 |
default: |
| 745 |
break; |
| 746 |
|
| 747 |
case SpvOpString: { |
| 748 |
++(p_parser->string_count); |
| 749 |
} break; |
| 750 |
|
| 751 |
case SpvOpSource: { |
| 752 |
CHECKED_READU32_CAST(p_parser, p_node->word_offset + 1, SpvSourceLanguage, p_parser->source_language); |
| 753 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_parser->source_language_version); |
| 754 |
if (p_node->word_count >= 4) { |
| 755 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_parser->source_file_id); |
| 756 |
} |
| 757 |
if (p_node->word_count >= 5) { |
| 758 |
const char* p_source = (const char*)(p_parser->spirv_code + p_node->word_offset + 4); |
| 759 |
|
| 760 |
const size_t source_len = strlen(p_source); |
| 761 |
char* p_source_temp = (char*)calloc(source_len + 1, sizeof(char)); |
| 762 |
|
| 763 |
if (IsNull(p_source_temp)) { |
| 764 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 765 |
} |
| 766 |
|
| 767 |
#ifdef _WIN32 |
| 768 |
strcpy_s(p_source_temp, source_len + 1, p_source); |
| 769 |
#else |
| 770 |
strcpy(p_source_temp, p_source); |
| 771 |
#endif |
| 772 |
|
| 773 |
SafeFree(p_parser->source_embedded); |
| 774 |
p_parser->source_embedded = p_source_temp; |
| 775 |
} |
| 776 |
} break; |
| 777 |
|
| 778 |
case SpvOpSourceContinued: { |
| 779 |
const char* p_source = (const char*)(p_parser->spirv_code + p_node->word_offset + 1); |
| 780 |
|
| 781 |
const size_t source_len = strlen(p_source); |
| 782 |
const size_t embedded_source_len = strlen(p_parser->source_embedded); |
| 783 |
char* p_continued_source = (char*)calloc(source_len + embedded_source_len + 1, sizeof(char)); |
| 784 |
|
| 785 |
if (IsNull(p_continued_source)) { |
| 786 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 787 |
} |
| 788 |
|
| 789 |
#ifdef _WIN32 |
| 790 |
strcpy_s(p_continued_source, embedded_source_len + 1, p_parser->source_embedded); |
| 791 |
strcat_s(p_continued_source, embedded_source_len + source_len + 1, p_source); |
| 792 |
#else |
| 793 |
strcpy(p_continued_source, p_parser->source_embedded); |
| 794 |
strcat(p_continued_source, p_source); |
| 795 |
#endif |
| 796 |
|
| 797 |
SafeFree(p_parser->source_embedded); |
| 798 |
p_parser->source_embedded = p_continued_source; |
| 799 |
} break; |
| 800 |
|
| 801 |
case SpvOpEntryPoint: { |
| 802 |
++(p_parser->entry_point_count); |
| 803 |
} break; |
| 804 |
|
| 805 |
case SpvOpCapability: { |
| 806 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->capability); |
| 807 |
++(p_parser->capability_count); |
| 808 |
} break; |
| 809 |
|
| 810 |
case SpvOpName: |
| 811 |
case SpvOpMemberName: { |
| 812 |
uint32_t member_offset = (p_node->op == SpvOpMemberName) ? 1 : 0; |
| 813 |
uint32_t name_start = p_node->word_offset + member_offset + 2; |
| 814 |
p_node->name = (const char*)(p_parser->spirv_code + name_start); |
| 815 |
} break; |
| 816 |
|
| 817 |
case SpvOpTypeStruct: { |
| 818 |
p_node->member_count = p_node->word_count - 2; |
| 819 |
FALLTHROUGH; |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
case SpvOpTypeVoid: |
| 826 |
case SpvOpTypeBool: |
| 827 |
case SpvOpTypeInt: |
| 828 |
case SpvOpTypeFloat: |
| 829 |
case SpvOpTypeVector: |
| 830 |
case SpvOpTypeMatrix: |
| 831 |
case SpvOpTypeSampler: |
| 832 |
case SpvOpTypeOpaque: |
| 833 |
case SpvOpTypeFunction: |
| 834 |
case SpvOpTypeEvent: |
| 835 |
case SpvOpTypeDeviceEvent: |
| 836 |
case SpvOpTypeReserveId: |
| 837 |
case SpvOpTypeQueue: |
| 838 |
case SpvOpTypePipe: |
| 839 |
case SpvOpTypeAccelerationStructureKHR: |
| 840 |
case SpvOpTypeRayQueryKHR: |
| 841 |
case SpvOpTypeHitObjectNV: |
| 842 |
case SpvOpTypeCooperativeMatrixNV: |
| 843 |
case SpvOpTypeCooperativeMatrixKHR: { |
| 844 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id); |
| 845 |
p_node->is_type = true; |
| 846 |
} break; |
| 847 |
|
| 848 |
case SpvOpTypeImage: { |
| 849 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id); |
| 850 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->image_traits.sampled_type_id); |
| 851 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->image_traits.dim); |
| 852 |
CHECKED_READU32(p_parser, p_node->word_offset + 4, p_node->image_traits.depth); |
| 853 |
CHECKED_READU32(p_parser, p_node->word_offset + 5, p_node->image_traits.arrayed); |
| 854 |
CHECKED_READU32(p_parser, p_node->word_offset + 6, p_node->image_traits.ms); |
| 855 |
CHECKED_READU32(p_parser, p_node->word_offset + 7, p_node->image_traits.sampled); |
| 856 |
CHECKED_READU32(p_parser, p_node->word_offset + 8, p_node->image_traits.image_format); |
| 857 |
p_node->is_type = true; |
| 858 |
} break; |
| 859 |
|
| 860 |
case SpvOpTypeSampledImage: { |
| 861 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id); |
| 862 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->image_type_id); |
| 863 |
p_node->is_type = true; |
| 864 |
} break; |
| 865 |
|
| 866 |
case SpvOpTypeArray: { |
| 867 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id); |
| 868 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->array_traits.element_type_id); |
| 869 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->array_traits.length_id); |
| 870 |
p_node->is_type = true; |
| 871 |
} break; |
| 872 |
|
| 873 |
case SpvOpTypeRuntimeArray: { |
| 874 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id); |
| 875 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->array_traits.element_type_id); |
| 876 |
p_node->is_type = true; |
| 877 |
} break; |
| 878 |
|
| 879 |
case SpvOpTypePointer: { |
| 880 |
uint32_t result_id; |
| 881 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, result_id); |
| 882 |
|
| 883 |
SpvReflectPrvNode* p_fwd_node = FindNode(p_parser, result_id); |
| 884 |
if (p_fwd_node) { |
| 885 |
p_fwd_node->result_id = 0; |
| 886 |
} |
| 887 |
|
| 888 |
p_node->result_id = result_id; |
| 889 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->storage_class); |
| 890 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->type_id); |
| 891 |
p_node->is_type = true; |
| 892 |
} break; |
| 893 |
|
| 894 |
case SpvOpTypeForwardPointer: { |
| 895 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id); |
| 896 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->storage_class); |
| 897 |
p_node->is_type = true; |
| 898 |
} break; |
| 899 |
|
| 900 |
case SpvOpConstantTrue: |
| 901 |
case SpvOpConstantFalse: |
| 902 |
case SpvOpConstant: |
| 903 |
case SpvOpConstantComposite: |
| 904 |
case SpvOpConstantSampler: |
| 905 |
case SpvOpConstantNull: { |
| 906 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id); |
| 907 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id); |
| 908 |
} break; |
| 909 |
|
| 910 |
case SpvOpSpecConstantTrue: |
| 911 |
case SpvOpSpecConstantFalse: |
| 912 |
case SpvOpSpecConstant: |
| 913 |
case SpvOpSpecConstantComposite: |
| 914 |
case SpvOpSpecConstantOp: { |
| 915 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id); |
| 916 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id); |
| 917 |
} break; |
| 918 |
|
| 919 |
case SpvOpVariable: { |
| 920 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->type_id); |
| 921 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id); |
| 922 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->storage_class); |
| 923 |
} break; |
| 924 |
|
| 925 |
case SpvOpLoad: { |
| 926 |
|
| 927 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id); |
| 928 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id); |
| 929 |
} break; |
| 930 |
|
| 931 |
case SpvOpAccessChain: { |
| 932 |
SpvReflectPrvAccessChain* p_access_chain = &(p_parser->access_chains[access_chain_index]); |
| 933 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_access_chain->result_type_id); |
| 934 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_access_chain->result_id); |
| 935 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_access_chain->base_id); |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
p_access_chain->index_count = (node_word_count - SPIRV_ACCESS_CHAIN_INDEX_OFFSET); |
| 941 |
if (p_access_chain->index_count > 0) { |
| 942 |
p_access_chain->indexes = (uint32_t*)calloc(p_access_chain->index_count, sizeof(*(p_access_chain->indexes))); |
| 943 |
if (IsNull(p_access_chain->indexes)) { |
| 944 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 945 |
} |
| 946 |
|
| 947 |
for (uint32_t index_index = 0; index_index < p_access_chain->index_count; ++index_index) { |
| 948 |
|
| 949 |
uint32_t index_id = 0; |
| 950 |
CHECKED_READU32(p_parser, p_node->word_offset + SPIRV_ACCESS_CHAIN_INDEX_OFFSET + index_index, index_id); |
| 951 |
|
| 952 |
SpvReflectPrvNode* p_index_value_node = FindNode(p_parser, index_id); |
| 953 |
if ((p_index_value_node != NULL) && |
| 954 |
(p_index_value_node->op == SpvOpConstant || p_index_value_node->op == SpvOpSpecConstant)) { |
| 955 |
|
| 956 |
uint32_t index_value = UINT32_MAX; |
| 957 |
CHECKED_READU32(p_parser, p_index_value_node->word_offset + 3, index_value); |
| 958 |
assert(index_value != UINT32_MAX); |
| 959 |
|
| 960 |
p_access_chain->indexes[index_index] = index_value; |
| 961 |
} |
| 962 |
} |
| 963 |
} |
| 964 |
++access_chain_index; |
| 965 |
} break; |
| 966 |
|
| 967 |
case SpvOpFunction: { |
| 968 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id); |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
function_node = node_index; |
| 974 |
} break; |
| 975 |
|
| 976 |
case SpvOpLabel: { |
| 977 |
if (function_node != (uint32_t)INVALID_VALUE) { |
| 978 |
SpvReflectPrvNode* p_func_node = &(p_parser->nodes[function_node]); |
| 979 |
CHECKED_READU32(p_parser, p_func_node->word_offset + 2, p_func_node->result_id); |
| 980 |
++(p_parser->function_count); |
| 981 |
} |
| 982 |
FALLTHROUGH; |
| 983 |
} |
| 984 |
|
| 985 |
case SpvOpFunctionEnd: { |
| 986 |
function_node = (uint32_t)INVALID_VALUE; |
| 987 |
} break; |
| 988 |
case SpvOpFunctionParameter: { |
| 989 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id); |
| 990 |
} break; |
| 991 |
case SpvOpBitcast: |
| 992 |
case SpvOpShiftRightLogical: |
| 993 |
case SpvOpIAdd: |
| 994 |
case SpvOpISub: |
| 995 |
case SpvOpIMul: |
| 996 |
case SpvOpUDiv: |
| 997 |
case SpvOpSDiv: { |
| 998 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id); |
| 999 |
} break; |
| 1000 |
} |
| 1001 |
|
| 1002 |
if (p_node->is_type) { |
| 1003 |
++(p_parser->type_count); |
| 1004 |
} |
| 1005 |
|
| 1006 |
spirv_word_index += node_word_count; |
| 1007 |
++node_index; |
| 1008 |
} |
| 1009 |
|
| 1010 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1011 |
} |
| 1012 |
|
| 1013 |
static SpvReflectResult ParseStrings(SpvReflectPrvParser* p_parser) { |
| 1014 |
assert(IsNotNull(p_parser)); |
| 1015 |
assert(IsNotNull(p_parser->spirv_code)); |
| 1016 |
assert(IsNotNull(p_parser->nodes)); |
| 1017 |
|
| 1018 |
|
| 1019 |
if (p_parser->string_count == 0) { |
| 1020 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1021 |
} |
| 1022 |
|
| 1023 |
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) { |
| 1024 |
|
| 1025 |
p_parser->strings = (SpvReflectPrvString*)calloc(p_parser->string_count, sizeof(*(p_parser->strings))); |
| 1026 |
|
| 1027 |
uint32_t string_index = 0; |
| 1028 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 1029 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1030 |
if (p_node->op != SpvOpString) { |
| 1031 |
continue; |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
assert(string_index < p_parser->string_count); |
| 1036 |
if (string_index >= p_parser->string_count) { |
| 1037 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
SpvReflectPrvString* p_string = &(p_parser->strings[string_index]); |
| 1042 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_string->result_id); |
| 1043 |
|
| 1044 |
|
| 1045 |
uint32_t string_start = p_node->word_offset + 2; |
| 1046 |
p_string->string = (const char*)(p_parser->spirv_code + string_start); |
| 1047 |
|
| 1048 |
|
| 1049 |
++string_index; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1054 |
} |
| 1055 |
|
| 1056 |
static SpvReflectResult ParseSource(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 1057 |
assert(IsNotNull(p_parser)); |
| 1058 |
assert(IsNotNull(p_parser->spirv_code)); |
| 1059 |
|
| 1060 |
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code)) { |
| 1061 |
|
| 1062 |
if (IsNotNull(p_parser->strings)) { |
| 1063 |
for (uint32_t i = 0; i < p_parser->string_count; ++i) { |
| 1064 |
SpvReflectPrvString* p_string = &(p_parser->strings[i]); |
| 1065 |
if (p_string->result_id == p_parser->source_file_id) { |
| 1066 |
p_module->source_file = p_string->string; |
| 1067 |
break; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
if (IsNotNull(p_parser->source_embedded)) { |
| 1074 |
const size_t source_len = strlen(p_parser->source_embedded); |
| 1075 |
char* p_source = (char*)calloc(source_len + 1, sizeof(char)); |
| 1076 |
|
| 1077 |
if (IsNull(p_source)) { |
| 1078 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1079 |
} |
| 1080 |
|
| 1081 |
#ifdef _WIN32 |
| 1082 |
strcpy_s(p_source, source_len + 1, p_parser->source_embedded); |
| 1083 |
#else |
| 1084 |
strcpy(p_source, p_parser->source_embedded); |
| 1085 |
#endif |
| 1086 |
|
| 1087 |
p_module->source_source = p_source; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1092 |
} |
| 1093 |
|
| 1094 |
static SpvReflectResult ParseFunction(SpvReflectPrvParser* p_parser, SpvReflectPrvNode* p_func_node, SpvReflectPrvFunction* p_func, |
| 1095 |
size_t first_label_index) { |
| 1096 |
p_func->id = p_func_node->result_id; |
| 1097 |
|
| 1098 |
p_func->callee_count = 0; |
| 1099 |
p_func->accessed_variable_count = 0; |
| 1100 |
|
| 1101 |
|
| 1102 |
for (size_t i = first_label_index; i < p_parser->node_count; ++i) { |
| 1103 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1104 |
if (p_node->op == SpvOpFunctionEnd) { |
| 1105 |
break; |
| 1106 |
} |
| 1107 |
switch (p_node->op) { |
| 1108 |
case SpvOpFunctionCall: { |
| 1109 |
++(p_func->callee_count); |
| 1110 |
} break; |
| 1111 |
case SpvOpLoad: |
| 1112 |
case SpvOpAccessChain: |
| 1113 |
case SpvOpInBoundsAccessChain: |
| 1114 |
case SpvOpPtrAccessChain: |
| 1115 |
case SpvOpArrayLength: |
| 1116 |
case SpvOpGenericPtrMemSemantics: |
| 1117 |
case SpvOpInBoundsPtrAccessChain: |
| 1118 |
case SpvOpStore: |
| 1119 |
case SpvOpImageTexelPointer: { |
| 1120 |
++(p_func->accessed_variable_count); |
| 1121 |
} break; |
| 1122 |
case SpvOpCopyMemory: |
| 1123 |
case SpvOpCopyMemorySized: { |
| 1124 |
p_func->accessed_variable_count += 2; |
| 1125 |
} break; |
| 1126 |
default: |
| 1127 |
break; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
if (p_func->callee_count > 0) { |
| 1132 |
p_func->callees = (uint32_t*)calloc(p_func->callee_count, sizeof(*(p_func->callees))); |
| 1133 |
if (IsNull(p_func->callees)) { |
| 1134 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
if (p_func->accessed_variable_count > 0) { |
| 1139 |
p_func->accessed_variables = |
| 1140 |
(SpvReflectPrvAccessedVariable*)calloc(p_func->accessed_variable_count, sizeof(*(p_func->accessed_variables))); |
| 1141 |
if (IsNull(p_func->accessed_variables)) { |
| 1142 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
p_func->callee_count = 0; |
| 1147 |
p_func->accessed_variable_count = 0; |
| 1148 |
|
| 1149 |
for (size_t i = first_label_index; i < p_parser->node_count; ++i) { |
| 1150 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1151 |
if (p_node->op == SpvOpFunctionEnd) { |
| 1152 |
break; |
| 1153 |
} |
| 1154 |
switch (p_node->op) { |
| 1155 |
case SpvOpFunctionCall: { |
| 1156 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_func->callees[p_func->callee_count]); |
| 1157 |
(++p_func->callee_count); |
| 1158 |
} break; |
| 1159 |
case SpvOpLoad: |
| 1160 |
case SpvOpAccessChain: |
| 1161 |
case SpvOpInBoundsAccessChain: |
| 1162 |
case SpvOpPtrAccessChain: |
| 1163 |
case SpvOpArrayLength: |
| 1164 |
case SpvOpGenericPtrMemSemantics: |
| 1165 |
case SpvOpInBoundsPtrAccessChain: |
| 1166 |
case SpvOpImageTexelPointer: { |
| 1167 |
const uint32_t result_index = p_node->word_offset + 2; |
| 1168 |
const uint32_t ptr_index = p_node->word_offset + 3; |
| 1169 |
SpvReflectPrvAccessedVariable* access_ptr = &p_func->accessed_variables[p_func->accessed_variable_count]; |
| 1170 |
|
| 1171 |
access_ptr->p_node = p_node; |
| 1172 |
|
| 1173 |
CHECKED_READU32(p_parser, result_index, access_ptr->result_id); |
| 1174 |
CHECKED_READU32(p_parser, ptr_index, access_ptr->variable_ptr); |
| 1175 |
(++p_func->accessed_variable_count); |
| 1176 |
} break; |
| 1177 |
case SpvOpStore: { |
| 1178 |
const uint32_t result_index = p_node->word_offset + 2; |
| 1179 |
CHECKED_READU32(p_parser, result_index, p_func->accessed_variables[p_func->accessed_variable_count].variable_ptr); |
| 1180 |
p_func->accessed_variables[p_func->accessed_variable_count].p_node = p_node; |
| 1181 |
(++p_func->accessed_variable_count); |
| 1182 |
} break; |
| 1183 |
case SpvOpCopyMemory: |
| 1184 |
case SpvOpCopyMemorySized: { |
| 1185 |
|
| 1186 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, |
| 1187 |
p_func->accessed_variables[p_func->accessed_variable_count].variable_ptr); |
| 1188 |
(++p_func->accessed_variable_count); |
| 1189 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, |
| 1190 |
p_func->accessed_variables[p_func->accessed_variable_count].variable_ptr); |
| 1191 |
(++p_func->accessed_variable_count); |
| 1192 |
} break; |
| 1193 |
default: |
| 1194 |
break; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
if (p_func->callee_count > 0) { |
| 1199 |
qsort(p_func->callees, p_func->callee_count, sizeof(*(p_func->callees)), SortCompareUint32); |
| 1200 |
} |
| 1201 |
p_func->callee_count = (uint32_t)DedupSortedUint32(p_func->callees, p_func->callee_count); |
| 1202 |
|
| 1203 |
if (p_func->accessed_variable_count > 0) { |
| 1204 |
qsort(p_func->accessed_variables, p_func->accessed_variable_count, sizeof(*(p_func->accessed_variables)), |
| 1205 |
SortCompareAccessedVariable); |
| 1206 |
} |
| 1207 |
|
| 1208 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1209 |
} |
| 1210 |
|
| 1211 |
static int SortCompareFunctions(const void* a, const void* b) { |
| 1212 |
const SpvReflectPrvFunction* af = (const SpvReflectPrvFunction*)a; |
| 1213 |
const SpvReflectPrvFunction* bf = (const SpvReflectPrvFunction*)b; |
| 1214 |
return (int)af->id - (int)bf->id; |
| 1215 |
} |
| 1216 |
|
| 1217 |
static SpvReflectResult ParseFunctions(SpvReflectPrvParser* p_parser) { |
| 1218 |
assert(IsNotNull(p_parser)); |
| 1219 |
assert(IsNotNull(p_parser->spirv_code)); |
| 1220 |
assert(IsNotNull(p_parser->nodes)); |
| 1221 |
|
| 1222 |
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) { |
| 1223 |
if (p_parser->function_count == 0) { |
| 1224 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1225 |
} |
| 1226 |
|
| 1227 |
p_parser->functions = (SpvReflectPrvFunction*)calloc(p_parser->function_count, sizeof(*(p_parser->functions))); |
| 1228 |
if (IsNull(p_parser->functions)) { |
| 1229 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1230 |
} |
| 1231 |
|
| 1232 |
size_t function_index = 0; |
| 1233 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 1234 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1235 |
if (p_node->op != SpvOpFunction) { |
| 1236 |
continue; |
| 1237 |
} |
| 1238 |
|
| 1239 |
|
| 1240 |
bool func_definition = false; |
| 1241 |
|
| 1242 |
|
| 1243 |
for (; i < p_parser->node_count; ++i) { |
| 1244 |
if (p_parser->nodes[i].op == SpvOpLabel) { |
| 1245 |
func_definition = true; |
| 1246 |
break; |
| 1247 |
} |
| 1248 |
if (p_parser->nodes[i].op == SpvOpFunctionEnd) { |
| 1249 |
break; |
| 1250 |
} |
| 1251 |
} |
| 1252 |
if (!func_definition) { |
| 1253 |
continue; |
| 1254 |
} |
| 1255 |
|
| 1256 |
SpvReflectPrvFunction* p_function = &(p_parser->functions[function_index]); |
| 1257 |
|
| 1258 |
SpvReflectResult result = ParseFunction(p_parser, p_node, p_function, i); |
| 1259 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 1260 |
return result; |
| 1261 |
} |
| 1262 |
|
| 1263 |
++function_index; |
| 1264 |
} |
| 1265 |
|
| 1266 |
qsort(p_parser->functions, p_parser->function_count, sizeof(*(p_parser->functions)), SortCompareFunctions); |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
for (size_t i = 0; i < p_parser->function_count; ++i) { |
| 1271 |
SpvReflectPrvFunction* p_func = &(p_parser->functions[i]); |
| 1272 |
if (p_func->callee_count == 0) { |
| 1273 |
continue; |
| 1274 |
} |
| 1275 |
p_func->callee_ptrs = (SpvReflectPrvFunction**)calloc(p_func->callee_count, sizeof(*(p_func->callee_ptrs))); |
| 1276 |
for (size_t j = 0, k = 0; j < p_func->callee_count; ++j) { |
| 1277 |
while (p_parser->functions[k].id != p_func->callees[j]) { |
| 1278 |
++k; |
| 1279 |
if (k >= p_parser->function_count) { |
| 1280 |
|
| 1281 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1282 |
} |
| 1283 |
} |
| 1284 |
p_func->callee_ptrs[j] = &(p_parser->functions[k]); |
| 1285 |
} |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1290 |
} |
| 1291 |
|
| 1292 |
static SpvReflectResult ParseMemberCounts(SpvReflectPrvParser* p_parser) { |
| 1293 |
assert(IsNotNull(p_parser)); |
| 1294 |
assert(IsNotNull(p_parser->spirv_code)); |
| 1295 |
assert(IsNotNull(p_parser->nodes)); |
| 1296 |
|
| 1297 |
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) { |
| 1298 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 1299 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1300 |
if ((p_node->op != SpvOpMemberName) && (p_node->op != SpvOpMemberDecorate)) { |
| 1301 |
continue; |
| 1302 |
} |
| 1303 |
|
| 1304 |
uint32_t target_id = 0; |
| 1305 |
uint32_t member_index = (uint32_t)INVALID_VALUE; |
| 1306 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, target_id); |
| 1307 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, member_index); |
| 1308 |
SpvReflectPrvNode* p_target_node = FindNode(p_parser, target_id); |
| 1309 |
|
| 1310 |
if (IsNull(p_target_node)) { |
| 1311 |
continue; |
| 1312 |
} |
| 1313 |
|
| 1314 |
if (member_index == INVALID_VALUE) { |
| 1315 |
return SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED; |
| 1316 |
} |
| 1317 |
|
| 1318 |
p_target_node->member_count = Max(p_target_node->member_count, member_index + 1); |
| 1319 |
} |
| 1320 |
|
| 1321 |
for (uint32_t i = 0; i < p_parser->node_count; ++i) { |
| 1322 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1323 |
if (p_node->member_count == 0) { |
| 1324 |
continue; |
| 1325 |
} |
| 1326 |
|
| 1327 |
p_node->member_names = (const char**)calloc(p_node->member_count, sizeof(*(p_node->member_names))); |
| 1328 |
if (IsNull(p_node->member_names)) { |
| 1329 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1330 |
} |
| 1331 |
|
| 1332 |
p_node->member_decorations = (SpvReflectPrvDecorations*)calloc(p_node->member_count, sizeof(*(p_node->member_decorations))); |
| 1333 |
if (IsNull(p_node->member_decorations)) { |
| 1334 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1335 |
} |
| 1336 |
} |
| 1337 |
} |
| 1338 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1339 |
} |
| 1340 |
|
| 1341 |
static SpvReflectResult ParseNames(SpvReflectPrvParser* p_parser) { |
| 1342 |
assert(IsNotNull(p_parser)); |
| 1343 |
assert(IsNotNull(p_parser->spirv_code)); |
| 1344 |
assert(IsNotNull(p_parser->nodes)); |
| 1345 |
|
| 1346 |
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) { |
| 1347 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 1348 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1349 |
if ((p_node->op != SpvOpName) && (p_node->op != SpvOpMemberName)) { |
| 1350 |
continue; |
| 1351 |
} |
| 1352 |
|
| 1353 |
uint32_t target_id = 0; |
| 1354 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, target_id); |
| 1355 |
SpvReflectPrvNode* p_target_node = FindNode(p_parser, target_id); |
| 1356 |
|
| 1357 |
if (IsNull(p_target_node)) { |
| 1358 |
continue; |
| 1359 |
} |
| 1360 |
|
| 1361 |
const char** pp_target_name = &(p_target_node->name); |
| 1362 |
if (p_node->op == SpvOpMemberName) { |
| 1363 |
uint32_t member_index = UINT32_MAX; |
| 1364 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, member_index); |
| 1365 |
pp_target_name = &(p_target_node->member_names[member_index]); |
| 1366 |
} |
| 1367 |
|
| 1368 |
*pp_target_name = p_node->name; |
| 1369 |
} |
| 1370 |
} |
| 1371 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1372 |
} |
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
static bool UserTypeMatches(const char* user_type, const char* pattern) { |
| 1378 |
const size_t pattern_length = strlen(pattern); |
| 1379 |
if (strncmp(user_type, pattern, pattern_length) == 0) { |
| 1380 |
if (user_type[pattern_length] == ':' || user_type[pattern_length] == '\0') { |
| 1381 |
return true; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
return false; |
| 1385 |
} |
| 1386 |
|
| 1387 |
static SpvReflectResult ParseDecorations(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 1388 |
uint32_t spec_constant_count = 0; |
| 1389 |
for (uint32_t i = 0; i < p_parser->node_count; ++i) { |
| 1390 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1391 |
|
| 1392 |
if ((p_node->op != SpvOpDecorate) && (p_node->op != SpvOpMemberDecorate) && (p_node->op != SpvOpDecorateId) && |
| 1393 |
(p_node->op != SpvOpDecorateString) && (p_node->op != SpvOpMemberDecorateString)) { |
| 1394 |
continue; |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
uint32_t member_offset = 0; |
| 1399 |
if (p_node->op == SpvOpMemberDecorate) { |
| 1400 |
member_offset = 1; |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
uint32_t decoration = (uint32_t)INVALID_VALUE; |
| 1405 |
CHECKED_READU32(p_parser, p_node->word_offset + member_offset + 2, decoration); |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
bool skip = false; |
| 1410 |
switch (decoration) { |
| 1411 |
default: { |
| 1412 |
skip = true; |
| 1413 |
} break; |
| 1414 |
case SpvDecorationRelaxedPrecision: |
| 1415 |
case SpvDecorationBlock: |
| 1416 |
case SpvDecorationBufferBlock: |
| 1417 |
case SpvDecorationColMajor: |
| 1418 |
case SpvDecorationRowMajor: |
| 1419 |
case SpvDecorationArrayStride: |
| 1420 |
case SpvDecorationMatrixStride: |
| 1421 |
case SpvDecorationBuiltIn: |
| 1422 |
case SpvDecorationNoPerspective: |
| 1423 |
case SpvDecorationFlat: |
| 1424 |
case SpvDecorationNonWritable: |
| 1425 |
case SpvDecorationNonReadable: |
| 1426 |
case SpvDecorationPatch: |
| 1427 |
case SpvDecorationPerVertexKHR: |
| 1428 |
case SpvDecorationPerTaskNV: |
| 1429 |
case SpvDecorationLocation: |
| 1430 |
case SpvDecorationComponent: |
| 1431 |
case SpvDecorationBinding: |
| 1432 |
case SpvDecorationDescriptorSet: |
| 1433 |
case SpvDecorationOffset: |
| 1434 |
case SpvDecorationInputAttachmentIndex: |
| 1435 |
case SpvDecorationSpecId: |
| 1436 |
case SpvDecorationWeightTextureQCOM: |
| 1437 |
case SpvDecorationBlockMatchTextureQCOM: |
| 1438 |
case SpvDecorationUserTypeGOOGLE: |
| 1439 |
case SpvDecorationHlslCounterBufferGOOGLE: |
| 1440 |
case SpvDecorationHlslSemanticGOOGLE: { |
| 1441 |
skip = false; |
| 1442 |
} break; |
| 1443 |
} |
| 1444 |
if (skip) { |
| 1445 |
continue; |
| 1446 |
} |
| 1447 |
|
| 1448 |
|
| 1449 |
uint32_t target_id = 0; |
| 1450 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, target_id); |
| 1451 |
SpvReflectPrvNode* p_target_node = FindNode(p_parser, target_id); |
| 1452 |
if (IsNull(p_target_node)) { |
| 1453 |
if ((p_node->op == (uint32_t)SpvOpDecorate) && (decoration == SpvDecorationRelaxedPrecision)) { |
| 1454 |
|
| 1455 |
|
| 1456 |
continue; |
| 1457 |
} |
| 1458 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1459 |
} |
| 1460 |
|
| 1461 |
SpvReflectPrvDecorations* p_target_decorations = &(p_target_node->decorations); |
| 1462 |
|
| 1463 |
if (p_node->op == SpvOpMemberDecorate) { |
| 1464 |
uint32_t member_index = (uint32_t)INVALID_VALUE; |
| 1465 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, member_index); |
| 1466 |
p_target_decorations = &(p_target_node->member_decorations[member_index]); |
| 1467 |
} |
| 1468 |
|
| 1469 |
switch (decoration) { |
| 1470 |
default: |
| 1471 |
break; |
| 1472 |
|
| 1473 |
case SpvDecorationRelaxedPrecision: { |
| 1474 |
p_target_decorations->is_relaxed_precision = true; |
| 1475 |
} break; |
| 1476 |
|
| 1477 |
case SpvDecorationBlock: { |
| 1478 |
p_target_decorations->is_block = true; |
| 1479 |
} break; |
| 1480 |
|
| 1481 |
case SpvDecorationBufferBlock: { |
| 1482 |
p_target_decorations->is_buffer_block = true; |
| 1483 |
} break; |
| 1484 |
|
| 1485 |
case SpvDecorationColMajor: { |
| 1486 |
p_target_decorations->is_column_major = true; |
| 1487 |
} break; |
| 1488 |
|
| 1489 |
case SpvDecorationRowMajor: { |
| 1490 |
p_target_decorations->is_row_major = true; |
| 1491 |
} break; |
| 1492 |
|
| 1493 |
case SpvDecorationArrayStride: { |
| 1494 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1495 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->array_stride); |
| 1496 |
} break; |
| 1497 |
|
| 1498 |
case SpvDecorationMatrixStride: { |
| 1499 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1500 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->matrix_stride); |
| 1501 |
} break; |
| 1502 |
|
| 1503 |
case SpvDecorationBuiltIn: { |
| 1504 |
p_target_decorations->is_built_in = true; |
| 1505 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1506 |
CHECKED_READU32_CAST(p_parser, word_offset, SpvBuiltIn, p_target_decorations->built_in); |
| 1507 |
} break; |
| 1508 |
|
| 1509 |
case SpvDecorationNoPerspective: { |
| 1510 |
p_target_decorations->is_noperspective = true; |
| 1511 |
} break; |
| 1512 |
|
| 1513 |
case SpvDecorationFlat: { |
| 1514 |
p_target_decorations->is_flat = true; |
| 1515 |
} break; |
| 1516 |
|
| 1517 |
case SpvDecorationNonWritable: { |
| 1518 |
p_target_decorations->is_non_writable = true; |
| 1519 |
} break; |
| 1520 |
|
| 1521 |
case SpvDecorationNonReadable: { |
| 1522 |
p_target_decorations->is_non_readable = true; |
| 1523 |
} break; |
| 1524 |
|
| 1525 |
case SpvDecorationPatch: { |
| 1526 |
p_target_decorations->is_patch = true; |
| 1527 |
} break; |
| 1528 |
|
| 1529 |
case SpvDecorationPerVertexKHR: { |
| 1530 |
p_target_decorations->is_per_vertex = true; |
| 1531 |
} break; |
| 1532 |
|
| 1533 |
case SpvDecorationPerTaskNV: { |
| 1534 |
p_target_decorations->is_per_task = true; |
| 1535 |
} break; |
| 1536 |
|
| 1537 |
case SpvDecorationLocation: { |
| 1538 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1539 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->location.value); |
| 1540 |
p_target_decorations->location.word_offset = word_offset; |
| 1541 |
} break; |
| 1542 |
|
| 1543 |
case SpvDecorationComponent: { |
| 1544 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1545 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->component.value); |
| 1546 |
p_target_decorations->component.word_offset = word_offset; |
| 1547 |
} break; |
| 1548 |
|
| 1549 |
case SpvDecorationBinding: { |
| 1550 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1551 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->binding.value); |
| 1552 |
p_target_decorations->binding.word_offset = word_offset; |
| 1553 |
} break; |
| 1554 |
|
| 1555 |
case SpvDecorationDescriptorSet: { |
| 1556 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1557 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->set.value); |
| 1558 |
p_target_decorations->set.word_offset = word_offset; |
| 1559 |
} break; |
| 1560 |
|
| 1561 |
case SpvDecorationOffset: { |
| 1562 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1563 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->offset.value); |
| 1564 |
p_target_decorations->offset.word_offset = word_offset; |
| 1565 |
} break; |
| 1566 |
|
| 1567 |
case SpvDecorationInputAttachmentIndex: { |
| 1568 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1569 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->input_attachment_index.value); |
| 1570 |
p_target_decorations->input_attachment_index.word_offset = word_offset; |
| 1571 |
} break; |
| 1572 |
|
| 1573 |
case SpvDecorationSpecId: { |
| 1574 |
uint32_t word_offset = p_node->word_offset + member_offset+ 3; |
| 1575 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->spec_id); |
| 1576 |
spec_constant_count++; |
| 1577 |
} break; |
| 1578 |
|
| 1579 |
case SpvDecorationHlslCounterBufferGOOGLE: { |
| 1580 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1581 |
CHECKED_READU32(p_parser, word_offset, p_target_decorations->uav_counter_buffer.value); |
| 1582 |
p_target_decorations->uav_counter_buffer.word_offset = word_offset; |
| 1583 |
} break; |
| 1584 |
|
| 1585 |
case SpvDecorationHlslSemanticGOOGLE: { |
| 1586 |
uint32_t word_offset = p_node->word_offset + member_offset + 3; |
| 1587 |
p_target_decorations->semantic.value = (const char*)(p_parser->spirv_code + word_offset); |
| 1588 |
p_target_decorations->semantic.word_offset = word_offset; |
| 1589 |
} break; |
| 1590 |
|
| 1591 |
case SpvDecorationWeightTextureQCOM: { |
| 1592 |
p_target_decorations->is_weight_texture = true; |
| 1593 |
} break; |
| 1594 |
|
| 1595 |
case SpvDecorationBlockMatchTextureQCOM: { |
| 1596 |
p_target_decorations->is_block_match_texture = true; |
| 1597 |
} break; |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (p_node->op == SpvOpDecorateString && decoration == SpvDecorationUserTypeGOOGLE) { |
| 1601 |
uint32_t terminator = 0; |
| 1602 |
SpvReflectResult result = ReadStr(p_parser, p_node->word_offset + 3, 0, p_node->word_count, &terminator, NULL); |
| 1603 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 1604 |
return result; |
| 1605 |
} |
| 1606 |
const char* name = (const char*)(p_parser->spirv_code + p_node->word_offset + 3); |
| 1607 |
if (UserTypeMatches(name, "cbuffer")) { |
| 1608 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_CBUFFER; |
| 1609 |
} else if (UserTypeMatches(name, "tbuffer")) { |
| 1610 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TBUFFER; |
| 1611 |
} else if (UserTypeMatches(name, "appendstructuredbuffer")) { |
| 1612 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_APPEND_STRUCTURED_BUFFER; |
| 1613 |
} else if (UserTypeMatches(name, "buffer")) { |
| 1614 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_BUFFER; |
| 1615 |
} else if (UserTypeMatches(name, "byteaddressbuffer")) { |
| 1616 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_BYTE_ADDRESS_BUFFER; |
| 1617 |
} else if (UserTypeMatches(name, "constantbuffer")) { |
| 1618 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_CONSTANT_BUFFER; |
| 1619 |
} else if (UserTypeMatches(name, "consumestructuredbuffer")) { |
| 1620 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_CONSUME_STRUCTURED_BUFFER; |
| 1621 |
} else if (UserTypeMatches(name, "inputpatch")) { |
| 1622 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_INPUT_PATCH; |
| 1623 |
} else if (UserTypeMatches(name, "outputpatch")) { |
| 1624 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_OUTPUT_PATCH; |
| 1625 |
} else if (UserTypeMatches(name, "rasterizerorderedbuffer")) { |
| 1626 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_BUFFER; |
| 1627 |
} else if (UserTypeMatches(name, "rasterizerorderedbyteaddressbuffer")) { |
| 1628 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_BYTE_ADDRESS_BUFFER; |
| 1629 |
} else if (UserTypeMatches(name, "rasterizerorderedstructuredbuffer")) { |
| 1630 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_STRUCTURED_BUFFER; |
| 1631 |
} else if (UserTypeMatches(name, "rasterizerorderedtexture1d")) { |
| 1632 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_1D; |
| 1633 |
} else if (UserTypeMatches(name, "rasterizerorderedtexture1darray")) { |
| 1634 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_1D_ARRAY; |
| 1635 |
} else if (UserTypeMatches(name, "rasterizerorderedtexture2d")) { |
| 1636 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_2D; |
| 1637 |
} else if (UserTypeMatches(name, "rasterizerorderedtexture2darray")) { |
| 1638 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_2D_ARRAY; |
| 1639 |
} else if (UserTypeMatches(name, "rasterizerorderedtexture3d")) { |
| 1640 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_3D; |
| 1641 |
} else if (UserTypeMatches(name, "raytracingaccelerationstructure")) { |
| 1642 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RAYTRACING_ACCELERATION_STRUCTURE; |
| 1643 |
} else if (UserTypeMatches(name, "rwbuffer")) { |
| 1644 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_BUFFER; |
| 1645 |
} else if (UserTypeMatches(name, "rwbyteaddressbuffer")) { |
| 1646 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_BYTE_ADDRESS_BUFFER; |
| 1647 |
} else if (UserTypeMatches(name, "rwstructuredbuffer")) { |
| 1648 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_STRUCTURED_BUFFER; |
| 1649 |
} else if (UserTypeMatches(name, "rwtexture1d")) { |
| 1650 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_1D; |
| 1651 |
} else if (UserTypeMatches(name, "rwtexture1darray")) { |
| 1652 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_1D_ARRAY; |
| 1653 |
} else if (UserTypeMatches(name, "rwtexture2d")) { |
| 1654 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_2D; |
| 1655 |
} else if (UserTypeMatches(name, "rwtexture2darray")) { |
| 1656 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_2D_ARRAY; |
| 1657 |
} else if (UserTypeMatches(name, "rwtexture3d")) { |
| 1658 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_3D; |
| 1659 |
} else if (UserTypeMatches(name, "structuredbuffer")) { |
| 1660 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_STRUCTURED_BUFFER; |
| 1661 |
} else if (UserTypeMatches(name, "subpassinput")) { |
| 1662 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_SUBPASS_INPUT; |
| 1663 |
} else if (UserTypeMatches(name, "subpassinputms")) { |
| 1664 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_SUBPASS_INPUT_MS; |
| 1665 |
} else if (UserTypeMatches(name, "texture1d")) { |
| 1666 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_1D; |
| 1667 |
} else if (UserTypeMatches(name, "texture1darray")) { |
| 1668 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_1D_ARRAY; |
| 1669 |
} else if (UserTypeMatches(name, "texture2d")) { |
| 1670 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_2D; |
| 1671 |
} else if (UserTypeMatches(name, "texture2darray")) { |
| 1672 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_2D_ARRAY; |
| 1673 |
} else if (UserTypeMatches(name, "texture2dms")) { |
| 1674 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_2DMS; |
| 1675 |
} else if (UserTypeMatches(name, "texture2dmsarray")) { |
| 1676 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_2DMS_ARRAY; |
| 1677 |
} else if (UserTypeMatches(name, "texture3d")) { |
| 1678 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_3D; |
| 1679 |
} else if (UserTypeMatches(name, "texturebuffer")) { |
| 1680 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_BUFFER; |
| 1681 |
} else if (UserTypeMatches(name, "texturecube")) { |
| 1682 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_CUBE; |
| 1683 |
} else if (UserTypeMatches(name, "texturecubearray")) { |
| 1684 |
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_CUBE_ARRAY; |
| 1685 |
} |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
if (spec_constant_count > 0) { |
| 1690 |
p_module->spec_constants = (SpvReflectSpecializationConstant*)calloc(spec_constant_count, sizeof(*p_module->spec_constants)); |
| 1691 |
if (IsNull(p_module->spec_constants)) { |
| 1692 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1693 |
} |
| 1694 |
} |
| 1695 |
for (uint32_t i = 0; i < p_parser->node_count; ++i) { |
| 1696 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 1697 |
const uint32_t count = p_module->spec_constant_count; |
| 1698 |
switch(p_node->op) { |
| 1699 |
default: continue; |
| 1700 |
case SpvOpSpecConstantTrue: { |
| 1701 |
p_module->spec_constants[count].constant_type = SPV_REFLECT_SPECIALIZATION_CONSTANT_BOOL; |
| 1702 |
p_module->spec_constants[count].default_value.int_bool_value = 1; |
| 1703 |
} break; |
| 1704 |
case SpvOpSpecConstantFalse: { |
| 1705 |
p_module->spec_constants[count].constant_type = SPV_REFLECT_SPECIALIZATION_CONSTANT_BOOL; |
| 1706 |
p_module->spec_constants[count].default_value.int_bool_value = 0; |
| 1707 |
} break; |
| 1708 |
case SpvOpSpecConstant: { |
| 1709 |
SpvReflectResult result = SPV_REFLECT_RESULT_SUCCESS; |
| 1710 |
uint32_t element_type_id = (uint32_t)INVALID_VALUE; |
| 1711 |
uint32_t default_value = 0; |
| 1712 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, element_type_id); |
| 1713 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, default_value); |
| 1714 |
|
| 1715 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, element_type_id); |
| 1716 |
|
| 1717 |
if (p_next_node->op == SpvOpTypeInt) { |
| 1718 |
p_module->spec_constants[count].constant_type = SPV_REFLECT_SPECIALIZATION_CONSTANT_INT; |
| 1719 |
} else if (p_next_node->op == SpvOpTypeFloat) { |
| 1720 |
p_module->spec_constants[count].constant_type = SPV_REFLECT_SPECIALIZATION_CONSTANT_FLOAT; |
| 1721 |
} else { |
| 1722 |
return SPV_REFLECT_RESULT_ERROR_PARSE_FAILED; |
| 1723 |
} |
| 1724 |
|
| 1725 |
p_module->spec_constants[count].default_value.int_bool_value = default_value; |
| 1726 |
} break; |
| 1727 |
} |
| 1728 |
|
| 1729 |
p_module->spec_constants[count].name = p_node->name; |
| 1730 |
p_module->spec_constants[count].constant_id = p_node->decorations.spec_id; |
| 1731 |
p_module->spec_constants[count].spirv_id = p_node->result_id; |
| 1732 |
|
| 1733 |
p_module->spec_constant_count++; |
| 1734 |
} |
| 1735 |
|
| 1736 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1737 |
} |
| 1738 |
|
| 1739 |
static SpvReflectResult EnumerateAllUniforms(SpvReflectShaderModule* p_module, size_t* p_uniform_count, uint32_t** pp_uniforms) { |
| 1740 |
*p_uniform_count = p_module->descriptor_binding_count; |
| 1741 |
if (*p_uniform_count == 0) { |
| 1742 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1743 |
} |
| 1744 |
*pp_uniforms = (uint32_t*)calloc(*p_uniform_count, sizeof(**pp_uniforms)); |
| 1745 |
|
| 1746 |
if (IsNull(*pp_uniforms)) { |
| 1747 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1748 |
} |
| 1749 |
|
| 1750 |
for (size_t i = 0; i < *p_uniform_count; ++i) { |
| 1751 |
(*pp_uniforms)[i] = p_module->descriptor_bindings[i].spirv_id; |
| 1752 |
} |
| 1753 |
qsort(*pp_uniforms, *p_uniform_count, sizeof(**pp_uniforms), SortCompareUint32); |
| 1754 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1755 |
} |
| 1756 |
|
| 1757 |
static SpvReflectResult ParseType(SpvReflectPrvParser* p_parser, SpvReflectPrvNode* p_node, |
| 1758 |
SpvReflectPrvDecorations* p_struct_member_decorations, SpvReflectShaderModule* p_module, |
| 1759 |
SpvReflectTypeDescription* p_type) { |
| 1760 |
SpvReflectResult result = SPV_REFLECT_RESULT_SUCCESS; |
| 1761 |
|
| 1762 |
if (p_node->member_count > 0) { |
| 1763 |
p_type->struct_type_description = FindType(p_module, p_node->result_id); |
| 1764 |
p_type->member_count = p_node->member_count; |
| 1765 |
p_type->members = (SpvReflectTypeDescription*)calloc(p_type->member_count, sizeof(*(p_type->members))); |
| 1766 |
if (IsNotNull(p_type->members)) { |
| 1767 |
|
| 1768 |
for (size_t i = 0; i < p_type->members->member_count; ++i) { |
| 1769 |
SpvReflectTypeDescription* p_member_type = &(p_type->members[i]); |
| 1770 |
p_member_type->id = (uint32_t)INVALID_VALUE; |
| 1771 |
p_member_type->op = (SpvOp)INVALID_VALUE; |
| 1772 |
p_member_type->storage_class = (SpvStorageClass)INVALID_VALUE; |
| 1773 |
} |
| 1774 |
} else { |
| 1775 |
result = SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 1780 |
|
| 1781 |
|
| 1782 |
if (p_type->id == INVALID_VALUE) { |
| 1783 |
p_type->id = p_node->result_id; |
| 1784 |
p_type->op = p_node->op; |
| 1785 |
p_type->decoration_flags = 0; |
| 1786 |
} |
| 1787 |
|
| 1788 |
|
| 1789 |
p_type->decoration_flags = ApplyDecorations(&p_node->decorations); |
| 1790 |
|
| 1791 |
switch (p_node->op) { |
| 1792 |
default: |
| 1793 |
break; |
| 1794 |
case SpvOpTypeVoid: |
| 1795 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_VOID; |
| 1796 |
break; |
| 1797 |
|
| 1798 |
case SpvOpTypeBool: |
| 1799 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_BOOL; |
| 1800 |
break; |
| 1801 |
|
| 1802 |
case SpvOpTypeInt: { |
| 1803 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_INT; |
| 1804 |
IF_READU32(result, p_parser, p_node->word_offset + 2, p_type->traits.numeric.scalar.width); |
| 1805 |
IF_READU32(result, p_parser, p_node->word_offset + 3, p_type->traits.numeric.scalar.signedness); |
| 1806 |
} break; |
| 1807 |
|
| 1808 |
case SpvOpTypeFloat: { |
| 1809 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_FLOAT; |
| 1810 |
IF_READU32(result, p_parser, p_node->word_offset + 2, p_type->traits.numeric.scalar.width); |
| 1811 |
} break; |
| 1812 |
|
| 1813 |
case SpvOpTypeVector: { |
| 1814 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_VECTOR; |
| 1815 |
uint32_t component_type_id = (uint32_t)INVALID_VALUE; |
| 1816 |
IF_READU32(result, p_parser, p_node->word_offset + 2, component_type_id); |
| 1817 |
IF_READU32(result, p_parser, p_node->word_offset + 3, p_type->traits.numeric.vector.component_count); |
| 1818 |
|
| 1819 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, component_type_id); |
| 1820 |
if (IsNotNull(p_next_node)) { |
| 1821 |
result = ParseType(p_parser, p_next_node, NULL, p_module, p_type); |
| 1822 |
} else { |
| 1823 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1824 |
SPV_REFLECT_ASSERT(false); |
| 1825 |
} |
| 1826 |
} break; |
| 1827 |
|
| 1828 |
case SpvOpTypeMatrix: { |
| 1829 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_MATRIX; |
| 1830 |
uint32_t column_type_id = (uint32_t)INVALID_VALUE; |
| 1831 |
IF_READU32(result, p_parser, p_node->word_offset + 2, column_type_id); |
| 1832 |
IF_READU32(result, p_parser, p_node->word_offset + 3, p_type->traits.numeric.matrix.column_count); |
| 1833 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, column_type_id); |
| 1834 |
if (IsNotNull(p_next_node)) { |
| 1835 |
result = ParseType(p_parser, p_next_node, NULL, p_module, p_type); |
| 1836 |
} else { |
| 1837 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1838 |
SPV_REFLECT_ASSERT(false); |
| 1839 |
} |
| 1840 |
p_type->traits.numeric.matrix.row_count = p_type->traits.numeric.vector.component_count; |
| 1841 |
p_type->traits.numeric.matrix.stride = p_node->decorations.matrix_stride; |
| 1842 |
|
| 1843 |
if (IsNotNull(p_struct_member_decorations)) { |
| 1844 |
p_type->traits.numeric.matrix.stride = p_struct_member_decorations->matrix_stride; |
| 1845 |
} |
| 1846 |
} break; |
| 1847 |
|
| 1848 |
case SpvOpTypeImage: { |
| 1849 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE; |
| 1850 |
uint32_t sampled_type_id = (uint32_t)INVALID_VALUE; |
| 1851 |
IF_READU32(result, p_parser, p_node->word_offset + 2, sampled_type_id); |
| 1852 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, sampled_type_id); |
| 1853 |
if (IsNotNull(p_next_node)) { |
| 1854 |
result = ParseType(p_parser, p_next_node, NULL, p_module, p_type); |
| 1855 |
} else { |
| 1856 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1857 |
} |
| 1858 |
IF_READU32_CAST(result, p_parser, p_node->word_offset + 3, SpvDim, p_type->traits.image.dim); |
| 1859 |
IF_READU32(result, p_parser, p_node->word_offset + 4, p_type->traits.image.depth); |
| 1860 |
IF_READU32(result, p_parser, p_node->word_offset + 5, p_type->traits.image.arrayed); |
| 1861 |
IF_READU32(result, p_parser, p_node->word_offset + 6, p_type->traits.image.ms); |
| 1862 |
IF_READU32(result, p_parser, p_node->word_offset + 7, p_type->traits.image.sampled); |
| 1863 |
IF_READU32_CAST(result, p_parser, p_node->word_offset + 8, SpvImageFormat, p_type->traits.image.image_format); |
| 1864 |
} break; |
| 1865 |
|
| 1866 |
case SpvOpTypeSampler: { |
| 1867 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLER; |
| 1868 |
} break; |
| 1869 |
|
| 1870 |
case SpvOpTypeSampledImage: { |
| 1871 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLED_IMAGE; |
| 1872 |
uint32_t image_type_id = (uint32_t)INVALID_VALUE; |
| 1873 |
IF_READU32(result, p_parser, p_node->word_offset + 2, image_type_id); |
| 1874 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, image_type_id); |
| 1875 |
if (IsNotNull(p_next_node)) { |
| 1876 |
result = ParseType(p_parser, p_next_node, NULL, p_module, p_type); |
| 1877 |
} else { |
| 1878 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1879 |
SPV_REFLECT_ASSERT(false); |
| 1880 |
} |
| 1881 |
} break; |
| 1882 |
|
| 1883 |
case SpvOpTypeArray: { |
| 1884 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_ARRAY; |
| 1885 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 1886 |
uint32_t element_type_id = (uint32_t)INVALID_VALUE; |
| 1887 |
uint32_t length_id = (uint32_t)INVALID_VALUE; |
| 1888 |
IF_READU32(result, p_parser, p_node->word_offset + 2, element_type_id); |
| 1889 |
IF_READU32(result, p_parser, p_node->word_offset + 3, length_id); |
| 1890 |
|
| 1891 |
|
| 1892 |
p_type->traits.array.stride = p_node->decorations.array_stride; |
| 1893 |
|
| 1894 |
SpvReflectPrvNode* p_length_node = FindNode(p_parser, length_id); |
| 1895 |
if (IsNotNull(p_length_node)) { |
| 1896 |
uint32_t dim_index = p_type->traits.array.dims_count; |
| 1897 |
uint32_t length = 0; |
| 1898 |
IF_READU32(result, p_parser, p_length_node->word_offset + 3, length); |
| 1899 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 1900 |
p_type->traits.array.dims[dim_index] = length; |
| 1901 |
p_type->traits.array.dims_count += 1; |
| 1902 |
p_type->traits.array.spec_constant_op_ids[dim_index] = |
| 1903 |
IsSpecConstant(p_length_node) ? p_length_node->decorations.spec_id : (uint32_t)INVALID_VALUE; |
| 1904 |
} else { |
| 1905 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1906 |
SPV_REFLECT_ASSERT(false); |
| 1907 |
} |
| 1908 |
|
| 1909 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, element_type_id); |
| 1910 |
if (IsNotNull(p_next_node)) { |
| 1911 |
result = ParseType(p_parser, p_next_node, NULL, p_module, p_type); |
| 1912 |
} |
| 1913 |
} else { |
| 1914 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1915 |
SPV_REFLECT_ASSERT(false); |
| 1916 |
} |
| 1917 |
} |
| 1918 |
} break; |
| 1919 |
|
| 1920 |
case SpvOpTypeRuntimeArray: { |
| 1921 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_ARRAY; |
| 1922 |
uint32_t element_type_id = (uint32_t)INVALID_VALUE; |
| 1923 |
IF_READU32(result, p_parser, p_node->word_offset + 2, element_type_id); |
| 1924 |
p_type->traits.array.stride = p_node->decorations.array_stride; |
| 1925 |
uint32_t dim_index = p_type->traits.array.dims_count; |
| 1926 |
p_type->traits.array.dims[dim_index] = (uint32_t)SPV_REFLECT_ARRAY_DIM_RUNTIME; |
| 1927 |
p_type->traits.array.spec_constant_op_ids[dim_index] = (uint32_t)INVALID_VALUE; |
| 1928 |
p_type->traits.array.dims_count += 1; |
| 1929 |
|
| 1930 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, element_type_id); |
| 1931 |
if (IsNotNull(p_next_node)) { |
| 1932 |
result = ParseType(p_parser, p_next_node, NULL, p_module, p_type); |
| 1933 |
} else { |
| 1934 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1935 |
SPV_REFLECT_ASSERT(false); |
| 1936 |
} |
| 1937 |
} break; |
| 1938 |
|
| 1939 |
case SpvOpTypeStruct: { |
| 1940 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_STRUCT; |
| 1941 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_BLOCK; |
| 1942 |
uint32_t word_index = 2; |
| 1943 |
uint32_t member_index = 0; |
| 1944 |
for (; word_index < p_node->word_count; ++word_index, ++member_index) { |
| 1945 |
uint32_t member_id = (uint32_t)INVALID_VALUE; |
| 1946 |
IF_READU32(result, p_parser, p_node->word_offset + word_index, member_id); |
| 1947 |
|
| 1948 |
SpvReflectPrvNode* p_member_node = FindNode(p_parser, member_id); |
| 1949 |
if (IsNull(p_member_node)) { |
| 1950 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 1951 |
SPV_REFLECT_ASSERT(false); |
| 1952 |
break; |
| 1953 |
} |
| 1954 |
|
| 1955 |
|
| 1956 |
SpvReflectPrvDecorations* p_member_decorations = &p_node->member_decorations[member_index]; |
| 1957 |
|
| 1958 |
assert(member_index < p_type->member_count); |
| 1959 |
|
| 1960 |
SpvReflectTypeDescription* p_member_type = &(p_type->members[member_index]); |
| 1961 |
p_member_type->id = member_id; |
| 1962 |
p_member_type->op = p_member_node->op; |
| 1963 |
result = ParseType(p_parser, p_member_node, p_member_decorations, p_module, p_member_type); |
| 1964 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 1965 |
break; |
| 1966 |
} |
| 1967 |
|
| 1968 |
|
| 1969 |
p_member_type->struct_member_name = p_node->member_names[member_index]; |
| 1970 |
} |
| 1971 |
} break; |
| 1972 |
|
| 1973 |
case SpvOpTypeOpaque: |
| 1974 |
break; |
| 1975 |
|
| 1976 |
case SpvOpTypePointer: { |
| 1977 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_REF; |
| 1978 |
IF_READU32_CAST(result, p_parser, p_node->word_offset + 2, SpvStorageClass, p_type->storage_class); |
| 1979 |
|
| 1980 |
bool found_recursion = false; |
| 1981 |
if (p_type->storage_class == SpvStorageClassPhysicalStorageBuffer) { |
| 1982 |
|
| 1983 |
for (uint32_t i = 0; i < p_parser->physical_pointer_count; i++) { |
| 1984 |
if (p_type->id == p_parser->physical_pointer_check[i]->id) { |
| 1985 |
found_recursion = true; |
| 1986 |
memcpy(p_type, p_parser->physical_pointer_check[i], sizeof(SpvReflectTypeDescription)); |
| 1987 |
p_type->copied = 1; |
| 1988 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 1989 |
} |
| 1990 |
} |
| 1991 |
if (!found_recursion) { |
| 1992 |
p_parser->physical_pointer_struct_count++; |
| 1993 |
p_parser->physical_pointer_check[p_parser->physical_pointer_count] = p_type; |
| 1994 |
p_parser->physical_pointer_count++; |
| 1995 |
if (p_parser->physical_pointer_count >= MAX_RECURSIVE_PHYSICAL_POINTER_CHECK) { |
| 1996 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_MAX_RECURSIVE_EXCEEDED; |
| 1997 |
} |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
|
| 2002 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, p_node->type_id); |
| 2003 |
if (IsNull(p_next_node)) { |
| 2004 |
result = SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2005 |
SPV_REFLECT_ASSERT(false); |
| 2006 |
} else if (!found_recursion) { |
| 2007 |
if (p_next_node->op == SpvOpTypeStruct) { |
| 2008 |
p_type->struct_type_description = FindType(p_module, p_next_node->result_id); |
| 2009 |
} |
| 2010 |
|
| 2011 |
result = ParseType(p_parser, p_next_node, NULL, p_module, p_type); |
| 2012 |
} |
| 2013 |
} break; |
| 2014 |
|
| 2015 |
case SpvOpTypeAccelerationStructureKHR: { |
| 2016 |
p_type->type_flags |= SPV_REFLECT_TYPE_FLAG_EXTERNAL_ACCELERATION_STRUCTURE; |
| 2017 |
} break; |
| 2018 |
} |
| 2019 |
|
| 2020 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 2021 |
|
| 2022 |
|
| 2023 |
if (IsNull(p_type->type_name)) { |
| 2024 |
p_type->type_name = p_node->name; |
| 2025 |
} |
| 2026 |
} |
| 2027 |
} |
| 2028 |
|
| 2029 |
return result; |
| 2030 |
} |
| 2031 |
|
| 2032 |
static SpvReflectResult ParseTypes(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 2033 |
if (p_parser->type_count == 0) { |
| 2034 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2035 |
} |
| 2036 |
|
| 2037 |
p_module->_internal->type_description_count = p_parser->type_count; |
| 2038 |
p_module->_internal->type_descriptions = (SpvReflectTypeDescription*)calloc(p_module->_internal->type_description_count, |
| 2039 |
sizeof(*(p_module->_internal->type_descriptions))); |
| 2040 |
if (IsNull(p_module->_internal->type_descriptions)) { |
| 2041 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 2042 |
} |
| 2043 |
|
| 2044 |
|
| 2045 |
for (size_t i = 0; i < p_module->_internal->type_description_count; ++i) { |
| 2046 |
SpvReflectTypeDescription* p_type = &(p_module->_internal->type_descriptions[i]); |
| 2047 |
p_type->id = (uint32_t)INVALID_VALUE; |
| 2048 |
p_type->op = (SpvOp)INVALID_VALUE; |
| 2049 |
p_type->storage_class = (SpvStorageClass)INVALID_VALUE; |
| 2050 |
} |
| 2051 |
|
| 2052 |
size_t type_index = 0; |
| 2053 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 2054 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 2055 |
if (!p_node->is_type) { |
| 2056 |
continue; |
| 2057 |
} |
| 2058 |
|
| 2059 |
SpvReflectTypeDescription* p_type = &(p_module->_internal->type_descriptions[type_index]); |
| 2060 |
p_parser->physical_pointer_count = 0; |
| 2061 |
SpvReflectResult result = ParseType(p_parser, p_node, NULL, p_module, p_type); |
| 2062 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2063 |
return result; |
| 2064 |
} |
| 2065 |
++type_index; |
| 2066 |
} |
| 2067 |
|
| 2068 |
|
| 2069 |
if (p_parser->physical_pointer_struct_count > 0) { |
| 2070 |
p_parser->physical_pointer_structs = (SpvReflectPrvPhysicalPointerStruct*)calloc(p_parser->physical_pointer_struct_count, |
| 2071 |
sizeof(*(p_parser->physical_pointer_structs))); |
| 2072 |
if (IsNull(p_parser->physical_pointer_structs)) { |
| 2073 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 2074 |
} |
| 2075 |
} |
| 2076 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2077 |
} |
| 2078 |
|
| 2079 |
static SpvReflectResult ParseCapabilities(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 2080 |
if (p_parser->capability_count == 0) { |
| 2081 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2082 |
} |
| 2083 |
|
| 2084 |
p_module->capability_count = p_parser->capability_count; |
| 2085 |
p_module->capabilities = (SpvReflectCapability*)calloc(p_module->capability_count, sizeof(*(p_module->capabilities))); |
| 2086 |
if (IsNull(p_module->capabilities)) { |
| 2087 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 2088 |
} |
| 2089 |
|
| 2090 |
|
| 2091 |
for (size_t i = 0; i < p_module->capability_count; ++i) { |
| 2092 |
SpvReflectCapability* p_cap = &(p_module->capabilities[i]); |
| 2093 |
p_cap->value = SpvCapabilityMax; |
| 2094 |
p_cap->word_offset = (uint32_t)INVALID_VALUE; |
| 2095 |
} |
| 2096 |
|
| 2097 |
size_t capability_index = 0; |
| 2098 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 2099 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 2100 |
if (SpvOpCapability != p_node->op) { |
| 2101 |
continue; |
| 2102 |
} |
| 2103 |
|
| 2104 |
SpvReflectCapability* p_cap = &(p_module->capabilities[capability_index]); |
| 2105 |
p_cap->value = p_node->capability; |
| 2106 |
p_cap->word_offset = p_node->word_offset + 1; |
| 2107 |
++capability_index; |
| 2108 |
} |
| 2109 |
|
| 2110 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2111 |
} |
| 2112 |
|
| 2113 |
static int SortCompareDescriptorBinding(const void* a, const void* b) { |
| 2114 |
const SpvReflectDescriptorBinding* p_elem_a = (const SpvReflectDescriptorBinding*)a; |
| 2115 |
const SpvReflectDescriptorBinding* p_elem_b = (const SpvReflectDescriptorBinding*)b; |
| 2116 |
int value = (int)(p_elem_a->binding) - (int)(p_elem_b->binding); |
| 2117 |
if (value == 0) { |
| 2118 |
|
| 2119 |
|
| 2120 |
assert(p_elem_a->spirv_id != p_elem_b->spirv_id); |
| 2121 |
value = (int)(p_elem_a->spirv_id) - (int)(p_elem_b->spirv_id); |
| 2122 |
} |
| 2123 |
return value; |
| 2124 |
} |
| 2125 |
|
| 2126 |
static SpvReflectResult ParseDescriptorBindings(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 2127 |
p_module->descriptor_binding_count = 0; |
| 2128 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 2129 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 2130 |
if ((p_node->op != SpvOpVariable) || |
| 2131 |
((p_node->storage_class != SpvStorageClassUniform) && (p_node->storage_class != SpvStorageClassStorageBuffer) && |
| 2132 |
(p_node->storage_class != SpvStorageClassUniformConstant))) { |
| 2133 |
continue; |
| 2134 |
} |
| 2135 |
if ((p_node->decorations.set.value == INVALID_VALUE) || (p_node->decorations.binding.value == INVALID_VALUE)) { |
| 2136 |
continue; |
| 2137 |
} |
| 2138 |
|
| 2139 |
p_module->descriptor_binding_count += 1; |
| 2140 |
} |
| 2141 |
|
| 2142 |
if (p_module->descriptor_binding_count == 0) { |
| 2143 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2144 |
} |
| 2145 |
|
| 2146 |
p_module->descriptor_bindings = |
| 2147 |
(SpvReflectDescriptorBinding*)calloc(p_module->descriptor_binding_count, sizeof(*(p_module->descriptor_bindings))); |
| 2148 |
if (IsNull(p_module->descriptor_bindings)) { |
| 2149 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) { |
| 2154 |
SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]); |
| 2155 |
p_descriptor->binding = (uint32_t)INVALID_VALUE; |
| 2156 |
p_descriptor->input_attachment_index = (uint32_t)INVALID_VALUE; |
| 2157 |
p_descriptor->set = (uint32_t)INVALID_VALUE; |
| 2158 |
p_descriptor->descriptor_type = (SpvReflectDescriptorType)INVALID_VALUE; |
| 2159 |
p_descriptor->uav_counter_id = (uint32_t)INVALID_VALUE; |
| 2160 |
} |
| 2161 |
|
| 2162 |
size_t descriptor_index = 0; |
| 2163 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 2164 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 2165 |
if ((p_node->op != SpvOpVariable) || |
| 2166 |
((p_node->storage_class != SpvStorageClassUniform) && (p_node->storage_class != SpvStorageClassStorageBuffer) && |
| 2167 |
(p_node->storage_class != SpvStorageClassUniformConstant))) { |
| 2168 |
continue; |
| 2169 |
} |
| 2170 |
if ((p_node->decorations.set.value == INVALID_VALUE) || (p_node->decorations.binding.value == INVALID_VALUE)) { |
| 2171 |
continue; |
| 2172 |
} |
| 2173 |
|
| 2174 |
SpvReflectTypeDescription* p_type = FindType(p_module, p_node->type_id); |
| 2175 |
if (IsNull(p_type)) { |
| 2176 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2177 |
} |
| 2178 |
|
| 2179 |
|
| 2180 |
SpvStorageClass pointer_storage_class = SpvStorageClassMax; |
| 2181 |
if (p_type->op == SpvOpTypePointer) { |
| 2182 |
pointer_storage_class = p_type->storage_class; |
| 2183 |
|
| 2184 |
SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 2185 |
if (IsNull(p_type_node)) { |
| 2186 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2187 |
} |
| 2188 |
|
| 2189 |
p_type = FindType(p_module, p_type_node->type_id); |
| 2190 |
if (IsNull(p_type)) { |
| 2191 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2192 |
} |
| 2193 |
} |
| 2194 |
|
| 2195 |
SpvReflectDescriptorBinding* p_descriptor = &p_module->descriptor_bindings[descriptor_index]; |
| 2196 |
p_descriptor->spirv_id = p_node->result_id; |
| 2197 |
p_descriptor->name = p_node->name; |
| 2198 |
p_descriptor->binding = p_node->decorations.binding.value; |
| 2199 |
p_descriptor->input_attachment_index = p_node->decorations.input_attachment_index.value; |
| 2200 |
p_descriptor->set = p_node->decorations.set.value; |
| 2201 |
p_descriptor->count = 1; |
| 2202 |
p_descriptor->uav_counter_id = p_node->decorations.uav_counter_buffer.value; |
| 2203 |
p_descriptor->type_description = p_type; |
| 2204 |
p_descriptor->decoration_flags = ApplyDecorations(&p_node->decorations); |
| 2205 |
p_descriptor->user_type = p_node->decorations.user_type; |
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
const SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 2211 |
if (IsNotNull(p_type_node) && p_type_node->member_count) { |
| 2212 |
SpvReflectPrvDecorations common_flags = p_type_node->member_decorations[0]; |
| 2213 |
|
| 2214 |
for (uint32_t m = 1; m < p_type_node->member_count; ++m) { |
| 2215 |
common_flags.is_relaxed_precision &= p_type_node->member_decorations[m].is_relaxed_precision; |
| 2216 |
common_flags.is_block &= p_type_node->member_decorations[m].is_block; |
| 2217 |
common_flags.is_buffer_block &= p_type_node->member_decorations[m].is_buffer_block; |
| 2218 |
common_flags.is_row_major &= p_type_node->member_decorations[m].is_row_major; |
| 2219 |
common_flags.is_column_major &= p_type_node->member_decorations[m].is_column_major; |
| 2220 |
common_flags.is_built_in &= p_type_node->member_decorations[m].is_built_in; |
| 2221 |
common_flags.is_noperspective &= p_type_node->member_decorations[m].is_noperspective; |
| 2222 |
common_flags.is_flat &= p_type_node->member_decorations[m].is_flat; |
| 2223 |
common_flags.is_non_writable &= p_type_node->member_decorations[m].is_non_writable; |
| 2224 |
common_flags.is_non_readable &= p_type_node->member_decorations[m].is_non_readable; |
| 2225 |
common_flags.is_patch &= p_type_node->member_decorations[m].is_patch; |
| 2226 |
common_flags.is_per_vertex &= p_type_node->member_decorations[m].is_per_vertex; |
| 2227 |
common_flags.is_per_task &= p_type_node->member_decorations[m].is_per_task; |
| 2228 |
common_flags.is_weight_texture &= p_type_node->member_decorations[m].is_weight_texture; |
| 2229 |
common_flags.is_block_match_texture &= p_type_node->member_decorations[m].is_block_match_texture; |
| 2230 |
} |
| 2231 |
|
| 2232 |
p_descriptor->decoration_flags |= ApplyDecorations(&common_flags); |
| 2233 |
} |
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
if (pointer_storage_class == SpvStorageClassStorageBuffer) { |
| 2244 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 2245 |
} |
| 2246 |
|
| 2247 |
|
| 2248 |
if ((p_type->type_flags & SPV_REFLECT_TYPE_FLAG_EXTERNAL_MASK) == SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE) { |
| 2249 |
memcpy(&p_descriptor->image, &p_type->traits.image, sizeof(p_descriptor->image)); |
| 2250 |
} |
| 2251 |
|
| 2252 |
|
| 2253 |
{ |
| 2254 |
const uint32_t resource_mask = SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLED_IMAGE | SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE; |
| 2255 |
if ((p_type->type_flags & resource_mask) == resource_mask) { |
| 2256 |
memcpy(&p_descriptor->image, &p_type->traits.image, sizeof(p_descriptor->image)); |
| 2257 |
} |
| 2258 |
} |
| 2259 |
|
| 2260 |
|
| 2261 |
if (p_type->traits.array.dims_count > 0) { |
| 2262 |
p_descriptor->array.dims_count = p_type->traits.array.dims_count; |
| 2263 |
for (uint32_t dim_index = 0; dim_index < p_type->traits.array.dims_count; ++dim_index) { |
| 2264 |
uint32_t dim_value = p_type->traits.array.dims[dim_index]; |
| 2265 |
p_descriptor->array.dims[dim_index] = dim_value; |
| 2266 |
p_descriptor->count *= dim_value; |
| 2267 |
} |
| 2268 |
} |
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
p_descriptor->word_offset.binding = p_node->decorations.binding.word_offset; |
| 2273 |
p_descriptor->word_offset.set = p_node->decorations.set.word_offset; |
| 2274 |
|
| 2275 |
++descriptor_index; |
| 2276 |
} |
| 2277 |
|
| 2278 |
if (p_module->descriptor_binding_count > 0) { |
| 2279 |
qsort(p_module->descriptor_bindings, p_module->descriptor_binding_count, sizeof(*(p_module->descriptor_bindings)), |
| 2280 |
SortCompareDescriptorBinding); |
| 2281 |
} |
| 2282 |
|
| 2283 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2284 |
} |
| 2285 |
|
| 2286 |
static SpvReflectResult ParseDescriptorType(SpvReflectShaderModule* p_module) { |
| 2287 |
if (p_module->descriptor_binding_count == 0) { |
| 2288 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2289 |
} |
| 2290 |
|
| 2291 |
for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) { |
| 2292 |
SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]); |
| 2293 |
SpvReflectTypeDescription* p_type = p_descriptor->type_description; |
| 2294 |
|
| 2295 |
if ((int)p_descriptor->descriptor_type == (int)INVALID_VALUE) { |
| 2296 |
switch (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_EXTERNAL_MASK) { |
| 2297 |
default: |
| 2298 |
assert(false && "unknown type flag"); |
| 2299 |
break; |
| 2300 |
|
| 2301 |
case SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE: { |
| 2302 |
if (p_descriptor->image.dim == SpvDimBuffer) { |
| 2303 |
switch (p_descriptor->image.sampled) { |
| 2304 |
default: |
| 2305 |
assert(false && "unknown texel buffer sampled value"); |
| 2306 |
break; |
| 2307 |
case IMAGE_SAMPLED: |
| 2308 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; |
| 2309 |
break; |
| 2310 |
case IMAGE_STORAGE: |
| 2311 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; |
| 2312 |
break; |
| 2313 |
} |
| 2314 |
} else if (p_descriptor->image.dim == SpvDimSubpassData) { |
| 2315 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_INPUT_ATTACHMENT; |
| 2316 |
} else { |
| 2317 |
switch (p_descriptor->image.sampled) { |
| 2318 |
default: |
| 2319 |
assert(false && "unknown image sampled value"); |
| 2320 |
break; |
| 2321 |
case IMAGE_SAMPLED: |
| 2322 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLED_IMAGE; |
| 2323 |
break; |
| 2324 |
case IMAGE_STORAGE: |
| 2325 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_IMAGE; |
| 2326 |
break; |
| 2327 |
} |
| 2328 |
} |
| 2329 |
} break; |
| 2330 |
|
| 2331 |
case SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLER: { |
| 2332 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLER; |
| 2333 |
} break; |
| 2334 |
|
| 2335 |
case (SPV_REFLECT_TYPE_FLAG_EXTERNAL_SAMPLED_IMAGE | SPV_REFLECT_TYPE_FLAG_EXTERNAL_IMAGE): { |
| 2336 |
|
| 2337 |
if (p_descriptor->image.dim == SpvDimBuffer) { |
| 2338 |
switch (p_descriptor->image.sampled) { |
| 2339 |
default: |
| 2340 |
assert(false && "unknown texel buffer sampled value"); |
| 2341 |
break; |
| 2342 |
case IMAGE_SAMPLED: |
| 2343 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; |
| 2344 |
break; |
| 2345 |
case IMAGE_STORAGE: |
| 2346 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; |
| 2347 |
break; |
| 2348 |
} |
| 2349 |
} else { |
| 2350 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 2351 |
} |
| 2352 |
} break; |
| 2353 |
|
| 2354 |
case SPV_REFLECT_TYPE_FLAG_EXTERNAL_BLOCK: { |
| 2355 |
if (p_type->decoration_flags & SPV_REFLECT_DECORATION_BLOCK) { |
| 2356 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 2357 |
} else if (p_type->decoration_flags & SPV_REFLECT_DECORATION_BUFFER_BLOCK) { |
| 2358 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 2359 |
} else { |
| 2360 |
assert(false && "unknown struct"); |
| 2361 |
} |
| 2362 |
} break; |
| 2363 |
|
| 2364 |
case SPV_REFLECT_TYPE_FLAG_EXTERNAL_ACCELERATION_STRUCTURE: { |
| 2365 |
p_descriptor->descriptor_type = SPV_REFLECT_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR; |
| 2366 |
} break; |
| 2367 |
} |
| 2368 |
} |
| 2369 |
|
| 2370 |
switch (p_descriptor->descriptor_type) { |
| 2371 |
case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLER: |
| 2372 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SAMPLER; |
| 2373 |
break; |
| 2374 |
case SPV_REFLECT_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 2375 |
p_descriptor->resource_type = (SpvReflectResourceType)(SPV_REFLECT_RESOURCE_FLAG_SAMPLER | SPV_REFLECT_RESOURCE_FLAG_SRV); |
| 2376 |
break; |
| 2377 |
case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 2378 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SRV; |
| 2379 |
break; |
| 2380 |
case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 2381 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_UAV; |
| 2382 |
break; |
| 2383 |
case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 2384 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SRV; |
| 2385 |
break; |
| 2386 |
case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 2387 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_UAV; |
| 2388 |
break; |
| 2389 |
case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 2390 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_CBV; |
| 2391 |
break; |
| 2392 |
case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 2393 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_CBV; |
| 2394 |
break; |
| 2395 |
case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 2396 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_UAV; |
| 2397 |
break; |
| 2398 |
case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 2399 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_UAV; |
| 2400 |
break; |
| 2401 |
case SPV_REFLECT_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 2402 |
break; |
| 2403 |
case SPV_REFLECT_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR: |
| 2404 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SRV; |
| 2405 |
break; |
| 2406 |
} |
| 2407 |
} |
| 2408 |
|
| 2409 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2410 |
} |
| 2411 |
|
| 2412 |
static SpvReflectResult ParseUAVCounterBindings(SpvReflectShaderModule* p_module) { |
| 2413 |
char name[MAX_NODE_NAME_LENGTH]; |
| 2414 |
const char* k_count_tag = "@count"; |
| 2415 |
|
| 2416 |
for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) { |
| 2417 |
SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]); |
| 2418 |
|
| 2419 |
if (p_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) { |
| 2420 |
continue; |
| 2421 |
} |
| 2422 |
|
| 2423 |
SpvReflectDescriptorBinding* p_counter_descriptor = NULL; |
| 2424 |
|
| 2425 |
if (p_descriptor->uav_counter_id != UINT32_MAX) { |
| 2426 |
for (uint32_t counter_descriptor_index = 0; counter_descriptor_index < p_module->descriptor_binding_count; |
| 2427 |
++counter_descriptor_index) { |
| 2428 |
SpvReflectDescriptorBinding* p_test_counter_descriptor = &(p_module->descriptor_bindings[counter_descriptor_index]); |
| 2429 |
if (p_test_counter_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) { |
| 2430 |
continue; |
| 2431 |
} |
| 2432 |
if (p_descriptor->uav_counter_id == p_test_counter_descriptor->spirv_id) { |
| 2433 |
p_counter_descriptor = p_test_counter_descriptor; |
| 2434 |
break; |
| 2435 |
} |
| 2436 |
} |
| 2437 |
} |
| 2438 |
|
| 2439 |
else { |
| 2440 |
const size_t descriptor_name_length = p_descriptor->name ? strlen(p_descriptor->name) : 0; |
| 2441 |
|
| 2442 |
memset(name, 0, MAX_NODE_NAME_LENGTH); |
| 2443 |
memcpy(name, p_descriptor->name, descriptor_name_length); |
| 2444 |
#if defined(_WIN32) |
| 2445 |
strcat_s(name, MAX_NODE_NAME_LENGTH, k_count_tag); |
| 2446 |
#else |
| 2447 |
strcat(name, k_count_tag); |
| 2448 |
#endif |
| 2449 |
|
| 2450 |
for (uint32_t counter_descriptor_index = 0; counter_descriptor_index < p_module->descriptor_binding_count; |
| 2451 |
++counter_descriptor_index) { |
| 2452 |
SpvReflectDescriptorBinding* p_test_counter_descriptor = &(p_module->descriptor_bindings[counter_descriptor_index]); |
| 2453 |
if (p_test_counter_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) { |
| 2454 |
continue; |
| 2455 |
} |
| 2456 |
if (p_test_counter_descriptor->name && strcmp(name, p_test_counter_descriptor->name) == 0) { |
| 2457 |
p_counter_descriptor = p_test_counter_descriptor; |
| 2458 |
break; |
| 2459 |
} |
| 2460 |
} |
| 2461 |
} |
| 2462 |
|
| 2463 |
if (p_counter_descriptor != NULL) { |
| 2464 |
p_descriptor->uav_counter_binding = p_counter_descriptor; |
| 2465 |
} |
| 2466 |
} |
| 2467 |
|
| 2468 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2469 |
} |
| 2470 |
|
| 2471 |
static SpvReflectResult ParseDescriptorBlockVariable(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module, |
| 2472 |
SpvReflectTypeDescription* p_type, SpvReflectBlockVariable* p_var) { |
| 2473 |
bool has_non_writable = false; |
| 2474 |
|
| 2475 |
if (IsNotNull(p_type->members) && (p_type->member_count > 0)) { |
| 2476 |
p_var->member_count = p_type->member_count; |
| 2477 |
p_var->members = (SpvReflectBlockVariable*)calloc(p_var->member_count, sizeof(*p_var->members)); |
| 2478 |
if (IsNull(p_var->members)) { |
| 2479 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 2480 |
} |
| 2481 |
|
| 2482 |
SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 2483 |
if (IsNull(p_type_node)) { |
| 2484 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2485 |
} |
| 2486 |
|
| 2487 |
while (p_type_node->op == SpvOpTypeArray || p_type_node->op == SpvOpTypeRuntimeArray) { |
| 2488 |
if (p_type_node->op == SpvOpTypeArray) { |
| 2489 |
p_type_node = FindNode(p_parser, p_type_node->array_traits.element_type_id); |
| 2490 |
} else { |
| 2491 |
|
| 2492 |
SpvReflectTypeDescription* p_type_temp = FindType(p_module, p_type_node->array_traits.element_type_id); |
| 2493 |
if (IsNull(p_type_temp)) { |
| 2494 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2495 |
} |
| 2496 |
|
| 2497 |
p_type_node = FindNode(p_parser, p_type_temp->id); |
| 2498 |
} |
| 2499 |
if (IsNull(p_type_node)) { |
| 2500 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2501 |
} |
| 2502 |
} |
| 2503 |
|
| 2504 |
|
| 2505 |
for (uint32_t member_index = 0; member_index < p_type->member_count; ++member_index) { |
| 2506 |
SpvReflectTypeDescription* p_member_type = &p_type->members[member_index]; |
| 2507 |
SpvReflectBlockVariable* p_member_var = &p_var->members[member_index]; |
| 2508 |
|
| 2509 |
SpvReflectTypeDescription* p_member_ptr_type = 0; |
| 2510 |
bool found_recursion = false; |
| 2511 |
|
| 2512 |
if ((p_member_type->storage_class == SpvStorageClassPhysicalStorageBuffer) && |
| 2513 |
(p_member_type->type_flags & SPV_REFLECT_TYPE_FLAG_REF)) { |
| 2514 |
|
| 2515 |
p_member_ptr_type = p_member_type; |
| 2516 |
|
| 2517 |
|
| 2518 |
if (p_member_type->op == SpvOpTypeArray) { |
| 2519 |
SpvReflectPrvNode* p_node = FindNode(p_parser, p_member_type->id); |
| 2520 |
if (p_node == NULL) { |
| 2521 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2522 |
} |
| 2523 |
uint32_t element_type_id = p_node->array_traits.element_type_id; |
| 2524 |
p_member_type = FindType(p_module, element_type_id); |
| 2525 |
if (p_member_type == NULL) { |
| 2526 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2527 |
} |
| 2528 |
} |
| 2529 |
|
| 2530 |
|
| 2531 |
for (uint32_t i = 0; i < p_parser->physical_pointer_count; i++) { |
| 2532 |
if (p_member_type->id == p_parser->physical_pointer_check[i]->id) { |
| 2533 |
found_recursion = true; |
| 2534 |
break; |
| 2535 |
} |
| 2536 |
} |
| 2537 |
if (!found_recursion) { |
| 2538 |
uint32_t struct_id = FindType(p_module, p_member_type->id)->struct_type_description->id; |
| 2539 |
p_parser->physical_pointer_structs[p_parser->physical_pointer_struct_count].struct_id = struct_id; |
| 2540 |
p_parser->physical_pointer_structs[p_parser->physical_pointer_struct_count].p_var = p_member_var; |
| 2541 |
p_parser->physical_pointer_struct_count++; |
| 2542 |
|
| 2543 |
p_parser->physical_pointer_check[p_parser->physical_pointer_count] = p_member_type; |
| 2544 |
p_parser->physical_pointer_count++; |
| 2545 |
if (p_parser->physical_pointer_count >= MAX_RECURSIVE_PHYSICAL_POINTER_CHECK) { |
| 2546 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_MAX_RECURSIVE_EXCEEDED; |
| 2547 |
} |
| 2548 |
} |
| 2549 |
|
| 2550 |
SpvReflectPrvNode* p_member_type_node = FindNode(p_parser, p_member_type->id); |
| 2551 |
if (IsNull(p_member_type_node)) { |
| 2552 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2553 |
} |
| 2554 |
|
| 2555 |
p_member_type = FindType(p_module, p_member_type_node->type_id); |
| 2556 |
if (IsNull(p_member_type)) { |
| 2557 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2558 |
} |
| 2559 |
} |
| 2560 |
bool is_struct = (p_member_type->type_flags & SPV_REFLECT_TYPE_FLAG_STRUCT) == SPV_REFLECT_TYPE_FLAG_STRUCT; |
| 2561 |
if (is_struct) { |
| 2562 |
if (!found_recursion) { |
| 2563 |
SpvReflectResult result = ParseDescriptorBlockVariable(p_parser, p_module, p_member_type, p_member_var); |
| 2564 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2565 |
return result; |
| 2566 |
} |
| 2567 |
} else { |
| 2568 |
|
| 2569 |
|
| 2570 |
for (uint32_t i = 0; i < p_parser->physical_pointer_struct_count; i++) { |
| 2571 |
if (p_parser->physical_pointer_structs[i].struct_id == p_member_type->id) { |
| 2572 |
p_member_var->members = p_parser->physical_pointer_structs[i].p_var->members; |
| 2573 |
p_member_var->member_count = p_parser->physical_pointer_structs[i].p_var->member_count; |
| 2574 |
|
| 2575 |
p_member_var->flags |= SPV_REFLECT_VARIABLE_FLAGS_PHYSICAL_POINTER_COPY; |
| 2576 |
} |
| 2577 |
} |
| 2578 |
} |
| 2579 |
} |
| 2580 |
|
| 2581 |
if (p_type_node->storage_class == SpvStorageClassPhysicalStorageBuffer && !p_type_node->member_names) { |
| 2582 |
|
| 2583 |
continue; |
| 2584 |
} |
| 2585 |
|
| 2586 |
p_member_var->name = p_type_node->member_names[member_index]; |
| 2587 |
p_member_var->offset = p_type_node->member_decorations[member_index].offset.value; |
| 2588 |
p_member_var->decoration_flags = ApplyDecorations(&p_type_node->member_decorations[member_index]); |
| 2589 |
p_member_var->flags |= SPV_REFLECT_VARIABLE_FLAGS_UNUSED; |
| 2590 |
if (!has_non_writable && (p_member_var->decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE)) { |
| 2591 |
has_non_writable = true; |
| 2592 |
} |
| 2593 |
ApplyNumericTraits(p_member_type, &p_member_var->numeric); |
| 2594 |
if (p_member_type->op == SpvOpTypeArray) { |
| 2595 |
ApplyArrayTraits(p_member_type, &p_member_var->array); |
| 2596 |
} |
| 2597 |
|
| 2598 |
p_member_var->word_offset.offset = p_type_node->member_decorations[member_index].offset.word_offset; |
| 2599 |
p_member_var->type_description = p_member_ptr_type ? p_member_ptr_type : p_member_type; |
| 2600 |
} |
| 2601 |
} |
| 2602 |
|
| 2603 |
p_var->name = p_type->type_name; |
| 2604 |
p_var->type_description = p_type; |
| 2605 |
if (has_non_writable) { |
| 2606 |
p_var->decoration_flags |= SPV_REFLECT_DECORATION_NON_WRITABLE; |
| 2607 |
} |
| 2608 |
|
| 2609 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2610 |
} |
| 2611 |
|
| 2612 |
static uint32_t GetPhysicalPointerStructSize(SpvReflectPrvParser* p_parser, uint32_t id) { |
| 2613 |
for (uint32_t i = 0; i < p_parser->physical_pointer_struct_count; i++) { |
| 2614 |
if (p_parser->physical_pointer_structs[i].struct_id == id) { |
| 2615 |
return p_parser->physical_pointer_structs[i].p_var->size; |
| 2616 |
} |
| 2617 |
} |
| 2618 |
return 0; |
| 2619 |
} |
| 2620 |
|
| 2621 |
static SpvReflectResult ParseDescriptorBlockVariableSizes(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module, |
| 2622 |
bool is_parent_root, bool is_parent_aos, bool is_parent_rta, |
| 2623 |
SpvReflectBlockVariable* p_var) { |
| 2624 |
if (p_var->member_count == 0) { |
| 2625 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2626 |
} |
| 2627 |
|
| 2628 |
bool is_parent_ref = p_var->type_description->op == SpvOpTypePointer; |
| 2629 |
|
| 2630 |
|
| 2631 |
for (uint32_t member_index = 0; member_index < p_var->member_count; ++member_index) { |
| 2632 |
SpvReflectBlockVariable* p_member_var = &p_var->members[member_index]; |
| 2633 |
if (is_parent_root) { |
| 2634 |
p_member_var->absolute_offset = p_member_var->offset; |
| 2635 |
} else { |
| 2636 |
p_member_var->absolute_offset = |
| 2637 |
is_parent_aos ? 0 : (is_parent_ref ? p_member_var->offset : p_member_var->offset + p_var->absolute_offset); |
| 2638 |
} |
| 2639 |
} |
| 2640 |
|
| 2641 |
|
| 2642 |
for (uint32_t member_index = 0; member_index < p_var->member_count; ++member_index) { |
| 2643 |
SpvReflectBlockVariable* p_member_var = &p_var->members[member_index]; |
| 2644 |
SpvReflectTypeDescription* p_member_type = p_member_var->type_description; |
| 2645 |
|
| 2646 |
if (!p_member_type) { |
| 2647 |
|
| 2648 |
continue; |
| 2649 |
} |
| 2650 |
switch (p_member_type->op) { |
| 2651 |
case SpvOpTypeBool: { |
| 2652 |
p_member_var->size = SPIRV_WORD_SIZE; |
| 2653 |
} break; |
| 2654 |
|
| 2655 |
case SpvOpTypeInt: |
| 2656 |
case SpvOpTypeFloat: { |
| 2657 |
p_member_var->size = p_member_type->traits.numeric.scalar.width / SPIRV_BYTE_WIDTH; |
| 2658 |
} break; |
| 2659 |
|
| 2660 |
case SpvOpTypeVector: { |
| 2661 |
uint32_t size = |
| 2662 |
p_member_type->traits.numeric.vector.component_count * (p_member_type->traits.numeric.scalar.width / SPIRV_BYTE_WIDTH); |
| 2663 |
p_member_var->size = size; |
| 2664 |
} break; |
| 2665 |
|
| 2666 |
case SpvOpTypeMatrix: { |
| 2667 |
if (p_member_var->decoration_flags & SPV_REFLECT_DECORATION_COLUMN_MAJOR) { |
| 2668 |
p_member_var->size = p_member_var->numeric.matrix.column_count * p_member_var->numeric.matrix.stride; |
| 2669 |
} else if (p_member_var->decoration_flags & SPV_REFLECT_DECORATION_ROW_MAJOR) { |
| 2670 |
p_member_var->size = p_member_var->numeric.matrix.row_count * p_member_var->numeric.matrix.stride; |
| 2671 |
} |
| 2672 |
} break; |
| 2673 |
|
| 2674 |
case SpvOpTypeArray: { |
| 2675 |
|
| 2676 |
bool is_struct = (p_member_type->type_flags & SPV_REFLECT_TYPE_FLAG_STRUCT) == SPV_REFLECT_TYPE_FLAG_STRUCT; |
| 2677 |
if (is_struct) { |
| 2678 |
if (p_member_var->flags & SPV_REFLECT_VARIABLE_FLAGS_PHYSICAL_POINTER_COPY) { |
| 2679 |
p_member_var->size = GetPhysicalPointerStructSize(p_parser, p_member_type->id); |
| 2680 |
} else { |
| 2681 |
SpvReflectResult result = |
| 2682 |
ParseDescriptorBlockVariableSizes(p_parser, p_module, false, true, is_parent_rta, p_member_var); |
| 2683 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2684 |
return result; |
| 2685 |
} |
| 2686 |
} |
| 2687 |
} |
| 2688 |
|
| 2689 |
uint32_t element_count = (p_member_var->array.dims_count > 0 ? 1 : 0); |
| 2690 |
for (uint32_t i = 0; i < p_member_var->array.dims_count; ++i) { |
| 2691 |
if (p_member_var->array.spec_constant_op_ids[i] != (uint32_t)INVALID_VALUE) { |
| 2692 |
|
| 2693 |
|
| 2694 |
element_count = 0; |
| 2695 |
} |
| 2696 |
element_count *= p_member_var->array.dims[i]; |
| 2697 |
} |
| 2698 |
p_member_var->size = element_count * p_member_var->array.stride; |
| 2699 |
} break; |
| 2700 |
|
| 2701 |
case SpvOpTypeRuntimeArray: { |
| 2702 |
bool is_struct = (p_member_type->type_flags & SPV_REFLECT_TYPE_FLAG_STRUCT) == SPV_REFLECT_TYPE_FLAG_STRUCT; |
| 2703 |
if (is_struct) { |
| 2704 |
SpvReflectResult result = ParseDescriptorBlockVariableSizes(p_parser, p_module, false, true, true, p_member_var); |
| 2705 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2706 |
return result; |
| 2707 |
} |
| 2708 |
} |
| 2709 |
} break; |
| 2710 |
|
| 2711 |
case SpvOpTypePointer: { |
| 2712 |
|
| 2713 |
SpvReflectPrvNode* p_member_type_node = FindNode(p_parser, p_member_type->id); |
| 2714 |
if (IsNull(p_member_type_node)) { |
| 2715 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2716 |
} |
| 2717 |
|
| 2718 |
p_member_type = FindType(p_module, p_member_type_node->type_id); |
| 2719 |
if (IsNull(p_member_type)) { |
| 2720 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2721 |
} |
| 2722 |
assert(p_member_type->op == SpvOpTypeStruct); |
| 2723 |
FALLTHROUGH; |
| 2724 |
} |
| 2725 |
|
| 2726 |
case SpvOpTypeStruct: { |
| 2727 |
if (p_member_var->flags & SPV_REFLECT_VARIABLE_FLAGS_PHYSICAL_POINTER_COPY) { |
| 2728 |
p_member_var->size = GetPhysicalPointerStructSize(p_parser, p_member_type->id); |
| 2729 |
} else { |
| 2730 |
SpvReflectResult result = |
| 2731 |
ParseDescriptorBlockVariableSizes(p_parser, p_module, false, is_parent_aos, is_parent_rta, p_member_var); |
| 2732 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2733 |
return result; |
| 2734 |
} |
| 2735 |
} |
| 2736 |
} break; |
| 2737 |
|
| 2738 |
default: |
| 2739 |
break; |
| 2740 |
} |
| 2741 |
} |
| 2742 |
|
| 2743 |
|
| 2744 |
|
| 2745 |
|
| 2746 |
|
| 2747 |
SpvReflectBlockVariable** pp_member_offset_order = |
| 2748 |
(SpvReflectBlockVariable**)calloc(p_var->member_count, sizeof(SpvReflectBlockVariable*)); |
| 2749 |
if (IsNull(pp_member_offset_order)) { |
| 2750 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 2751 |
} |
| 2752 |
|
| 2753 |
uint32_t bottom_bound = 0; |
| 2754 |
for (uint32_t i = 0; i < p_var->member_count; ++i) { |
| 2755 |
uint32_t lowest_offset = UINT32_MAX; |
| 2756 |
uint32_t member_index = 0; |
| 2757 |
for (uint32_t j = 0; j < p_var->member_count; ++j) { |
| 2758 |
const uint32_t offset = p_var->members[j].offset; |
| 2759 |
if (offset < lowest_offset && offset >= bottom_bound) { |
| 2760 |
member_index = j; |
| 2761 |
lowest_offset = offset; |
| 2762 |
} |
| 2763 |
} |
| 2764 |
pp_member_offset_order[i] = &p_var->members[member_index]; |
| 2765 |
bottom_bound = lowest_offset + 1; |
| 2766 |
} |
| 2767 |
|
| 2768 |
|
| 2769 |
for (uint32_t i = 0; i < (p_var->member_count - 1); ++i) { |
| 2770 |
SpvReflectBlockVariable* p_member_var = pp_member_offset_order[i]; |
| 2771 |
SpvReflectBlockVariable* p_next_member_var = pp_member_offset_order[i + 1]; |
| 2772 |
p_member_var->padded_size = p_next_member_var->offset - p_member_var->offset; |
| 2773 |
if (p_member_var->size > p_member_var->padded_size) { |
| 2774 |
p_member_var->size = p_member_var->padded_size; |
| 2775 |
} |
| 2776 |
if (is_parent_rta) { |
| 2777 |
p_member_var->padded_size = p_member_var->size; |
| 2778 |
} |
| 2779 |
} |
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
SpvReflectBlockVariable* p_last_member_var = pp_member_offset_order[p_var->member_count - 1]; |
| 2785 |
p_last_member_var->padded_size = |
| 2786 |
RoundUp(p_last_member_var->offset + p_last_member_var->size, SPIRV_DATA_ALIGNMENT) - p_last_member_var->offset; |
| 2787 |
if (p_last_member_var->size > p_last_member_var->padded_size) { |
| 2788 |
p_last_member_var->size = p_last_member_var->padded_size; |
| 2789 |
} |
| 2790 |
if (is_parent_rta) { |
| 2791 |
p_last_member_var->padded_size = p_last_member_var->size; |
| 2792 |
} |
| 2793 |
|
| 2794 |
SafeFree(pp_member_offset_order); |
| 2795 |
|
| 2796 |
|
| 2797 |
if (is_parent_ref) { |
| 2798 |
p_var->size = p_var->padded_size = 8; |
| 2799 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2800 |
} |
| 2801 |
|
| 2802 |
|
| 2803 |
p_var->size = p_last_member_var->offset + p_last_member_var->padded_size; |
| 2804 |
p_var->padded_size = p_var->size; |
| 2805 |
|
| 2806 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2807 |
} |
| 2808 |
|
| 2809 |
static void MarkSelfAndAllMemberVarsAsUsed(SpvReflectBlockVariable* p_var) { |
| 2810 |
|
| 2811 |
p_var->flags &= ~SPV_REFLECT_VARIABLE_FLAGS_UNUSED; |
| 2812 |
|
| 2813 |
SpvOp op_type = p_var->type_description->op; |
| 2814 |
switch (op_type) { |
| 2815 |
default: |
| 2816 |
break; |
| 2817 |
|
| 2818 |
case SpvOpTypeArray: { |
| 2819 |
} break; |
| 2820 |
|
| 2821 |
case SpvOpTypeStruct: { |
| 2822 |
for (uint32_t i = 0; i < p_var->member_count; ++i) { |
| 2823 |
SpvReflectBlockVariable* p_member_var = &p_var->members[i]; |
| 2824 |
MarkSelfAndAllMemberVarsAsUsed(p_member_var); |
| 2825 |
} |
| 2826 |
} break; |
| 2827 |
} |
| 2828 |
} |
| 2829 |
|
| 2830 |
static SpvReflectResult ParseDescriptorBlockVariableUsage(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module, |
| 2831 |
SpvReflectPrvAccessChain* p_access_chain, uint32_t index_index, |
| 2832 |
SpvOp override_op_type, SpvReflectBlockVariable* p_var) { |
| 2833 |
|
| 2834 |
p_var->flags &= ~SPV_REFLECT_VARIABLE_FLAGS_UNUSED; |
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
SpvReflectTypeDescription* p_type = p_var->type_description; |
| 2839 |
SpvOp op_type = p_type->op; |
| 2840 |
if (override_op_type != (SpvOp)INVALID_VALUE) { |
| 2841 |
op_type = override_op_type; |
| 2842 |
} |
| 2843 |
|
| 2844 |
switch (op_type) { |
| 2845 |
default: |
| 2846 |
break; |
| 2847 |
|
| 2848 |
case SpvOpTypeArray: { |
| 2849 |
|
| 2850 |
while ((p_type->op == SpvOpTypeArray) && (index_index < p_access_chain->index_count)) { |
| 2851 |
|
| 2852 |
SpvReflectPrvNode* p_node = FindNode(p_parser, p_type->id); |
| 2853 |
if (p_node == NULL) { |
| 2854 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2855 |
} |
| 2856 |
uint32_t element_type_id = p_node->array_traits.element_type_id; |
| 2857 |
|
| 2858 |
p_type = FindType(p_module, element_type_id); |
| 2859 |
if (p_type == NULL) { |
| 2860 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2861 |
} |
| 2862 |
|
| 2863 |
index_index += 1; |
| 2864 |
} |
| 2865 |
|
| 2866 |
|
| 2867 |
|
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
if (index_index < p_access_chain->index_count) { |
| 2874 |
|
| 2875 |
SpvReflectResult result = |
| 2876 |
ParseDescriptorBlockVariableUsage(p_parser, p_module, p_access_chain, index_index, p_type->op, p_var); |
| 2877 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2878 |
return result; |
| 2879 |
} |
| 2880 |
} else { |
| 2881 |
|
| 2882 |
MarkSelfAndAllMemberVarsAsUsed(p_var); |
| 2883 |
} |
| 2884 |
} break; |
| 2885 |
|
| 2886 |
case SpvOpTypePointer: { |
| 2887 |
|
| 2888 |
SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 2889 |
if (IsNull(p_type_node)) { |
| 2890 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2891 |
} |
| 2892 |
|
| 2893 |
p_type = FindType(p_module, p_type_node->type_id); |
| 2894 |
if (IsNull(p_type)) { |
| 2895 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 2896 |
} |
| 2897 |
if (p_type->op != SpvOpTypeStruct) { |
| 2898 |
break; |
| 2899 |
} |
| 2900 |
FALLTHROUGH; |
| 2901 |
} |
| 2902 |
|
| 2903 |
case SpvOpTypeStruct: { |
| 2904 |
assert(p_var->member_count > 0); |
| 2905 |
if (p_var->member_count == 0) { |
| 2906 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_BLOCK_DATA; |
| 2907 |
} |
| 2908 |
|
| 2909 |
|
| 2910 |
if (p_access_chain->index_count == 0) { |
| 2911 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2912 |
} |
| 2913 |
|
| 2914 |
|
| 2915 |
uint32_t index = p_access_chain->indexes[index_index]; |
| 2916 |
if (index >= p_var->member_count) { |
| 2917 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_BLOCK_MEMBER_REFERENCE; |
| 2918 |
} |
| 2919 |
SpvReflectBlockVariable* p_member_var = &p_var->members[index]; |
| 2920 |
|
| 2921 |
bool is_pointer_to_pointer = IsPointerToPointer(p_parser, p_access_chain->result_type_id); |
| 2922 |
if (is_pointer_to_pointer) { |
| 2923 |
|
| 2924 |
p_access_chain->block_var = p_member_var; |
| 2925 |
} |
| 2926 |
|
| 2927 |
|
| 2928 |
index_index += 1; |
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
|
| 2933 |
|
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
if (index_index < p_access_chain->index_count) { |
| 2938 |
SpvReflectResult result = |
| 2939 |
ParseDescriptorBlockVariableUsage(p_parser, p_module, p_access_chain, index_index, (SpvOp)INVALID_VALUE, p_member_var); |
| 2940 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2941 |
return result; |
| 2942 |
} |
| 2943 |
} else if (!is_pointer_to_pointer) { |
| 2944 |
|
| 2945 |
MarkSelfAndAllMemberVarsAsUsed(p_member_var); |
| 2946 |
} |
| 2947 |
} break; |
| 2948 |
} |
| 2949 |
|
| 2950 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2951 |
} |
| 2952 |
|
| 2953 |
static SpvReflectResult ParseDescriptorBlocks(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 2954 |
if (p_module->descriptor_binding_count == 0) { |
| 2955 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 2956 |
} |
| 2957 |
|
| 2958 |
p_parser->physical_pointer_struct_count = 0; |
| 2959 |
|
| 2960 |
for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) { |
| 2961 |
SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]); |
| 2962 |
SpvReflectTypeDescription* p_type = p_descriptor->type_description; |
| 2963 |
if ((p_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER) && |
| 2964 |
(p_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER)) { |
| 2965 |
continue; |
| 2966 |
} |
| 2967 |
|
| 2968 |
|
| 2969 |
p_descriptor->block.flags |= SPV_REFLECT_VARIABLE_FLAGS_UNUSED; |
| 2970 |
p_parser->physical_pointer_count = 0; |
| 2971 |
|
| 2972 |
SpvReflectResult result = ParseDescriptorBlockVariable(p_parser, p_module, p_type, &p_descriptor->block); |
| 2973 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2974 |
return result; |
| 2975 |
} |
| 2976 |
|
| 2977 |
for (uint32_t access_chain_index = 0; access_chain_index < p_parser->access_chain_count; ++access_chain_index) { |
| 2978 |
SpvReflectPrvAccessChain* p_access_chain = &(p_parser->access_chains[access_chain_index]); |
| 2979 |
|
| 2980 |
if (p_descriptor->spirv_id != p_access_chain->base_id) { |
| 2981 |
continue; |
| 2982 |
} |
| 2983 |
result = ParseDescriptorBlockVariableUsage(p_parser, p_module, p_access_chain, 0, (SpvOp)INVALID_VALUE, &p_descriptor->block); |
| 2984 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2985 |
return result; |
| 2986 |
} |
| 2987 |
} |
| 2988 |
|
| 2989 |
p_descriptor->block.name = p_descriptor->name; |
| 2990 |
|
| 2991 |
bool is_parent_rta = (p_descriptor->descriptor_type == SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 2992 |
result = ParseDescriptorBlockVariableSizes(p_parser, p_module, true, false, is_parent_rta, &p_descriptor->block); |
| 2993 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 2994 |
return result; |
| 2995 |
} |
| 2996 |
|
| 2997 |
if (is_parent_rta) { |
| 2998 |
p_descriptor->block.size = 0; |
| 2999 |
p_descriptor->block.padded_size = 0; |
| 3000 |
} |
| 3001 |
} |
| 3002 |
|
| 3003 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3004 |
} |
| 3005 |
|
| 3006 |
static SpvReflectResult ParseFormat(const SpvReflectTypeDescription* p_type, SpvReflectFormat* p_format) { |
| 3007 |
SpvReflectResult result = SPV_REFLECT_RESULT_ERROR_INTERNAL_ERROR; |
| 3008 |
bool signedness = (p_type->traits.numeric.scalar.signedness != 0); |
| 3009 |
uint32_t bit_width = p_type->traits.numeric.scalar.width; |
| 3010 |
if (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_VECTOR) { |
| 3011 |
uint32_t component_count = p_type->traits.numeric.vector.component_count; |
| 3012 |
if (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_FLOAT) { |
| 3013 |
switch (bit_width) { |
| 3014 |
case 16: { |
| 3015 |
switch (component_count) { |
| 3016 |
case 2: |
| 3017 |
*p_format = SPV_REFLECT_FORMAT_R16G16_SFLOAT; |
| 3018 |
break; |
| 3019 |
case 3: |
| 3020 |
*p_format = SPV_REFLECT_FORMAT_R16G16B16_SFLOAT; |
| 3021 |
break; |
| 3022 |
case 4: |
| 3023 |
*p_format = SPV_REFLECT_FORMAT_R16G16B16A16_SFLOAT; |
| 3024 |
break; |
| 3025 |
} |
| 3026 |
} break; |
| 3027 |
|
| 3028 |
case 32: { |
| 3029 |
switch (component_count) { |
| 3030 |
case 2: |
| 3031 |
*p_format = SPV_REFLECT_FORMAT_R32G32_SFLOAT; |
| 3032 |
break; |
| 3033 |
case 3: |
| 3034 |
*p_format = SPV_REFLECT_FORMAT_R32G32B32_SFLOAT; |
| 3035 |
break; |
| 3036 |
case 4: |
| 3037 |
*p_format = SPV_REFLECT_FORMAT_R32G32B32A32_SFLOAT; |
| 3038 |
break; |
| 3039 |
} |
| 3040 |
} break; |
| 3041 |
|
| 3042 |
case 64: { |
| 3043 |
switch (component_count) { |
| 3044 |
case 2: |
| 3045 |
*p_format = SPV_REFLECT_FORMAT_R64G64_SFLOAT; |
| 3046 |
break; |
| 3047 |
case 3: |
| 3048 |
*p_format = SPV_REFLECT_FORMAT_R64G64B64_SFLOAT; |
| 3049 |
break; |
| 3050 |
case 4: |
| 3051 |
*p_format = SPV_REFLECT_FORMAT_R64G64B64A64_SFLOAT; |
| 3052 |
break; |
| 3053 |
} |
| 3054 |
} |
| 3055 |
} |
| 3056 |
result = SPV_REFLECT_RESULT_SUCCESS; |
| 3057 |
} else if (p_type->type_flags & (SPV_REFLECT_TYPE_FLAG_INT | SPV_REFLECT_TYPE_FLAG_BOOL)) { |
| 3058 |
switch (bit_width) { |
| 3059 |
case 16: { |
| 3060 |
switch (component_count) { |
| 3061 |
case 2: |
| 3062 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R16G16_SINT : SPV_REFLECT_FORMAT_R16G16_UINT; |
| 3063 |
break; |
| 3064 |
case 3: |
| 3065 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R16G16B16_SINT : SPV_REFLECT_FORMAT_R16G16B16_UINT; |
| 3066 |
break; |
| 3067 |
case 4: |
| 3068 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R16G16B16A16_SINT : SPV_REFLECT_FORMAT_R16G16B16A16_UINT; |
| 3069 |
break; |
| 3070 |
} |
| 3071 |
} break; |
| 3072 |
|
| 3073 |
case 32: { |
| 3074 |
switch (component_count) { |
| 3075 |
case 2: |
| 3076 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R32G32_SINT : SPV_REFLECT_FORMAT_R32G32_UINT; |
| 3077 |
break; |
| 3078 |
case 3: |
| 3079 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R32G32B32_SINT : SPV_REFLECT_FORMAT_R32G32B32_UINT; |
| 3080 |
break; |
| 3081 |
case 4: |
| 3082 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R32G32B32A32_SINT : SPV_REFLECT_FORMAT_R32G32B32A32_UINT; |
| 3083 |
break; |
| 3084 |
} |
| 3085 |
} break; |
| 3086 |
|
| 3087 |
case 64: { |
| 3088 |
switch (component_count) { |
| 3089 |
case 2: |
| 3090 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R64G64_SINT : SPV_REFLECT_FORMAT_R64G64_UINT; |
| 3091 |
break; |
| 3092 |
case 3: |
| 3093 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R64G64B64_SINT : SPV_REFLECT_FORMAT_R64G64B64_UINT; |
| 3094 |
break; |
| 3095 |
case 4: |
| 3096 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R64G64B64A64_SINT : SPV_REFLECT_FORMAT_R64G64B64A64_UINT; |
| 3097 |
break; |
| 3098 |
} |
| 3099 |
} |
| 3100 |
} |
| 3101 |
result = SPV_REFLECT_RESULT_SUCCESS; |
| 3102 |
} |
| 3103 |
} else if (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_FLOAT) { |
| 3104 |
switch (bit_width) { |
| 3105 |
case 16: |
| 3106 |
*p_format = SPV_REFLECT_FORMAT_R16_SFLOAT; |
| 3107 |
break; |
| 3108 |
case 32: |
| 3109 |
*p_format = SPV_REFLECT_FORMAT_R32_SFLOAT; |
| 3110 |
break; |
| 3111 |
case 64: |
| 3112 |
*p_format = SPV_REFLECT_FORMAT_R64_SFLOAT; |
| 3113 |
break; |
| 3114 |
} |
| 3115 |
result = SPV_REFLECT_RESULT_SUCCESS; |
| 3116 |
} else if (p_type->type_flags & (SPV_REFLECT_TYPE_FLAG_INT | SPV_REFLECT_TYPE_FLAG_BOOL)) { |
| 3117 |
switch (bit_width) { |
| 3118 |
case 16: |
| 3119 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R16_SINT : SPV_REFLECT_FORMAT_R16_UINT; |
| 3120 |
break; |
| 3121 |
break; |
| 3122 |
case 32: |
| 3123 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R32_SINT : SPV_REFLECT_FORMAT_R32_UINT; |
| 3124 |
break; |
| 3125 |
break; |
| 3126 |
case 64: |
| 3127 |
*p_format = signedness ? SPV_REFLECT_FORMAT_R64_SINT : SPV_REFLECT_FORMAT_R64_UINT; |
| 3128 |
break; |
| 3129 |
} |
| 3130 |
result = SPV_REFLECT_RESULT_SUCCESS; |
| 3131 |
} else if (p_type->type_flags & SPV_REFLECT_TYPE_FLAG_STRUCT) { |
| 3132 |
*p_format = SPV_REFLECT_FORMAT_UNDEFINED; |
| 3133 |
result = SPV_REFLECT_RESULT_SUCCESS; |
| 3134 |
} |
| 3135 |
return result; |
| 3136 |
} |
| 3137 |
|
| 3138 |
static SpvReflectResult ParseInterfaceVariable(SpvReflectPrvParser* p_parser, |
| 3139 |
const SpvReflectPrvDecorations* p_var_node_decorations, |
| 3140 |
const SpvReflectPrvDecorations* p_type_node_decorations, |
| 3141 |
SpvReflectShaderModule* p_module, SpvReflectTypeDescription* p_type, |
| 3142 |
SpvReflectInterfaceVariable* p_var, bool* p_has_built_in) { |
| 3143 |
SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 3144 |
if (IsNull(p_type_node)) { |
| 3145 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3146 |
} |
| 3147 |
|
| 3148 |
if (p_type->member_count > 0) { |
| 3149 |
p_var->member_count = p_type->member_count; |
| 3150 |
p_var->members = (SpvReflectInterfaceVariable*)calloc(p_var->member_count, sizeof(*p_var->members)); |
| 3151 |
if (IsNull(p_var->members)) { |
| 3152 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3153 |
} |
| 3154 |
|
| 3155 |
for (uint32_t member_index = 0; member_index < p_type_node->member_count; ++member_index) { |
| 3156 |
SpvReflectPrvDecorations* p_member_decorations = &p_type_node->member_decorations[member_index]; |
| 3157 |
SpvReflectTypeDescription* p_member_type = &p_type->members[member_index]; |
| 3158 |
SpvReflectInterfaceVariable* p_member_var = &p_var->members[member_index]; |
| 3159 |
|
| 3160 |
|
| 3161 |
p_member_var->storage_class = p_var->storage_class; |
| 3162 |
|
| 3163 |
SpvReflectResult result = |
| 3164 |
ParseInterfaceVariable(p_parser, NULL, p_member_decorations, p_module, p_member_type, p_member_var, p_has_built_in); |
| 3165 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3166 |
SPV_REFLECT_ASSERT(false); |
| 3167 |
return result; |
| 3168 |
} |
| 3169 |
} |
| 3170 |
} |
| 3171 |
|
| 3172 |
p_var->name = p_type_node->name; |
| 3173 |
p_var->decoration_flags = ApplyDecorations(p_type_node_decorations); |
| 3174 |
if (p_var_node_decorations != NULL) { |
| 3175 |
p_var->decoration_flags |= ApplyDecorations(p_var_node_decorations); |
| 3176 |
} else { |
| 3177 |
|
| 3178 |
p_var->location = p_type_node_decorations->location.value; |
| 3179 |
p_var->component = p_type_node_decorations->component.value; |
| 3180 |
} |
| 3181 |
|
| 3182 |
p_var->built_in = p_type_node_decorations->built_in; |
| 3183 |
ApplyNumericTraits(p_type, &p_var->numeric); |
| 3184 |
if (p_type->op == SpvOpTypeArray) { |
| 3185 |
ApplyArrayTraits(p_type, &p_var->array); |
| 3186 |
} |
| 3187 |
|
| 3188 |
p_var->type_description = p_type; |
| 3189 |
|
| 3190 |
*p_has_built_in |= p_type_node_decorations->is_built_in; |
| 3191 |
|
| 3192 |
|
| 3193 |
if ((p_var->storage_class == SpvStorageClassInput) || (p_var->storage_class == SpvStorageClassOutput)) { |
| 3194 |
SpvReflectResult result = ParseFormat(p_var->type_description, &p_var->format); |
| 3195 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3196 |
SPV_REFLECT_ASSERT(false); |
| 3197 |
return result; |
| 3198 |
} |
| 3199 |
} |
| 3200 |
|
| 3201 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3202 |
} |
| 3203 |
|
| 3204 |
static SpvReflectResult ParseInterfaceVariables(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module, |
| 3205 |
SpvReflectEntryPoint* p_entry, uint32_t interface_variable_count, |
| 3206 |
uint32_t* p_interface_variable_ids) { |
| 3207 |
if (interface_variable_count == 0) { |
| 3208 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3209 |
} |
| 3210 |
|
| 3211 |
p_entry->interface_variable_count = interface_variable_count; |
| 3212 |
p_entry->input_variable_count = 0; |
| 3213 |
p_entry->output_variable_count = 0; |
| 3214 |
for (size_t i = 0; i < interface_variable_count; ++i) { |
| 3215 |
uint32_t var_result_id = *(p_interface_variable_ids + i); |
| 3216 |
SpvReflectPrvNode* p_node = FindNode(p_parser, var_result_id); |
| 3217 |
if (IsNull(p_node)) { |
| 3218 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3219 |
} |
| 3220 |
|
| 3221 |
if (p_node->storage_class == SpvStorageClassInput) { |
| 3222 |
p_entry->input_variable_count += 1; |
| 3223 |
} else if (p_node->storage_class == SpvStorageClassOutput) { |
| 3224 |
p_entry->output_variable_count += 1; |
| 3225 |
} |
| 3226 |
} |
| 3227 |
|
| 3228 |
if (p_entry->input_variable_count > 0) { |
| 3229 |
p_entry->input_variables = |
| 3230 |
(SpvReflectInterfaceVariable**)calloc(p_entry->input_variable_count, sizeof(*(p_entry->input_variables))); |
| 3231 |
if (IsNull(p_entry->input_variables)) { |
| 3232 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3233 |
} |
| 3234 |
} |
| 3235 |
|
| 3236 |
if (p_entry->output_variable_count > 0) { |
| 3237 |
p_entry->output_variables = |
| 3238 |
(SpvReflectInterfaceVariable**)calloc(p_entry->output_variable_count, sizeof(*(p_entry->output_variables))); |
| 3239 |
if (IsNull(p_entry->output_variables)) { |
| 3240 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3241 |
} |
| 3242 |
} |
| 3243 |
|
| 3244 |
if (p_entry->interface_variable_count > 0) { |
| 3245 |
p_entry->interface_variables = |
| 3246 |
(SpvReflectInterfaceVariable*)calloc(p_entry->interface_variable_count, sizeof(*(p_entry->interface_variables))); |
| 3247 |
if (IsNull(p_entry->interface_variables)) { |
| 3248 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3249 |
} |
| 3250 |
} |
| 3251 |
|
| 3252 |
size_t input_index = 0; |
| 3253 |
size_t output_index = 0; |
| 3254 |
for (size_t i = 0; i < interface_variable_count; ++i) { |
| 3255 |
uint32_t var_result_id = *(p_interface_variable_ids + i); |
| 3256 |
SpvReflectPrvNode* p_node = FindNode(p_parser, var_result_id); |
| 3257 |
if (IsNull(p_node)) { |
| 3258 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3259 |
} |
| 3260 |
|
| 3261 |
SpvReflectTypeDescription* p_type = FindType(p_module, p_node->type_id); |
| 3262 |
if (IsNull(p_node) || IsNull(p_type)) { |
| 3263 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3264 |
} |
| 3265 |
|
| 3266 |
if (p_type->op == SpvOpTypePointer) { |
| 3267 |
|
| 3268 |
SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 3269 |
if (IsNull(p_type_node)) { |
| 3270 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3271 |
} |
| 3272 |
|
| 3273 |
p_type = FindType(p_module, p_type_node->type_id); |
| 3274 |
if (IsNull(p_type)) { |
| 3275 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3276 |
} |
| 3277 |
} |
| 3278 |
|
| 3279 |
SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 3280 |
if (IsNull(p_type_node)) { |
| 3281 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3282 |
} |
| 3283 |
|
| 3284 |
SpvReflectInterfaceVariable* p_var = &(p_entry->interface_variables[i]); |
| 3285 |
p_var->storage_class = p_node->storage_class; |
| 3286 |
|
| 3287 |
bool has_built_in = p_node->decorations.is_built_in; |
| 3288 |
SpvReflectResult result = |
| 3289 |
ParseInterfaceVariable(p_parser, &p_node->decorations, &p_type_node->decorations, p_module, p_type, p_var, &has_built_in); |
| 3290 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3291 |
SPV_REFLECT_ASSERT(false); |
| 3292 |
return result; |
| 3293 |
} |
| 3294 |
|
| 3295 |
|
| 3296 |
if (p_var->storage_class == SpvStorageClassInput) { |
| 3297 |
p_entry->input_variables[input_index] = p_var; |
| 3298 |
++input_index; |
| 3299 |
} else if (p_node->storage_class == SpvStorageClassOutput) { |
| 3300 |
p_entry->output_variables[output_index] = p_var; |
| 3301 |
++output_index; |
| 3302 |
} |
| 3303 |
|
| 3304 |
|
| 3305 |
p_var->spirv_id = p_node->result_id; |
| 3306 |
|
| 3307 |
p_var->name = p_node->name; |
| 3308 |
|
| 3309 |
p_var->semantic = p_node->decorations.semantic.value; |
| 3310 |
|
| 3311 |
|
| 3312 |
if (has_built_in) { |
| 3313 |
p_var->decoration_flags |= SPV_REFLECT_DECORATION_BUILT_IN; |
| 3314 |
} |
| 3315 |
|
| 3316 |
|
| 3317 |
p_var->location = p_node->decorations.location.value; |
| 3318 |
p_var->component = p_node->decorations.component.value; |
| 3319 |
p_var->word_offset.location = p_node->decorations.location.word_offset; |
| 3320 |
|
| 3321 |
|
| 3322 |
if (p_node->decorations.is_built_in) { |
| 3323 |
p_var->built_in = p_node->decorations.built_in; |
| 3324 |
} |
| 3325 |
} |
| 3326 |
|
| 3327 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3328 |
} |
| 3329 |
|
| 3330 |
static SpvReflectResult EnumerateAllPushConstants(SpvReflectShaderModule* p_module, size_t* p_push_constant_count, |
| 3331 |
uint32_t** p_push_constants) { |
| 3332 |
*p_push_constant_count = p_module->push_constant_block_count; |
| 3333 |
if (*p_push_constant_count == 0) { |
| 3334 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3335 |
} |
| 3336 |
*p_push_constants = (uint32_t*)calloc(*p_push_constant_count, sizeof(**p_push_constants)); |
| 3337 |
|
| 3338 |
if (IsNull(*p_push_constants)) { |
| 3339 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3340 |
} |
| 3341 |
|
| 3342 |
for (size_t i = 0; i < *p_push_constant_count; ++i) { |
| 3343 |
(*p_push_constants)[i] = p_module->push_constant_blocks[i].spirv_id; |
| 3344 |
} |
| 3345 |
qsort(*p_push_constants, *p_push_constant_count, sizeof(**p_push_constants), SortCompareUint32); |
| 3346 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3347 |
} |
| 3348 |
|
| 3349 |
static SpvReflectResult TraverseCallGraph(SpvReflectPrvParser* p_parser, SpvReflectPrvFunction* p_func, size_t* p_func_count, |
| 3350 |
uint32_t* p_func_ids, uint32_t depth) { |
| 3351 |
if (depth > p_parser->function_count) { |
| 3352 |
|
| 3353 |
|
| 3354 |
|
| 3355 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_RECURSION; |
| 3356 |
} |
| 3357 |
if (IsNotNull(p_func_ids)) { |
| 3358 |
p_func_ids[(*p_func_count)++] = p_func->id; |
| 3359 |
} else { |
| 3360 |
++*p_func_count; |
| 3361 |
} |
| 3362 |
for (size_t i = 0; i < p_func->callee_count; ++i) { |
| 3363 |
SpvReflectResult result = TraverseCallGraph(p_parser, p_func->callee_ptrs[i], p_func_count, p_func_ids, depth + 1); |
| 3364 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3365 |
return result; |
| 3366 |
} |
| 3367 |
} |
| 3368 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3369 |
} |
| 3370 |
|
| 3371 |
static uint32_t GetUint32Constant(SpvReflectPrvParser* p_parser, uint32_t id) { |
| 3372 |
uint32_t result = (uint32_t)INVALID_VALUE; |
| 3373 |
SpvReflectPrvNode* p_node = FindNode(p_parser, id); |
| 3374 |
if (p_node && p_node->op == SpvOpConstant) { |
| 3375 |
UNCHECKED_READU32(p_parser, p_node->word_offset + 3, result); |
| 3376 |
} |
| 3377 |
return result; |
| 3378 |
} |
| 3379 |
|
| 3380 |
static bool HasByteAddressBufferOffset(SpvReflectPrvNode* p_node, SpvReflectDescriptorBinding* p_binding) { |
| 3381 |
return IsNotNull(p_node) && IsNotNull(p_binding) && p_node->op == SpvOpAccessChain && p_node->word_count == 6 && |
| 3382 |
(p_binding->user_type == SPV_REFLECT_USER_TYPE_BYTE_ADDRESS_BUFFER || |
| 3383 |
p_binding->user_type == SPV_REFLECT_USER_TYPE_RW_BYTE_ADDRESS_BUFFER); |
| 3384 |
} |
| 3385 |
|
| 3386 |
static SpvReflectResult ParseByteAddressBuffer(SpvReflectPrvParser* p_parser, SpvReflectPrvNode* p_node, |
| 3387 |
SpvReflectDescriptorBinding* p_binding) { |
| 3388 |
const SpvReflectResult not_found = SPV_REFLECT_RESULT_SUCCESS; |
| 3389 |
if (!HasByteAddressBufferOffset(p_node, p_binding)) { |
| 3390 |
return not_found; |
| 3391 |
} |
| 3392 |
|
| 3393 |
uint32_t offset = 0; |
| 3394 |
|
| 3395 |
uint32_t base_id = 0; |
| 3396 |
|
| 3397 |
UNCHECKED_READU32(p_parser, p_node->word_offset + 4, base_id); |
| 3398 |
if (GetUint32Constant(p_parser, base_id) != 0) { |
| 3399 |
return not_found; |
| 3400 |
} |
| 3401 |
UNCHECKED_READU32(p_parser, p_node->word_offset + 5, base_id); |
| 3402 |
SpvReflectPrvNode* p_next_node = FindNode(p_parser, base_id); |
| 3403 |
if (IsNull(p_next_node)) { |
| 3404 |
return not_found; |
| 3405 |
} else if (p_next_node->op == SpvOpConstant) { |
| 3406 |
|
| 3407 |
offset = GetUint32Constant(p_parser, base_id); |
| 3408 |
p_binding->byte_address_buffer_offsets[p_binding->byte_address_buffer_offset_count] = offset; |
| 3409 |
p_binding->byte_address_buffer_offset_count++; |
| 3410 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3411 |
} |
| 3412 |
|
| 3413 |
|
| 3414 |
SpvReflectPrvNode* arithmetic_node_stack[8]; |
| 3415 |
uint32_t arithmetic_count = 0; |
| 3416 |
|
| 3417 |
while (IsNotNull(p_next_node)) { |
| 3418 |
if (p_next_node->op == SpvOpLoad || p_next_node->op == SpvOpBitcast || p_next_node->op == SpvOpConstant) { |
| 3419 |
break; |
| 3420 |
} |
| 3421 |
arithmetic_node_stack[arithmetic_count++] = p_next_node; |
| 3422 |
if (arithmetic_count >= 8) { |
| 3423 |
return not_found; |
| 3424 |
} |
| 3425 |
|
| 3426 |
UNCHECKED_READU32(p_parser, p_next_node->word_offset + 3, base_id); |
| 3427 |
p_next_node = FindNode(p_parser, base_id); |
| 3428 |
} |
| 3429 |
|
| 3430 |
const uint32_t count = arithmetic_count; |
| 3431 |
for (uint32_t i = 0; i < count; i++) { |
| 3432 |
p_next_node = arithmetic_node_stack[--arithmetic_count]; |
| 3433 |
|
| 3434 |
UNCHECKED_READU32(p_parser, p_next_node->word_offset + 4, base_id); |
| 3435 |
uint32_t value = GetUint32Constant(p_parser, base_id); |
| 3436 |
if (value == INVALID_VALUE) { |
| 3437 |
return not_found; |
| 3438 |
} |
| 3439 |
|
| 3440 |
switch (p_next_node->op) { |
| 3441 |
case SpvOpShiftRightLogical: |
| 3442 |
offset >>= value; |
| 3443 |
break; |
| 3444 |
case SpvOpIAdd: |
| 3445 |
offset += value; |
| 3446 |
break; |
| 3447 |
case SpvOpISub: |
| 3448 |
offset -= value; |
| 3449 |
break; |
| 3450 |
case SpvOpIMul: |
| 3451 |
offset *= value; |
| 3452 |
break; |
| 3453 |
case SpvOpUDiv: |
| 3454 |
offset /= value; |
| 3455 |
break; |
| 3456 |
case SpvOpSDiv: |
| 3457 |
|
| 3458 |
assert((int32_t)value > 0); |
| 3459 |
offset /= value; |
| 3460 |
break; |
| 3461 |
default: |
| 3462 |
return not_found; |
| 3463 |
} |
| 3464 |
} |
| 3465 |
|
| 3466 |
p_binding->byte_address_buffer_offsets[p_binding->byte_address_buffer_offset_count] = offset; |
| 3467 |
p_binding->byte_address_buffer_offset_count++; |
| 3468 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3469 |
} |
| 3470 |
|
| 3471 |
static SpvReflectResult ParseStaticallyUsedResources(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module, |
| 3472 |
SpvReflectEntryPoint* p_entry, size_t uniform_count, uint32_t* uniforms, |
| 3473 |
size_t push_constant_count, uint32_t* push_constants) { |
| 3474 |
|
| 3475 |
SpvReflectPrvFunction* p_func = NULL; |
| 3476 |
for (size_t i = 0; i < p_parser->function_count; ++i) { |
| 3477 |
if (p_parser->functions[i].id == p_entry->id) { |
| 3478 |
p_func = &(p_parser->functions[i]); |
| 3479 |
break; |
| 3480 |
} |
| 3481 |
} |
| 3482 |
if (p_func == NULL) { |
| 3483 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3484 |
} |
| 3485 |
|
| 3486 |
size_t called_function_count = 0; |
| 3487 |
SpvReflectResult result = TraverseCallGraph(p_parser, p_func, &called_function_count, NULL, 0); |
| 3488 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3489 |
return result; |
| 3490 |
} |
| 3491 |
|
| 3492 |
uint32_t* p_called_functions = NULL; |
| 3493 |
if (called_function_count > 0) { |
| 3494 |
p_called_functions = (uint32_t*)calloc(called_function_count, sizeof(*p_called_functions)); |
| 3495 |
if (IsNull(p_called_functions)) { |
| 3496 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3497 |
} |
| 3498 |
} |
| 3499 |
|
| 3500 |
called_function_count = 0; |
| 3501 |
result = TraverseCallGraph(p_parser, p_func, &called_function_count, p_called_functions, 0); |
| 3502 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3503 |
SafeFree(p_called_functions); |
| 3504 |
return result; |
| 3505 |
} |
| 3506 |
|
| 3507 |
if (called_function_count > 0) { |
| 3508 |
qsort(p_called_functions, called_function_count, sizeof(*p_called_functions), SortCompareUint32); |
| 3509 |
} |
| 3510 |
called_function_count = DedupSortedUint32(p_called_functions, called_function_count); |
| 3511 |
|
| 3512 |
uint32_t used_acessed_count = 0; |
| 3513 |
for (size_t i = 0, j = 0; i < called_function_count; ++i) { |
| 3514 |
|
| 3515 |
|
| 3516 |
while (p_parser->functions[j].id != p_called_functions[i]) { |
| 3517 |
++j; |
| 3518 |
} |
| 3519 |
used_acessed_count += p_parser->functions[j].accessed_variable_count; |
| 3520 |
} |
| 3521 |
SpvReflectPrvAccessedVariable* p_used_accesses = NULL; |
| 3522 |
if (used_acessed_count > 0) { |
| 3523 |
p_used_accesses = (SpvReflectPrvAccessedVariable*)calloc(used_acessed_count, sizeof(SpvReflectPrvAccessedVariable)); |
| 3524 |
if (IsNull(p_used_accesses)) { |
| 3525 |
SafeFree(p_called_functions); |
| 3526 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3527 |
} |
| 3528 |
} |
| 3529 |
used_acessed_count = 0; |
| 3530 |
for (size_t i = 0, j = 0; i < called_function_count; ++i) { |
| 3531 |
while (p_parser->functions[j].id != p_called_functions[i]) { |
| 3532 |
++j; |
| 3533 |
} |
| 3534 |
|
| 3535 |
memcpy(&p_used_accesses[used_acessed_count], p_parser->functions[j].accessed_variables, |
| 3536 |
p_parser->functions[j].accessed_variable_count * sizeof(SpvReflectPrvAccessedVariable)); |
| 3537 |
used_acessed_count += p_parser->functions[j].accessed_variable_count; |
| 3538 |
} |
| 3539 |
SafeFree(p_called_functions); |
| 3540 |
|
| 3541 |
if (used_acessed_count > 0) { |
| 3542 |
qsort(p_used_accesses, used_acessed_count, sizeof(*p_used_accesses), SortCompareAccessedVariable); |
| 3543 |
} |
| 3544 |
|
| 3545 |
|
| 3546 |
size_t used_uniform_count = 0; |
| 3547 |
result = IntersectSortedAccessedVariable(p_used_accesses, used_acessed_count, uniforms, uniform_count, &p_entry->used_uniforms, |
| 3548 |
&used_uniform_count); |
| 3549 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3550 |
SafeFree(p_used_accesses); |
| 3551 |
return result; |
| 3552 |
} |
| 3553 |
|
| 3554 |
size_t used_push_constant_count = 0; |
| 3555 |
result = IntersectSortedAccessedVariable(p_used_accesses, used_acessed_count, push_constants, push_constant_count, |
| 3556 |
&p_entry->used_push_constants, &used_push_constant_count); |
| 3557 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3558 |
SafeFree(p_used_accesses); |
| 3559 |
return result; |
| 3560 |
} |
| 3561 |
|
| 3562 |
for (uint32_t i = 0; i < p_module->descriptor_binding_count; ++i) { |
| 3563 |
SpvReflectDescriptorBinding* p_binding = &p_module->descriptor_bindings[i]; |
| 3564 |
uint32_t byte_address_buffer_offset_count = 0; |
| 3565 |
|
| 3566 |
for (uint32_t j = 0; j < used_acessed_count; j++) { |
| 3567 |
if (p_used_accesses[j].variable_ptr == p_binding->spirv_id) { |
| 3568 |
p_binding->accessed = 1; |
| 3569 |
|
| 3570 |
if (HasByteAddressBufferOffset(p_used_accesses[j].p_node, p_binding)) { |
| 3571 |
byte_address_buffer_offset_count++; |
| 3572 |
} |
| 3573 |
} |
| 3574 |
} |
| 3575 |
|
| 3576 |
|
| 3577 |
if (byte_address_buffer_offset_count > 0) { |
| 3578 |
bool multi_entrypoint = p_binding->byte_address_buffer_offset_count > 0; |
| 3579 |
if (multi_entrypoint) { |
| 3580 |
|
| 3581 |
|
| 3582 |
uint32_t* prev_byte_address_buffer_offsets = p_binding->byte_address_buffer_offsets; |
| 3583 |
p_binding->byte_address_buffer_offsets = |
| 3584 |
(uint32_t*)calloc(byte_address_buffer_offset_count + p_binding->byte_address_buffer_offset_count, sizeof(uint32_t)); |
| 3585 |
memcpy(p_binding->byte_address_buffer_offsets, prev_byte_address_buffer_offsets, |
| 3586 |
sizeof(uint32_t) * p_binding->byte_address_buffer_offset_count); |
| 3587 |
SafeFree(prev_byte_address_buffer_offsets); |
| 3588 |
} else { |
| 3589 |
|
| 3590 |
p_binding->byte_address_buffer_offsets = (uint32_t*)calloc(byte_address_buffer_offset_count, sizeof(uint32_t)); |
| 3591 |
} |
| 3592 |
|
| 3593 |
if (IsNull(p_binding->byte_address_buffer_offsets)) { |
| 3594 |
SafeFree(p_used_accesses); |
| 3595 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3596 |
} |
| 3597 |
|
| 3598 |
for (uint32_t j = 0; j < used_acessed_count; j++) { |
| 3599 |
if (p_used_accesses[j].variable_ptr == p_binding->spirv_id) { |
| 3600 |
result = ParseByteAddressBuffer(p_parser, p_used_accesses[j].p_node, p_binding); |
| 3601 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3602 |
SafeFree(p_used_accesses); |
| 3603 |
return result; |
| 3604 |
} |
| 3605 |
} |
| 3606 |
} |
| 3607 |
|
| 3608 |
if (multi_entrypoint) { |
| 3609 |
qsort(p_binding->byte_address_buffer_offsets, p_binding->byte_address_buffer_offset_count, |
| 3610 |
sizeof(*(p_binding->byte_address_buffer_offsets)), SortCompareUint32); |
| 3611 |
p_binding->byte_address_buffer_offset_count = |
| 3612 |
(uint32_t)DedupSortedUint32(p_binding->byte_address_buffer_offsets, p_binding->byte_address_buffer_offset_count); |
| 3613 |
} |
| 3614 |
} |
| 3615 |
} |
| 3616 |
|
| 3617 |
SafeFree(p_used_accesses); |
| 3618 |
|
| 3619 |
p_entry->used_uniform_count = (uint32_t)used_uniform_count; |
| 3620 |
p_entry->used_push_constant_count = (uint32_t)used_push_constant_count; |
| 3621 |
|
| 3622 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3623 |
} |
| 3624 |
|
| 3625 |
static SpvReflectResult ParseEntryPoints(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 3626 |
if (p_parser->entry_point_count == 0) { |
| 3627 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3628 |
} |
| 3629 |
|
| 3630 |
p_module->entry_point_count = p_parser->entry_point_count; |
| 3631 |
p_module->entry_points = (SpvReflectEntryPoint*)calloc(p_module->entry_point_count, sizeof(*(p_module->entry_points))); |
| 3632 |
if (IsNull(p_module->entry_points)) { |
| 3633 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3634 |
} |
| 3635 |
|
| 3636 |
SpvReflectResult result; |
| 3637 |
size_t uniform_count = 0; |
| 3638 |
uint32_t* uniforms = NULL; |
| 3639 |
if ((result = EnumerateAllUniforms(p_module, &uniform_count, &uniforms)) != SPV_REFLECT_RESULT_SUCCESS) { |
| 3640 |
return result; |
| 3641 |
} |
| 3642 |
size_t push_constant_count = 0; |
| 3643 |
uint32_t* push_constants = NULL; |
| 3644 |
if ((result = EnumerateAllPushConstants(p_module, &push_constant_count, &push_constants)) != SPV_REFLECT_RESULT_SUCCESS) { |
| 3645 |
return result; |
| 3646 |
} |
| 3647 |
|
| 3648 |
size_t entry_point_index = 0; |
| 3649 |
for (size_t i = 0; entry_point_index < p_parser->entry_point_count && i < p_parser->node_count; ++i) { |
| 3650 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 3651 |
if (p_node->op != SpvOpEntryPoint) { |
| 3652 |
continue; |
| 3653 |
} |
| 3654 |
|
| 3655 |
SpvReflectEntryPoint* p_entry_point = &(p_module->entry_points[entry_point_index]); |
| 3656 |
CHECKED_READU32_CAST(p_parser, p_node->word_offset + 1, SpvExecutionModel, p_entry_point->spirv_execution_model); |
| 3657 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_entry_point->id); |
| 3658 |
|
| 3659 |
switch (p_entry_point->spirv_execution_model) { |
| 3660 |
default: |
| 3661 |
break; |
| 3662 |
case SpvExecutionModelVertex: |
| 3663 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_VERTEX_BIT; |
| 3664 |
break; |
| 3665 |
case SpvExecutionModelTessellationControl: |
| 3666 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 3667 |
break; |
| 3668 |
case SpvExecutionModelTessellationEvaluation: |
| 3669 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 3670 |
break; |
| 3671 |
case SpvExecutionModelGeometry: |
| 3672 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_GEOMETRY_BIT; |
| 3673 |
break; |
| 3674 |
case SpvExecutionModelFragment: |
| 3675 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_FRAGMENT_BIT; |
| 3676 |
break; |
| 3677 |
case SpvExecutionModelGLCompute: |
| 3678 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_COMPUTE_BIT; |
| 3679 |
break; |
| 3680 |
case SpvExecutionModelTaskNV: |
| 3681 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_TASK_BIT_NV; |
| 3682 |
break; |
| 3683 |
case SpvExecutionModelTaskEXT: |
| 3684 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_TASK_BIT_EXT; |
| 3685 |
break; |
| 3686 |
case SpvExecutionModelMeshNV: |
| 3687 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_MESH_BIT_NV; |
| 3688 |
break; |
| 3689 |
case SpvExecutionModelMeshEXT: |
| 3690 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_MESH_BIT_EXT; |
| 3691 |
break; |
| 3692 |
case SpvExecutionModelRayGenerationKHR: |
| 3693 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_RAYGEN_BIT_KHR; |
| 3694 |
break; |
| 3695 |
case SpvExecutionModelIntersectionKHR: |
| 3696 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_INTERSECTION_BIT_KHR; |
| 3697 |
break; |
| 3698 |
case SpvExecutionModelAnyHitKHR: |
| 3699 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_ANY_HIT_BIT_KHR; |
| 3700 |
break; |
| 3701 |
case SpvExecutionModelClosestHitKHR: |
| 3702 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_CLOSEST_HIT_BIT_KHR; |
| 3703 |
break; |
| 3704 |
case SpvExecutionModelMissKHR: |
| 3705 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_MISS_BIT_KHR; |
| 3706 |
break; |
| 3707 |
case SpvExecutionModelCallableKHR: |
| 3708 |
p_entry_point->shader_stage = SPV_REFLECT_SHADER_STAGE_CALLABLE_BIT_KHR; |
| 3709 |
break; |
| 3710 |
} |
| 3711 |
|
| 3712 |
++entry_point_index; |
| 3713 |
|
| 3714 |
|
| 3715 |
uint32_t name_start_word_offset = 3; |
| 3716 |
uint32_t name_length_with_terminator = 0; |
| 3717 |
result = |
| 3718 |
ReadStr(p_parser, p_node->word_offset + name_start_word_offset, 0, p_node->word_count, &name_length_with_terminator, NULL); |
| 3719 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3720 |
return result; |
| 3721 |
} |
| 3722 |
p_entry_point->name = (const char*)(p_parser->spirv_code + p_node->word_offset + name_start_word_offset); |
| 3723 |
|
| 3724 |
uint32_t name_word_count = RoundUp(name_length_with_terminator, SPIRV_WORD_SIZE) / SPIRV_WORD_SIZE; |
| 3725 |
uint32_t interface_variable_count = (p_node->word_count - (name_start_word_offset + name_word_count)); |
| 3726 |
uint32_t* p_interface_variables = NULL; |
| 3727 |
if (interface_variable_count > 0) { |
| 3728 |
p_interface_variables = (uint32_t*)calloc(interface_variable_count, sizeof(*(p_interface_variables))); |
| 3729 |
if (IsNull(p_interface_variables)) { |
| 3730 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3731 |
} |
| 3732 |
} |
| 3733 |
|
| 3734 |
for (uint32_t var_index = 0; var_index < interface_variable_count; ++var_index) { |
| 3735 |
uint32_t var_result_id = (uint32_t)INVALID_VALUE; |
| 3736 |
uint32_t offset = name_start_word_offset + name_word_count + var_index; |
| 3737 |
CHECKED_READU32(p_parser, p_node->word_offset + offset, var_result_id); |
| 3738 |
p_interface_variables[var_index] = var_result_id; |
| 3739 |
} |
| 3740 |
|
| 3741 |
result = ParseInterfaceVariables(p_parser, p_module, p_entry_point, interface_variable_count, p_interface_variables); |
| 3742 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3743 |
return result; |
| 3744 |
} |
| 3745 |
SafeFree(p_interface_variables); |
| 3746 |
|
| 3747 |
result = ParseStaticallyUsedResources(p_parser, p_module, p_entry_point, uniform_count, uniforms, push_constant_count, |
| 3748 |
push_constants); |
| 3749 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3750 |
return result; |
| 3751 |
} |
| 3752 |
} |
| 3753 |
|
| 3754 |
SafeFree(uniforms); |
| 3755 |
SafeFree(push_constants); |
| 3756 |
|
| 3757 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3758 |
} |
| 3759 |
|
| 3760 |
static SpvReflectResult ParseExecutionModes(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 3761 |
assert(IsNotNull(p_parser)); |
| 3762 |
assert(IsNotNull(p_parser->nodes)); |
| 3763 |
assert(IsNotNull(p_module)); |
| 3764 |
|
| 3765 |
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) { |
| 3766 |
for (size_t node_idx = 0; node_idx < p_parser->node_count; ++node_idx) { |
| 3767 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[node_idx]); |
| 3768 |
if (p_node->op != SpvOpExecutionMode && p_node->op != SpvOpExecutionModeId) { |
| 3769 |
continue; |
| 3770 |
} |
| 3771 |
|
| 3772 |
|
| 3773 |
uint32_t entry_point_id = 0; |
| 3774 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, entry_point_id); |
| 3775 |
|
| 3776 |
|
| 3777 |
SpvReflectEntryPoint* p_entry_point = NULL; |
| 3778 |
for (size_t entry_point_idx = 0; entry_point_idx < p_module->entry_point_count; ++entry_point_idx) { |
| 3779 |
if (p_module->entry_points[entry_point_idx].id == entry_point_id) { |
| 3780 |
p_entry_point = &p_module->entry_points[entry_point_idx]; |
| 3781 |
break; |
| 3782 |
} |
| 3783 |
} |
| 3784 |
|
| 3785 |
if (IsNull(p_entry_point)) { |
| 3786 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ENTRY_POINT; |
| 3787 |
} |
| 3788 |
|
| 3789 |
|
| 3790 |
uint32_t execution_mode = (uint32_t)INVALID_VALUE; |
| 3791 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, execution_mode); |
| 3792 |
|
| 3793 |
|
| 3794 |
switch (execution_mode) { |
| 3795 |
case SpvExecutionModeInvocations: { |
| 3796 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_entry_point->invocations); |
| 3797 |
} break; |
| 3798 |
|
| 3799 |
case SpvExecutionModeLocalSize: { |
| 3800 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_entry_point->local_size.x); |
| 3801 |
CHECKED_READU32(p_parser, p_node->word_offset + 4, p_entry_point->local_size.y); |
| 3802 |
CHECKED_READU32(p_parser, p_node->word_offset + 5, p_entry_point->local_size.z); |
| 3803 |
} break; |
| 3804 |
case SpvExecutionModeLocalSizeId: { |
| 3805 |
uint32_t local_size_x_id = 0; |
| 3806 |
uint32_t local_size_y_id = 0; |
| 3807 |
uint32_t local_size_z_id = 0; |
| 3808 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, local_size_x_id); |
| 3809 |
CHECKED_READU32(p_parser, p_node->word_offset + 4, local_size_y_id); |
| 3810 |
CHECKED_READU32(p_parser, p_node->word_offset + 5, local_size_z_id); |
| 3811 |
|
| 3812 |
SpvReflectPrvNode* x_node = FindNode(p_parser, local_size_x_id); |
| 3813 |
SpvReflectPrvNode* y_node = FindNode(p_parser, local_size_y_id); |
| 3814 |
SpvReflectPrvNode* z_node = FindNode(p_parser, local_size_z_id); |
| 3815 |
if (IsNotNull(x_node) && IsNotNull(y_node) && IsNotNull(z_node)) { |
| 3816 |
if (IsSpecConstant(x_node)) { |
| 3817 |
p_entry_point->local_size.x = (uint32_t)SPV_REFLECT_EXECUTION_MODE_SPEC_CONSTANT; |
| 3818 |
} else { |
| 3819 |
CHECKED_READU32(p_parser, x_node->word_offset + 3, p_entry_point->local_size.x); |
| 3820 |
} |
| 3821 |
|
| 3822 |
if (IsSpecConstant(y_node)) { |
| 3823 |
p_entry_point->local_size.y = (uint32_t)SPV_REFLECT_EXECUTION_MODE_SPEC_CONSTANT; |
| 3824 |
} else { |
| 3825 |
CHECKED_READU32(p_parser, y_node->word_offset + 3, p_entry_point->local_size.y); |
| 3826 |
} |
| 3827 |
|
| 3828 |
if (IsSpecConstant(z_node)) { |
| 3829 |
p_entry_point->local_size.z = (uint32_t)SPV_REFLECT_EXECUTION_MODE_SPEC_CONSTANT; |
| 3830 |
} else { |
| 3831 |
CHECKED_READU32(p_parser, z_node->word_offset + 3, p_entry_point->local_size.z); |
| 3832 |
} |
| 3833 |
} |
| 3834 |
} break; |
| 3835 |
|
| 3836 |
case SpvExecutionModeOutputVertices: { |
| 3837 |
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_entry_point->output_vertices); |
| 3838 |
} break; |
| 3839 |
|
| 3840 |
default: |
| 3841 |
break; |
| 3842 |
} |
| 3843 |
p_entry_point->execution_mode_count++; |
| 3844 |
} |
| 3845 |
uint32_t* indices = (uint32_t*)calloc(p_module->entry_point_count, sizeof(indices)); |
| 3846 |
if (IsNull(indices)) { |
| 3847 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3848 |
} |
| 3849 |
for (size_t entry_point_idx = 0; entry_point_idx < p_module->entry_point_count; ++entry_point_idx) { |
| 3850 |
SpvReflectEntryPoint* p_entry_point = &p_module->entry_points[entry_point_idx]; |
| 3851 |
if (p_entry_point->execution_mode_count > 0) { |
| 3852 |
p_entry_point->execution_modes = |
| 3853 |
(SpvExecutionMode*)calloc(p_entry_point->execution_mode_count, sizeof(*p_entry_point->execution_modes)); |
| 3854 |
if (IsNull(p_entry_point->execution_modes)) { |
| 3855 |
SafeFree(indices); |
| 3856 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3857 |
} |
| 3858 |
} |
| 3859 |
} |
| 3860 |
|
| 3861 |
for (size_t node_idx = 0; node_idx < p_parser->node_count; ++node_idx) { |
| 3862 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[node_idx]); |
| 3863 |
if (p_node->op != SpvOpExecutionMode) { |
| 3864 |
continue; |
| 3865 |
} |
| 3866 |
|
| 3867 |
|
| 3868 |
uint32_t entry_point_id = 0; |
| 3869 |
CHECKED_READU32(p_parser, p_node->word_offset + 1, entry_point_id); |
| 3870 |
|
| 3871 |
|
| 3872 |
SpvReflectEntryPoint* p_entry_point = NULL; |
| 3873 |
uint32_t* idx = NULL; |
| 3874 |
for (size_t entry_point_idx = 0; entry_point_idx < p_module->entry_point_count; ++entry_point_idx) { |
| 3875 |
if (p_module->entry_points[entry_point_idx].id == entry_point_id) { |
| 3876 |
p_entry_point = &p_module->entry_points[entry_point_idx]; |
| 3877 |
idx = &indices[entry_point_idx]; |
| 3878 |
break; |
| 3879 |
} |
| 3880 |
} |
| 3881 |
|
| 3882 |
|
| 3883 |
uint32_t execution_mode = (uint32_t)INVALID_VALUE; |
| 3884 |
CHECKED_READU32(p_parser, p_node->word_offset + 2, execution_mode); |
| 3885 |
p_entry_point->execution_modes[(*idx)++] = (SpvExecutionMode)execution_mode; |
| 3886 |
} |
| 3887 |
SafeFree(indices); |
| 3888 |
} |
| 3889 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3890 |
} |
| 3891 |
|
| 3892 |
static SpvReflectResult ParsePushConstantBlocks(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) { |
| 3893 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 3894 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 3895 |
if ((p_node->op != SpvOpVariable) || (p_node->storage_class != SpvStorageClassPushConstant)) { |
| 3896 |
continue; |
| 3897 |
} |
| 3898 |
|
| 3899 |
p_module->push_constant_block_count += 1; |
| 3900 |
} |
| 3901 |
|
| 3902 |
if (p_module->push_constant_block_count == 0) { |
| 3903 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3904 |
} |
| 3905 |
|
| 3906 |
p_module->push_constant_blocks = |
| 3907 |
(SpvReflectBlockVariable*)calloc(p_module->push_constant_block_count, sizeof(*p_module->push_constant_blocks)); |
| 3908 |
if (IsNull(p_module->push_constant_blocks)) { |
| 3909 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 3910 |
} |
| 3911 |
|
| 3912 |
p_parser->physical_pointer_struct_count = 0; |
| 3913 |
uint32_t push_constant_index = 0; |
| 3914 |
for (size_t i = 0; i < p_parser->node_count; ++i) { |
| 3915 |
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]); |
| 3916 |
if ((p_node->op != SpvOpVariable) || (p_node->storage_class != SpvStorageClassPushConstant)) { |
| 3917 |
continue; |
| 3918 |
} |
| 3919 |
|
| 3920 |
SpvReflectTypeDescription* p_type = FindType(p_module, p_node->type_id); |
| 3921 |
if (IsNull(p_node) || IsNull(p_type)) { |
| 3922 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3923 |
} |
| 3924 |
|
| 3925 |
if (p_type->op == SpvOpTypePointer) { |
| 3926 |
|
| 3927 |
SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 3928 |
if (IsNull(p_type_node)) { |
| 3929 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3930 |
} |
| 3931 |
|
| 3932 |
p_type = FindType(p_module, p_type_node->type_id); |
| 3933 |
if (IsNull(p_type)) { |
| 3934 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3935 |
} |
| 3936 |
} |
| 3937 |
|
| 3938 |
SpvReflectPrvNode* p_type_node = FindNode(p_parser, p_type->id); |
| 3939 |
if (IsNull(p_type_node)) { |
| 3940 |
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE; |
| 3941 |
} |
| 3942 |
|
| 3943 |
SpvReflectBlockVariable* p_push_constant = &p_module->push_constant_blocks[push_constant_index]; |
| 3944 |
p_push_constant->spirv_id = p_node->result_id; |
| 3945 |
p_parser->physical_pointer_count = 0; |
| 3946 |
SpvReflectResult result = ParseDescriptorBlockVariable(p_parser, p_module, p_type, p_push_constant); |
| 3947 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3948 |
return result; |
| 3949 |
} |
| 3950 |
|
| 3951 |
for (uint32_t access_chain_index = 0; access_chain_index < p_parser->access_chain_count; ++access_chain_index) { |
| 3952 |
SpvReflectPrvAccessChain* p_access_chain = &(p_parser->access_chains[access_chain_index]); |
| 3953 |
|
| 3954 |
if (p_push_constant->spirv_id != FindAccessChainBaseVariable(p_parser, p_access_chain)) { |
| 3955 |
continue; |
| 3956 |
} |
| 3957 |
SpvReflectBlockVariable* p_var = |
| 3958 |
(p_access_chain->base_id == p_push_constant->spirv_id) ? p_push_constant : GetRefBlkVar(p_parser, p_access_chain); |
| 3959 |
result = ParseDescriptorBlockVariableUsage(p_parser, p_module, p_access_chain, 0, (SpvOp)INVALID_VALUE, p_var); |
| 3960 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3961 |
return result; |
| 3962 |
} |
| 3963 |
} |
| 3964 |
|
| 3965 |
p_push_constant->name = p_node->name; |
| 3966 |
result = ParseDescriptorBlockVariableSizes(p_parser, p_module, true, false, false, p_push_constant); |
| 3967 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 3968 |
return result; |
| 3969 |
} |
| 3970 |
|
| 3971 |
|
| 3972 |
|
| 3973 |
p_push_constant->offset = UINT32_MAX; |
| 3974 |
for (uint32_t k = 0; k < p_push_constant->member_count; ++k) { |
| 3975 |
const uint32_t member_offset = p_push_constant->members[k].offset; |
| 3976 |
p_push_constant->offset = Min(p_push_constant->offset, member_offset); |
| 3977 |
} |
| 3978 |
|
| 3979 |
++push_constant_index; |
| 3980 |
} |
| 3981 |
|
| 3982 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 3983 |
} |
| 3984 |
|
| 3985 |
static int SortCompareDescriptorSet(const void* a, const void* b) { |
| 3986 |
const SpvReflectDescriptorSet* p_elem_a = (const SpvReflectDescriptorSet*)a; |
| 3987 |
const SpvReflectDescriptorSet* p_elem_b = (const SpvReflectDescriptorSet*)b; |
| 3988 |
int value = (int)(p_elem_a->set) - (int)(p_elem_b->set); |
| 3989 |
|
| 3990 |
|
| 3991 |
assert(value != 0); |
| 3992 |
return value; |
| 3993 |
} |
| 3994 |
|
| 3995 |
static SpvReflectResult ParseEntrypointDescriptorSets(SpvReflectShaderModule* p_module) { |
| 3996 |
|
| 3997 |
for (uint32_t i = 0; i < p_module->entry_point_count; ++i) { |
| 3998 |
SpvReflectEntryPoint* p_entry = &p_module->entry_points[i]; |
| 3999 |
for (uint32_t j = 0; j < p_entry->descriptor_set_count; ++j) { |
| 4000 |
SafeFree(p_entry->descriptor_sets[j].bindings); |
| 4001 |
} |
| 4002 |
SafeFree(p_entry->descriptor_sets); |
| 4003 |
p_entry->descriptor_set_count = 0; |
| 4004 |
for (uint32_t j = 0; j < p_module->descriptor_set_count; ++j) { |
| 4005 |
const SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[j]; |
| 4006 |
for (uint32_t k = 0; k < p_set->binding_count; ++k) { |
| 4007 |
bool found = SearchSortedUint32(p_entry->used_uniforms, p_entry->used_uniform_count, p_set->bindings[k]->spirv_id); |
| 4008 |
if (found) { |
| 4009 |
++p_entry->descriptor_set_count; |
| 4010 |
break; |
| 4011 |
} |
| 4012 |
} |
| 4013 |
} |
| 4014 |
|
| 4015 |
p_entry->descriptor_sets = NULL; |
| 4016 |
if (p_entry->descriptor_set_count > 0) { |
| 4017 |
p_entry->descriptor_sets = (SpvReflectDescriptorSet*)calloc(p_entry->descriptor_set_count, sizeof(*p_entry->descriptor_sets)); |
| 4018 |
if (IsNull(p_entry->descriptor_sets)) { |
| 4019 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 4020 |
} |
| 4021 |
} |
| 4022 |
p_entry->descriptor_set_count = 0; |
| 4023 |
for (uint32_t j = 0; j < p_module->descriptor_set_count; ++j) { |
| 4024 |
const SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[j]; |
| 4025 |
uint32_t count = 0; |
| 4026 |
for (uint32_t k = 0; k < p_set->binding_count; ++k) { |
| 4027 |
bool found = SearchSortedUint32(p_entry->used_uniforms, p_entry->used_uniform_count, p_set->bindings[k]->spirv_id); |
| 4028 |
if (found) { |
| 4029 |
++count; |
| 4030 |
} |
| 4031 |
} |
| 4032 |
if (count == 0) { |
| 4033 |
continue; |
| 4034 |
} |
| 4035 |
SpvReflectDescriptorSet* p_entry_set = &p_entry->descriptor_sets[p_entry->descriptor_set_count++]; |
| 4036 |
p_entry_set->set = p_set->set; |
| 4037 |
p_entry_set->bindings = (SpvReflectDescriptorBinding**)calloc(count, sizeof(*p_entry_set->bindings)); |
| 4038 |
if (IsNull(p_entry_set->bindings)) { |
| 4039 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 4040 |
} |
| 4041 |
for (uint32_t k = 0; k < p_set->binding_count; ++k) { |
| 4042 |
bool found = SearchSortedUint32(p_entry->used_uniforms, p_entry->used_uniform_count, p_set->bindings[k]->spirv_id); |
| 4043 |
if (found) { |
| 4044 |
p_entry_set->bindings[p_entry_set->binding_count++] = p_set->bindings[k]; |
| 4045 |
} |
| 4046 |
} |
| 4047 |
} |
| 4048 |
} |
| 4049 |
|
| 4050 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4051 |
} |
| 4052 |
|
| 4053 |
static SpvReflectResult ParseDescriptorSets(SpvReflectShaderModule* p_module) { |
| 4054 |
|
| 4055 |
for (uint32_t i = 0; i < p_module->descriptor_binding_count; ++i) { |
| 4056 |
SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[i]); |
| 4057 |
|
| 4058 |
|
| 4059 |
SpvReflectDescriptorSet* p_target_set = NULL; |
| 4060 |
for (uint32_t j = 0; j < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++j) { |
| 4061 |
SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[j]; |
| 4062 |
if (p_set->set == p_descriptor->set) { |
| 4063 |
p_target_set = p_set; |
| 4064 |
break; |
| 4065 |
} |
| 4066 |
} |
| 4067 |
|
| 4068 |
|
| 4069 |
if (IsNull(p_target_set)) { |
| 4070 |
for (uint32_t j = 0; j < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++j) { |
| 4071 |
SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[j]; |
| 4072 |
if (p_set->set == (uint32_t)INVALID_VALUE) { |
| 4073 |
p_target_set = p_set; |
| 4074 |
p_target_set->set = p_descriptor->set; |
| 4075 |
break; |
| 4076 |
} |
| 4077 |
} |
| 4078 |
} |
| 4079 |
|
| 4080 |
if (IsNull(p_target_set)) { |
| 4081 |
return SPV_REFLECT_RESULT_ERROR_INTERNAL_ERROR; |
| 4082 |
} |
| 4083 |
|
| 4084 |
p_target_set->binding_count += 1; |
| 4085 |
} |
| 4086 |
|
| 4087 |
|
| 4088 |
for (uint32_t i = 0; i < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++i) { |
| 4089 |
const SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[i]; |
| 4090 |
if (p_set->set != (uint32_t)INVALID_VALUE) { |
| 4091 |
p_module->descriptor_set_count += 1; |
| 4092 |
} |
| 4093 |
} |
| 4094 |
|
| 4095 |
|
| 4096 |
if (p_module->descriptor_set_count > 0) { |
| 4097 |
qsort(p_module->descriptor_sets, p_module->descriptor_set_count, sizeof(*(p_module->descriptor_sets)), |
| 4098 |
SortCompareDescriptorSet); |
| 4099 |
} |
| 4100 |
|
| 4101 |
|
| 4102 |
for (uint32_t i = 0; i < p_module->descriptor_set_count; ++i) { |
| 4103 |
SpvReflectDescriptorSet* p_set = &(p_module->descriptor_sets[i]); |
| 4104 |
p_set->bindings = (SpvReflectDescriptorBinding**)calloc(p_set->binding_count, sizeof(*(p_set->bindings))); |
| 4105 |
|
| 4106 |
uint32_t descriptor_index = 0; |
| 4107 |
for (uint32_t j = 0; j < p_module->descriptor_binding_count; ++j) { |
| 4108 |
SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[j]); |
| 4109 |
if (p_descriptor->set == p_set->set) { |
| 4110 |
assert(descriptor_index < p_set->binding_count); |
| 4111 |
p_set->bindings[descriptor_index] = p_descriptor; |
| 4112 |
++descriptor_index; |
| 4113 |
} |
| 4114 |
} |
| 4115 |
} |
| 4116 |
|
| 4117 |
return ParseEntrypointDescriptorSets(p_module); |
| 4118 |
} |
| 4119 |
|
| 4120 |
static SpvReflectResult DisambiguateStorageBufferSrvUav(SpvReflectShaderModule* p_module) { |
| 4121 |
if (p_module->descriptor_binding_count == 0) { |
| 4122 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4123 |
} |
| 4124 |
|
| 4125 |
for (uint32_t descriptor_index = 0; descriptor_index < p_module->descriptor_binding_count; ++descriptor_index) { |
| 4126 |
SpvReflectDescriptorBinding* p_descriptor = &(p_module->descriptor_bindings[descriptor_index]); |
| 4127 |
|
| 4128 |
if (p_descriptor->descriptor_type != SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER) { |
| 4129 |
continue; |
| 4130 |
} |
| 4131 |
|
| 4132 |
|
| 4133 |
|
| 4134 |
|
| 4135 |
|
| 4136 |
|
| 4137 |
|
| 4138 |
if (p_descriptor->block.decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE) { |
| 4139 |
p_descriptor->resource_type = SPV_REFLECT_RESOURCE_FLAG_SRV; |
| 4140 |
} |
| 4141 |
} |
| 4142 |
|
| 4143 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4144 |
} |
| 4145 |
|
| 4146 |
static SpvReflectResult SynchronizeDescriptorSets(SpvReflectShaderModule* p_module) { |
| 4147 |
|
| 4148 |
for (uint32_t i = 0; i < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++i) { |
| 4149 |
SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[i]; |
| 4150 |
SafeFree(p_set->bindings); |
| 4151 |
p_set->binding_count = 0; |
| 4152 |
p_set->set = (uint32_t)INVALID_VALUE; |
| 4153 |
} |
| 4154 |
|
| 4155 |
p_module->descriptor_set_count = 0; |
| 4156 |
|
| 4157 |
SpvReflectResult result = ParseDescriptorSets(p_module); |
| 4158 |
return result; |
| 4159 |
} |
| 4160 |
|
| 4161 |
static SpvReflectResult CreateShaderModule(uint32_t flags, size_t size, const void* p_code, SpvReflectShaderModule* p_module) { |
| 4162 |
|
| 4163 |
memset(p_module, 0, sizeof(*p_module)); |
| 4164 |
|
| 4165 |
|
| 4166 |
#ifdef __cplusplus |
| 4167 |
p_module->_internal = (SpvReflectShaderModule::Internal*)calloc(1, sizeof(*(p_module->_internal))); |
| 4168 |
#else |
| 4169 |
p_module->_internal = calloc(1, sizeof(*(p_module->_internal))); |
| 4170 |
#endif |
| 4171 |
if (IsNull(p_module->_internal)) { |
| 4172 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 4173 |
} |
| 4174 |
|
| 4175 |
p_module->_internal->module_flags = flags; |
| 4176 |
|
| 4177 |
if (flags & SPV_REFLECT_MODULE_FLAG_NO_COPY) { |
| 4178 |
|
| 4179 |
p_module->_internal->spirv_size = size; |
| 4180 |
#if defined(__cplusplus) |
| 4181 |
p_module->_internal->spirv_code = const_cast<uint32_t*>(static_cast<const uint32_t*>(p_code)); |
| 4182 |
#else |
| 4183 |
p_module->_internal->spirv_code = (void*)p_code; |
| 4184 |
#endif |
| 4185 |
p_module->_internal->spirv_word_count = (uint32_t)(size / SPIRV_WORD_SIZE); |
| 4186 |
} else { |
| 4187 |
|
| 4188 |
p_module->_internal->spirv_size = size; |
| 4189 |
p_module->_internal->spirv_code = (uint32_t*)calloc(1, p_module->_internal->spirv_size); |
| 4190 |
p_module->_internal->spirv_word_count = (uint32_t)(size / SPIRV_WORD_SIZE); |
| 4191 |
if (IsNull(p_module->_internal->spirv_code)) { |
| 4192 |
SafeFree(p_module->_internal); |
| 4193 |
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED; |
| 4194 |
} |
| 4195 |
|
| 4196 |
memcpy(p_module->_internal->spirv_code, p_code, size); |
| 4197 |
} |
| 4198 |
|
| 4199 |
|
| 4200 |
SpvReflectPrvParser parser; |
| 4201 |
memset(&parser, 0, sizeof(SpvReflectPrvParser)); |
| 4202 |
|
| 4203 |
|
| 4204 |
SpvReflectResult result = CreateParser(p_module->_internal->spirv_size, p_module->_internal->spirv_code, &parser); |
| 4205 |
|
| 4206 |
|
| 4207 |
{ |
| 4208 |
const uint32_t* p_ptr = (const uint32_t*)p_module->_internal->spirv_code; |
| 4209 |
p_module->generator = (SpvReflectGenerator)((*(p_ptr + 2) & 0xFFFF0000) >> 16); |
| 4210 |
} |
| 4211 |
|
| 4212 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4213 |
result = ParseNodes(&parser); |
| 4214 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4215 |
} |
| 4216 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4217 |
result = ParseStrings(&parser); |
| 4218 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4219 |
} |
| 4220 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4221 |
result = ParseSource(&parser, p_module); |
| 4222 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4223 |
} |
| 4224 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4225 |
result = ParseFunctions(&parser); |
| 4226 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4227 |
} |
| 4228 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4229 |
result = ParseMemberCounts(&parser); |
| 4230 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4231 |
} |
| 4232 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4233 |
result = ParseNames(&parser); |
| 4234 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4235 |
} |
| 4236 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4237 |
result = ParseDecorations(&parser, p_module); |
| 4238 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4239 |
} |
| 4240 |
|
| 4241 |
|
| 4242 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4243 |
p_module->source_language = parser.source_language; |
| 4244 |
p_module->source_language_version = parser.source_language_version; |
| 4245 |
|
| 4246 |
|
| 4247 |
p_module->descriptor_set_count = 0; |
| 4248 |
memset(p_module->descriptor_sets, 0, SPV_REFLECT_MAX_DESCRIPTOR_SETS * sizeof(*p_module->descriptor_sets)); |
| 4249 |
|
| 4250 |
for (uint32_t set_number = 0; set_number < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++set_number) { |
| 4251 |
p_module->descriptor_sets[set_number].set = (uint32_t)INVALID_VALUE; |
| 4252 |
} |
| 4253 |
} |
| 4254 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4255 |
result = ParseTypes(&parser, p_module); |
| 4256 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4257 |
} |
| 4258 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4259 |
result = ParseDescriptorBindings(&parser, p_module); |
| 4260 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4261 |
} |
| 4262 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4263 |
result = ParseDescriptorType(p_module); |
| 4264 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4265 |
} |
| 4266 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4267 |
result = ParseUAVCounterBindings(p_module); |
| 4268 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4269 |
} |
| 4270 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4271 |
result = ParseDescriptorBlocks(&parser, p_module); |
| 4272 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4273 |
} |
| 4274 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4275 |
result = ParsePushConstantBlocks(&parser, p_module); |
| 4276 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4277 |
} |
| 4278 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4279 |
result = ParseEntryPoints(&parser, p_module); |
| 4280 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4281 |
} |
| 4282 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4283 |
result = ParseCapabilities(&parser, p_module); |
| 4284 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4285 |
} |
| 4286 |
if (result == SPV_REFLECT_RESULT_SUCCESS && p_module->entry_point_count > 0) { |
| 4287 |
SpvReflectEntryPoint* p_entry = &(p_module->entry_points[0]); |
| 4288 |
p_module->entry_point_name = p_entry->name; |
| 4289 |
p_module->entry_point_id = p_entry->id; |
| 4290 |
p_module->spirv_execution_model = p_entry->spirv_execution_model; |
| 4291 |
p_module->shader_stage = p_entry->shader_stage; |
| 4292 |
p_module->input_variable_count = p_entry->input_variable_count; |
| 4293 |
p_module->input_variables = p_entry->input_variables; |
| 4294 |
p_module->output_variable_count = p_entry->output_variable_count; |
| 4295 |
p_module->output_variables = p_entry->output_variables; |
| 4296 |
p_module->interface_variable_count = p_entry->interface_variable_count; |
| 4297 |
p_module->interface_variables = p_entry->interface_variables; |
| 4298 |
} |
| 4299 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4300 |
result = DisambiguateStorageBufferSrvUav(p_module); |
| 4301 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4302 |
} |
| 4303 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4304 |
result = SynchronizeDescriptorSets(p_module); |
| 4305 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4306 |
} |
| 4307 |
if (result == SPV_REFLECT_RESULT_SUCCESS) { |
| 4308 |
result = ParseExecutionModes(&parser, p_module); |
| 4309 |
SPV_REFLECT_ASSERT(result == SPV_REFLECT_RESULT_SUCCESS); |
| 4310 |
} |
| 4311 |
|
| 4312 |
|
| 4313 |
if (result != SPV_REFLECT_RESULT_SUCCESS) { |
| 4314 |
spvReflectDestroyShaderModule(p_module); |
| 4315 |
} |
| 4316 |
|
| 4317 |
DestroyParser(&parser); |
| 4318 |
|
| 4319 |
return result; |
| 4320 |
} |
| 4321 |
|
| 4322 |
SpvReflectResult spvReflectCreateShaderModule(size_t size, const void* p_code, SpvReflectShaderModule* p_module) { |
| 4323 |
return CreateShaderModule(0, size, p_code, p_module); |
| 4324 |
} |
| 4325 |
|
| 4326 |
SpvReflectResult spvReflectCreateShaderModule2(uint32_t flags, size_t size, const void* p_code, SpvReflectShaderModule* p_module) { |
| 4327 |
return CreateShaderModule(flags, size, p_code, p_module); |
| 4328 |
} |
| 4329 |
|
| 4330 |
SpvReflectResult spvReflectGetShaderModule(size_t size, const void* p_code, SpvReflectShaderModule* p_module) { |
| 4331 |
return spvReflectCreateShaderModule(size, p_code, p_module); |
| 4332 |
} |
| 4333 |
|
| 4334 |
static void SafeFreeTypes(SpvReflectTypeDescription* p_type) { |
| 4335 |
if (IsNull(p_type) || p_type->copied) { |
| 4336 |
return; |
| 4337 |
} |
| 4338 |
|
| 4339 |
if (IsNotNull(p_type->members)) { |
| 4340 |
for (size_t i = 0; i < p_type->member_count; ++i) { |
| 4341 |
SpvReflectTypeDescription* p_member = &p_type->members[i]; |
| 4342 |
SafeFreeTypes(p_member); |
| 4343 |
} |
| 4344 |
|
| 4345 |
SafeFree(p_type->members); |
| 4346 |
p_type->members = NULL; |
| 4347 |
} |
| 4348 |
} |
| 4349 |
|
| 4350 |
static void SafeFreeBlockVariables(SpvReflectBlockVariable* p_block) { |
| 4351 |
if (IsNull(p_block)) { |
| 4352 |
return; |
| 4353 |
} |
| 4354 |
|
| 4355 |
|
| 4356 |
if (p_block->flags & SPV_REFLECT_VARIABLE_FLAGS_PHYSICAL_POINTER_COPY) { |
| 4357 |
return; |
| 4358 |
} |
| 4359 |
|
| 4360 |
if (IsNotNull(p_block->members)) { |
| 4361 |
for (size_t i = 0; i < p_block->member_count; ++i) { |
| 4362 |
SpvReflectBlockVariable* p_member = &p_block->members[i]; |
| 4363 |
SafeFreeBlockVariables(p_member); |
| 4364 |
} |
| 4365 |
|
| 4366 |
SafeFree(p_block->members); |
| 4367 |
p_block->members = NULL; |
| 4368 |
} |
| 4369 |
} |
| 4370 |
|
| 4371 |
static void SafeFreeInterfaceVariable(SpvReflectInterfaceVariable* p_interface) { |
| 4372 |
if (IsNull(p_interface)) { |
| 4373 |
return; |
| 4374 |
} |
| 4375 |
|
| 4376 |
if (IsNotNull(p_interface->members)) { |
| 4377 |
for (size_t i = 0; i < p_interface->member_count; ++i) { |
| 4378 |
SpvReflectInterfaceVariable* p_member = &p_interface->members[i]; |
| 4379 |
SafeFreeInterfaceVariable(p_member); |
| 4380 |
} |
| 4381 |
|
| 4382 |
SafeFree(p_interface->members); |
| 4383 |
p_interface->members = NULL; |
| 4384 |
} |
| 4385 |
} |
| 4386 |
|
| 4387 |
void spvReflectDestroyShaderModule(SpvReflectShaderModule* p_module) { |
| 4388 |
if (IsNull(p_module->_internal)) { |
| 4389 |
return; |
| 4390 |
} |
| 4391 |
|
| 4392 |
SafeFree(p_module->source_source); |
| 4393 |
|
| 4394 |
|
| 4395 |
for (size_t i = 0; i < p_module->descriptor_set_count; ++i) { |
| 4396 |
SpvReflectDescriptorSet* p_set = &p_module->descriptor_sets[i]; |
| 4397 |
free(p_set->bindings); |
| 4398 |
} |
| 4399 |
|
| 4400 |
|
| 4401 |
for (size_t i = 0; i < p_module->descriptor_binding_count; ++i) { |
| 4402 |
SpvReflectDescriptorBinding* p_descriptor = &p_module->descriptor_bindings[i]; |
| 4403 |
if (IsNotNull(p_descriptor->byte_address_buffer_offsets)) { |
| 4404 |
SafeFree(p_descriptor->byte_address_buffer_offsets); |
| 4405 |
} |
| 4406 |
SafeFreeBlockVariables(&p_descriptor->block); |
| 4407 |
} |
| 4408 |
SafeFree(p_module->descriptor_bindings); |
| 4409 |
|
| 4410 |
|
| 4411 |
for (size_t i = 0; i < p_module->entry_point_count; ++i) { |
| 4412 |
SpvReflectEntryPoint* p_entry = &p_module->entry_points[i]; |
| 4413 |
for (size_t j = 0; j < p_entry->interface_variable_count; j++) { |
| 4414 |
SafeFreeInterfaceVariable(&p_entry->interface_variables[j]); |
| 4415 |
} |
| 4416 |
for (uint32_t j = 0; j < p_entry->descriptor_set_count; ++j) { |
| 4417 |
SafeFree(p_entry->descriptor_sets[j].bindings); |
| 4418 |
} |
| 4419 |
SafeFree(p_entry->descriptor_sets); |
| 4420 |
SafeFree(p_entry->input_variables); |
| 4421 |
SafeFree(p_entry->output_variables); |
| 4422 |
SafeFree(p_entry->interface_variables); |
| 4423 |
SafeFree(p_entry->used_uniforms); |
| 4424 |
SafeFree(p_entry->used_push_constants); |
| 4425 |
SafeFree(p_entry->execution_modes); |
| 4426 |
} |
| 4427 |
SafeFree(p_module->capabilities); |
| 4428 |
SafeFree(p_module->entry_points); |
| 4429 |
SafeFree(p_module->spec_constants); |
| 4430 |
|
| 4431 |
|
| 4432 |
for (size_t i = 0; i < p_module->push_constant_block_count; ++i) { |
| 4433 |
SafeFreeBlockVariables(&p_module->push_constant_blocks[i]); |
| 4434 |
} |
| 4435 |
SafeFree(p_module->push_constant_blocks); |
| 4436 |
|
| 4437 |
|
| 4438 |
for (size_t i = 0; i < p_module->_internal->type_description_count; ++i) { |
| 4439 |
SpvReflectTypeDescription* p_type = &p_module->_internal->type_descriptions[i]; |
| 4440 |
if (IsNotNull(p_type->members)) { |
| 4441 |
SafeFreeTypes(p_type); |
| 4442 |
} |
| 4443 |
SafeFree(p_type->members); |
| 4444 |
} |
| 4445 |
SafeFree(p_module->_internal->type_descriptions); |
| 4446 |
|
| 4447 |
|
| 4448 |
if ((p_module->_internal->module_flags & SPV_REFLECT_MODULE_FLAG_NO_COPY) == 0) { |
| 4449 |
SafeFree(p_module->_internal->spirv_code); |
| 4450 |
} |
| 4451 |
|
| 4452 |
SafeFree(p_module->_internal); |
| 4453 |
} |
| 4454 |
|
| 4455 |
uint32_t spvReflectGetCodeSize(const SpvReflectShaderModule* p_module) { |
| 4456 |
if (IsNull(p_module)) { |
| 4457 |
return 0; |
| 4458 |
} |
| 4459 |
|
| 4460 |
return (uint32_t)(p_module->_internal->spirv_size); |
| 4461 |
} |
| 4462 |
|
| 4463 |
const uint32_t* spvReflectGetCode(const SpvReflectShaderModule* p_module) { |
| 4464 |
if (IsNull(p_module)) { |
| 4465 |
return NULL; |
| 4466 |
} |
| 4467 |
|
| 4468 |
return p_module->_internal->spirv_code; |
| 4469 |
} |
| 4470 |
|
| 4471 |
const SpvReflectEntryPoint* spvReflectGetEntryPoint(const SpvReflectShaderModule* p_module, const char* entry_point) { |
| 4472 |
if (IsNull(p_module) || IsNull(entry_point)) { |
| 4473 |
return NULL; |
| 4474 |
} |
| 4475 |
|
| 4476 |
for (uint32_t i = 0; i < p_module->entry_point_count; ++i) { |
| 4477 |
if (strcmp(p_module->entry_points[i].name, entry_point) == 0) { |
| 4478 |
return &p_module->entry_points[i]; |
| 4479 |
} |
| 4480 |
} |
| 4481 |
return NULL; |
| 4482 |
} |
| 4483 |
|
| 4484 |
SpvReflectResult spvReflectEnumerateDescriptorBindings(const SpvReflectShaderModule* p_module, uint32_t* p_count, |
| 4485 |
SpvReflectDescriptorBinding** pp_bindings) { |
| 4486 |
if (IsNull(p_module)) { |
| 4487 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4488 |
} |
| 4489 |
if (IsNull(p_count)) { |
| 4490 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4491 |
} |
| 4492 |
|
| 4493 |
if (IsNotNull(pp_bindings)) { |
| 4494 |
if (*p_count != p_module->descriptor_binding_count) { |
| 4495 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4496 |
} |
| 4497 |
|
| 4498 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4499 |
SpvReflectDescriptorBinding* p_bindings = (SpvReflectDescriptorBinding*)&p_module->descriptor_bindings[index]; |
| 4500 |
pp_bindings[index] = p_bindings; |
| 4501 |
} |
| 4502 |
} else { |
| 4503 |
*p_count = p_module->descriptor_binding_count; |
| 4504 |
} |
| 4505 |
|
| 4506 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4507 |
} |
| 4508 |
|
| 4509 |
SpvReflectResult spvReflectEnumerateEntryPointDescriptorBindings(const SpvReflectShaderModule* p_module, const char* entry_point, |
| 4510 |
uint32_t* p_count, SpvReflectDescriptorBinding** pp_bindings) { |
| 4511 |
if (IsNull(p_module)) { |
| 4512 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4513 |
} |
| 4514 |
if (IsNull(p_count)) { |
| 4515 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4516 |
} |
| 4517 |
|
| 4518 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4519 |
if (IsNull(p_entry)) { |
| 4520 |
return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4521 |
} |
| 4522 |
|
| 4523 |
uint32_t count = 0; |
| 4524 |
for (uint32_t i = 0; i < p_module->descriptor_binding_count; ++i) { |
| 4525 |
bool found = SearchSortedUint32(p_entry->used_uniforms, p_entry->used_uniform_count, p_module->descriptor_bindings[i].spirv_id); |
| 4526 |
if (found) { |
| 4527 |
if (IsNotNull(pp_bindings)) { |
| 4528 |
if (count >= *p_count) { |
| 4529 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4530 |
} |
| 4531 |
pp_bindings[count++] = (SpvReflectDescriptorBinding*)&p_module->descriptor_bindings[i]; |
| 4532 |
} else { |
| 4533 |
++count; |
| 4534 |
} |
| 4535 |
} |
| 4536 |
} |
| 4537 |
if (IsNotNull(pp_bindings)) { |
| 4538 |
if (count != *p_count) { |
| 4539 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4540 |
} |
| 4541 |
} else { |
| 4542 |
*p_count = count; |
| 4543 |
} |
| 4544 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4545 |
} |
| 4546 |
|
| 4547 |
SpvReflectResult spvReflectEnumerateDescriptorSets(const SpvReflectShaderModule* p_module, uint32_t* p_count, |
| 4548 |
SpvReflectDescriptorSet** pp_sets) { |
| 4549 |
if (IsNull(p_module)) { |
| 4550 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4551 |
} |
| 4552 |
if (IsNull(p_count)) { |
| 4553 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4554 |
} |
| 4555 |
|
| 4556 |
if (IsNotNull(pp_sets)) { |
| 4557 |
if (*p_count != p_module->descriptor_set_count) { |
| 4558 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4559 |
} |
| 4560 |
|
| 4561 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4562 |
SpvReflectDescriptorSet* p_set = (SpvReflectDescriptorSet*)&p_module->descriptor_sets[index]; |
| 4563 |
pp_sets[index] = p_set; |
| 4564 |
} |
| 4565 |
} else { |
| 4566 |
*p_count = p_module->descriptor_set_count; |
| 4567 |
} |
| 4568 |
|
| 4569 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4570 |
} |
| 4571 |
|
| 4572 |
SpvReflectResult spvReflectEnumerateEntryPointDescriptorSets(const SpvReflectShaderModule* p_module, const char* entry_point, |
| 4573 |
uint32_t* p_count, SpvReflectDescriptorSet** pp_sets) { |
| 4574 |
if (IsNull(p_module)) { |
| 4575 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4576 |
} |
| 4577 |
if (IsNull(p_count)) { |
| 4578 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4579 |
} |
| 4580 |
|
| 4581 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4582 |
if (IsNull(p_entry)) { |
| 4583 |
return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4584 |
} |
| 4585 |
|
| 4586 |
if (IsNotNull(pp_sets)) { |
| 4587 |
if (*p_count != p_entry->descriptor_set_count) { |
| 4588 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4589 |
} |
| 4590 |
|
| 4591 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4592 |
SpvReflectDescriptorSet* p_set = (SpvReflectDescriptorSet*)&p_entry->descriptor_sets[index]; |
| 4593 |
pp_sets[index] = p_set; |
| 4594 |
} |
| 4595 |
} else { |
| 4596 |
*p_count = p_entry->descriptor_set_count; |
| 4597 |
} |
| 4598 |
|
| 4599 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4600 |
} |
| 4601 |
|
| 4602 |
SpvReflectResult spvReflectEnumerateInterfaceVariables(const SpvReflectShaderModule* p_module, uint32_t* p_count, |
| 4603 |
SpvReflectInterfaceVariable** pp_variables) { |
| 4604 |
if (IsNull(p_module)) { |
| 4605 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4606 |
} |
| 4607 |
if (IsNull(p_count)) { |
| 4608 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4609 |
} |
| 4610 |
|
| 4611 |
if (IsNotNull(pp_variables)) { |
| 4612 |
if (*p_count != p_module->interface_variable_count) { |
| 4613 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4614 |
} |
| 4615 |
|
| 4616 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4617 |
SpvReflectInterfaceVariable* p_var = &p_module->interface_variables[index]; |
| 4618 |
pp_variables[index] = p_var; |
| 4619 |
} |
| 4620 |
} else { |
| 4621 |
*p_count = p_module->interface_variable_count; |
| 4622 |
} |
| 4623 |
|
| 4624 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4625 |
} |
| 4626 |
|
| 4627 |
SpvReflectResult spvReflectEnumerateEntryPointInterfaceVariables(const SpvReflectShaderModule* p_module, const char* entry_point, |
| 4628 |
uint32_t* p_count, SpvReflectInterfaceVariable** pp_variables) { |
| 4629 |
if (IsNull(p_module)) { |
| 4630 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4631 |
} |
| 4632 |
if (IsNull(p_count)) { |
| 4633 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4634 |
} |
| 4635 |
|
| 4636 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4637 |
if (IsNull(p_entry)) { |
| 4638 |
return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4639 |
} |
| 4640 |
|
| 4641 |
if (IsNotNull(pp_variables)) { |
| 4642 |
if (*p_count != p_entry->interface_variable_count) { |
| 4643 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4644 |
} |
| 4645 |
|
| 4646 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4647 |
SpvReflectInterfaceVariable* p_var = &p_entry->interface_variables[index]; |
| 4648 |
pp_variables[index] = p_var; |
| 4649 |
} |
| 4650 |
} else { |
| 4651 |
*p_count = p_entry->interface_variable_count; |
| 4652 |
} |
| 4653 |
|
| 4654 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4655 |
} |
| 4656 |
|
| 4657 |
SpvReflectResult spvReflectEnumerateInputVariables(const SpvReflectShaderModule* p_module, uint32_t* p_count, |
| 4658 |
SpvReflectInterfaceVariable** pp_variables) { |
| 4659 |
if (IsNull(p_module)) { |
| 4660 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4661 |
} |
| 4662 |
if (IsNull(p_count)) { |
| 4663 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4664 |
} |
| 4665 |
|
| 4666 |
if (IsNotNull(pp_variables)) { |
| 4667 |
if (*p_count != p_module->input_variable_count) { |
| 4668 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4669 |
} |
| 4670 |
|
| 4671 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4672 |
SpvReflectInterfaceVariable* p_var = p_module->input_variables[index]; |
| 4673 |
pp_variables[index] = p_var; |
| 4674 |
} |
| 4675 |
} else { |
| 4676 |
*p_count = p_module->input_variable_count; |
| 4677 |
} |
| 4678 |
|
| 4679 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4680 |
} |
| 4681 |
|
| 4682 |
SpvReflectResult spvReflectEnumerateEntryPointInputVariables(const SpvReflectShaderModule* p_module, const char* entry_point, |
| 4683 |
uint32_t* p_count, SpvReflectInterfaceVariable** pp_variables) { |
| 4684 |
if (IsNull(p_module)) { |
| 4685 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4686 |
} |
| 4687 |
if (IsNull(p_count)) { |
| 4688 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4689 |
} |
| 4690 |
|
| 4691 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4692 |
if (IsNull(p_entry)) { |
| 4693 |
return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4694 |
} |
| 4695 |
|
| 4696 |
if (IsNotNull(pp_variables)) { |
| 4697 |
if (*p_count != p_entry->input_variable_count) { |
| 4698 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4699 |
} |
| 4700 |
|
| 4701 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4702 |
SpvReflectInterfaceVariable* p_var = p_entry->input_variables[index]; |
| 4703 |
pp_variables[index] = p_var; |
| 4704 |
} |
| 4705 |
} else { |
| 4706 |
*p_count = p_entry->input_variable_count; |
| 4707 |
} |
| 4708 |
|
| 4709 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4710 |
} |
| 4711 |
|
| 4712 |
SpvReflectResult spvReflectEnumerateOutputVariables(const SpvReflectShaderModule* p_module, uint32_t* p_count, |
| 4713 |
SpvReflectInterfaceVariable** pp_variables) { |
| 4714 |
if (IsNull(p_module)) { |
| 4715 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4716 |
} |
| 4717 |
if (IsNull(p_count)) { |
| 4718 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4719 |
} |
| 4720 |
|
| 4721 |
if (IsNotNull(pp_variables)) { |
| 4722 |
if (*p_count != p_module->output_variable_count) { |
| 4723 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4724 |
} |
| 4725 |
|
| 4726 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4727 |
SpvReflectInterfaceVariable* p_var = p_module->output_variables[index]; |
| 4728 |
pp_variables[index] = p_var; |
| 4729 |
} |
| 4730 |
} else { |
| 4731 |
*p_count = p_module->output_variable_count; |
| 4732 |
} |
| 4733 |
|
| 4734 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4735 |
} |
| 4736 |
|
| 4737 |
SpvReflectResult spvReflectEnumerateEntryPointOutputVariables(const SpvReflectShaderModule* p_module, const char* entry_point, |
| 4738 |
uint32_t* p_count, SpvReflectInterfaceVariable** pp_variables) { |
| 4739 |
if (IsNull(p_module)) { |
| 4740 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4741 |
} |
| 4742 |
if (IsNull(p_count)) { |
| 4743 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4744 |
} |
| 4745 |
|
| 4746 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4747 |
if (IsNull(p_entry)) { |
| 4748 |
return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4749 |
} |
| 4750 |
|
| 4751 |
if (IsNotNull(pp_variables)) { |
| 4752 |
if (*p_count != p_entry->output_variable_count) { |
| 4753 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4754 |
} |
| 4755 |
|
| 4756 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4757 |
SpvReflectInterfaceVariable* p_var = p_entry->output_variables[index]; |
| 4758 |
pp_variables[index] = p_var; |
| 4759 |
} |
| 4760 |
} else { |
| 4761 |
*p_count = p_entry->output_variable_count; |
| 4762 |
} |
| 4763 |
|
| 4764 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4765 |
} |
| 4766 |
|
| 4767 |
SpvReflectResult spvReflectEnumeratePushConstantBlocks(const SpvReflectShaderModule* p_module, uint32_t* p_count, |
| 4768 |
SpvReflectBlockVariable** pp_blocks) { |
| 4769 |
if (IsNull(p_module)) { |
| 4770 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4771 |
} |
| 4772 |
if (IsNull(p_count)) { |
| 4773 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4774 |
} |
| 4775 |
|
| 4776 |
if (pp_blocks != NULL) { |
| 4777 |
if (*p_count != p_module->push_constant_block_count) { |
| 4778 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4779 |
} |
| 4780 |
|
| 4781 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4782 |
SpvReflectBlockVariable* p_push_constant_blocks = (SpvReflectBlockVariable*)&p_module->push_constant_blocks[index]; |
| 4783 |
pp_blocks[index] = p_push_constant_blocks; |
| 4784 |
} |
| 4785 |
} else { |
| 4786 |
*p_count = p_module->push_constant_block_count; |
| 4787 |
} |
| 4788 |
|
| 4789 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4790 |
} |
| 4791 |
SpvReflectResult spvReflectEnumeratePushConstants(const SpvReflectShaderModule* p_module, uint32_t* p_count, |
| 4792 |
SpvReflectBlockVariable** pp_blocks) { |
| 4793 |
return spvReflectEnumeratePushConstantBlocks(p_module, p_count, pp_blocks); |
| 4794 |
} |
| 4795 |
|
| 4796 |
SpvReflectResult spvReflectEnumerateEntryPointPushConstantBlocks(const SpvReflectShaderModule* p_module, const char* entry_point, |
| 4797 |
uint32_t* p_count, SpvReflectBlockVariable** pp_blocks) { |
| 4798 |
if (IsNull(p_module)) { |
| 4799 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4800 |
} |
| 4801 |
if (IsNull(p_count)) { |
| 4802 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4803 |
} |
| 4804 |
|
| 4805 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4806 |
if (IsNull(p_entry)) { |
| 4807 |
return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4808 |
} |
| 4809 |
|
| 4810 |
uint32_t count = 0; |
| 4811 |
for (uint32_t i = 0; i < p_module->push_constant_block_count; ++i) { |
| 4812 |
bool found = SearchSortedUint32(p_entry->used_push_constants, p_entry->used_push_constant_count, |
| 4813 |
p_module->push_constant_blocks[i].spirv_id); |
| 4814 |
if (found) { |
| 4815 |
if (IsNotNull(pp_blocks)) { |
| 4816 |
if (count >= *p_count) { |
| 4817 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4818 |
} |
| 4819 |
pp_blocks[count++] = (SpvReflectBlockVariable*)&p_module->push_constant_blocks[i]; |
| 4820 |
} else { |
| 4821 |
++count; |
| 4822 |
} |
| 4823 |
} |
| 4824 |
} |
| 4825 |
if (IsNotNull(pp_blocks)) { |
| 4826 |
if (count != *p_count) { |
| 4827 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4828 |
} |
| 4829 |
} else { |
| 4830 |
*p_count = count; |
| 4831 |
} |
| 4832 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4833 |
} |
| 4834 |
|
| 4835 |
SpvReflectResult spvReflectEnumerateSpecializationConstants(const SpvReflectShaderModule* p_module, uint32_t* p_count, |
| 4836 |
SpvReflectSpecializationConstant** pp_constants) { |
| 4837 |
if (IsNull(p_module)) { |
| 4838 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4839 |
} |
| 4840 |
if (IsNull(p_count)) { |
| 4841 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 4842 |
} |
| 4843 |
|
| 4844 |
if (IsNotNull(pp_constants)) { |
| 4845 |
if (*p_count != p_module->spec_constant_count) { |
| 4846 |
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH; |
| 4847 |
} |
| 4848 |
|
| 4849 |
for (uint32_t index = 0; index < *p_count; ++index) { |
| 4850 |
SpvReflectSpecializationConstant* p_constant = (SpvReflectSpecializationConstant*)&p_module->spec_constants[index]; |
| 4851 |
pp_constants[index] = p_constant; |
| 4852 |
} |
| 4853 |
} else { |
| 4854 |
*p_count = p_module->spec_constant_count; |
| 4855 |
} |
| 4856 |
|
| 4857 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 4858 |
} |
| 4859 |
|
| 4860 |
const SpvReflectDescriptorBinding* spvReflectGetDescriptorBinding(const SpvReflectShaderModule* p_module, uint32_t binding_number, |
| 4861 |
uint32_t set_number, SpvReflectResult* p_result) { |
| 4862 |
const SpvReflectDescriptorBinding* p_descriptor = NULL; |
| 4863 |
if (IsNotNull(p_module)) { |
| 4864 |
for (uint32_t index = 0; index < p_module->descriptor_binding_count; ++index) { |
| 4865 |
const SpvReflectDescriptorBinding* p_potential = &p_module->descriptor_bindings[index]; |
| 4866 |
if ((p_potential->binding == binding_number) && (p_potential->set == set_number)) { |
| 4867 |
p_descriptor = p_potential; |
| 4868 |
break; |
| 4869 |
} |
| 4870 |
} |
| 4871 |
} |
| 4872 |
if (IsNotNull(p_result)) { |
| 4873 |
*p_result = IsNotNull(p_descriptor) |
| 4874 |
? SPV_REFLECT_RESULT_SUCCESS |
| 4875 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 4876 |
} |
| 4877 |
return p_descriptor; |
| 4878 |
} |
| 4879 |
|
| 4880 |
const SpvReflectDescriptorBinding* spvReflectGetEntryPointDescriptorBinding(const SpvReflectShaderModule* p_module, |
| 4881 |
const char* entry_point, uint32_t binding_number, |
| 4882 |
uint32_t set_number, SpvReflectResult* p_result) { |
| 4883 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4884 |
if (IsNull(p_entry)) { |
| 4885 |
if (IsNotNull(p_result)) { |
| 4886 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4887 |
} |
| 4888 |
return NULL; |
| 4889 |
} |
| 4890 |
const SpvReflectDescriptorBinding* p_descriptor = NULL; |
| 4891 |
if (IsNotNull(p_module)) { |
| 4892 |
for (uint32_t index = 0; index < p_module->descriptor_binding_count; ++index) { |
| 4893 |
const SpvReflectDescriptorBinding* p_potential = &p_module->descriptor_bindings[index]; |
| 4894 |
bool found = SearchSortedUint32(p_entry->used_uniforms, p_entry->used_uniform_count, p_potential->spirv_id); |
| 4895 |
if ((p_potential->binding == binding_number) && (p_potential->set == set_number) && found) { |
| 4896 |
p_descriptor = p_potential; |
| 4897 |
break; |
| 4898 |
} |
| 4899 |
} |
| 4900 |
} |
| 4901 |
if (IsNotNull(p_result)) { |
| 4902 |
*p_result = IsNotNull(p_descriptor) |
| 4903 |
? SPV_REFLECT_RESULT_SUCCESS |
| 4904 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 4905 |
} |
| 4906 |
return p_descriptor; |
| 4907 |
} |
| 4908 |
|
| 4909 |
const SpvReflectDescriptorSet* spvReflectGetDescriptorSet(const SpvReflectShaderModule* p_module, uint32_t set_number, |
| 4910 |
SpvReflectResult* p_result) { |
| 4911 |
const SpvReflectDescriptorSet* p_set = NULL; |
| 4912 |
if (IsNotNull(p_module)) { |
| 4913 |
for (uint32_t index = 0; index < p_module->descriptor_set_count; ++index) { |
| 4914 |
const SpvReflectDescriptorSet* p_potential = &p_module->descriptor_sets[index]; |
| 4915 |
if (p_potential->set == set_number) { |
| 4916 |
p_set = p_potential; |
| 4917 |
} |
| 4918 |
} |
| 4919 |
} |
| 4920 |
if (IsNotNull(p_result)) { |
| 4921 |
*p_result = IsNotNull(p_set) |
| 4922 |
? SPV_REFLECT_RESULT_SUCCESS |
| 4923 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 4924 |
} |
| 4925 |
return p_set; |
| 4926 |
} |
| 4927 |
|
| 4928 |
const SpvReflectDescriptorSet* spvReflectGetEntryPointDescriptorSet(const SpvReflectShaderModule* p_module, const char* entry_point, |
| 4929 |
uint32_t set_number, SpvReflectResult* p_result) { |
| 4930 |
const SpvReflectDescriptorSet* p_set = NULL; |
| 4931 |
if (IsNotNull(p_module)) { |
| 4932 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4933 |
if (IsNull(p_entry)) { |
| 4934 |
if (IsNotNull(p_result)) { |
| 4935 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4936 |
} |
| 4937 |
return NULL; |
| 4938 |
} |
| 4939 |
for (uint32_t index = 0; index < p_entry->descriptor_set_count; ++index) { |
| 4940 |
const SpvReflectDescriptorSet* p_potential = &p_entry->descriptor_sets[index]; |
| 4941 |
if (p_potential->set == set_number) { |
| 4942 |
p_set = p_potential; |
| 4943 |
} |
| 4944 |
} |
| 4945 |
} |
| 4946 |
if (IsNotNull(p_result)) { |
| 4947 |
*p_result = IsNotNull(p_set) |
| 4948 |
? SPV_REFLECT_RESULT_SUCCESS |
| 4949 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 4950 |
} |
| 4951 |
return p_set; |
| 4952 |
} |
| 4953 |
|
| 4954 |
const SpvReflectInterfaceVariable* spvReflectGetInputVariableByLocation(const SpvReflectShaderModule* p_module, uint32_t location, |
| 4955 |
SpvReflectResult* p_result) { |
| 4956 |
if (location == INVALID_VALUE) { |
| 4957 |
if (IsNotNull(p_result)) { |
| 4958 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4959 |
} |
| 4960 |
return NULL; |
| 4961 |
} |
| 4962 |
const SpvReflectInterfaceVariable* p_var = NULL; |
| 4963 |
if (IsNotNull(p_module)) { |
| 4964 |
for (uint32_t index = 0; index < p_module->input_variable_count; ++index) { |
| 4965 |
const SpvReflectInterfaceVariable* p_potential = p_module->input_variables[index]; |
| 4966 |
if (p_potential->location == location) { |
| 4967 |
p_var = p_potential; |
| 4968 |
} |
| 4969 |
} |
| 4970 |
} |
| 4971 |
if (IsNotNull(p_result)) { |
| 4972 |
*p_result = IsNotNull(p_var) |
| 4973 |
? SPV_REFLECT_RESULT_SUCCESS |
| 4974 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 4975 |
} |
| 4976 |
return p_var; |
| 4977 |
} |
| 4978 |
const SpvReflectInterfaceVariable* spvReflectGetInputVariable(const SpvReflectShaderModule* p_module, uint32_t location, |
| 4979 |
SpvReflectResult* p_result) { |
| 4980 |
return spvReflectGetInputVariableByLocation(p_module, location, p_result); |
| 4981 |
} |
| 4982 |
|
| 4983 |
const SpvReflectInterfaceVariable* spvReflectGetEntryPointInputVariableByLocation(const SpvReflectShaderModule* p_module, |
| 4984 |
const char* entry_point, uint32_t location, |
| 4985 |
SpvReflectResult* p_result) { |
| 4986 |
if (location == INVALID_VALUE) { |
| 4987 |
if (IsNotNull(p_result)) { |
| 4988 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4989 |
} |
| 4990 |
return NULL; |
| 4991 |
} |
| 4992 |
|
| 4993 |
const SpvReflectInterfaceVariable* p_var = NULL; |
| 4994 |
if (IsNotNull(p_module)) { |
| 4995 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 4996 |
if (IsNull(p_entry)) { |
| 4997 |
if (IsNotNull(p_result)) { |
| 4998 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 4999 |
} |
| 5000 |
return NULL; |
| 5001 |
} |
| 5002 |
for (uint32_t index = 0; index < p_entry->input_variable_count; ++index) { |
| 5003 |
const SpvReflectInterfaceVariable* p_potential = p_entry->input_variables[index]; |
| 5004 |
if (p_potential->location == location) { |
| 5005 |
p_var = p_potential; |
| 5006 |
} |
| 5007 |
} |
| 5008 |
} |
| 5009 |
if (IsNotNull(p_result)) { |
| 5010 |
*p_result = IsNotNull(p_var) |
| 5011 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5012 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5013 |
} |
| 5014 |
return p_var; |
| 5015 |
} |
| 5016 |
|
| 5017 |
const SpvReflectInterfaceVariable* spvReflectGetInputVariableBySemantic(const SpvReflectShaderModule* p_module, |
| 5018 |
const char* semantic, SpvReflectResult* p_result) { |
| 5019 |
if (IsNull(semantic)) { |
| 5020 |
if (IsNotNull(p_result)) { |
| 5021 |
*p_result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5022 |
} |
| 5023 |
return NULL; |
| 5024 |
} |
| 5025 |
if (semantic[0] == '\0') { |
| 5026 |
if (IsNotNull(p_result)) { |
| 5027 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5028 |
} |
| 5029 |
return NULL; |
| 5030 |
} |
| 5031 |
const SpvReflectInterfaceVariable* p_var = NULL; |
| 5032 |
if (IsNotNull(p_module)) { |
| 5033 |
for (uint32_t index = 0; index < p_module->input_variable_count; ++index) { |
| 5034 |
const SpvReflectInterfaceVariable* p_potential = p_module->input_variables[index]; |
| 5035 |
if (p_potential->semantic != NULL && strcmp(p_potential->semantic, semantic) == 0) { |
| 5036 |
p_var = p_potential; |
| 5037 |
} |
| 5038 |
} |
| 5039 |
} |
| 5040 |
if (IsNotNull(p_result)) { |
| 5041 |
*p_result = IsNotNull(p_var) |
| 5042 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5043 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5044 |
} |
| 5045 |
return p_var; |
| 5046 |
} |
| 5047 |
|
| 5048 |
const SpvReflectInterfaceVariable* spvReflectGetEntryPointInputVariableBySemantic(const SpvReflectShaderModule* p_module, |
| 5049 |
const char* entry_point, const char* semantic, |
| 5050 |
SpvReflectResult* p_result) { |
| 5051 |
if (IsNull(semantic)) { |
| 5052 |
if (IsNotNull(p_result)) { |
| 5053 |
*p_result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5054 |
} |
| 5055 |
return NULL; |
| 5056 |
} |
| 5057 |
if (semantic[0] == '\0') { |
| 5058 |
if (IsNotNull(p_result)) { |
| 5059 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5060 |
} |
| 5061 |
return NULL; |
| 5062 |
} |
| 5063 |
const SpvReflectInterfaceVariable* p_var = NULL; |
| 5064 |
if (IsNotNull(p_module)) { |
| 5065 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 5066 |
if (IsNull(p_entry)) { |
| 5067 |
if (IsNotNull(p_result)) { |
| 5068 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5069 |
} |
| 5070 |
return NULL; |
| 5071 |
} |
| 5072 |
for (uint32_t index = 0; index < p_entry->input_variable_count; ++index) { |
| 5073 |
const SpvReflectInterfaceVariable* p_potential = p_entry->input_variables[index]; |
| 5074 |
if (p_potential->semantic != NULL && strcmp(p_potential->semantic, semantic) == 0) { |
| 5075 |
p_var = p_potential; |
| 5076 |
} |
| 5077 |
} |
| 5078 |
} |
| 5079 |
if (IsNotNull(p_result)) { |
| 5080 |
*p_result = IsNotNull(p_var) |
| 5081 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5082 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5083 |
} |
| 5084 |
return p_var; |
| 5085 |
} |
| 5086 |
|
| 5087 |
const SpvReflectInterfaceVariable* spvReflectGetOutputVariableByLocation(const SpvReflectShaderModule* p_module, uint32_t location, |
| 5088 |
SpvReflectResult* p_result) { |
| 5089 |
if (location == INVALID_VALUE) { |
| 5090 |
if (IsNotNull(p_result)) { |
| 5091 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5092 |
} |
| 5093 |
return NULL; |
| 5094 |
} |
| 5095 |
const SpvReflectInterfaceVariable* p_var = NULL; |
| 5096 |
if (IsNotNull(p_module)) { |
| 5097 |
for (uint32_t index = 0; index < p_module->output_variable_count; ++index) { |
| 5098 |
const SpvReflectInterfaceVariable* p_potential = p_module->output_variables[index]; |
| 5099 |
if (p_potential->location == location) { |
| 5100 |
p_var = p_potential; |
| 5101 |
} |
| 5102 |
} |
| 5103 |
} |
| 5104 |
if (IsNotNull(p_result)) { |
| 5105 |
*p_result = IsNotNull(p_var) |
| 5106 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5107 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5108 |
} |
| 5109 |
return p_var; |
| 5110 |
} |
| 5111 |
const SpvReflectInterfaceVariable* spvReflectGetOutputVariable(const SpvReflectShaderModule* p_module, uint32_t location, |
| 5112 |
SpvReflectResult* p_result) { |
| 5113 |
return spvReflectGetOutputVariableByLocation(p_module, location, p_result); |
| 5114 |
} |
| 5115 |
|
| 5116 |
const SpvReflectInterfaceVariable* spvReflectGetEntryPointOutputVariableByLocation(const SpvReflectShaderModule* p_module, |
| 5117 |
const char* entry_point, uint32_t location, |
| 5118 |
SpvReflectResult* p_result) { |
| 5119 |
if (location == INVALID_VALUE) { |
| 5120 |
if (IsNotNull(p_result)) { |
| 5121 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5122 |
} |
| 5123 |
return NULL; |
| 5124 |
} |
| 5125 |
|
| 5126 |
const SpvReflectInterfaceVariable* p_var = NULL; |
| 5127 |
if (IsNotNull(p_module)) { |
| 5128 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 5129 |
if (IsNull(p_entry)) { |
| 5130 |
if (IsNotNull(p_result)) { |
| 5131 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5132 |
} |
| 5133 |
return NULL; |
| 5134 |
} |
| 5135 |
for (uint32_t index = 0; index < p_entry->output_variable_count; ++index) { |
| 5136 |
const SpvReflectInterfaceVariable* p_potential = p_entry->output_variables[index]; |
| 5137 |
if (p_potential->location == location) { |
| 5138 |
p_var = p_potential; |
| 5139 |
} |
| 5140 |
} |
| 5141 |
} |
| 5142 |
if (IsNotNull(p_result)) { |
| 5143 |
*p_result = IsNotNull(p_var) |
| 5144 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5145 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5146 |
} |
| 5147 |
return p_var; |
| 5148 |
} |
| 5149 |
|
| 5150 |
const SpvReflectInterfaceVariable* spvReflectGetOutputVariableBySemantic(const SpvReflectShaderModule* p_module, |
| 5151 |
const char* semantic, SpvReflectResult* p_result) { |
| 5152 |
if (IsNull(semantic)) { |
| 5153 |
if (IsNotNull(p_result)) { |
| 5154 |
*p_result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5155 |
} |
| 5156 |
return NULL; |
| 5157 |
} |
| 5158 |
if (semantic[0] == '\0') { |
| 5159 |
if (IsNotNull(p_result)) { |
| 5160 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5161 |
} |
| 5162 |
return NULL; |
| 5163 |
} |
| 5164 |
const SpvReflectInterfaceVariable* p_var = NULL; |
| 5165 |
if (IsNotNull(p_module)) { |
| 5166 |
for (uint32_t index = 0; index < p_module->output_variable_count; ++index) { |
| 5167 |
const SpvReflectInterfaceVariable* p_potential = p_module->output_variables[index]; |
| 5168 |
if (p_potential->semantic != NULL && strcmp(p_potential->semantic, semantic) == 0) { |
| 5169 |
p_var = p_potential; |
| 5170 |
} |
| 5171 |
} |
| 5172 |
} |
| 5173 |
if (IsNotNull(p_result)) { |
| 5174 |
*p_result = IsNotNull(p_var) |
| 5175 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5176 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5177 |
} |
| 5178 |
return p_var; |
| 5179 |
} |
| 5180 |
|
| 5181 |
const SpvReflectInterfaceVariable* spvReflectGetEntryPointOutputVariableBySemantic(const SpvReflectShaderModule* p_module, |
| 5182 |
const char* entry_point, const char* semantic, |
| 5183 |
SpvReflectResult* p_result) { |
| 5184 |
if (IsNull(semantic)) { |
| 5185 |
if (IsNotNull(p_result)) { |
| 5186 |
*p_result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5187 |
} |
| 5188 |
return NULL; |
| 5189 |
} |
| 5190 |
if (semantic[0] == '\0') { |
| 5191 |
if (IsNotNull(p_result)) { |
| 5192 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5193 |
} |
| 5194 |
return NULL; |
| 5195 |
} |
| 5196 |
const SpvReflectInterfaceVariable* p_var = NULL; |
| 5197 |
if (IsNotNull(p_module)) { |
| 5198 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 5199 |
if (IsNull(p_entry)) { |
| 5200 |
if (IsNotNull(p_result)) { |
| 5201 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5202 |
} |
| 5203 |
return NULL; |
| 5204 |
} |
| 5205 |
for (uint32_t index = 0; index < p_entry->output_variable_count; ++index) { |
| 5206 |
const SpvReflectInterfaceVariable* p_potential = p_entry->output_variables[index]; |
| 5207 |
if (p_potential->semantic != NULL && strcmp(p_potential->semantic, semantic) == 0) { |
| 5208 |
p_var = p_potential; |
| 5209 |
} |
| 5210 |
} |
| 5211 |
} |
| 5212 |
if (IsNotNull(p_result)) { |
| 5213 |
*p_result = IsNotNull(p_var) |
| 5214 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5215 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5216 |
} |
| 5217 |
return p_var; |
| 5218 |
} |
| 5219 |
|
| 5220 |
const SpvReflectBlockVariable* spvReflectGetPushConstantBlock(const SpvReflectShaderModule* p_module, uint32_t index, |
| 5221 |
SpvReflectResult* p_result) { |
| 5222 |
const SpvReflectBlockVariable* p_push_constant = NULL; |
| 5223 |
if (IsNotNull(p_module)) { |
| 5224 |
if (index < p_module->push_constant_block_count) { |
| 5225 |
p_push_constant = &p_module->push_constant_blocks[index]; |
| 5226 |
} |
| 5227 |
} |
| 5228 |
if (IsNotNull(p_result)) { |
| 5229 |
*p_result = IsNotNull(p_push_constant) |
| 5230 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5231 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5232 |
} |
| 5233 |
return p_push_constant; |
| 5234 |
} |
| 5235 |
const SpvReflectBlockVariable* spvReflectGetPushConstant(const SpvReflectShaderModule* p_module, uint32_t index, |
| 5236 |
SpvReflectResult* p_result) { |
| 5237 |
return spvReflectGetPushConstantBlock(p_module, index, p_result); |
| 5238 |
} |
| 5239 |
|
| 5240 |
const SpvReflectBlockVariable* spvReflectGetEntryPointPushConstantBlock(const SpvReflectShaderModule* p_module, |
| 5241 |
const char* entry_point, SpvReflectResult* p_result) { |
| 5242 |
const SpvReflectBlockVariable* p_push_constant = NULL; |
| 5243 |
if (IsNotNull(p_module)) { |
| 5244 |
const SpvReflectEntryPoint* p_entry = spvReflectGetEntryPoint(p_module, entry_point); |
| 5245 |
if (IsNull(p_entry)) { |
| 5246 |
if (IsNotNull(p_result)) { |
| 5247 |
*p_result = SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5248 |
} |
| 5249 |
return NULL; |
| 5250 |
} |
| 5251 |
for (uint32_t i = 0; i < p_module->push_constant_block_count; ++i) { |
| 5252 |
bool found = SearchSortedUint32(p_entry->used_push_constants, p_entry->used_push_constant_count, |
| 5253 |
p_module->push_constant_blocks[i].spirv_id); |
| 5254 |
if (found) { |
| 5255 |
p_push_constant = &p_module->push_constant_blocks[i]; |
| 5256 |
break; |
| 5257 |
} |
| 5258 |
} |
| 5259 |
} |
| 5260 |
if (IsNotNull(p_result)) { |
| 5261 |
*p_result = IsNotNull(p_push_constant) |
| 5262 |
? SPV_REFLECT_RESULT_SUCCESS |
| 5263 |
: (IsNull(p_module) ? SPV_REFLECT_RESULT_ERROR_NULL_POINTER : SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND); |
| 5264 |
} |
| 5265 |
return p_push_constant; |
| 5266 |
} |
| 5267 |
|
| 5268 |
SpvReflectResult spvReflectChangeDescriptorBindingNumbers(SpvReflectShaderModule* p_module, |
| 5269 |
const SpvReflectDescriptorBinding* p_binding, uint32_t new_binding_number, |
| 5270 |
uint32_t new_set_binding) { |
| 5271 |
if (IsNull(p_module)) { |
| 5272 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5273 |
} |
| 5274 |
if (IsNull(p_binding)) { |
| 5275 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5276 |
} |
| 5277 |
|
| 5278 |
SpvReflectDescriptorBinding* p_target_descriptor = NULL; |
| 5279 |
for (uint32_t index = 0; index < p_module->descriptor_binding_count; ++index) { |
| 5280 |
if (&p_module->descriptor_bindings[index] == p_binding) { |
| 5281 |
p_target_descriptor = &p_module->descriptor_bindings[index]; |
| 5282 |
break; |
| 5283 |
} |
| 5284 |
} |
| 5285 |
|
| 5286 |
if (IsNotNull(p_target_descriptor)) { |
| 5287 |
if (p_target_descriptor->word_offset.binding > (p_module->_internal->spirv_word_count - 1)) { |
| 5288 |
return SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED; |
| 5289 |
} |
| 5290 |
|
| 5291 |
if (new_binding_number != (uint32_t)SPV_REFLECT_BINDING_NUMBER_DONT_CHANGE) { |
| 5292 |
uint32_t* p_code = p_module->_internal->spirv_code + p_target_descriptor->word_offset.binding; |
| 5293 |
*p_code = new_binding_number; |
| 5294 |
p_target_descriptor->binding = new_binding_number; |
| 5295 |
} |
| 5296 |
|
| 5297 |
if (new_set_binding != (uint32_t)SPV_REFLECT_SET_NUMBER_DONT_CHANGE) { |
| 5298 |
uint32_t* p_code = p_module->_internal->spirv_code + p_target_descriptor->word_offset.set; |
| 5299 |
*p_code = new_set_binding; |
| 5300 |
p_target_descriptor->set = new_set_binding; |
| 5301 |
} |
| 5302 |
} |
| 5303 |
|
| 5304 |
SpvReflectResult result = SPV_REFLECT_RESULT_SUCCESS; |
| 5305 |
if (new_set_binding != (uint32_t)SPV_REFLECT_SET_NUMBER_DONT_CHANGE) { |
| 5306 |
result = SynchronizeDescriptorSets(p_module); |
| 5307 |
} |
| 5308 |
return result; |
| 5309 |
} |
| 5310 |
SpvReflectResult spvReflectChangeDescriptorBindingNumber(SpvReflectShaderModule* p_module, |
| 5311 |
const SpvReflectDescriptorBinding* p_descriptor_binding, |
| 5312 |
uint32_t new_binding_number, uint32_t optional_new_set_number) { |
| 5313 |
return spvReflectChangeDescriptorBindingNumbers(p_module, p_descriptor_binding, new_binding_number, optional_new_set_number); |
| 5314 |
} |
| 5315 |
|
| 5316 |
SpvReflectResult spvReflectChangeDescriptorSetNumber(SpvReflectShaderModule* p_module, const SpvReflectDescriptorSet* p_set, |
| 5317 |
uint32_t new_set_number) { |
| 5318 |
if (IsNull(p_module)) { |
| 5319 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5320 |
} |
| 5321 |
if (IsNull(p_set)) { |
| 5322 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5323 |
} |
| 5324 |
SpvReflectDescriptorSet* p_target_set = NULL; |
| 5325 |
for (uint32_t index = 0; index < SPV_REFLECT_MAX_DESCRIPTOR_SETS; ++index) { |
| 5326 |
|
| 5327 |
|
| 5328 |
if (p_module->descriptor_sets[index].set == p_set->set) { |
| 5329 |
p_target_set = (SpvReflectDescriptorSet*)p_set; |
| 5330 |
break; |
| 5331 |
} |
| 5332 |
} |
| 5333 |
|
| 5334 |
SpvReflectResult result = SPV_REFLECT_RESULT_SUCCESS; |
| 5335 |
if (IsNotNull(p_target_set) && new_set_number != (uint32_t)SPV_REFLECT_SET_NUMBER_DONT_CHANGE) { |
| 5336 |
for (uint32_t index = 0; index < p_target_set->binding_count; ++index) { |
| 5337 |
SpvReflectDescriptorBinding* p_descriptor = p_target_set->bindings[index]; |
| 5338 |
if (p_descriptor->word_offset.set > (p_module->_internal->spirv_word_count - 1)) { |
| 5339 |
return SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED; |
| 5340 |
} |
| 5341 |
|
| 5342 |
uint32_t* p_code = p_module->_internal->spirv_code + p_descriptor->word_offset.set; |
| 5343 |
*p_code = new_set_number; |
| 5344 |
p_descriptor->set = new_set_number; |
| 5345 |
} |
| 5346 |
|
| 5347 |
result = SynchronizeDescriptorSets(p_module); |
| 5348 |
} |
| 5349 |
|
| 5350 |
return result; |
| 5351 |
} |
| 5352 |
|
| 5353 |
static SpvReflectResult ChangeVariableLocation(SpvReflectShaderModule* p_module, SpvReflectInterfaceVariable* p_variable, |
| 5354 |
uint32_t new_location) { |
| 5355 |
if (p_variable->word_offset.location > (p_module->_internal->spirv_word_count - 1)) { |
| 5356 |
return SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED; |
| 5357 |
} |
| 5358 |
uint32_t* p_code = p_module->_internal->spirv_code + p_variable->word_offset.location; |
| 5359 |
*p_code = new_location; |
| 5360 |
p_variable->location = new_location; |
| 5361 |
return SPV_REFLECT_RESULT_SUCCESS; |
| 5362 |
} |
| 5363 |
|
| 5364 |
SpvReflectResult spvReflectChangeInputVariableLocation(SpvReflectShaderModule* p_module, |
| 5365 |
const SpvReflectInterfaceVariable* p_input_variable, uint32_t new_location) { |
| 5366 |
if (IsNull(p_module)) { |
| 5367 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5368 |
} |
| 5369 |
if (IsNull(p_input_variable)) { |
| 5370 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5371 |
} |
| 5372 |
for (uint32_t index = 0; index < p_module->input_variable_count; ++index) { |
| 5373 |
if (p_module->input_variables[index] == p_input_variable) { |
| 5374 |
return ChangeVariableLocation(p_module, p_module->input_variables[index], new_location); |
| 5375 |
} |
| 5376 |
} |
| 5377 |
return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5378 |
} |
| 5379 |
|
| 5380 |
SpvReflectResult spvReflectChangeOutputVariableLocation(SpvReflectShaderModule* p_module, |
| 5381 |
const SpvReflectInterfaceVariable* p_output_variable, |
| 5382 |
uint32_t new_location) { |
| 5383 |
if (IsNull(p_module)) { |
| 5384 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5385 |
} |
| 5386 |
if (IsNull(p_output_variable)) { |
| 5387 |
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER; |
| 5388 |
} |
| 5389 |
for (uint32_t index = 0; index < p_module->output_variable_count; ++index) { |
| 5390 |
if (p_module->output_variables[index] == p_output_variable) { |
| 5391 |
return ChangeVariableLocation(p_module, p_module->output_variables[index], new_location); |
| 5392 |
} |
| 5393 |
} |
| 5394 |
return SPV_REFLECT_RESULT_ERROR_ELEMENT_NOT_FOUND; |
| 5395 |
} |
| 5396 |
|
| 5397 |
const char* spvReflectSourceLanguage(SpvSourceLanguage source_lang) { |
| 5398 |
switch (source_lang) { |
| 5399 |
case SpvSourceLanguageESSL: |
| 5400 |
return "ESSL"; |
| 5401 |
case SpvSourceLanguageGLSL: |
| 5402 |
return "GLSL"; |
| 5403 |
case SpvSourceLanguageOpenCL_C: |
| 5404 |
return "OpenCL_C"; |
| 5405 |
case SpvSourceLanguageOpenCL_CPP: |
| 5406 |
return "OpenCL_CPP"; |
| 5407 |
case SpvSourceLanguageHLSL: |
| 5408 |
return "HLSL"; |
| 5409 |
case SpvSourceLanguageCPP_for_OpenCL: |
| 5410 |
return "CPP_for_OpenCL"; |
| 5411 |
case SpvSourceLanguageSYCL: |
| 5412 |
return "SYCL"; |
| 5413 |
case SpvSourceLanguageHERO_C: |
| 5414 |
return "Hero C"; |
| 5415 |
case SpvSourceLanguageNZSL: |
| 5416 |
return "NZSL"; |
| 5417 |
default: |
| 5418 |
break; |
| 5419 |
} |
| 5420 |
|
| 5421 |
|
| 5422 |
return "Unknown"; |
| 5423 |
} |
| 5424 |
|
| 5425 |
const char* spvReflectBlockVariableTypeName(const SpvReflectBlockVariable* p_var) { |
| 5426 |
if (p_var == NULL) { |
| 5427 |
return NULL; |
| 5428 |
} |
| 5429 |
return p_var->type_description->type_name; |
| 5430 |
} |