Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/dep/reshadefx/include/effect_codegen.hpp
4246 views
1
/*
2
* Copyright (C) 2014 Patrick Mours
3
* SPDX-License-Identifier: BSD-3-Clause
4
*/
5
6
#pragma once
7
8
#include "effect_module.hpp"
9
#include <memory> // std::unique_ptr
10
#include <algorithm> // std::find_if
11
12
namespace reshadefx
13
{
14
/// <summary>
15
/// A SSA code generation back-end interface for the parser to call into.
16
/// </summary>
17
class codegen
18
{
19
friend class parser;
20
21
public:
22
/// <summary>
23
/// Virtual destructor to guarantee that memory of the implementations deriving from this interface is properly destroyed.
24
/// </summary>
25
virtual ~codegen() {}
26
27
/// <summary>
28
/// Gets the module describing the generated code.
29
/// </summary>
30
const effect_module &module() const { return _module; }
31
32
/// <summary>
33
/// Finalizes and returns the generated code for the entire module (all entry points).
34
/// </summary>
35
virtual std::basic_string<char> finalize_code() const = 0;
36
/// <summary>
37
/// Finalizes and returns the generated code for the specified entry point (and no other entry points).
38
/// </summary>
39
/// <param name="entry_point_name">Name of the entry point function to generate code for.</param>
40
virtual std::basic_string<char> finalize_code_for_entry_point(const std::string &entry_point_name) const = 0;
41
42
protected:
43
/// <summary>
44
/// An opaque ID referring to a SSA value or basic block.
45
/// </summary>
46
using id = uint32_t;
47
48
/// <summary>
49
/// Defines a new struct type.
50
/// </summary>
51
/// <param name="loc">Source location matching this definition (for debugging).</param>
52
/// <param name="info">Description of the type.</param>
53
/// <returns>New SSA ID of the type.</returns>
54
virtual id define_struct(const location &loc, struct_type &info) = 0;
55
/// <summary>
56
/// Defines a new texture binding.
57
/// </summary>
58
/// <param name="loc">Source location matching this definition (for debugging).</param>
59
/// <param name="info">Description of the texture object.</param>
60
/// <returns>New SSA ID of the binding.</returns>
61
virtual id define_texture(const location &loc, texture &info) = 0;
62
/// <summary>
63
/// Defines a new sampler binding.
64
/// </summary>
65
/// <param name="loc">Source location matching this definition (for debugging).</param>
66
/// <param name="tex_info">Description of the texture this sampler object references.</param>
67
/// <param name="info">Description of the sampler object.</param>
68
/// <returns>New SSA ID of the binding.</returns>
69
virtual id define_sampler(const location &loc, const texture &tex_info, sampler &info) = 0;
70
/// <summary>
71
/// Defines a new storage binding.
72
/// </summary>
73
/// <param name="loc">Source location matching this definition (for debugging).</param>
74
/// <param name="tex_info">Description of the texture this storage object references.</param>
75
/// <param name="info">Description of the storage object.</param>
76
/// <returns>New SSA ID of the binding.</returns>
77
virtual id define_storage(const location &loc, const texture &tex_info, storage &info) = 0;
78
/// <summary>
79
/// Defines a new uniform variable.
80
/// </summary>
81
/// <param name="loc">Source location matching this definition (for debugging).</param>
82
/// <param name="info">Description of the uniform variable.</param>
83
/// <returns>New SSA ID of the variable.</returns>
84
virtual id define_uniform(const location &loc, uniform &info) = 0;
85
/// <summary>
86
/// Defines a new variable.
87
/// </summary>
88
/// <param name="loc">Source location matching this definition (for debugging).</param>
89
/// <param name="type">Data type of the variable.</param>
90
/// <param name="name">Name of the variable.</param>
91
/// <param name="global"><c>true</c> if this variable is in global scope, <c>false</c> otherwise.</param>
92
/// <param name="initializer_value">SSA ID of an optional initializer value.</param>
93
/// <returns>New SSA ID of the variable.</returns>
94
virtual id define_variable(const location &loc, const type &type, std::string name = std::string(), bool global = false, id initializer_value = 0) = 0;
95
/// <summary>
96
/// Defines a new function and its function parameters and make it current.
97
/// Any code added after this call is added to this function.
98
/// </summary>
99
/// <param name="loc">Source location matching this definition (for debugging).</param>
100
/// <param name="info">Description of the function.</param>
101
/// <returns>New SSA ID of the function.</returns>
102
virtual id define_function(const location &loc, function &info) = 0;
103
104
/// <summary>
105
/// Defines a new effect technique.
106
/// </summary>
107
/// <param name="loc">Source location matching this definition (for debugging).</param>
108
/// <param name="info">Description of the technique.</param>
109
void define_technique(technique &&info) { _module.techniques.push_back(std::move(info)); }
110
/// <summary>
111
/// Makes a function a shader entry point.
112
/// </summary>
113
/// <param name="function">Function to use as entry point. May be overwritten to point to a new uniquely generated function.</param>
114
virtual void define_entry_point(function &function) = 0;
115
116
/// <summary>
117
/// Resolves the access chain and add a load operation to the output.
118
/// </summary>
119
/// <param name="chain">Access chain pointing to the variable to load from.</param>
120
/// <param name="force_new_id">Set to <see langword="true"/> to force this to return a new SSA ID for l-value loads.</param>
121
/// <returns>New SSA ID with the loaded value.</returns>
122
virtual id emit_load(const expression &chain, bool force_new_id = false) = 0;
123
/// <summary>
124
/// Resolves the access chain and add a store operation to the output.
125
/// </summary>
126
/// <param name="chain">Access chain pointing to the variable to store to.</param>
127
/// <param name="value">SSA ID of the value to store.</param>
128
virtual void emit_store(const expression &chain, id value) = 0;
129
/// <summary>
130
/// Resolves the access chain, but do not add a load operation. This returns a pointer instead.
131
/// </summary>
132
/// <param name="chain">Access chain pointing to the variable to resolve.</param>
133
/// <param name="chain_index">Output value which is set to the index in the access chain up to which the access chain went.</param>
134
/// <returns>New SSA ID with a pointer to the value.</returns>
135
virtual id emit_access_chain(const expression &chain, size_t &chain_index) { chain_index = chain.chain.size(); return emit_load(chain); }
136
137
/// <summary>
138
/// Creates a SSA constant value.
139
/// </summary>
140
/// <param name="type">Data type of the constant.</param>
141
/// <param name="data">Actual constant data to convert into a SSA ID.</param>
142
/// <returns>New SSA ID with the constant value.</returns>
143
virtual id emit_constant(const type &type, const constant &data) = 0;
144
id emit_constant(const type &data_type, uint32_t value)
145
{
146
// Create a constant value of the specified type
147
constant data = {}; // Initialize to zero, so that components not set below still have a defined value for lookup via std::memcmp
148
for (unsigned int i = 0; i < data_type.components(); ++i)
149
{
150
if (data_type.is_integral())
151
data.as_uint[i] = value;
152
else
153
data.as_float[i] = static_cast<float>(value);
154
}
155
return emit_constant(data_type, data);
156
}
157
158
/// <summary>
159
/// Adds an unary operation to the output (built-in operation with one argument).
160
/// </summary>
161
/// <param name="loc">Source location matching this operation (for debugging).</param>
162
/// <param name="op">Unary operator to use.</param>
163
/// <param name="type">Data type of the input value.</param>
164
/// <param name="val">SSA ID of value to perform the operation on.</param>
165
/// <returns>New SSA ID with the result of the operation.</returns>
166
virtual id emit_unary_op(const location &loc, tokenid op, const type &type, id val) = 0;
167
/// <summary>
168
/// Adds a binary operation to the output (built-in operation with two arguments).
169
/// </summary>
170
/// <param name="loc">Source location matching this operation (for debugging).</param>
171
/// <param name="op">Binary operator to use.</param>
172
/// <param name="res_type">Data type of the result.</param>
173
/// <param name="type">Data type of the input values.</param>
174
/// <param name="lhs">SSA ID of the value on the left-hand side of the binary operation.</param>
175
/// <param name="rhs">SSA ID of the value on the right-hand side of the binary operation.</param>
176
/// <returns>New SSA ID with the result of the operation.</returns>
177
virtual id emit_binary_op(const location &loc, tokenid op, const type &res_type, const type &type, id lhs, id rhs) = 0;
178
id emit_binary_op(const location &loc, tokenid op, const type &type, id lhs, id rhs) { return emit_binary_op(loc, op, type, type, lhs, rhs); }
179
/// <summary>
180
/// Adds a ternary operation to the output (built-in operation with three arguments).
181
/// </summary>
182
/// <param name="loc">Source location matching this operation (for debugging).</param>
183
/// <param name="op">Ternary operator to use.</param>
184
/// <param name="type">Data type of the input values.</param>
185
/// <param name="condition">SSA ID of the condition value of the ternary operation.</param>
186
/// <param name="true_value">SSA ID of the first value of the ternary operation.</param>
187
/// <param name="false_value">SSA ID of the second value of the ternary operation.</param>
188
/// <returns>New SSA ID with the result of the operation.</returns>
189
virtual id emit_ternary_op(const location &loc, tokenid op, const type &type, id condition, id true_value, id false_value) = 0;
190
/// <summary>
191
/// Adds a function call to the output.
192
/// </summary>
193
/// <param name="loc">Source location matching this operation (for debugging).</param>
194
/// <param name="function">SSA ID of the function to call.</param>
195
/// <param name="res_type">Data type of the call result.</param>
196
/// <param name="args">List of SSA IDs representing the call arguments.</param>
197
/// <returns>New SSA ID with the result of the function call.</returns>
198
virtual id emit_call(const location &loc, id function, const type &res_type, const std::vector<expression> &args) = 0;
199
/// <summary>
200
/// Adds an intrinsic function call to the output.
201
/// </summary>
202
/// <param name="loc">Source location matching this operation (for debugging).</param>
203
/// <param name="function">Intrinsic to call.</param>
204
/// <param name="res_type">Data type of the call result.</param>
205
/// <param name="args">List of SSA IDs representing the call arguments.</param>
206
/// <returns>New SSA ID with the result of the function call.</returns>
207
virtual id emit_call_intrinsic(const location &loc, id function, const type &res_type, const std::vector<expression> &args) = 0;
208
/// <summary>
209
/// Adds a type constructor call to the output.
210
/// </summary>
211
/// <param name="type">Data type to construct.</param>
212
/// <param name="args">List of SSA IDs representing the scalar constructor arguments.</param>
213
/// <returns>New SSA ID with the constructed value.</returns>
214
virtual id emit_construct(const location &loc, const type &type, const std::vector<expression> &args) = 0;
215
216
/// <summary>
217
/// Adds a structured branch control flow to the output.
218
/// </summary>
219
/// <param name="loc">Source location matching this branch (for debugging).</param>
220
/// <param name="flags">0 - default, 1 - flatten, 2 - do not flatten</param>
221
virtual void emit_if(const location &loc, id condition_value, id condition_block, id true_statement_block, id false_statement_block, unsigned int flags) = 0;
222
/// <summary>
223
/// Adds a branch control flow with a SSA phi operation to the output.
224
/// </summary>
225
/// <param name="loc">Source location matching this branch (for debugging).</param>
226
/// <returns>New SSA ID with the result of the phi operation.</returns>
227
virtual id emit_phi(const location &loc, id condition_value, id condition_block, id true_value, id true_statement_block, id false_value, id false_statement_block, const type &type) = 0;
228
/// <summary>
229
/// Adds a structured loop control flow to the output.
230
/// </summary>
231
/// <param name="loc">Source location matching this loop (for debugging).</param>
232
/// <param name="flags">0 - default, 1 - unroll, 2 - do not unroll</param>
233
virtual void emit_loop(const location &loc, id condition_value, id prev_block, id header_block, id condition_block, id loop_block, id continue_block, unsigned int flags) = 0;
234
/// <summary>
235
/// Adds a structured switch control flow to the output.
236
/// </summary>
237
/// <param name="loc">Source location matching this switch (for debugging).</param>
238
/// <param name="flags">0 - default, 1 - flatten, 2 - do not flatten</param>
239
virtual void emit_switch(const location &loc, id selector_value, id selector_block, id default_label, id default_block, const std::vector<id> &case_literal_and_labels, const std::vector<id> &case_blocks, unsigned int flags) = 0;
240
241
/// <summary>
242
/// Returns <see langword="true"/> if code is currently added to a basic block.
243
/// </summary>
244
bool is_in_block() const { return _current_block != 0; }
245
/// <summary>
246
/// Returns <see langword="true"/> if code is currently added to a function.
247
/// </summary>
248
bool is_in_function() const { return _current_function != nullptr; }
249
250
/// <summary>
251
/// Creates a new basic block.
252
/// </summary>
253
/// <returns>New ID of the basic block.</returns>
254
virtual id create_block() { return make_id(); }
255
/// <summary>
256
/// Overwrites the current block ID.
257
/// </summary>
258
/// <param name="id">ID of the block to make current.</param>
259
/// <returns>ID of the previous basic block.</returns>
260
virtual id set_block(id id) = 0;
261
/// <summary>
262
/// Creates a new basic block and make it current.
263
/// </summary>
264
/// <param name="id">ID of the basic block to create and make current.</param>
265
virtual void enter_block(id id) = 0;
266
/// <summary>
267
/// Returns from the current basic block and kill the shader invocation.
268
/// </summary>
269
/// <returns>ID of the current basic block.</returns>
270
virtual id leave_block_and_kill() = 0;
271
/// <summary>
272
/// Returns from the current basic block and hand control flow over to the function call side.
273
/// </summary>
274
/// <param name="value">Optional SSA ID of a return value.</param>
275
/// <returns>ID of the current basic block.</returns>
276
virtual id leave_block_and_return(id value = 0) = 0;
277
/// <summary>
278
/// Diverges the current control flow and enter a switch.
279
/// </summary>
280
/// <param name="value">SSA ID of the selector value to decide the switch path.</param>
281
/// <returns>ID of the current basic block.</returns>
282
virtual id leave_block_and_switch(id value, id default_target) = 0;
283
/// <summary>
284
/// Diverges the current control flow and jump to the specified target block.
285
/// </summary>
286
/// <param name="target">ID of the basic block to jump to.</param>
287
/// <param name="is_continue">Set to <see langword="true"/> if this corresponds to a loop continue statement.</param>
288
/// <returns>ID of the current basic block.</returns>
289
virtual id leave_block_and_branch(id target, unsigned int loop_flow = 0) = 0;
290
/// <summary>
291
/// Diverges the current control flow and jump to one of the specified target blocks, depending on the condition.
292
/// </summary>
293
/// <param name="condition">SSA ID of a value used to choose which path to take.</param>
294
/// <param name="true_target">ID of the basic block to jump to when the condition is true.</param>
295
/// <param name="false_target">ID of the basic block to jump to when the condition is false.</param>
296
/// <returns>ID of the current basic block.</returns>
297
virtual id leave_block_and_branch_conditional(id condition, id true_target, id false_target) = 0;
298
299
/// <summary>
300
/// Leaves the current function. Any code added after this call is added in the global scope.
301
/// </summary>
302
virtual void leave_function() = 0;
303
304
/// <summary>
305
/// Recalculates sampler and storage bindings to take as little binding space as possible for each entry point.
306
/// </summary>
307
virtual void optimize_bindings();
308
309
/// <summary>
310
/// Looks up an existing struct type.
311
/// </summary>
312
/// <param name="id">SSA ID of the type to find.</param>
313
/// <returns>Reference to the struct description.</returns>
314
const struct_type &get_struct(id id) const
315
{
316
return *std::find_if(_structs.begin(), _structs.end(),
317
[id](const struct_type &info) { return info.id == id; });
318
}
319
/// <summary>
320
/// Looks up an existing texture binding.
321
/// </summary>
322
/// <param name="id">SSA ID of the texture binding to find.</param>
323
/// <returns>Reference to the texture description.</returns>
324
texture &get_texture(id id)
325
{
326
return *std::find_if(_module.textures.begin(), _module.textures.end(),
327
[id](const texture &info) { return info.id == id; });
328
}
329
/// <summary>
330
/// Looks up an existing sampler binding.
331
/// </summary>
332
/// <param name="id">SSA ID of the sampler binding to find.</param>
333
/// <returns>Reference to the sampler description.</returns>
334
const sampler &get_sampler(id id) const
335
{
336
return *std::find_if(_module.samplers.begin(), _module.samplers.end(),
337
[id](const sampler &info) { return info.id == id; });
338
}
339
/// <summary>
340
/// Looks up an existing storage binding.
341
/// </summary>
342
/// <param name="id">SSA ID of the storage binding to find.</param>
343
/// <returns>Reference to the storage description.</returns>
344
const storage &get_storage(id id) const
345
{
346
return *std::find_if(_module.storages.begin(), _module.storages.end(),
347
[id](const storage &info) { return info.id == id; });
348
}
349
/// <summary>
350
/// Looks up an existing function definition.
351
/// </summary>
352
/// <param name="id">SSA ID of the function variable to find.</param>
353
/// <returns>Reference to the function description.</returns>
354
function &get_function(id id)
355
{
356
return *std::find_if(_functions.begin(), _functions.end(),
357
[id](const std::unique_ptr<function> &info) { return info->id == id; })->get();
358
}
359
function &get_function(const std::string &unique_name)
360
{
361
return *std::find_if(_functions.begin(), _functions.end(),
362
[&unique_name](const std::unique_ptr<function> &info) { return info->unique_name == unique_name; })->get();
363
}
364
365
id make_id() { return _next_id++; }
366
367
effect_module _module;
368
std::vector<struct_type> _structs;
369
std::vector<std::unique_ptr<function>> _functions;
370
371
id _next_id = 1;
372
id _last_block = 0;
373
id _current_block = 0;
374
function *_current_function = nullptr;
375
};
376
377
/// <summary>
378
/// Creates a back-end implementation for GLSL code generation.
379
/// </summary>
380
/// <param name="version">GLSL version to insert at the beginning of the file.</param>
381
/// <param name="gles">Generate GLSL ES code instead of core OpenGL.</param>
382
/// <param name="vulkan_semantics">Generate GLSL for OpenGL or for Vulkan.</param>
383
/// <param name="debug_info">Whether to append debug information like line directives to the generated code.</param>
384
/// <param name="uniforms_to_spec_constants">Whether to convert uniform variables to specialization constants.</param>
385
/// <param name="enable_16bit_types">Use real 16-bit types for the minimum precision types "min16int", "min16uint" and "min16float".</param>
386
/// <param name="flip_vert_y">Insert code to flip the Y component of the output position in vertex shaders.</param>
387
codegen *create_codegen_glsl(unsigned version, bool gles, bool vulkan_semantics, bool debug_info, bool uniforms_to_spec_constants, bool enable_16bit_types = false, bool flip_vert_y = false);
388
/// <summary>
389
/// Creates a back-end implementation for HLSL code generation.
390
/// </summary>
391
/// <param name="shader_model">The HLSL shader model version (e.g. 30, 41, 50, 60, ...)</param>
392
/// <param name="debug_info">Whether to append debug information like line directives to the generated code.</param>
393
/// <param name="uniforms_to_spec_constants">Whether to convert uniform variables to specialization constants.</param>
394
codegen *create_codegen_hlsl(unsigned int shader_model, bool debug_info, bool uniforms_to_spec_constants);
395
/// <summary>
396
/// Creates a back-end implementation for SPIR-V code generation.
397
/// </summary>
398
/// <param name="vulkan_semantics">Generate SPIR-V for OpenGL or for Vulkan.</param>
399
/// <param name="debug_info">Whether to append debug information like line directives to the generated code.</param>
400
/// <param name="uniforms_to_spec_constants">Whether to convert uniform variables to specialization constants.</param>
401
/// <param name="enable_16bit_types">Use real 16-bit types for the minimum precision types "min16int", "min16uint" and "min16float".</param>
402
/// <param name="flip_vert_y">Insert code to flip the Y component of the output position in vertex shaders.</param>
403
codegen *create_codegen_spirv(bool vulkan_semantics, bool debug_info, bool uniforms_to_spec_constants, bool enable_16bit_types = false, bool flip_vert_y = false);
404
}
405
406