summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/backend/spirv/spirv_emit_context.cpp')
-rw-r--r--src/shader_recompiler/backend/spirv/spirv_emit_context.cpp1585
1 files changed, 1585 insertions, 0 deletions
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
new file mode 100644
index 000000000..723455462
--- /dev/null
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
@@ -0,0 +1,1585 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <array>
7#include <climits>
8#include <string_view>
9
10#include <boost/container/static_vector.hpp>
11
12#include <fmt/format.h>
13
14#include "common/common_types.h"
15#include "common/div_ceil.h"
16#include "shader_recompiler/backend/spirv/emit_context.h"
17#include "shader_recompiler/backend/spirv/emit_spirv.h"
18
19namespace Shader::Backend::SPIRV {
20namespace {
21constexpr size_t NUM_FIXEDFNCTEXTURE = 10;
22
23enum class Operation {
24 Increment,
25 Decrement,
26 FPAdd,
27 FPMin,
28 FPMax,
29};
30
31struct AttrInfo {
32 Id pointer;
33 Id id;
34 bool needs_cast;
35};
36
37Id ImageType(EmitContext& ctx, const TextureDescriptor& desc) {
38 const spv::ImageFormat format{spv::ImageFormat::Unknown};
39 const Id type{ctx.F32[1]};
40 const bool depth{desc.is_depth};
41 switch (desc.type) {
42 case TextureType::Color1D:
43 return ctx.TypeImage(type, spv::Dim::Dim1D, depth, false, false, 1, format);
44 case TextureType::ColorArray1D:
45 return ctx.TypeImage(type, spv::Dim::Dim1D, depth, true, false, 1, format);
46 case TextureType::Color2D:
47 return ctx.TypeImage(type, spv::Dim::Dim2D, depth, false, false, 1, format);
48 case TextureType::ColorArray2D:
49 return ctx.TypeImage(type, spv::Dim::Dim2D, depth, true, false, 1, format);
50 case TextureType::Color3D:
51 return ctx.TypeImage(type, spv::Dim::Dim3D, depth, false, false, 1, format);
52 case TextureType::ColorCube:
53 return ctx.TypeImage(type, spv::Dim::Cube, depth, false, false, 1, format);
54 case TextureType::ColorArrayCube:
55 return ctx.TypeImage(type, spv::Dim::Cube, depth, true, false, 1, format);
56 case TextureType::Buffer:
57 break;
58 }
59 throw InvalidArgument("Invalid texture type {}", desc.type);
60}
61
62spv::ImageFormat GetImageFormat(ImageFormat format) {
63 switch (format) {
64 case ImageFormat::Typeless:
65 return spv::ImageFormat::Unknown;
66 case ImageFormat::R8_UINT:
67 return spv::ImageFormat::R8ui;
68 case ImageFormat::R8_SINT:
69 return spv::ImageFormat::R8i;
70 case ImageFormat::R16_UINT:
71 return spv::ImageFormat::R16ui;
72 case ImageFormat::R16_SINT:
73 return spv::ImageFormat::R16i;
74 case ImageFormat::R32_UINT:
75 return spv::ImageFormat::R32ui;
76 case ImageFormat::R32G32_UINT:
77 return spv::ImageFormat::Rg32ui;
78 case ImageFormat::R32G32B32A32_UINT:
79 return spv::ImageFormat::Rgba32ui;
80 }
81 throw InvalidArgument("Invalid image format {}", format);
82}
83
84Id ImageType(EmitContext& ctx, const ImageDescriptor& desc) {
85 const spv::ImageFormat format{GetImageFormat(desc.format)};
86 const Id type{ctx.U32[1]};
87 switch (desc.type) {
88 case TextureType::Color1D:
89 return ctx.TypeImage(type, spv::Dim::Dim1D, false, false, false, 2, format);
90 case TextureType::ColorArray1D:
91 return ctx.TypeImage(type, spv::Dim::Dim1D, false, true, false, 2, format);
92 case TextureType::Color2D:
93 return ctx.TypeImage(type, spv::Dim::Dim2D, false, false, false, 2, format);
94 case TextureType::ColorArray2D:
95 return ctx.TypeImage(type, spv::Dim::Dim2D, false, true, false, 2, format);
96 case TextureType::Color3D:
97 return ctx.TypeImage(type, spv::Dim::Dim3D, false, false, false, 2, format);
98 case TextureType::Buffer:
99 throw NotImplementedException("Image buffer");
100 default:
101 break;
102 }
103 throw InvalidArgument("Invalid texture type {}", desc.type);
104}
105
106Id DefineVariable(EmitContext& ctx, Id type, std::optional<spv::BuiltIn> builtin,
107 spv::StorageClass storage_class) {
108 const Id pointer_type{ctx.TypePointer(storage_class, type)};
109 const Id id{ctx.AddGlobalVariable(pointer_type, storage_class)};
110 if (builtin) {
111 ctx.Decorate(id, spv::Decoration::BuiltIn, *builtin);
112 }
113 ctx.interfaces.push_back(id);
114 return id;
115}
116
117u32 NumVertices(InputTopology input_topology) {
118 switch (input_topology) {
119 case InputTopology::Points:
120 return 1;
121 case InputTopology::Lines:
122 return 2;
123 case InputTopology::LinesAdjacency:
124 return 4;
125 case InputTopology::Triangles:
126 return 3;
127 case InputTopology::TrianglesAdjacency:
128 return 6;
129 }
130 throw InvalidArgument("Invalid input topology {}", input_topology);
131}
132
133Id DefineInput(EmitContext& ctx, Id type, bool per_invocation,
134 std::optional<spv::BuiltIn> builtin = std::nullopt) {
135 switch (ctx.stage) {
136 case Stage::TessellationControl:
137 case Stage::TessellationEval:
138 if (per_invocation) {
139 type = ctx.TypeArray(type, ctx.Const(32u));
140 }
141 break;
142 case Stage::Geometry:
143 if (per_invocation) {
144 const u32 num_vertices{NumVertices(ctx.runtime_info.input_topology)};
145 type = ctx.TypeArray(type, ctx.Const(num_vertices));
146 }
147 break;
148 default:
149 break;
150 }
151 return DefineVariable(ctx, type, builtin, spv::StorageClass::Input);
152}
153
154Id DefineOutput(EmitContext& ctx, Id type, std::optional<u32> invocations,
155 std::optional<spv::BuiltIn> builtin = std::nullopt) {
156 if (invocations && ctx.stage == Stage::TessellationControl) {
157 type = ctx.TypeArray(type, ctx.Const(*invocations));
158 }
159 return DefineVariable(ctx, type, builtin, spv::StorageClass::Output);
160}
161
162void DefineGenericOutput(EmitContext& ctx, size_t index, std::optional<u32> invocations) {
163 static constexpr std::string_view swizzle{"xyzw"};
164 const size_t base_attr_index{static_cast<size_t>(IR::Attribute::Generic0X) + index * 4};
165 u32 element{0};
166 while (element < 4) {
167 const u32 remainder{4 - element};
168 const TransformFeedbackVarying* xfb_varying{};
169 if (!ctx.runtime_info.xfb_varyings.empty()) {
170 xfb_varying = &ctx.runtime_info.xfb_varyings[base_attr_index + element];
171 xfb_varying = xfb_varying && xfb_varying->components > 0 ? xfb_varying : nullptr;
172 }
173 const u32 num_components{xfb_varying ? xfb_varying->components : remainder};
174
175 const Id id{DefineOutput(ctx, ctx.F32[num_components], invocations)};
176 ctx.Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
177 if (element > 0) {
178 ctx.Decorate(id, spv::Decoration::Component, element);
179 }
180 if (xfb_varying) {
181 ctx.Decorate(id, spv::Decoration::XfbBuffer, xfb_varying->buffer);
182 ctx.Decorate(id, spv::Decoration::XfbStride, xfb_varying->stride);
183 ctx.Decorate(id, spv::Decoration::Offset, xfb_varying->offset);
184 }
185 if (num_components < 4 || element > 0) {
186 const std::string_view subswizzle{swizzle.substr(element, num_components)};
187 ctx.Name(id, fmt::format("out_attr{}_{}", index, subswizzle));
188 } else {
189 ctx.Name(id, fmt::format("out_attr{}", index));
190 }
191 const GenericElementInfo info{
192 .id = id,
193 .first_element = element,
194 .num_components = num_components,
195 };
196 std::fill_n(ctx.output_generics[index].begin() + element, num_components, info);
197 element += num_components;
198 }
199}
200
201Id GetAttributeType(EmitContext& ctx, AttributeType type) {
202 switch (type) {
203 case AttributeType::Float:
204 return ctx.F32[4];
205 case AttributeType::SignedInt:
206 return ctx.TypeVector(ctx.TypeInt(32, true), 4);
207 case AttributeType::UnsignedInt:
208 return ctx.U32[4];
209 case AttributeType::Disabled:
210 break;
211 }
212 throw InvalidArgument("Invalid attribute type {}", type);
213}
214
215std::optional<AttrInfo> AttrTypes(EmitContext& ctx, u32 index) {
216 const AttributeType type{ctx.runtime_info.generic_input_types.at(index)};
217 switch (type) {
218 case AttributeType::Float:
219 return AttrInfo{ctx.input_f32, ctx.F32[1], false};
220 case AttributeType::UnsignedInt:
221 return AttrInfo{ctx.input_u32, ctx.U32[1], true};
222 case AttributeType::SignedInt:
223 return AttrInfo{ctx.input_s32, ctx.TypeInt(32, true), true};
224 case AttributeType::Disabled:
225 return std::nullopt;
226 }
227 throw InvalidArgument("Invalid attribute type {}", type);
228}
229
230std::string_view StageName(Stage stage) {
231 switch (stage) {
232 case Stage::VertexA:
233 return "vs_a";
234 case Stage::VertexB:
235 return "vs";
236 case Stage::TessellationControl:
237 return "tcs";
238 case Stage::TessellationEval:
239 return "tes";
240 case Stage::Geometry:
241 return "gs";
242 case Stage::Fragment:
243 return "fs";
244 case Stage::Compute:
245 return "cs";
246 }
247 throw InvalidArgument("Invalid stage {}", stage);
248}
249
250template <typename... Args>
251void Name(EmitContext& ctx, Id object, std::string_view format_str, Args&&... args) {
252 ctx.Name(object, fmt::format(fmt::runtime(format_str), StageName(ctx.stage),
253 std::forward<Args>(args)...)
254 .c_str());
255}
256
257void DefineConstBuffers(EmitContext& ctx, const Info& info, Id UniformDefinitions::*member_type,
258 u32 binding, Id type, char type_char, u32 element_size) {
259 const Id array_type{ctx.TypeArray(type, ctx.Const(65536U / element_size))};
260 ctx.Decorate(array_type, spv::Decoration::ArrayStride, element_size);
261
262 const Id struct_type{ctx.TypeStruct(array_type)};
263 Name(ctx, struct_type, "{}_cbuf_block_{}{}", ctx.stage, type_char, element_size * CHAR_BIT);
264 ctx.Decorate(struct_type, spv::Decoration::Block);
265 ctx.MemberName(struct_type, 0, "data");
266 ctx.MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U);
267
268 const Id struct_pointer_type{ctx.TypePointer(spv::StorageClass::Uniform, struct_type)};
269 const Id uniform_type{ctx.TypePointer(spv::StorageClass::Uniform, type)};
270 ctx.uniform_types.*member_type = uniform_type;
271
272 for (const ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
273 const Id id{ctx.AddGlobalVariable(struct_pointer_type, spv::StorageClass::Uniform)};
274 ctx.Decorate(id, spv::Decoration::Binding, binding);
275 ctx.Decorate(id, spv::Decoration::DescriptorSet, 0U);
276 ctx.Name(id, fmt::format("c{}", desc.index));
277 for (size_t i = 0; i < desc.count; ++i) {
278 ctx.cbufs[desc.index + i].*member_type = id;
279 }
280 if (ctx.profile.supported_spirv >= 0x00010400) {
281 ctx.interfaces.push_back(id);
282 }
283 binding += desc.count;
284 }
285}
286
287void DefineSsbos(EmitContext& ctx, StorageTypeDefinition& type_def,
288 Id StorageDefinitions::*member_type, const Info& info, u32 binding, Id type,
289 u32 stride) {
290 const Id array_type{ctx.TypeRuntimeArray(type)};
291 ctx.Decorate(array_type, spv::Decoration::ArrayStride, stride);
292
293 const Id struct_type{ctx.TypeStruct(array_type)};
294 ctx.Decorate(struct_type, spv::Decoration::Block);
295 ctx.MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U);
296
297 const Id struct_pointer{ctx.TypePointer(spv::StorageClass::StorageBuffer, struct_type)};
298 type_def.array = struct_pointer;
299 type_def.element = ctx.TypePointer(spv::StorageClass::StorageBuffer, type);
300
301 u32 index{};
302 for (const StorageBufferDescriptor& desc : info.storage_buffers_descriptors) {
303 const Id id{ctx.AddGlobalVariable(struct_pointer, spv::StorageClass::StorageBuffer)};
304 ctx.Decorate(id, spv::Decoration::Binding, binding);
305 ctx.Decorate(id, spv::Decoration::DescriptorSet, 0U);
306 ctx.Name(id, fmt::format("ssbo{}", index));
307 if (ctx.profile.supported_spirv >= 0x00010400) {
308 ctx.interfaces.push_back(id);
309 }
310 for (size_t i = 0; i < desc.count; ++i) {
311 ctx.ssbos[index + i].*member_type = id;
312 }
313 index += desc.count;
314 binding += desc.count;
315 }
316}
317
318Id CasFunction(EmitContext& ctx, Operation operation, Id value_type) {
319 const Id func_type{ctx.TypeFunction(value_type, value_type, value_type)};
320 const Id func{ctx.OpFunction(value_type, spv::FunctionControlMask::MaskNone, func_type)};
321 const Id op_a{ctx.OpFunctionParameter(value_type)};
322 const Id op_b{ctx.OpFunctionParameter(value_type)};
323 ctx.AddLabel();
324 Id result{};
325 switch (operation) {
326 case Operation::Increment: {
327 const Id pred{ctx.OpUGreaterThanEqual(ctx.U1, op_a, op_b)};
328 const Id incr{ctx.OpIAdd(value_type, op_a, ctx.Constant(value_type, 1))};
329 result = ctx.OpSelect(value_type, pred, ctx.u32_zero_value, incr);
330 break;
331 }
332 case Operation::Decrement: {
333 const Id lhs{ctx.OpIEqual(ctx.U1, op_a, ctx.Constant(value_type, 0u))};
334 const Id rhs{ctx.OpUGreaterThan(ctx.U1, op_a, op_b)};
335 const Id pred{ctx.OpLogicalOr(ctx.U1, lhs, rhs)};
336 const Id decr{ctx.OpISub(value_type, op_a, ctx.Constant(value_type, 1))};
337 result = ctx.OpSelect(value_type, pred, op_b, decr);
338 break;
339 }
340 case Operation::FPAdd:
341 result = ctx.OpFAdd(value_type, op_a, op_b);
342 break;
343 case Operation::FPMin:
344 result = ctx.OpFMin(value_type, op_a, op_b);
345 break;
346 case Operation::FPMax:
347 result = ctx.OpFMax(value_type, op_a, op_b);
348 break;
349 default:
350 break;
351 }
352 ctx.OpReturnValue(result);
353 ctx.OpFunctionEnd();
354 return func;
355}
356
357Id CasLoop(EmitContext& ctx, Operation operation, Id array_pointer, Id element_pointer,
358 Id value_type, Id memory_type, spv::Scope scope) {
359 const bool is_shared{scope == spv::Scope::Workgroup};
360 const bool is_struct{!is_shared || ctx.profile.support_explicit_workgroup_layout};
361 const Id cas_func{CasFunction(ctx, operation, value_type)};
362 const Id zero{ctx.u32_zero_value};
363 const Id scope_id{ctx.Const(static_cast<u32>(scope))};
364
365 const Id loop_header{ctx.OpLabel()};
366 const Id continue_block{ctx.OpLabel()};
367 const Id merge_block{ctx.OpLabel()};
368 const Id func_type{is_shared
369 ? ctx.TypeFunction(value_type, ctx.U32[1], value_type)
370 : ctx.TypeFunction(value_type, ctx.U32[1], value_type, array_pointer)};
371
372 const Id func{ctx.OpFunction(value_type, spv::FunctionControlMask::MaskNone, func_type)};
373 const Id index{ctx.OpFunctionParameter(ctx.U32[1])};
374 const Id op_b{ctx.OpFunctionParameter(value_type)};
375 const Id base{is_shared ? ctx.shared_memory_u32 : ctx.OpFunctionParameter(array_pointer)};
376 ctx.AddLabel();
377 ctx.OpBranch(loop_header);
378 ctx.AddLabel(loop_header);
379
380 ctx.OpLoopMerge(merge_block, continue_block, spv::LoopControlMask::MaskNone);
381 ctx.OpBranch(continue_block);
382
383 ctx.AddLabel(continue_block);
384 const Id word_pointer{is_struct ? ctx.OpAccessChain(element_pointer, base, zero, index)
385 : ctx.OpAccessChain(element_pointer, base, index)};
386 if (value_type.value == ctx.F32[2].value) {
387 const Id u32_value{ctx.OpLoad(ctx.U32[1], word_pointer)};
388 const Id value{ctx.OpUnpackHalf2x16(ctx.F32[2], u32_value)};
389 const Id new_value{ctx.OpFunctionCall(value_type, cas_func, value, op_b)};
390 const Id u32_new_value{ctx.OpPackHalf2x16(ctx.U32[1], new_value)};
391 const Id atomic_res{ctx.OpAtomicCompareExchange(ctx.U32[1], word_pointer, scope_id, zero,
392 zero, u32_new_value, u32_value)};
393 const Id success{ctx.OpIEqual(ctx.U1, atomic_res, u32_value)};
394 ctx.OpBranchConditional(success, merge_block, loop_header);
395
396 ctx.AddLabel(merge_block);
397 ctx.OpReturnValue(ctx.OpUnpackHalf2x16(ctx.F32[2], atomic_res));
398 } else {
399 const Id value{ctx.OpLoad(memory_type, word_pointer)};
400 const bool matching_type{value_type.value == memory_type.value};
401 const Id bitcast_value{matching_type ? value : ctx.OpBitcast(value_type, value)};
402 const Id cal_res{ctx.OpFunctionCall(value_type, cas_func, bitcast_value, op_b)};
403 const Id new_value{matching_type ? cal_res : ctx.OpBitcast(memory_type, cal_res)};
404 const Id atomic_res{ctx.OpAtomicCompareExchange(ctx.U32[1], word_pointer, scope_id, zero,
405 zero, new_value, value)};
406 const Id success{ctx.OpIEqual(ctx.U1, atomic_res, value)};
407 ctx.OpBranchConditional(success, merge_block, loop_header);
408
409 ctx.AddLabel(merge_block);
410 ctx.OpReturnValue(ctx.OpBitcast(value_type, atomic_res));
411 }
412 ctx.OpFunctionEnd();
413 return func;
414}
415
416template <typename Desc>
417std::string NameOf(Stage stage, const Desc& desc, std::string_view prefix) {
418 if (desc.count > 1) {
419 return fmt::format("{}_{}{}_{:02x}x{}", StageName(stage), prefix, desc.cbuf_index,
420 desc.cbuf_offset, desc.count);
421 } else {
422 return fmt::format("{}_{}{}_{:02x}", StageName(stage), prefix, desc.cbuf_index,
423 desc.cbuf_offset);
424 }
425}
426
427Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) {
428 if (count > 1) {
429 const Id array_type{ctx.TypeArray(sampled_type, ctx.Const(count))};
430 return ctx.TypePointer(spv::StorageClass::UniformConstant, array_type);
431 } else {
432 return pointer_type;
433 }
434}
435
436size_t FindAndSetNextUnusedLocation(std::bitset<IR::NUM_GENERICS>& used_locations,
437 size_t& start_offset) {
438 for (size_t location = start_offset; location < used_locations.size(); ++location) {
439 if (!used_locations.test(location)) {
440 start_offset = location;
441 used_locations.set(location);
442 return location;
443 }
444 }
445 throw RuntimeError("Unable to get an unused location for legacy attribute");
446}
447
448Id DefineLegacyInput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
449 size_t& start_offset) {
450 const Id id{DefineInput(ctx, ctx.F32[4], true)};
451 const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
452 ctx.Decorate(id, spv::Decoration::Location, location);
453 return id;
454}
455
456Id DefineLegacyOutput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
457 size_t& start_offset, std::optional<u32> invocations) {
458 const Id id{DefineOutput(ctx, ctx.F32[4], invocations)};
459 const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
460 ctx.Decorate(id, spv::Decoration::Location, location);
461 return id;
462}
463} // Anonymous namespace
464
465void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
466 defs[0] = sirit_ctx.Name(base_type, name);
467
468 std::array<char, 6> def_name;
469 for (int i = 1; i < 4; ++i) {
470 const std::string_view def_name_view(
471 def_name.data(),
472 fmt::format_to_n(def_name.data(), def_name.size(), "{}x{}", name, i + 1).size);
473 defs[static_cast<size_t>(i)] =
474 sirit_ctx.Name(sirit_ctx.TypeVector(base_type, i + 1), def_name_view);
475 }
476}
477
478EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_info_,
479 IR::Program& program, Bindings& bindings)
480 : Sirit::Module(profile_.supported_spirv), profile{profile_}, runtime_info{runtime_info_},
481 stage{program.stage}, texture_rescaling_index{bindings.texture_scaling_index},
482 image_rescaling_index{bindings.image_scaling_index} {
483 const bool is_unified{profile.unified_descriptor_binding};
484 u32& uniform_binding{is_unified ? bindings.unified : bindings.uniform_buffer};
485 u32& storage_binding{is_unified ? bindings.unified : bindings.storage_buffer};
486 u32& texture_binding{is_unified ? bindings.unified : bindings.texture};
487 u32& image_binding{is_unified ? bindings.unified : bindings.image};
488 AddCapability(spv::Capability::Shader);
489 DefineCommonTypes(program.info);
490 DefineCommonConstants();
491 DefineInterfaces(program);
492 DefineLocalMemory(program);
493 DefineSharedMemory(program);
494 DefineSharedMemoryFunctions(program);
495 DefineConstantBuffers(program.info, uniform_binding);
496 DefineStorageBuffers(program.info, storage_binding);
497 DefineTextureBuffers(program.info, texture_binding);
498 DefineImageBuffers(program.info, image_binding);
499 DefineTextures(program.info, texture_binding, bindings.texture_scaling_index);
500 DefineImages(program.info, image_binding, bindings.image_scaling_index);
501 DefineAttributeMemAccess(program.info);
502 DefineGlobalMemoryFunctions(program.info);
503 DefineRescalingInput(program.info);
504}
505
506EmitContext::~EmitContext() = default;
507
508Id EmitContext::Def(const IR::Value& value) {
509 if (!value.IsImmediate()) {
510 return value.InstRecursive()->Definition<Id>();
511 }
512 switch (value.Type()) {
513 case IR::Type::Void:
514 // Void instructions are used for optional arguments (e.g. texture offsets)
515 // They are not meant to be used in the SPIR-V module
516 return Id{};
517 case IR::Type::U1:
518 return value.U1() ? true_value : false_value;
519 case IR::Type::U32:
520 return Const(value.U32());
521 case IR::Type::U64:
522 return Constant(U64, value.U64());
523 case IR::Type::F32:
524 return Const(value.F32());
525 case IR::Type::F64:
526 return Constant(F64[1], value.F64());
527 default:
528 throw NotImplementedException("Immediate type {}", value.Type());
529 }
530}
531
532Id EmitContext::BitOffset8(const IR::Value& offset) {
533 if (offset.IsImmediate()) {
534 return Const((offset.U32() % 4) * 8);
535 }
536 return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(24u));
537}
538
539Id EmitContext::BitOffset16(const IR::Value& offset) {
540 if (offset.IsImmediate()) {
541 return Const(((offset.U32() / 2) % 2) * 16);
542 }
543 return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u));
544}
545
546Id EmitContext::InputLegacyAttribute(IR::Attribute attribute) {
547 if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
548 attribute <= IR::Attribute::ColorFrontDiffuseA) {
549 return input_front_color;
550 }
551 if (attribute >= IR::Attribute::ColorFrontSpecularR &&
552 attribute <= IR::Attribute::ColorFrontSpecularA) {
553 return input_front_secondary_color;
554 }
555 if (attribute >= IR::Attribute::ColorBackDiffuseR &&
556 attribute <= IR::Attribute::ColorBackDiffuseA) {
557 return input_back_color;
558 }
559 if (attribute >= IR::Attribute::ColorBackSpecularR &&
560 attribute <= IR::Attribute::ColorBackSpecularA) {
561 return input_back_secondary_color;
562 }
563 if (attribute == IR::Attribute::FogCoordinate) {
564 return input_fog_frag_coord;
565 }
566 if (attribute >= IR::Attribute::FixedFncTexture0S &&
567 attribute <= IR::Attribute::FixedFncTexture9Q) {
568 u32 index =
569 (static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
570 return input_fixed_fnc_textures[index];
571 }
572 throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
573}
574
575Id EmitContext::OutputLegacyAttribute(IR::Attribute attribute) {
576 if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
577 attribute <= IR::Attribute::ColorFrontDiffuseA) {
578 return output_front_color;
579 }
580 if (attribute >= IR::Attribute::ColorFrontSpecularR &&
581 attribute <= IR::Attribute::ColorFrontSpecularA) {
582 return output_front_secondary_color;
583 }
584 if (attribute >= IR::Attribute::ColorBackDiffuseR &&
585 attribute <= IR::Attribute::ColorBackDiffuseA) {
586 return output_back_color;
587 }
588 if (attribute >= IR::Attribute::ColorBackSpecularR &&
589 attribute <= IR::Attribute::ColorBackSpecularA) {
590 return output_back_secondary_color;
591 }
592 if (attribute == IR::Attribute::FogCoordinate) {
593 return output_fog_frag_coord;
594 }
595 if (attribute >= IR::Attribute::FixedFncTexture0S &&
596 attribute <= IR::Attribute::FixedFncTexture9Q) {
597 u32 index =
598 (static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
599 return output_fixed_fnc_textures[index];
600 }
601 throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
602}
603
604void EmitContext::DefineCommonTypes(const Info& info) {
605 void_id = TypeVoid();
606
607 U1 = Name(TypeBool(), "u1");
608
609 F32.Define(*this, TypeFloat(32), "f32");
610 U32.Define(*this, TypeInt(32, false), "u32");
611 S32.Define(*this, TypeInt(32, true), "s32");
612
613 private_u32 = Name(TypePointer(spv::StorageClass::Private, U32[1]), "private_u32");
614
615 input_f32 = Name(TypePointer(spv::StorageClass::Input, F32[1]), "input_f32");
616 input_u32 = Name(TypePointer(spv::StorageClass::Input, U32[1]), "input_u32");
617 input_s32 = Name(TypePointer(spv::StorageClass::Input, TypeInt(32, true)), "input_s32");
618
619 output_f32 = Name(TypePointer(spv::StorageClass::Output, F32[1]), "output_f32");
620 output_u32 = Name(TypePointer(spv::StorageClass::Output, U32[1]), "output_u32");
621
622 if (info.uses_int8 && profile.support_int8) {
623 AddCapability(spv::Capability::Int8);
624 U8 = Name(TypeInt(8, false), "u8");
625 S8 = Name(TypeInt(8, true), "s8");
626 }
627 if (info.uses_int16 && profile.support_int16) {
628 AddCapability(spv::Capability::Int16);
629 U16 = Name(TypeInt(16, false), "u16");
630 S16 = Name(TypeInt(16, true), "s16");
631 }
632 if (info.uses_int64) {
633 AddCapability(spv::Capability::Int64);
634 U64 = Name(TypeInt(64, false), "u64");
635 }
636 if (info.uses_fp16) {
637 AddCapability(spv::Capability::Float16);
638 F16.Define(*this, TypeFloat(16), "f16");
639 }
640 if (info.uses_fp64) {
641 AddCapability(spv::Capability::Float64);
642 F64.Define(*this, TypeFloat(64), "f64");
643 }
644}
645
646void EmitContext::DefineCommonConstants() {
647 true_value = ConstantTrue(U1);
648 false_value = ConstantFalse(U1);
649 u32_zero_value = Const(0U);
650 f32_zero_value = Const(0.0f);
651}
652
653void EmitContext::DefineInterfaces(const IR::Program& program) {
654 DefineInputs(program);
655 DefineOutputs(program);
656}
657
658void EmitContext::DefineLocalMemory(const IR::Program& program) {
659 if (program.local_memory_size == 0) {
660 return;
661 }
662 const u32 num_elements{Common::DivCeil(program.local_memory_size, 4U)};
663 const Id type{TypeArray(U32[1], Const(num_elements))};
664 const Id pointer{TypePointer(spv::StorageClass::Private, type)};
665 local_memory = AddGlobalVariable(pointer, spv::StorageClass::Private);
666 if (profile.supported_spirv >= 0x00010400) {
667 interfaces.push_back(local_memory);
668 }
669}
670
671void EmitContext::DefineSharedMemory(const IR::Program& program) {
672 if (program.shared_memory_size == 0) {
673 return;
674 }
675 const auto make{[&](Id element_type, u32 element_size) {
676 const u32 num_elements{Common::DivCeil(program.shared_memory_size, element_size)};
677 const Id array_type{TypeArray(element_type, Const(num_elements))};
678 Decorate(array_type, spv::Decoration::ArrayStride, element_size);
679
680 const Id struct_type{TypeStruct(array_type)};
681 MemberDecorate(struct_type, 0U, spv::Decoration::Offset, 0U);
682 Decorate(struct_type, spv::Decoration::Block);
683
684 const Id pointer{TypePointer(spv::StorageClass::Workgroup, struct_type)};
685 const Id element_pointer{TypePointer(spv::StorageClass::Workgroup, element_type)};
686 const Id variable{AddGlobalVariable(pointer, spv::StorageClass::Workgroup)};
687 Decorate(variable, spv::Decoration::Aliased);
688 interfaces.push_back(variable);
689
690 return std::make_tuple(variable, element_pointer, pointer);
691 }};
692 if (profile.support_explicit_workgroup_layout) {
693 AddExtension("SPV_KHR_workgroup_memory_explicit_layout");
694 AddCapability(spv::Capability::WorkgroupMemoryExplicitLayoutKHR);
695 if (program.info.uses_int8) {
696 AddCapability(spv::Capability::WorkgroupMemoryExplicitLayout8BitAccessKHR);
697 std::tie(shared_memory_u8, shared_u8, std::ignore) = make(U8, 1);
698 }
699 if (program.info.uses_int16) {
700 AddCapability(spv::Capability::WorkgroupMemoryExplicitLayout16BitAccessKHR);
701 std::tie(shared_memory_u16, shared_u16, std::ignore) = make(U16, 2);
702 }
703 if (program.info.uses_int64) {
704 std::tie(shared_memory_u64, shared_u64, std::ignore) = make(U64, 8);
705 }
706 std::tie(shared_memory_u32, shared_u32, shared_memory_u32_type) = make(U32[1], 4);
707 std::tie(shared_memory_u32x2, shared_u32x2, std::ignore) = make(U32[2], 8);
708 std::tie(shared_memory_u32x4, shared_u32x4, std::ignore) = make(U32[4], 16);
709 return;
710 }
711 const u32 num_elements{Common::DivCeil(program.shared_memory_size, 4U)};
712 const Id type{TypeArray(U32[1], Const(num_elements))};
713 shared_memory_u32_type = TypePointer(spv::StorageClass::Workgroup, type);
714
715 shared_u32 = TypePointer(spv::StorageClass::Workgroup, U32[1]);
716 shared_memory_u32 = AddGlobalVariable(shared_memory_u32_type, spv::StorageClass::Workgroup);
717 interfaces.push_back(shared_memory_u32);
718
719 const Id func_type{TypeFunction(void_id, U32[1], U32[1])};
720 const auto make_function{[&](u32 mask, u32 size) {
721 const Id loop_header{OpLabel()};
722 const Id continue_block{OpLabel()};
723 const Id merge_block{OpLabel()};
724
725 const Id func{OpFunction(void_id, spv::FunctionControlMask::MaskNone, func_type)};
726 const Id offset{OpFunctionParameter(U32[1])};
727 const Id insert_value{OpFunctionParameter(U32[1])};
728 AddLabel();
729 OpBranch(loop_header);
730
731 AddLabel(loop_header);
732 const Id word_offset{OpShiftRightArithmetic(U32[1], offset, Const(2U))};
733 const Id shift_offset{OpShiftLeftLogical(U32[1], offset, Const(3U))};
734 const Id bit_offset{OpBitwiseAnd(U32[1], shift_offset, Const(mask))};
735 const Id count{Const(size)};
736 OpLoopMerge(merge_block, continue_block, spv::LoopControlMask::MaskNone);
737 OpBranch(continue_block);
738
739 AddLabel(continue_block);
740 const Id word_pointer{OpAccessChain(shared_u32, shared_memory_u32, word_offset)};
741 const Id old_value{OpLoad(U32[1], word_pointer)};
742 const Id new_value{OpBitFieldInsert(U32[1], old_value, insert_value, bit_offset, count)};
743 const Id atomic_res{OpAtomicCompareExchange(U32[1], word_pointer, Const(1U), u32_zero_value,
744 u32_zero_value, new_value, old_value)};
745 const Id success{OpIEqual(U1, atomic_res, old_value)};
746 OpBranchConditional(success, merge_block, loop_header);
747
748 AddLabel(merge_block);
749 OpReturn();
750 OpFunctionEnd();
751 return func;
752 }};
753 if (program.info.uses_int8) {
754 shared_store_u8_func = make_function(24, 8);
755 }
756 if (program.info.uses_int16) {
757 shared_store_u16_func = make_function(16, 16);
758 }
759}
760
761void EmitContext::DefineSharedMemoryFunctions(const IR::Program& program) {
762 if (program.info.uses_shared_increment) {
763 increment_cas_shared = CasLoop(*this, Operation::Increment, shared_memory_u32_type,
764 shared_u32, U32[1], U32[1], spv::Scope::Workgroup);
765 }
766 if (program.info.uses_shared_decrement) {
767 decrement_cas_shared = CasLoop(*this, Operation::Decrement, shared_memory_u32_type,
768 shared_u32, U32[1], U32[1], spv::Scope::Workgroup);
769 }
770}
771
772void EmitContext::DefineAttributeMemAccess(const Info& info) {
773 const auto make_load{[&] {
774 const bool is_array{stage == Stage::Geometry};
775 const Id end_block{OpLabel()};
776 const Id default_label{OpLabel()};
777
778 const Id func_type_load{is_array ? TypeFunction(F32[1], U32[1], U32[1])
779 : TypeFunction(F32[1], U32[1])};
780 const Id func{OpFunction(F32[1], spv::FunctionControlMask::MaskNone, func_type_load)};
781 const Id offset{OpFunctionParameter(U32[1])};
782 const Id vertex{is_array ? OpFunctionParameter(U32[1]) : Id{}};
783
784 AddLabel();
785 const Id base_index{OpShiftRightArithmetic(U32[1], offset, Const(2U))};
786 const Id masked_index{OpBitwiseAnd(U32[1], base_index, Const(3U))};
787 const Id compare_index{OpShiftRightArithmetic(U32[1], base_index, Const(2U))};
788 std::vector<Sirit::Literal> literals;
789 std::vector<Id> labels;
790 if (info.loads.AnyComponent(IR::Attribute::PositionX)) {
791 literals.push_back(static_cast<u32>(IR::Attribute::PositionX) >> 2);
792 labels.push_back(OpLabel());
793 }
794 const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
795 for (u32 index = 0; index < static_cast<u32>(IR::NUM_GENERICS); ++index) {
796 if (!info.loads.Generic(index)) {
797 continue;
798 }
799 literals.push_back(base_attribute_value + index);
800 labels.push_back(OpLabel());
801 }
802 OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone);
803 OpSwitch(compare_index, default_label, literals, labels);
804 AddLabel(default_label);
805 OpReturnValue(Const(0.0f));
806 size_t label_index{0};
807 if (info.loads.AnyComponent(IR::Attribute::PositionX)) {
808 AddLabel(labels[label_index]);
809 const Id pointer{is_array
810 ? OpAccessChain(input_f32, input_position, vertex, masked_index)
811 : OpAccessChain(input_f32, input_position, masked_index)};
812 const Id result{OpLoad(F32[1], pointer)};
813 OpReturnValue(result);
814 ++label_index;
815 }
816 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
817 if (!info.loads.Generic(index)) {
818 continue;
819 }
820 AddLabel(labels[label_index]);
821 const auto type{AttrTypes(*this, static_cast<u32>(index))};
822 if (!type) {
823 OpReturnValue(Const(0.0f));
824 ++label_index;
825 continue;
826 }
827 const Id generic_id{input_generics.at(index)};
828 const Id pointer{is_array
829 ? OpAccessChain(type->pointer, generic_id, vertex, masked_index)
830 : OpAccessChain(type->pointer, generic_id, masked_index)};
831 const Id value{OpLoad(type->id, pointer)};
832 const Id result{type->needs_cast ? OpBitcast(F32[1], value) : value};
833 OpReturnValue(result);
834 ++label_index;
835 }
836 AddLabel(end_block);
837 OpUnreachable();
838 OpFunctionEnd();
839 return func;
840 }};
841 const auto make_store{[&] {
842 const Id end_block{OpLabel()};
843 const Id default_label{OpLabel()};
844
845 const Id func_type_store{TypeFunction(void_id, U32[1], F32[1])};
846 const Id func{OpFunction(void_id, spv::FunctionControlMask::MaskNone, func_type_store)};
847 const Id offset{OpFunctionParameter(U32[1])};
848 const Id store_value{OpFunctionParameter(F32[1])};
849 AddLabel();
850 const Id base_index{OpShiftRightArithmetic(U32[1], offset, Const(2U))};
851 const Id masked_index{OpBitwiseAnd(U32[1], base_index, Const(3U))};
852 const Id compare_index{OpShiftRightArithmetic(U32[1], base_index, Const(2U))};
853 std::vector<Sirit::Literal> literals;
854 std::vector<Id> labels;
855 if (info.stores.AnyComponent(IR::Attribute::PositionX)) {
856 literals.push_back(static_cast<u32>(IR::Attribute::PositionX) >> 2);
857 labels.push_back(OpLabel());
858 }
859 const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
860 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
861 if (!info.stores.Generic(index)) {
862 continue;
863 }
864 literals.push_back(base_attribute_value + static_cast<u32>(index));
865 labels.push_back(OpLabel());
866 }
867 if (info.stores.ClipDistances()) {
868 literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance0) >> 2);
869 labels.push_back(OpLabel());
870 literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance4) >> 2);
871 labels.push_back(OpLabel());
872 }
873 OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone);
874 OpSwitch(compare_index, default_label, literals, labels);
875 AddLabel(default_label);
876 OpReturn();
877 size_t label_index{0};
878 if (info.stores.AnyComponent(IR::Attribute::PositionX)) {
879 AddLabel(labels[label_index]);
880 const Id pointer{OpAccessChain(output_f32, output_position, masked_index)};
881 OpStore(pointer, store_value);
882 OpReturn();
883 ++label_index;
884 }
885 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
886 if (!info.stores.Generic(index)) {
887 continue;
888 }
889 if (output_generics[index][0].num_components != 4) {
890 throw NotImplementedException("Physical stores and transform feedbacks");
891 }
892 AddLabel(labels[label_index]);
893 const Id generic_id{output_generics[index][0].id};
894 const Id pointer{OpAccessChain(output_f32, generic_id, masked_index)};
895 OpStore(pointer, store_value);
896 OpReturn();
897 ++label_index;
898 }
899 if (info.stores.ClipDistances()) {
900 AddLabel(labels[label_index]);
901 const Id pointer{OpAccessChain(output_f32, clip_distances, masked_index)};
902 OpStore(pointer, store_value);
903 OpReturn();
904 ++label_index;
905 AddLabel(labels[label_index]);
906 const Id fixed_index{OpIAdd(U32[1], masked_index, Const(4U))};
907 const Id pointer2{OpAccessChain(output_f32, clip_distances, fixed_index)};
908 OpStore(pointer2, store_value);
909 OpReturn();
910 ++label_index;
911 }
912 AddLabel(end_block);
913 OpUnreachable();
914 OpFunctionEnd();
915 return func;
916 }};
917 if (info.loads_indexed_attributes) {
918 indexed_load_func = make_load();
919 }
920 if (info.stores_indexed_attributes) {
921 indexed_store_func = make_store();
922 }
923}
924
925void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
926 if (!info.uses_global_memory || !profile.support_int64) {
927 return;
928 }
929 using DefPtr = Id StorageDefinitions::*;
930 const Id zero{u32_zero_value};
931 const auto define_body{[&](DefPtr ssbo_member, Id addr, Id element_pointer, u32 shift,
932 auto&& callback) {
933 AddLabel();
934 const size_t num_buffers{info.storage_buffers_descriptors.size()};
935 for (size_t index = 0; index < num_buffers; ++index) {
936 if (!info.nvn_buffer_used[index]) {
937 continue;
938 }
939 const auto& ssbo{info.storage_buffers_descriptors[index]};
940 const Id ssbo_addr_cbuf_offset{Const(ssbo.cbuf_offset / 8)};
941 const Id ssbo_size_cbuf_offset{Const(ssbo.cbuf_offset / 4 + 2)};
942 const Id ssbo_addr_pointer{OpAccessChain(
943 uniform_types.U32x2, cbufs[ssbo.cbuf_index].U32x2, zero, ssbo_addr_cbuf_offset)};
944 const Id ssbo_size_pointer{OpAccessChain(uniform_types.U32, cbufs[ssbo.cbuf_index].U32,
945 zero, ssbo_size_cbuf_offset)};
946
947 const Id ssbo_addr{OpBitcast(U64, OpLoad(U32[2], ssbo_addr_pointer))};
948 const Id ssbo_size{OpUConvert(U64, OpLoad(U32[1], ssbo_size_pointer))};
949 const Id ssbo_end{OpIAdd(U64, ssbo_addr, ssbo_size)};
950 const Id cond{OpLogicalAnd(U1, OpUGreaterThanEqual(U1, addr, ssbo_addr),
951 OpULessThan(U1, addr, ssbo_end))};
952 const Id then_label{OpLabel()};
953 const Id else_label{OpLabel()};
954 OpSelectionMerge(else_label, spv::SelectionControlMask::MaskNone);
955 OpBranchConditional(cond, then_label, else_label);
956 AddLabel(then_label);
957 const Id ssbo_id{ssbos[index].*ssbo_member};
958 const Id ssbo_offset{OpUConvert(U32[1], OpISub(U64, addr, ssbo_addr))};
959 const Id ssbo_index{OpShiftRightLogical(U32[1], ssbo_offset, Const(shift))};
960 const Id ssbo_pointer{OpAccessChain(element_pointer, ssbo_id, zero, ssbo_index)};
961 callback(ssbo_pointer);
962 AddLabel(else_label);
963 }
964 }};
965 const auto define_load{[&](DefPtr ssbo_member, Id element_pointer, Id type, u32 shift) {
966 const Id function_type{TypeFunction(type, U64)};
967 const Id func_id{OpFunction(type, spv::FunctionControlMask::MaskNone, function_type)};
968 const Id addr{OpFunctionParameter(U64)};
969 define_body(ssbo_member, addr, element_pointer, shift,
970 [&](Id ssbo_pointer) { OpReturnValue(OpLoad(type, ssbo_pointer)); });
971 OpReturnValue(ConstantNull(type));
972 OpFunctionEnd();
973 return func_id;
974 }};
975 const auto define_write{[&](DefPtr ssbo_member, Id element_pointer, Id type, u32 shift) {
976 const Id function_type{TypeFunction(void_id, U64, type)};
977 const Id func_id{OpFunction(void_id, spv::FunctionControlMask::MaskNone, function_type)};
978 const Id addr{OpFunctionParameter(U64)};
979 const Id data{OpFunctionParameter(type)};
980 define_body(ssbo_member, addr, element_pointer, shift, [&](Id ssbo_pointer) {
981 OpStore(ssbo_pointer, data);
982 OpReturn();
983 });
984 OpReturn();
985 OpFunctionEnd();
986 return func_id;
987 }};
988 const auto define{
989 [&](DefPtr ssbo_member, const StorageTypeDefinition& type_def, Id type, size_t size) {
990 const Id element_type{type_def.element};
991 const u32 shift{static_cast<u32>(std::countr_zero(size))};
992 const Id load_func{define_load(ssbo_member, element_type, type, shift)};
993 const Id write_func{define_write(ssbo_member, element_type, type, shift)};
994 return std::make_pair(load_func, write_func);
995 }};
996 std::tie(load_global_func_u32, write_global_func_u32) =
997 define(&StorageDefinitions::U32, storage_types.U32, U32[1], sizeof(u32));
998 std::tie(load_global_func_u32x2, write_global_func_u32x2) =
999 define(&StorageDefinitions::U32x2, storage_types.U32x2, U32[2], sizeof(u32[2]));
1000 std::tie(load_global_func_u32x4, write_global_func_u32x4) =
1001 define(&StorageDefinitions::U32x4, storage_types.U32x4, U32[4], sizeof(u32[4]));
1002}
1003
1004void EmitContext::DefineRescalingInput(const Info& info) {
1005 if (!info.uses_rescaling_uniform) {
1006 return;
1007 }
1008 if (profile.unified_descriptor_binding) {
1009 DefineRescalingInputPushConstant();
1010 } else {
1011 DefineRescalingInputUniformConstant();
1012 }
1013}
1014
1015void EmitContext::DefineRescalingInputPushConstant() {
1016 boost::container::static_vector<Id, 3> members{};
1017 u32 member_index{0};
1018
1019 rescaling_textures_type = TypeArray(U32[1], Const(4u));
1020 Decorate(rescaling_textures_type, spv::Decoration::ArrayStride, 4u);
1021 members.push_back(rescaling_textures_type);
1022 rescaling_textures_member_index = member_index++;
1023
1024 rescaling_images_type = TypeArray(U32[1], Const(NUM_IMAGE_SCALING_WORDS));
1025 Decorate(rescaling_images_type, spv::Decoration::ArrayStride, 4u);
1026 members.push_back(rescaling_images_type);
1027 rescaling_images_member_index = member_index++;
1028
1029 if (stage != Stage::Compute) {
1030 members.push_back(F32[1]);
1031 rescaling_downfactor_member_index = member_index++;
1032 }
1033 const Id push_constant_struct{TypeStruct(std::span(members.data(), members.size()))};
1034 Decorate(push_constant_struct, spv::Decoration::Block);
1035 Name(push_constant_struct, "ResolutionInfo");
1036
1037 MemberDecorate(push_constant_struct, rescaling_textures_member_index, spv::Decoration::Offset,
1038 static_cast<u32>(offsetof(RescalingLayout, rescaling_textures)));
1039 MemberName(push_constant_struct, rescaling_textures_member_index, "rescaling_textures");
1040
1041 MemberDecorate(push_constant_struct, rescaling_images_member_index, spv::Decoration::Offset,
1042 static_cast<u32>(offsetof(RescalingLayout, rescaling_images)));
1043 MemberName(push_constant_struct, rescaling_images_member_index, "rescaling_images");
1044
1045 if (stage != Stage::Compute) {
1046 MemberDecorate(push_constant_struct, rescaling_downfactor_member_index,
1047 spv::Decoration::Offset,
1048 static_cast<u32>(offsetof(RescalingLayout, down_factor)));
1049 MemberName(push_constant_struct, rescaling_downfactor_member_index, "down_factor");
1050 }
1051 const Id pointer_type{TypePointer(spv::StorageClass::PushConstant, push_constant_struct)};
1052 rescaling_push_constants = AddGlobalVariable(pointer_type, spv::StorageClass::PushConstant);
1053 Name(rescaling_push_constants, "rescaling_push_constants");
1054
1055 if (profile.supported_spirv >= 0x00010400) {
1056 interfaces.push_back(rescaling_push_constants);
1057 }
1058}
1059
1060void EmitContext::DefineRescalingInputUniformConstant() {
1061 const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, F32[4])};
1062 rescaling_uniform_constant =
1063 AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant);
1064 Decorate(rescaling_uniform_constant, spv::Decoration::Location, 0u);
1065
1066 if (profile.supported_spirv >= 0x00010400) {
1067 interfaces.push_back(rescaling_uniform_constant);
1068 }
1069}
1070
1071void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) {
1072 if (info.constant_buffer_descriptors.empty()) {
1073 return;
1074 }
1075 if (!profile.support_descriptor_aliasing) {
1076 DefineConstBuffers(*this, info, &UniformDefinitions::U32x4, binding, U32[4], 'u',
1077 sizeof(u32[4]));
1078 for (const ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
1079 binding += desc.count;
1080 }
1081 return;
1082 }
1083 IR::Type types{info.used_constant_buffer_types};
1084 if (True(types & IR::Type::U8)) {
1085 if (profile.support_int8) {
1086 DefineConstBuffers(*this, info, &UniformDefinitions::U8, binding, U8, 'u', sizeof(u8));
1087 DefineConstBuffers(*this, info, &UniformDefinitions::S8, binding, S8, 's', sizeof(s8));
1088 } else {
1089 types |= IR::Type::U32;
1090 }
1091 }
1092 if (True(types & IR::Type::U16)) {
1093 if (profile.support_int16) {
1094 DefineConstBuffers(*this, info, &UniformDefinitions::U16, binding, U16, 'u',
1095 sizeof(u16));
1096 DefineConstBuffers(*this, info, &UniformDefinitions::S16, binding, S16, 's',
1097 sizeof(s16));
1098 } else {
1099 types |= IR::Type::U32;
1100 }
1101 }
1102 if (True(types & IR::Type::U32)) {
1103 DefineConstBuffers(*this, info, &UniformDefinitions::U32, binding, U32[1], 'u',
1104 sizeof(u32));
1105 }
1106 if (True(types & IR::Type::F32)) {
1107 DefineConstBuffers(*this, info, &UniformDefinitions::F32, binding, F32[1], 'f',
1108 sizeof(f32));
1109 }
1110 if (True(types & IR::Type::U32x2)) {
1111 DefineConstBuffers(*this, info, &UniformDefinitions::U32x2, binding, U32[2], 'u',
1112 sizeof(u32[2]));
1113 }
1114 binding += static_cast<u32>(info.constant_buffer_descriptors.size());
1115}
1116
1117void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) {
1118 if (info.storage_buffers_descriptors.empty()) {
1119 return;
1120 }
1121 AddExtension("SPV_KHR_storage_buffer_storage_class");
1122
1123 const IR::Type used_types{profile.support_descriptor_aliasing ? info.used_storage_buffer_types
1124 : IR::Type::U32};
1125 if (profile.support_int8 && True(used_types & IR::Type::U8)) {
1126 DefineSsbos(*this, storage_types.U8, &StorageDefinitions::U8, info, binding, U8,
1127 sizeof(u8));
1128 DefineSsbos(*this, storage_types.S8, &StorageDefinitions::S8, info, binding, S8,
1129 sizeof(u8));
1130 }
1131 if (profile.support_int16 && True(used_types & IR::Type::U16)) {
1132 DefineSsbos(*this, storage_types.U16, &StorageDefinitions::U16, info, binding, U16,
1133 sizeof(u16));
1134 DefineSsbos(*this, storage_types.S16, &StorageDefinitions::S16, info, binding, S16,
1135 sizeof(u16));
1136 }
1137 if (True(used_types & IR::Type::U32)) {
1138 DefineSsbos(*this, storage_types.U32, &StorageDefinitions::U32, info, binding, U32[1],
1139 sizeof(u32));
1140 }
1141 if (True(used_types & IR::Type::F32)) {
1142 DefineSsbos(*this, storage_types.F32, &StorageDefinitions::F32, info, binding, F32[1],
1143 sizeof(f32));
1144 }
1145 if (True(used_types & IR::Type::U64)) {
1146 DefineSsbos(*this, storage_types.U64, &StorageDefinitions::U64, info, binding, U64,
1147 sizeof(u64));
1148 }
1149 if (True(used_types & IR::Type::U32x2)) {
1150 DefineSsbos(*this, storage_types.U32x2, &StorageDefinitions::U32x2, info, binding, U32[2],
1151 sizeof(u32[2]));
1152 }
1153 if (True(used_types & IR::Type::U32x4)) {
1154 DefineSsbos(*this, storage_types.U32x4, &StorageDefinitions::U32x4, info, binding, U32[4],
1155 sizeof(u32[4]));
1156 }
1157 for (const StorageBufferDescriptor& desc : info.storage_buffers_descriptors) {
1158 binding += desc.count;
1159 }
1160 const bool needs_function{
1161 info.uses_global_increment || info.uses_global_decrement || info.uses_atomic_f32_add ||
1162 info.uses_atomic_f16x2_add || info.uses_atomic_f16x2_min || info.uses_atomic_f16x2_max ||
1163 info.uses_atomic_f32x2_add || info.uses_atomic_f32x2_min || info.uses_atomic_f32x2_max};
1164 if (needs_function) {
1165 AddCapability(spv::Capability::VariablePointersStorageBuffer);
1166 }
1167 if (info.uses_global_increment) {
1168 increment_cas_ssbo = CasLoop(*this, Operation::Increment, storage_types.U32.array,
1169 storage_types.U32.element, U32[1], U32[1], spv::Scope::Device);
1170 }
1171 if (info.uses_global_decrement) {
1172 decrement_cas_ssbo = CasLoop(*this, Operation::Decrement, storage_types.U32.array,
1173 storage_types.U32.element, U32[1], U32[1], spv::Scope::Device);
1174 }
1175 if (info.uses_atomic_f32_add) {
1176 f32_add_cas = CasLoop(*this, Operation::FPAdd, storage_types.U32.array,
1177 storage_types.U32.element, F32[1], U32[1], spv::Scope::Device);
1178 }
1179 if (info.uses_atomic_f16x2_add) {
1180 f16x2_add_cas = CasLoop(*this, Operation::FPAdd, storage_types.U32.array,
1181 storage_types.U32.element, F16[2], F16[2], spv::Scope::Device);
1182 }
1183 if (info.uses_atomic_f16x2_min) {
1184 f16x2_min_cas = CasLoop(*this, Operation::FPMin, storage_types.U32.array,
1185 storage_types.U32.element, F16[2], F16[2], spv::Scope::Device);
1186 }
1187 if (info.uses_atomic_f16x2_max) {
1188 f16x2_max_cas = CasLoop(*this, Operation::FPMax, storage_types.U32.array,
1189 storage_types.U32.element, F16[2], F16[2], spv::Scope::Device);
1190 }
1191 if (info.uses_atomic_f32x2_add) {
1192 f32x2_add_cas = CasLoop(*this, Operation::FPAdd, storage_types.U32.array,
1193 storage_types.U32.element, F32[2], F32[2], spv::Scope::Device);
1194 }
1195 if (info.uses_atomic_f32x2_min) {
1196 f32x2_min_cas = CasLoop(*this, Operation::FPMin, storage_types.U32.array,
1197 storage_types.U32.element, F32[2], F32[2], spv::Scope::Device);
1198 }
1199 if (info.uses_atomic_f32x2_max) {
1200 f32x2_max_cas = CasLoop(*this, Operation::FPMax, storage_types.U32.array,
1201 storage_types.U32.element, F32[2], F32[2], spv::Scope::Device);
1202 }
1203}
1204
1205void EmitContext::DefineTextureBuffers(const Info& info, u32& binding) {
1206 if (info.texture_buffer_descriptors.empty()) {
1207 return;
1208 }
1209 const spv::ImageFormat format{spv::ImageFormat::Unknown};
1210 image_buffer_type = TypeImage(F32[1], spv::Dim::Buffer, 0U, false, false, 1, format);
1211 sampled_texture_buffer_type = TypeSampledImage(image_buffer_type);
1212
1213 const Id type{TypePointer(spv::StorageClass::UniformConstant, sampled_texture_buffer_type)};
1214 texture_buffers.reserve(info.texture_buffer_descriptors.size());
1215 for (const TextureBufferDescriptor& desc : info.texture_buffer_descriptors) {
1216 if (desc.count != 1) {
1217 throw NotImplementedException("Array of texture buffers");
1218 }
1219 const Id id{AddGlobalVariable(type, spv::StorageClass::UniformConstant)};
1220 Decorate(id, spv::Decoration::Binding, binding);
1221 Decorate(id, spv::Decoration::DescriptorSet, 0U);
1222 Name(id, NameOf(stage, desc, "texbuf"));
1223 texture_buffers.push_back({
1224 .id = id,
1225 .count = desc.count,
1226 });
1227 if (profile.supported_spirv >= 0x00010400) {
1228 interfaces.push_back(id);
1229 }
1230 ++binding;
1231 }
1232}
1233
1234void EmitContext::DefineImageBuffers(const Info& info, u32& binding) {
1235 image_buffers.reserve(info.image_buffer_descriptors.size());
1236 for (const ImageBufferDescriptor& desc : info.image_buffer_descriptors) {
1237 if (desc.count != 1) {
1238 throw NotImplementedException("Array of image buffers");
1239 }
1240 const spv::ImageFormat format{GetImageFormat(desc.format)};
1241 const Id image_type{TypeImage(U32[1], spv::Dim::Buffer, false, false, false, 2, format)};
1242 const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
1243 const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
1244 Decorate(id, spv::Decoration::Binding, binding);
1245 Decorate(id, spv::Decoration::DescriptorSet, 0U);
1246 Name(id, NameOf(stage, desc, "imgbuf"));
1247 image_buffers.push_back({
1248 .id = id,
1249 .image_type = image_type,
1250 .count = desc.count,
1251 });
1252 if (profile.supported_spirv >= 0x00010400) {
1253 interfaces.push_back(id);
1254 }
1255 ++binding;
1256 }
1257}
1258
1259void EmitContext::DefineTextures(const Info& info, u32& binding, u32& scaling_index) {
1260 textures.reserve(info.texture_descriptors.size());
1261 for (const TextureDescriptor& desc : info.texture_descriptors) {
1262 const Id image_type{ImageType(*this, desc)};
1263 const Id sampled_type{TypeSampledImage(image_type)};
1264 const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, sampled_type)};
1265 const Id desc_type{DescType(*this, sampled_type, pointer_type, desc.count)};
1266 const Id id{AddGlobalVariable(desc_type, spv::StorageClass::UniformConstant)};
1267 Decorate(id, spv::Decoration::Binding, binding);
1268 Decorate(id, spv::Decoration::DescriptorSet, 0U);
1269 Name(id, NameOf(stage, desc, "tex"));
1270 textures.push_back({
1271 .id = id,
1272 .sampled_type = sampled_type,
1273 .pointer_type = pointer_type,
1274 .image_type = image_type,
1275 .count = desc.count,
1276 });
1277 if (profile.supported_spirv >= 0x00010400) {
1278 interfaces.push_back(id);
1279 }
1280 ++binding;
1281 ++scaling_index;
1282 }
1283 if (info.uses_atomic_image_u32) {
1284 image_u32 = TypePointer(spv::StorageClass::Image, U32[1]);
1285 }
1286}
1287
1288void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_index) {
1289 images.reserve(info.image_descriptors.size());
1290 for (const ImageDescriptor& desc : info.image_descriptors) {
1291 if (desc.count != 1) {
1292 throw NotImplementedException("Array of images");
1293 }
1294 const Id image_type{ImageType(*this, desc)};
1295 const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
1296 const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
1297 Decorate(id, spv::Decoration::Binding, binding);
1298 Decorate(id, spv::Decoration::DescriptorSet, 0U);
1299 Name(id, NameOf(stage, desc, "img"));
1300 images.push_back({
1301 .id = id,
1302 .image_type = image_type,
1303 .count = desc.count,
1304 });
1305 if (profile.supported_spirv >= 0x00010400) {
1306 interfaces.push_back(id);
1307 }
1308 ++binding;
1309 ++scaling_index;
1310 }
1311}
1312
1313void EmitContext::DefineInputs(const IR::Program& program) {
1314 const Info& info{program.info};
1315 const VaryingState loads{info.loads.mask | info.passthrough.mask};
1316
1317 if (info.uses_workgroup_id) {
1318 workgroup_id = DefineInput(*this, U32[3], false, spv::BuiltIn::WorkgroupId);
1319 }
1320 if (info.uses_local_invocation_id) {
1321 local_invocation_id = DefineInput(*this, U32[3], false, spv::BuiltIn::LocalInvocationId);
1322 }
1323 if (info.uses_invocation_id) {
1324 invocation_id = DefineInput(*this, U32[1], false, spv::BuiltIn::InvocationId);
1325 }
1326 if (info.uses_sample_id) {
1327 sample_id = DefineInput(*this, U32[1], false, spv::BuiltIn::SampleId);
1328 }
1329 if (info.uses_is_helper_invocation) {
1330 is_helper_invocation = DefineInput(*this, U1, false, spv::BuiltIn::HelperInvocation);
1331 }
1332 if (info.uses_subgroup_mask) {
1333 subgroup_mask_eq = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupEqMaskKHR);
1334 subgroup_mask_lt = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupLtMaskKHR);
1335 subgroup_mask_le = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupLeMaskKHR);
1336 subgroup_mask_gt = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupGtMaskKHR);
1337 subgroup_mask_ge = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupGeMaskKHR);
1338 }
1339 if (info.uses_subgroup_invocation_id || info.uses_subgroup_shuffles ||
1340 (profile.warp_size_potentially_larger_than_guest &&
1341 (info.uses_subgroup_vote || info.uses_subgroup_mask))) {
1342 subgroup_local_invocation_id =
1343 DefineInput(*this, U32[1], false, spv::BuiltIn::SubgroupLocalInvocationId);
1344 }
1345 if (info.uses_fswzadd) {
1346 const Id f32_one{Const(1.0f)};
1347 const Id f32_minus_one{Const(-1.0f)};
1348 const Id f32_zero{Const(0.0f)};
1349 fswzadd_lut_a = ConstantComposite(F32[4], f32_minus_one, f32_one, f32_minus_one, f32_zero);
1350 fswzadd_lut_b =
1351 ConstantComposite(F32[4], f32_minus_one, f32_minus_one, f32_one, f32_minus_one);
1352 }
1353 if (loads[IR::Attribute::PrimitiveId]) {
1354 primitive_id = DefineInput(*this, U32[1], false, spv::BuiltIn::PrimitiveId);
1355 }
1356 if (loads.AnyComponent(IR::Attribute::PositionX)) {
1357 const bool is_fragment{stage != Stage::Fragment};
1358 const spv::BuiltIn built_in{is_fragment ? spv::BuiltIn::Position : spv::BuiltIn::FragCoord};
1359 input_position = DefineInput(*this, F32[4], true, built_in);
1360 if (profile.support_geometry_shader_passthrough) {
1361 if (info.passthrough.AnyComponent(IR::Attribute::PositionX)) {
1362 Decorate(input_position, spv::Decoration::PassthroughNV);
1363 }
1364 }
1365 }
1366 if (loads[IR::Attribute::InstanceId]) {
1367 if (profile.support_vertex_instance_id) {
1368 instance_id = DefineInput(*this, U32[1], true, spv::BuiltIn::InstanceId);
1369 } else {
1370 instance_index = DefineInput(*this, U32[1], true, spv::BuiltIn::InstanceIndex);
1371 base_instance = DefineInput(*this, U32[1], true, spv::BuiltIn::BaseInstance);
1372 }
1373 }
1374 if (loads[IR::Attribute::VertexId]) {
1375 if (profile.support_vertex_instance_id) {
1376 vertex_id = DefineInput(*this, U32[1], true, spv::BuiltIn::VertexId);
1377 } else {
1378 vertex_index = DefineInput(*this, U32[1], true, spv::BuiltIn::VertexIndex);
1379 base_vertex = DefineInput(*this, U32[1], true, spv::BuiltIn::BaseVertex);
1380 }
1381 }
1382 if (loads[IR::Attribute::FrontFace]) {
1383 front_face = DefineInput(*this, U1, true, spv::BuiltIn::FrontFacing);
1384 }
1385 if (loads[IR::Attribute::PointSpriteS] || loads[IR::Attribute::PointSpriteT]) {
1386 point_coord = DefineInput(*this, F32[2], true, spv::BuiltIn::PointCoord);
1387 }
1388 if (loads[IR::Attribute::TessellationEvaluationPointU] ||
1389 loads[IR::Attribute::TessellationEvaluationPointV]) {
1390 tess_coord = DefineInput(*this, F32[3], false, spv::BuiltIn::TessCoord);
1391 }
1392 std::bitset<IR::NUM_GENERICS> used_locations{};
1393 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
1394 const AttributeType input_type{runtime_info.generic_input_types[index]};
1395 if (!runtime_info.previous_stage_stores.Generic(index)) {
1396 continue;
1397 }
1398 if (!loads.Generic(index)) {
1399 continue;
1400 }
1401 if (input_type == AttributeType::Disabled) {
1402 continue;
1403 }
1404 used_locations.set(index);
1405 const Id type{GetAttributeType(*this, input_type)};
1406 const Id id{DefineInput(*this, type, true)};
1407 Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
1408 Name(id, fmt::format("in_attr{}", index));
1409 input_generics[index] = id;
1410
1411 if (info.passthrough.Generic(index) && profile.support_geometry_shader_passthrough) {
1412 Decorate(id, spv::Decoration::PassthroughNV);
1413 }
1414 if (stage != Stage::Fragment) {
1415 continue;
1416 }
1417 switch (info.interpolation[index]) {
1418 case Interpolation::Smooth:
1419 // Default
1420 // Decorate(id, spv::Decoration::Smooth);
1421 break;
1422 case Interpolation::NoPerspective:
1423 Decorate(id, spv::Decoration::NoPerspective);
1424 break;
1425 case Interpolation::Flat:
1426 Decorate(id, spv::Decoration::Flat);
1427 break;
1428 }
1429 }
1430 size_t previous_unused_location = 0;
1431 if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
1432 input_front_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
1433 }
1434 if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
1435 input_front_secondary_color =
1436 DefineLegacyInput(*this, used_locations, previous_unused_location);
1437 }
1438 if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
1439 input_back_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
1440 }
1441 if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
1442 input_back_secondary_color =
1443 DefineLegacyInput(*this, used_locations, previous_unused_location);
1444 }
1445 if (loads.AnyComponent(IR::Attribute::FogCoordinate)) {
1446 input_fog_frag_coord = DefineLegacyInput(*this, used_locations, previous_unused_location);
1447 }
1448 for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
1449 if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
1450 input_fixed_fnc_textures[index] =
1451 DefineLegacyInput(*this, used_locations, previous_unused_location);
1452 }
1453 }
1454 if (stage == Stage::TessellationEval) {
1455 for (size_t index = 0; index < info.uses_patches.size(); ++index) {
1456 if (!info.uses_patches[index]) {
1457 continue;
1458 }
1459 const Id id{DefineInput(*this, F32[4], false)};
1460 Decorate(id, spv::Decoration::Patch);
1461 Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
1462 patches[index] = id;
1463 }
1464 }
1465}
1466
1467void EmitContext::DefineOutputs(const IR::Program& program) {
1468 const Info& info{program.info};
1469 const std::optional<u32> invocations{program.invocations};
1470 if (info.stores.AnyComponent(IR::Attribute::PositionX) || stage == Stage::VertexB) {
1471 output_position = DefineOutput(*this, F32[4], invocations, spv::BuiltIn::Position);
1472 }
1473 if (info.stores[IR::Attribute::PointSize] || runtime_info.fixed_state_point_size) {
1474 if (stage == Stage::Fragment) {
1475 throw NotImplementedException("Storing PointSize in fragment stage");
1476 }
1477 output_point_size = DefineOutput(*this, F32[1], invocations, spv::BuiltIn::PointSize);
1478 }
1479 if (info.stores.ClipDistances()) {
1480 if (stage == Stage::Fragment) {
1481 throw NotImplementedException("Storing ClipDistance in fragment stage");
1482 }
1483 const Id type{TypeArray(F32[1], Const(8U))};
1484 clip_distances = DefineOutput(*this, type, invocations, spv::BuiltIn::ClipDistance);
1485 }
1486 if (info.stores[IR::Attribute::Layer] &&
1487 (profile.support_viewport_index_layer_non_geometry || stage == Stage::Geometry)) {
1488 if (stage == Stage::Fragment) {
1489 throw NotImplementedException("Storing Layer in fragment stage");
1490 }
1491 layer = DefineOutput(*this, U32[1], invocations, spv::BuiltIn::Layer);
1492 }
1493 if (info.stores[IR::Attribute::ViewportIndex] &&
1494 (profile.support_viewport_index_layer_non_geometry || stage == Stage::Geometry)) {
1495 if (stage == Stage::Fragment) {
1496 throw NotImplementedException("Storing ViewportIndex in fragment stage");
1497 }
1498 viewport_index = DefineOutput(*this, U32[1], invocations, spv::BuiltIn::ViewportIndex);
1499 }
1500 if (info.stores[IR::Attribute::ViewportMask] && profile.support_viewport_mask) {
1501 viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt,
1502 spv::BuiltIn::ViewportMaskNV);
1503 }
1504 std::bitset<IR::NUM_GENERICS> used_locations{};
1505 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
1506 if (info.stores.Generic(index)) {
1507 DefineGenericOutput(*this, index, invocations);
1508 used_locations.set(index);
1509 }
1510 }
1511 size_t previous_unused_location = 0;
1512 if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
1513 output_front_color =
1514 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1515 }
1516 if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
1517 output_front_secondary_color =
1518 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1519 }
1520 if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
1521 output_back_color =
1522 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1523 }
1524 if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
1525 output_back_secondary_color =
1526 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1527 }
1528 if (info.stores.AnyComponent(IR::Attribute::FogCoordinate)) {
1529 output_fog_frag_coord =
1530 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1531 }
1532 for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
1533 if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
1534 output_fixed_fnc_textures[index] =
1535 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1536 }
1537 }
1538 switch (stage) {
1539 case Stage::TessellationControl:
1540 if (info.stores_tess_level_outer) {
1541 const Id type{TypeArray(F32[1], Const(4U))};
1542 output_tess_level_outer =
1543 DefineOutput(*this, type, std::nullopt, spv::BuiltIn::TessLevelOuter);
1544 Decorate(output_tess_level_outer, spv::Decoration::Patch);
1545 }
1546 if (info.stores_tess_level_inner) {
1547 const Id type{TypeArray(F32[1], Const(2U))};
1548 output_tess_level_inner =
1549 DefineOutput(*this, type, std::nullopt, spv::BuiltIn::TessLevelInner);
1550 Decorate(output_tess_level_inner, spv::Decoration::Patch);
1551 }
1552 for (size_t index = 0; index < info.uses_patches.size(); ++index) {
1553 if (!info.uses_patches[index]) {
1554 continue;
1555 }
1556 const Id id{DefineOutput(*this, F32[4], std::nullopt)};
1557 Decorate(id, spv::Decoration::Patch);
1558 Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
1559 patches[index] = id;
1560 }
1561 break;
1562 case Stage::Fragment:
1563 for (u32 index = 0; index < 8; ++index) {
1564 if (!info.stores_frag_color[index] && !profile.need_declared_frag_colors) {
1565 continue;
1566 }
1567 frag_color[index] = DefineOutput(*this, F32[4], std::nullopt);
1568 Decorate(frag_color[index], spv::Decoration::Location, index);
1569 Name(frag_color[index], fmt::format("frag_color{}", index));
1570 }
1571 if (info.stores_frag_depth) {
1572 frag_depth = DefineOutput(*this, F32[1], std::nullopt);
1573 Decorate(frag_depth, spv::Decoration::BuiltIn, spv::BuiltIn::FragDepth);
1574 }
1575 if (info.stores_sample_mask) {
1576 sample_mask = DefineOutput(*this, U32[1], std::nullopt);
1577 Decorate(sample_mask, spv::Decoration::BuiltIn, spv::BuiltIn::SampleMask);
1578 }
1579 break;
1580 default:
1581 break;
1582 }
1583}
1584
1585} // namespace Shader::Backend::SPIRV