Newer
Older
/*
* Copyright 2017 Józef Kucia for CodeWeavers
* Copyright 2021 Conor McCarthy for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "vkd3d_shader_private.h"
#include <stdarg.h>
#ifdef HAVE_SPIRV_UNIFIED1_SPIRV_H
# include "spirv/unified1/spirv.h"
#else
# include "vulkan/spirv.h"
#endif /* HAVE_SPIRV_UNIFIED1_SPIRV_H */
#ifdef HAVE_SPIRV_UNIFIED1_GLSL_STD_450_H
# include "spirv/unified1/GLSL.std.450.h"
#else
# include "vulkan/GLSL.std.450.h"
#endif /* HAVE_SPIRV_UNIFIED1_GLSL_STD_450_H */
Józef Kucia
committed
#ifdef HAVE_SPIRV_TOOLS
# include "spirv-tools/libspirv.h"
Józef Kucia
committed
Henri Verbeet
committed
static spv_target_env spv_target_env_from_vkd3d(enum vkd3d_shader_spirv_environment environment)
Henri Verbeet
committed
switch (environment)
Henri Verbeet
committed
case VKD3D_SHADER_SPIRV_ENVIRONMENT_OPENGL_4_5:
return SPV_ENV_OPENGL_4_5;
Henri Verbeet
committed
case VKD3D_SHADER_SPIRV_ENVIRONMENT_VULKAN_1_0:
return SPV_ENV_VULKAN_1_0;
default:
Henri Verbeet
committed
ERR("Invalid environment %#x.\n", environment);
return SPV_ENV_VULKAN_1_0;
}
}
static uint32_t get_binary_to_text_options(enum vkd3d_shader_compile_option_formatting_flags formatting)
{
uint32_t out = 0;
unsigned int i;
static const struct
{
enum vkd3d_shader_compile_option_formatting_flags vkd3d;
uint32_t spv;
bool invert;
}
valuemap[] =
{
{VKD3D_SHADER_COMPILE_OPTION_FORMATTING_COLOUR, SPV_BINARY_TO_TEXT_OPTION_COLOR },
{VKD3D_SHADER_COMPILE_OPTION_FORMATTING_INDENT, SPV_BINARY_TO_TEXT_OPTION_INDENT },
{VKD3D_SHADER_COMPILE_OPTION_FORMATTING_OFFSETS, SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET},
{VKD3D_SHADER_COMPILE_OPTION_FORMATTING_HEADER, SPV_BINARY_TO_TEXT_OPTION_NO_HEADER, true},
{VKD3D_SHADER_COMPILE_OPTION_FORMATTING_RAW_IDS, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES, true},
for (i = 0; i < ARRAY_SIZE(valuemap); ++i)
{
if (valuemap[i].invert == !(formatting & valuemap[i].vkd3d))
out |= valuemap[i].spv;
return out;
}
static enum vkd3d_result vkd3d_spirv_binary_to_text(const struct vkd3d_shader_code *spirv,
Henri Verbeet
committed
enum vkd3d_shader_spirv_environment environment,
enum vkd3d_shader_compile_option_formatting_flags formatting, struct vkd3d_shader_code *out)
Józef Kucia
committed
{
spv_diagnostic diagnostic = NULL;
spv_text text = NULL;
spv_context context;
spv_result_t spvret;
enum vkd3d_result result = VKD3D_OK;
Józef Kucia
committed
Henri Verbeet
committed
context = spvContextCreate(spv_target_env_from_vkd3d(environment));
Józef Kucia
committed
if (!(spvret = spvBinaryToText(context, spirv->code, spirv->size / sizeof(uint32_t),
Henri Verbeet
committed
get_binary_to_text_options(formatting), &text, &diagnostic)))
Józef Kucia
committed
{
void *code = vkd3d_malloc(text->length);
if (code)
Józef Kucia
committed
{
memcpy(code, text->str, text->length);
out->size = text->length;
out->code = code;
Józef Kucia
committed
}
else
result = VKD3D_ERROR_OUT_OF_MEMORY;
Józef Kucia
committed
}
else
{
FIXME("Failed to convert SPIR-V binary to text, ret %d.\n", spvret);
Józef Kucia
committed
FIXME("Diagnostic message: %s.\n", debugstr_a(diagnostic->error));
result = VKD3D_ERROR;
Józef Kucia
committed
}
spvTextDestroy(text);
spvDiagnosticDestroy(diagnostic);
spvContextDestroy(context);
return result;
}
static void vkd3d_spirv_dump(const struct vkd3d_shader_code *spirv,
enum vkd3d_shader_spirv_environment environment)
{
Henri Verbeet
committed
static const enum vkd3d_shader_compile_option_formatting_flags formatting
= VKD3D_SHADER_COMPILE_OPTION_FORMATTING_INDENT | VKD3D_SHADER_COMPILE_OPTION_FORMATTING_HEADER;
struct vkd3d_shader_code text;
Henri Verbeet
committed
if (!vkd3d_spirv_binary_to_text(spirv, environment, formatting, &text))
Elizabeth Figura
committed
vkd3d_shader_trace_text(text.code, text.size);
vkd3d_shader_free_shader_code(&text);
}
Józef Kucia
committed
}
Giovanni Mascellani
committed
static bool vkd3d_spirv_validate(struct vkd3d_string_buffer *buffer, const struct vkd3d_shader_code *spirv,
Henri Verbeet
committed
enum vkd3d_shader_spirv_environment environment)
Józef Kucia
committed
{
spv_diagnostic diagnostic = NULL;
spv_context context;
spv_result_t ret;
Henri Verbeet
committed
context = spvContextCreate(spv_target_env_from_vkd3d(environment));
Józef Kucia
committed
if ((ret = spvValidateBinary(context, spirv->code, spirv->size / sizeof(uint32_t),
&diagnostic)))
{
Giovanni Mascellani
committed
vkd3d_string_buffer_printf(buffer, "%s", diagnostic->error);
Józef Kucia
committed
}
spvDiagnosticDestroy(diagnostic);
spvContextDestroy(context);
Giovanni Mascellani
committed
return !ret;
Józef Kucia
committed
}
#else
static enum vkd3d_result vkd3d_spirv_binary_to_text(const struct vkd3d_shader_code *spirv,
Henri Verbeet
committed
enum vkd3d_shader_spirv_environment environment,
enum vkd3d_shader_compile_option_formatting_flags formatting, struct vkd3d_shader_code *out)
{
return VKD3D_ERROR;
}
Henri Verbeet
committed
static void vkd3d_spirv_dump(const struct vkd3d_shader_code *spirv,
enum vkd3d_shader_spirv_environment environment) {}
Giovanni Mascellani
committed
static bool vkd3d_spirv_validate(struct vkd3d_string_buffer *buffer, const struct vkd3d_shader_code *spirv,
enum vkd3d_shader_spirv_environment environment)
{
return true;
}
Józef Kucia
committed
Conor McCarthy
committed
enum vkd3d_shader_input_sysval_semantic vkd3d_siv_from_sysval_indexed(enum vkd3d_shader_sysval_semantic sysval,
unsigned int index)
{
switch (sysval)
{
case VKD3D_SHADER_SV_COVERAGE:
case VKD3D_SHADER_SV_DEPTH:
case VKD3D_SHADER_SV_DEPTH_GREATER_EQUAL:
case VKD3D_SHADER_SV_DEPTH_LESS_EQUAL:
Henri Verbeet
committed
case VKD3D_SHADER_SV_NONE:
case VKD3D_SHADER_SV_STENCIL_REF:
case VKD3D_SHADER_SV_TARGET:
return VKD3D_SIV_NONE;
Henri Verbeet
committed
case VKD3D_SHADER_SV_POSITION:
return VKD3D_SIV_POSITION;
Henri Verbeet
committed
case VKD3D_SHADER_SV_CLIP_DISTANCE:
Henri Verbeet
committed
return VKD3D_SIV_CLIP_DISTANCE;
Henri Verbeet
committed
case VKD3D_SHADER_SV_CULL_DISTANCE:
Henri Verbeet
committed
return VKD3D_SIV_CULL_DISTANCE;
case VKD3D_SHADER_SV_INSTANCE_ID:
return VKD3D_SIV_INSTANCE_ID;
case VKD3D_SHADER_SV_IS_FRONT_FACE:
return VKD3D_SIV_IS_FRONT_FACE;
case VKD3D_SHADER_SV_PRIMITIVE_ID:
return VKD3D_SIV_PRIMITIVE_ID;
case VKD3D_SHADER_SV_RENDER_TARGET_ARRAY_INDEX:
return VKD3D_SIV_RENDER_TARGET_ARRAY_INDEX;
case VKD3D_SHADER_SV_SAMPLE_INDEX:
return VKD3D_SIV_SAMPLE_INDEX;
Henri Verbeet
committed
case VKD3D_SHADER_SV_TESS_FACTOR_QUADEDGE:
return VKD3D_SIV_QUAD_U0_TESS_FACTOR + index;
Henri Verbeet
committed
case VKD3D_SHADER_SV_TESS_FACTOR_QUADINT:
return VKD3D_SIV_QUAD_U_INNER_TESS_FACTOR + index;
Henri Verbeet
committed
case VKD3D_SHADER_SV_TESS_FACTOR_TRIEDGE:
return VKD3D_SIV_TRIANGLE_U_TESS_FACTOR + index;
Henri Verbeet
committed
case VKD3D_SHADER_SV_TESS_FACTOR_TRIINT:
return VKD3D_SIV_TRIANGLE_INNER_TESS_FACTOR;
Henri Verbeet
committed
case VKD3D_SHADER_SV_TESS_FACTOR_LINEDET:
return VKD3D_SIV_LINE_DETAIL_TESS_FACTOR;
Henri Verbeet
committed
case VKD3D_SHADER_SV_TESS_FACTOR_LINEDEN:
return VKD3D_SIV_LINE_DENSITY_TESS_FACTOR;
case VKD3D_SHADER_SV_VERTEX_ID:
return VKD3D_SIV_VERTEX_ID;
case VKD3D_SHADER_SV_VIEWPORT_ARRAY_INDEX:
return VKD3D_SIV_VIEWPORT_ARRAY_INDEX;
FIXME("Unhandled sysval %#x, index %u.\n", sysval, index);
return VKD3D_SIV_NONE;
}
}
static inline bool register_is_undef(const struct vkd3d_shader_register *reg)
{
return reg->type == VKD3DSPR_UNDEF;
}
static inline bool register_is_constant_or_undef(const struct vkd3d_shader_register *reg)
{
return register_is_constant(reg) || register_is_undef(reg);
}
#define VKD3D_SPIRV_VERSION 0x00010000
#define VKD3D_SPIRV_GENERATOR_ID 18
#define VKD3D_SPIRV_GENERATOR_MAGIC vkd3d_make_u32(VKD3D_SPIRV_GENERATOR_VERSION, VKD3D_SPIRV_GENERATOR_ID)
struct vkd3d_spirv_stream
{
uint32_t *words;
size_t capacity;
size_t word_count;
struct list inserted_chunks;
};
static void vkd3d_spirv_stream_init(struct vkd3d_spirv_stream *stream)
{
stream->capacity = 256;
if (!(stream->words = vkd3d_calloc(stream->capacity, sizeof(*stream->words))))
stream->capacity = 0;
stream->word_count = 0;
list_init(&stream->inserted_chunks);
}
struct vkd3d_spirv_chunk
{
struct list entry;
size_t location;
size_t word_count;
uint32_t words[];
};
static void vkd3d_spirv_stream_clear(struct vkd3d_spirv_stream *stream)
{
struct vkd3d_spirv_chunk *c1, *c2;
stream->word_count = 0;
LIST_FOR_EACH_ENTRY_SAFE(c1, c2, &stream->inserted_chunks, struct vkd3d_spirv_chunk, entry)
vkd3d_free(c1);
list_init(&stream->inserted_chunks);
}
static void vkd3d_spirv_stream_free(struct vkd3d_spirv_stream *stream)
{
vkd3d_free(stream->words);
vkd3d_spirv_stream_clear(stream);
static size_t vkd3d_spirv_stream_current_location(struct vkd3d_spirv_stream *stream)
return stream->word_count;
}
static void vkd3d_spirv_stream_insert(struct vkd3d_spirv_stream *stream,
size_t location, const uint32_t *words, unsigned int word_count)
{
struct vkd3d_spirv_chunk *chunk, *current;
if (!(chunk = vkd3d_malloc(offsetof(struct vkd3d_spirv_chunk, words[word_count]))))
return;
chunk->location = location;
chunk->word_count = word_count;
memcpy(chunk->words, words, word_count * sizeof(*words));
LIST_FOR_EACH_ENTRY(current, &stream->inserted_chunks, struct vkd3d_spirv_chunk, entry)
{
if (current->location > location)
{
list_add_before(¤t->entry, &chunk->entry);
return;
}
}
list_add_tail(&stream->inserted_chunks, &chunk->entry);
static bool vkd3d_spirv_stream_append(struct vkd3d_spirv_stream *dst_stream,
const struct vkd3d_spirv_stream *src_stream)
{
size_t word_count, src_word_count = src_stream->word_count;
struct vkd3d_spirv_chunk *chunk;
size_t src_location = 0;
assert(list_empty(&dst_stream->inserted_chunks));
LIST_FOR_EACH_ENTRY(chunk, &src_stream->inserted_chunks, struct vkd3d_spirv_chunk, entry)
src_word_count += chunk->word_count;
if (!vkd3d_array_reserve((void **)&dst_stream->words, &dst_stream->capacity,
dst_stream->word_count + src_word_count, sizeof(*dst_stream->words)))
assert(dst_stream->word_count + src_word_count <= dst_stream->capacity);
LIST_FOR_EACH_ENTRY(chunk, &src_stream->inserted_chunks, struct vkd3d_spirv_chunk, entry)
{
assert(src_location <= chunk->location);
word_count = chunk->location - src_location;
memcpy(&dst_stream->words[dst_stream->word_count], &src_stream->words[src_location],
word_count * sizeof(*src_stream->words));
dst_stream->word_count += word_count;
src_location += word_count;
assert(src_location == chunk->location);
memcpy(&dst_stream->words[dst_stream->word_count], chunk->words,
chunk->word_count * sizeof(*chunk->words));
dst_stream->word_count += chunk->word_count;
}
word_count = src_stream->word_count - src_location;
memcpy(&dst_stream->words[dst_stream->word_count], &src_stream->words[src_location],
word_count * sizeof(*src_stream->words));
dst_stream->word_count += word_count;
struct vkd3d_spirv_builder
{
uint64_t capability_mask;
SpvCapability *capabilities;
size_t capabilities_size;
size_t capabilities_count;
uint32_t ext_instr_set_glsl_450;
uint32_t invocation_count;
SpvExecutionModel execution_model;
uint32_t current_id;
struct rb_tree declarations;
Józef Kucia
committed
uint32_t type_sampler_id;
Józef Kucia
committed
uint32_t type_bool_id;
uint32_t type_void_id;
struct vkd3d_spirv_stream debug_stream; /* debug instructions */
struct vkd3d_spirv_stream annotation_stream; /* decoration instructions */
struct vkd3d_spirv_stream global_stream; /* types, constants, global variables */
struct vkd3d_spirv_stream function_stream; /* function definitions */
struct vkd3d_spirv_stream execution_mode_stream; /* execution mode instructions */
struct vkd3d_spirv_stream original_function_stream;
struct vkd3d_spirv_stream insertion_stream;
size_t insertion_location;
Józef Kucia
committed
size_t main_function_location;
/* entry point interface */
uint32_t *iface;
size_t iface_capacity;
size_t iface_element_count;
static uint32_t vkd3d_spirv_alloc_id(struct vkd3d_spirv_builder *builder)
{
return builder->current_id++;
}
static bool vkd3d_spirv_capability_is_enabled(struct vkd3d_spirv_builder *builder,
SpvCapability cap)
{
size_t i;
if (cap < sizeof(builder->capability_mask) * CHAR_BIT)
return (builder->capability_mask >> cap) & 1;
for (i = 0; i < builder->capabilities_count; ++i)
if (builder->capabilities[i] == cap)
return true;
return false;
}
static void vkd3d_spirv_enable_capability(struct vkd3d_spirv_builder *builder,
SpvCapability cap)
{
if (cap < sizeof(builder->capability_mask) * CHAR_BIT)
{
builder->capability_mask |= 1ull << cap;
return;
if (vkd3d_spirv_capability_is_enabled(builder, cap))
return;
vkd3d_array_reserve((void **)&builder->capabilities, &builder->capabilities_size,
builder->capabilities_count + 1, sizeof(*builder->capabilities));
builder->capabilities[builder->capabilities_count++] = cap;
static uint32_t vkd3d_spirv_get_glsl_std450_instr_set(struct vkd3d_spirv_builder *builder)
{
if (!builder->ext_instr_set_glsl_450)
builder->ext_instr_set_glsl_450 = vkd3d_spirv_alloc_id(builder);
return builder->ext_instr_set_glsl_450;
}
static void vkd3d_spirv_add_iface_variable(struct vkd3d_spirv_builder *builder,
uint32_t id)
{
if (!vkd3d_array_reserve((void **)&builder->iface, &builder->iface_capacity,
builder->iface_element_count + 1, sizeof(*builder->iface)))
return;
builder->iface[builder->iface_element_count++] = id;
}
static void vkd3d_spirv_set_execution_model(struct vkd3d_spirv_builder *builder,
SpvExecutionModel model)
{
builder->execution_model = model;
switch (model)
{
case SpvExecutionModelVertex:
case SpvExecutionModelFragment:
case SpvExecutionModelGLCompute:
vkd3d_spirv_enable_capability(builder, SpvCapabilityShader);
break;
case SpvExecutionModelTessellationControl:
case SpvExecutionModelTessellationEvaluation:
vkd3d_spirv_enable_capability(builder, SpvCapabilityTessellation);
break;
case SpvExecutionModelGeometry:
vkd3d_spirv_enable_capability(builder, SpvCapabilityGeometry);
break;
default:
ERR("Unhandled execution model %#x.\n", model);
}
}
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
static uint32_t vkd3d_spirv_opcode_word(SpvOp op, unsigned int word_count)
{
assert(!(op & ~SpvOpCodeMask));
return (word_count << SpvWordCountShift) | op;
}
static void vkd3d_spirv_build_word(struct vkd3d_spirv_stream *stream, uint32_t word)
{
if (!vkd3d_array_reserve((void **)&stream->words, &stream->capacity,
stream->word_count + 1, sizeof(*stream->words)))
return;
stream->words[stream->word_count++] = word;
}
static unsigned int vkd3d_spirv_string_word_count(const char *str)
{
return align(strlen(str) + 1, sizeof(uint32_t)) / sizeof(uint32_t);
}
static void vkd3d_spirv_build_string(struct vkd3d_spirv_stream *stream,
const char *str, unsigned int word_count)
{
unsigned int word_idx, i;
const char *ptr = str;
for (word_idx = 0; word_idx < word_count; ++word_idx)
{
uint32_t word = 0;
for (i = 0; i < sizeof(uint32_t) && *ptr; ++i)
word |= (uint32_t)*ptr++ << (8 * i);
vkd3d_spirv_build_word(stream, word);
}
}
typedef uint32_t (*vkd3d_spirv_build_pfn)(struct vkd3d_spirv_builder *builder);
typedef uint32_t (*vkd3d_spirv_build1_pfn)(struct vkd3d_spirv_builder *builder,
uint32_t operand0);
typedef uint32_t (*vkd3d_spirv_build1v_pfn)(struct vkd3d_spirv_builder *builder,
uint32_t operand0, const uint32_t *operands, unsigned int operand_count);
typedef uint32_t (*vkd3d_spirv_build2_pfn)(struct vkd3d_spirv_builder *builder,
uint32_t operand0, uint32_t operand1);
typedef uint32_t (*vkd3d_spirv_build7_pfn)(struct vkd3d_spirv_builder *builder,
uint32_t operand0, uint32_t operand1, uint32_t operand2, uint32_t operand3,
uint32_t operand4, uint32_t operand5, uint32_t operand6);
Józef Kucia
committed
static uint32_t vkd3d_spirv_build_once(struct vkd3d_spirv_builder *builder,
uint32_t *id, vkd3d_spirv_build_pfn build_pfn)
Józef Kucia
committed
{
if (!(*id))
*id = build_pfn(builder);
return *id;
}
#define MAX_SPIRV_DECLARATION_PARAMETER_COUNT 7
struct vkd3d_spirv_declaration
{
struct rb_entry entry;
SpvOp op;
unsigned int parameter_count;
uint32_t parameters[MAX_SPIRV_DECLARATION_PARAMETER_COUNT];
uint32_t id;
};
static int vkd3d_spirv_declaration_compare(const void *key, const struct rb_entry *e)
{
const struct vkd3d_spirv_declaration *a = key;
const struct vkd3d_spirv_declaration *b = RB_ENTRY_VALUE(e, const struct vkd3d_spirv_declaration, entry);
Henri Verbeet
committed
int ret;
Henri Verbeet
committed
if ((ret = vkd3d_u32_compare(a->op, b->op)))
return ret;
if ((ret = vkd3d_u32_compare(a->parameter_count, b->parameter_count)))
return ret;
assert(a->parameter_count <= ARRAY_SIZE(a->parameters));
return memcmp(&a->parameters, &b->parameters, a->parameter_count * sizeof(*a->parameters));
}
static void vkd3d_spirv_declaration_free(struct rb_entry *entry, void *context)
{
struct vkd3d_spirv_declaration *d = RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry);
vkd3d_free(d);
}
static void vkd3d_spirv_insert_declaration(struct vkd3d_spirv_builder *builder,
const struct vkd3d_spirv_declaration *declaration)
{
struct vkd3d_spirv_declaration *d;
assert(declaration->parameter_count <= ARRAY_SIZE(declaration->parameters));
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
if (!(d = vkd3d_malloc(sizeof(*d))))
return;
memcpy(d, declaration, sizeof(*d));
if (rb_put(&builder->declarations, d, &d->entry) == -1)
{
ERR("Failed to insert declaration entry.\n");
vkd3d_free(d);
}
}
static uint32_t vkd3d_spirv_build_once1(struct vkd3d_spirv_builder *builder,
SpvOp op, uint32_t operand0, vkd3d_spirv_build1_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
struct rb_entry *entry;
declaration.op = op;
declaration.parameter_count = 1;
declaration.parameters[0] = operand0;
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operand0);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
static uint32_t vkd3d_spirv_build_once1v(struct vkd3d_spirv_builder *builder,
SpvOp op, uint32_t operand0, const uint32_t *operands, unsigned int operand_count,
vkd3d_spirv_build1v_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
unsigned int i, param_idx = 0;
struct rb_entry *entry;
if (operand_count >= ARRAY_SIZE(declaration.parameters))
{
WARN("Unsupported parameter count %u (opcode %#x).\n", operand_count + 1, op);
return build_pfn(builder, operand0, operands, operand_count);
}
declaration.op = op;
declaration.parameters[param_idx++] = operand0;
for (i = 0; i < operand_count; ++i)
declaration.parameters[param_idx++] = operands[i];
declaration.parameter_count = param_idx;
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operand0, operands, operand_count);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
static uint32_t vkd3d_spirv_build_once2(struct vkd3d_spirv_builder *builder,
SpvOp op, uint32_t operand0, uint32_t operand1, vkd3d_spirv_build2_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
struct rb_entry *entry;
declaration.op = op;
declaration.parameter_count = 2;
declaration.parameters[0] = operand0;
declaration.parameters[1] = operand1;
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operand0, operand1);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
static uint32_t vkd3d_spirv_build_once7(struct vkd3d_spirv_builder *builder,
SpvOp op, const uint32_t *operands, vkd3d_spirv_build7_pfn build_pfn)
{
struct vkd3d_spirv_declaration declaration;
struct rb_entry *entry;
declaration.op = op;
declaration.parameter_count = 7;
memcpy(&declaration.parameters, operands, declaration.parameter_count * sizeof(*operands));
if ((entry = rb_get(&builder->declarations, &declaration)))
return RB_ENTRY_VALUE(entry, struct vkd3d_spirv_declaration, entry)->id;
declaration.id = build_pfn(builder, operands[0], operands[1], operands[2],
operands[3], operands[4], operands[5], operands[6]);
vkd3d_spirv_insert_declaration(builder, &declaration);
return declaration.id;
}
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
* vkd3d_spirv_build_op[1-3][v]()
* vkd3d_spirv_build_op_[t][r][1-3][v]()
*
* t - result type
* r - result id
* 1-3 - the number of operands
* v - variable number of operands
*/
static void vkd3d_spirv_build_op(struct vkd3d_spirv_stream *stream, SpvOp op)
{
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 1));
}
static void vkd3d_spirv_build_op1(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand)
{
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 2));
vkd3d_spirv_build_word(stream, operand);
}
static void vkd3d_spirv_build_op1v(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, const uint32_t *operands, unsigned int operand_count)
{
unsigned int i;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 2 + operand_count));
vkd3d_spirv_build_word(stream, operand0);
for (i = 0; i < operand_count; ++i)
vkd3d_spirv_build_word(stream, operands[i]);
}
static void vkd3d_spirv_build_op2v(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, uint32_t operand1,
const uint32_t *operands, unsigned int operand_count)
{
unsigned int i;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 3 + operand_count));
vkd3d_spirv_build_word(stream, operand0);
vkd3d_spirv_build_word(stream, operand1);
for (i = 0; i < operand_count; ++i)
vkd3d_spirv_build_word(stream, operands[i]);
}
static void vkd3d_spirv_build_op3v(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, uint32_t operand1, uint32_t operand2,
const uint32_t *operands, unsigned int operand_count)
{
unsigned int i;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 4 + operand_count));
vkd3d_spirv_build_word(stream, operand0);
vkd3d_spirv_build_word(stream, operand1);
vkd3d_spirv_build_word(stream, operand2);
for (i = 0; i < operand_count; ++i)
vkd3d_spirv_build_word(stream, operands[i]);
}
static void vkd3d_spirv_build_op2(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, uint32_t operand1)
{
return vkd3d_spirv_build_op2v(stream, op, operand0, operand1, NULL, 0);
}
static void vkd3d_spirv_build_op3(struct vkd3d_spirv_stream *stream,
SpvOp op, uint32_t operand0, uint32_t operand1, uint32_t operand2)
{
return vkd3d_spirv_build_op2v(stream, op, operand0, operand1, &operand2, 1);
}
static uint32_t vkd3d_spirv_build_op_rv(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op,
const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
vkd3d_spirv_build_op1v(stream, op, result_id, operands, operand_count);
return result_id;
}
static uint32_t vkd3d_spirv_build_op_r(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op)
{
return vkd3d_spirv_build_op_rv(builder, stream, op, NULL, 0);
}
static uint32_t vkd3d_spirv_build_op_r1(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t operand0)
{
return vkd3d_spirv_build_op_rv(builder, stream, op, &operand0, 1);
}
static uint32_t vkd3d_spirv_build_op_r2(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t operand0, uint32_t operand1)
{
uint32_t operands[] = {operand0, operand1};
return vkd3d_spirv_build_op_rv(builder, stream, op, operands, ARRAY_SIZE(operands));
}
static uint32_t vkd3d_spirv_build_op_r1v(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t operand0,
const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
vkd3d_spirv_build_op2v(stream, op, result_id, operand0, operands, operand_count);
return result_id;
}
static uint32_t vkd3d_spirv_build_op_trv(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
vkd3d_spirv_build_op2v(stream, op, result_type, result_id, operands, operand_count);
return result_id;
}
static uint32_t vkd3d_spirv_build_op_tr(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type)
{
return vkd3d_spirv_build_op_trv(builder, stream, op, result_type, NULL, 0);
}
static uint32_t vkd3d_spirv_build_op_tr1(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0)
{
return vkd3d_spirv_build_op_trv(builder, stream, op, result_type, &operand0, 1);
}
static uint32_t vkd3d_spirv_build_op_tr2(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0, uint32_t operand1)
{
uint32_t operands[] = {operand0, operand1};
return vkd3d_spirv_build_op_trv(builder, stream, op, result_type,
operands, ARRAY_SIZE(operands));
}
static uint32_t vkd3d_spirv_build_op_tr3(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0, uint32_t operand1, uint32_t operand2)
{
uint32_t operands[] = {operand0, operand1, operand2};
return vkd3d_spirv_build_op_trv(builder, stream, op, result_type,
operands, ARRAY_SIZE(operands));
}
static uint32_t vkd3d_spirv_build_op_tr1v(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0, const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
vkd3d_spirv_build_op3v(stream, op, result_type, result_id, operand0, operands, operand_count);
return result_id;
}
static uint32_t vkd3d_spirv_build_op_tr2v(struct vkd3d_spirv_builder *builder,
struct vkd3d_spirv_stream *stream, SpvOp op, uint32_t result_type,
uint32_t operand0, uint32_t operand1, const uint32_t *operands, unsigned int operand_count)
{
uint32_t result_id = vkd3d_spirv_alloc_id(builder);
unsigned int i;
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 5 + operand_count));
vkd3d_spirv_build_word(stream, result_type);
vkd3d_spirv_build_word(stream, result_id);
vkd3d_spirv_build_word(stream, operand0);
vkd3d_spirv_build_word(stream, operand1);
for (i = 0; i < operand_count; ++i)
vkd3d_spirv_build_word(stream, operands[i]);
return result_id;
}
static void vkd3d_spirv_begin_function_stream_insertion(struct vkd3d_spirv_builder *builder,
size_t location)
{
assert(builder->insertion_location == ~(size_t)0);
Józef Kucia
committed
if (vkd3d_spirv_stream_current_location(&builder->function_stream) == location)
return;
builder->original_function_stream = builder->function_stream;
builder->function_stream = builder->insertion_stream;
builder->insertion_location = location;
}
static void vkd3d_spirv_end_function_stream_insertion(struct vkd3d_spirv_builder *builder)
{
struct vkd3d_spirv_stream *insertion_stream = &builder->insertion_stream;
Józef Kucia
committed
if (builder->insertion_location == ~(size_t)0)
return;
builder->insertion_stream = builder->function_stream;
builder->function_stream = builder->original_function_stream;
vkd3d_spirv_stream_insert(&builder->function_stream, builder->insertion_location,
insertion_stream->words, insertion_stream->word_count);
vkd3d_spirv_stream_clear(insertion_stream);
builder->insertion_location = ~(size_t)0;
}
struct vkd3d_spirv_op_branch_conditional
{
uint32_t opcode;
uint32_t condition_id;
uint32_t true_label;
uint32_t false_label;
};
static struct vkd3d_spirv_op_branch_conditional *vkd3d_spirv_as_op_branch_conditional(
struct vkd3d_spirv_stream *stream, size_t location)
{
return (struct vkd3d_spirv_op_branch_conditional *)&stream->words[location];
}
static void vkd3d_spirv_build_op_capability(struct vkd3d_spirv_stream *stream,
SpvCapability cap)
{
vkd3d_spirv_build_op1(stream, SpvOpCapability, cap);
}
static void vkd3d_spirv_build_op_extension(struct vkd3d_spirv_stream *stream,
const char *name)
{
unsigned int name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpExtension, 1 + name_size));
vkd3d_spirv_build_string(stream, name, name_size);
}
static void vkd3d_spirv_build_op_ext_inst_import(struct vkd3d_spirv_stream *stream,
uint32_t result_id, const char *name)
{
unsigned int name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpExtInstImport, 2 + name_size));
vkd3d_spirv_build_word(stream, result_id);
vkd3d_spirv_build_string(stream, name, name_size);
}
static uint32_t vkd3d_spirv_build_op_ext_inst(struct vkd3d_spirv_builder *builder,
uint32_t result_type, uint32_t inst_set, uint32_t inst_number,
uint32_t *operands, unsigned int operand_count)
{
return vkd3d_spirv_build_op_tr2v(builder, &builder->function_stream,
SpvOpExtInst, result_type, inst_set, inst_number, operands, operand_count);
}
static void vkd3d_spirv_build_op_memory_model(struct vkd3d_spirv_stream *stream,
SpvAddressingModel addressing_model, SpvMemoryModel memory_model)
{
vkd3d_spirv_build_op2(stream, SpvOpMemoryModel, addressing_model, memory_model);
}
static void vkd3d_spirv_build_op_entry_point(struct vkd3d_spirv_stream *stream,
SpvExecutionModel model, uint32_t function_id, const char *name,
uint32_t *interface_list, unsigned int interface_size)
{
unsigned int i, name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpEntryPoint, 3 + name_size + interface_size));
vkd3d_spirv_build_word(stream, model);
vkd3d_spirv_build_word(stream, function_id);
vkd3d_spirv_build_string(stream, name, name_size);
for (i = 0; i < interface_size; ++i)
vkd3d_spirv_build_word(stream, interface_list[i]);
}
static void vkd3d_spirv_build_op_execution_mode(struct vkd3d_spirv_stream *stream,
uint32_t entry_point, SpvExecutionMode mode, const uint32_t *literals, unsigned int literal_count)
{
vkd3d_spirv_build_op2v(stream, SpvOpExecutionMode, entry_point, mode, literals, literal_count);
}
static void vkd3d_spirv_build_op_name(struct vkd3d_spirv_builder *builder,
uint32_t id, const char *fmt, ...)
{
struct vkd3d_spirv_stream *stream = &builder->debug_stream;
unsigned int name_size;
char name[1024];
va_list args;
va_start(args, fmt);
vsnprintf(name, ARRAY_SIZE(name), fmt, args);
name[ARRAY_SIZE(name) - 1] = '\0';
va_end(args);
name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpName, 2 + name_size));
vkd3d_spirv_build_word(stream, id);
vkd3d_spirv_build_string(stream, name, name_size);
}
static void vkd3d_spirv_build_op_member_name(struct vkd3d_spirv_builder *builder,
uint32_t type_id, uint32_t member, const char *fmt, ...)
{
struct vkd3d_spirv_stream *stream = &builder->debug_stream;
unsigned int name_size;
char name[1024];
va_list args;
va_start(args, fmt);
vsnprintf(name, ARRAY_SIZE(name), fmt, args);
name[ARRAY_SIZE(name) - 1] = '\0';
va_end(args);
name_size = vkd3d_spirv_string_word_count(name);
vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpMemberName, 3 + name_size));
vkd3d_spirv_build_word(stream, type_id);
vkd3d_spirv_build_word(stream, member);
vkd3d_spirv_build_string(stream, name, name_size);
}
static void vkd3d_spirv_build_op_decorate(struct vkd3d_spirv_builder *builder,
uint32_t target_id, SpvDecoration decoration,
uint32_t *literals, uint32_t literal_count)
{
vkd3d_spirv_build_op2v(&builder->annotation_stream,
SpvOpDecorate, target_id, decoration, literals, literal_count);
}
static void vkd3d_spirv_build_op_decorate1(struct vkd3d_spirv_builder *builder,
uint32_t target_id, SpvDecoration decoration, uint32_t operand0)
{
return vkd3d_spirv_build_op_decorate(builder, target_id, decoration, &operand0, 1);
}
static void vkd3d_spirv_build_op_member_decorate(struct vkd3d_spirv_builder *builder,
uint32_t structure_type_id, uint32_t member_idx, SpvDecoration decoration,
uint32_t *literals, uint32_t literal_count)
{
vkd3d_spirv_build_op3v(&builder->annotation_stream, SpvOpMemberDecorate,
structure_type_id, member_idx, decoration, literals, literal_count);
}
static void vkd3d_spirv_build_op_member_decorate1(struct vkd3d_spirv_builder *builder,
uint32_t structure_type_id, uint32_t member_idx, SpvDecoration decoration, uint32_t operand0)
{
vkd3d_spirv_build_op_member_decorate(builder, structure_type_id, member_idx, decoration, &operand0, 1);
}
static uint32_t vkd3d_spirv_build_op_type_void(struct vkd3d_spirv_builder *builder)
{
return vkd3d_spirv_build_op_r(builder, &builder->global_stream, SpvOpTypeVoid);
}
Józef Kucia
committed
static uint32_t vkd3d_spirv_get_op_type_void(struct vkd3d_spirv_builder *builder)