xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_compiler.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef D3D12_COMPILER_H
25 #define D3D12_COMPILER_H
26 
27 #include "d3d12_common.h"
28 
29 #include "dxil_nir_lower_int_samplers.h"
30 
31 #include "pipe/p_defines.h"
32 #include "pipe/p_state.h"
33 
34 #include "compiler/shader_info.h"
35 #include "program/prog_statevars.h"
36 
37 #include "nir.h"
38 
39 struct pipe_screen;
40 struct d3d12_context;
41 struct d3d12_screen;
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 enum d3d12_state_var {
48    D3D12_STATE_VAR_Y_FLIP = 0,
49    D3D12_STATE_VAR_PT_SPRITE,
50    D3D12_STATE_VAR_DRAW_PARAMS,
51    D3D12_STATE_VAR_DEPTH_TRANSFORM,
52    D3D12_STATE_VAR_DEFAULT_INNER_TESS_LEVEL,
53    D3D12_STATE_VAR_DEFAULT_OUTER_TESS_LEVEL,
54    D3D12_STATE_VAR_PATCH_VERTICES_IN,
55    D3D12_MAX_GRAPHICS_STATE_VARS,
56 
57    D3D12_STATE_VAR_NUM_WORKGROUPS = 0,
58    D3D12_STATE_VAR_TRANSFORM_GENERIC0,
59    D3D12_STATE_VAR_TRANSFORM_GENERIC1,
60    D3D12_MAX_COMPUTE_STATE_VARS,
61 
62    D3D12_MAX_STATE_VARS = MAX2(D3D12_MAX_GRAPHICS_STATE_VARS, D3D12_MAX_COMPUTE_STATE_VARS)
63 };
64 
65 #define D3D12_MAX_POINT_SIZE 255.0f
66 
67 const void *
68 d3d12_get_compiler_options(struct pipe_screen *screen,
69                            enum pipe_shader_ir ir,
70                            enum pipe_shader_type shader);
71 
72 
73 void
74 d3d12_varying_cache_init(struct d3d12_screen *ctx);
75 
76 void
77 d3d12_varying_cache_destroy(struct d3d12_screen *ctx);
78 
79 
80 struct d3d12_varying_info {
81    struct {
82       const struct glsl_type *types[4];
83       uint8_t location_frac_mask:4;
84       uint8_t patch:1;
85       struct {
86          unsigned interpolation:3;   // INTERP_MODE_COUNT = 5
87          unsigned driver_location:6; // VARYING_SLOT_MAX = 64
88          unsigned compact:1;
89          unsigned always_active_io:1;
90       } vars[4];
91    } slots[VARYING_SLOT_MAX];
92    uint64_t mask;
93    uint32_t hash;
94    uint32_t max;
95 };
96 
97 struct d3d12_image_format_conversion_info {
98    enum pipe_format view_format, emulated_format;
99 };
100 struct d3d12_image_format_conversion_info_arr {
101    int n_images;
102    struct d3d12_image_format_conversion_info* image_format_conversion;
103 };
104 
105 struct d3d12_shader_key {
106    uint32_t hash;
107    enum pipe_shader_type stage;
108 
109    uint64_t next_varying_inputs;
110    uint64_t prev_varying_outputs;
111    BITSET_WORD *next_varying_frac_inputs;
112    BITSET_WORD *prev_varying_frac_outputs;
113    union {
114       struct {
115          unsigned last_vertex_processing_stage : 1;
116          unsigned invert_depth : 16;
117          unsigned halfz : 1;
118          unsigned samples_int_textures : 1;
119          unsigned input_clip_size : 4;
120          unsigned next_has_frac_inputs : 1;
121          unsigned prev_has_frac_outputs : 1;
122       };
123       uint32_t common_all;
124    };
125    unsigned tex_saturate_s : PIPE_MAX_SAMPLERS;
126    unsigned tex_saturate_r : PIPE_MAX_SAMPLERS;
127    unsigned tex_saturate_t : PIPE_MAX_SAMPLERS;
128    union {
129       struct {
130          unsigned needs_format_emulation:1;
131          enum pipe_format format_conversion[PIPE_MAX_ATTRIBS];
132       } vs;
133 
134       union {
135          struct {
136             unsigned sprite_coord_enable:24;
137             unsigned sprite_origin_upper_left:1;
138             unsigned point_pos_stream_out:1;
139             unsigned writes_psize:1;
140             unsigned point_size_per_vertex:1;
141             unsigned aa_point:1;
142             unsigned stream_output_factor:3;
143             unsigned primitive_id:1;
144             unsigned triangle_strip:1;
145          };
146          uint64_t all;
147       } gs;
148 
149       struct {
150          union {
151             struct {
152                uint32_t next_patch_inputs;
153                unsigned primitive_mode:2;
154                unsigned ccw:1;
155                unsigned point_mode:1;
156                unsigned spacing:2;
157                unsigned patch_vertices_in:5;
158             };
159             uint64_t all;
160          };
161       } hs;
162 
163       struct {
164          unsigned tcs_vertices_out;
165          uint32_t prev_patch_outputs;
166       } ds;
167 
168       union {
169          struct {
170             unsigned missing_dual_src_outputs : 2;
171             unsigned frag_result_color_lowering : 4;
172             unsigned cast_to_uint : 1;
173             unsigned cast_to_int : 1;
174             unsigned provoking_vertex : 2;
175             unsigned manual_depth_range : 1;
176             unsigned polygon_stipple : 1;
177             unsigned remap_front_facing : 1;
178             unsigned multisample_disabled : 1;
179          };
180          unsigned short all;
181       } fs;
182 
183       struct {
184          unsigned workgroup_size[3];
185       } cs;
186    };
187 
188    int n_texture_states;
189    dxil_wrap_sampler_state *tex_wrap_states;
190    dxil_texture_swizzle_state swizzle_state[PIPE_MAX_SHADER_SAMPLER_VIEWS];
191    enum compare_func sampler_compare_funcs[PIPE_MAX_SHADER_SAMPLER_VIEWS];
192 
193    int n_images;
194    struct d3d12_image_format_conversion_info image_format_conversion[PIPE_MAX_SHADER_IMAGES];
195 };
196 
197 struct d3d12_shader {
198    void *bytecode;
199    size_t bytecode_length;
200 
201    nir_shader *nir;
202 
203    /* UBOs can be sparse, if there's no uniforms then ubo0 is unused, and state vars are an internal ubo */
204    uint32_t begin_ubo_binding;
205    uint32_t end_ubo_binding;
206 
207    struct {
208       enum d3d12_state_var var;
209       unsigned offset;
210    } state_vars[D3D12_MAX_STATE_VARS];
211    unsigned num_state_vars;
212    size_t state_vars_size;
213    bool state_vars_used;
214 
215    /* Samplers/textures can be sparse for some internal shaders */
216    struct {
217       uint32_t dimension;
218    } srv_bindings[PIPE_MAX_SHADER_SAMPLER_VIEWS];
219    uint32_t begin_srv_binding;
220    uint32_t end_srv_binding;
221 
222    /* Images and SSBOs are never sparse */
223    struct {
224       uint32_t dimension;
225    } uav_bindings[PIPE_MAX_SHADER_IMAGES];
226 
227    unsigned pstipple_binding;
228 
229    struct d3d12_shader_key key;
230    struct d3d12_shader *next_variant;
231 };
232 
233 struct d3d12_gs_variant_key
234 {
235    union {
236       struct {
237          unsigned passthrough:1;
238          unsigned provoking_vertex:3;
239          unsigned alternate_tri:1;
240          unsigned fill_mode:2;
241          unsigned cull_mode:2;
242          unsigned has_front_face:1;
243          unsigned front_ccw:1;
244          unsigned edge_flag_fix:1;
245          unsigned flatshade_first:1;
246       };
247       uint64_t all;
248    };
249    uint64_t flat_varyings;
250    struct d3d12_varying_info *varyings;
251 };
252 
253 struct d3d12_tcs_variant_key
254 {
255    unsigned vertices_out;
256    struct d3d12_varying_info *varyings;
257 };
258 
259 struct d3d12_shader_selector {
260    enum pipe_shader_type stage;
261    const nir_shader *initial;
262    struct d3d12_varying_info *initial_output_vars;
263    struct d3d12_varying_info *initial_input_vars;
264 
265    struct d3d12_shader *first;
266    struct d3d12_shader *current;
267 
268    struct pipe_stream_output_info so_info;
269 
270    unsigned samples_int_textures:1;
271    unsigned compare_with_lod_bias_grad:1;
272    unsigned workgroup_size_variable:1;
273    unsigned has_frac_inputs:1;
274    unsigned has_frac_outputs:1;
275 
276    BITSET_DECLARE(varying_frac_inputs, 64 * 4);
277    BITSET_DECLARE(varying_frac_outputs, 64 * 4);
278 
279    bool is_variant;
280    union {
281       struct d3d12_gs_variant_key gs_key;
282       struct d3d12_tcs_variant_key tcs_key;
283    };
284 };
285 
286 struct d3d12_shader_selector *
287 d3d12_create_shader(struct d3d12_context *ctx,
288                     enum pipe_shader_type stage,
289                     const struct pipe_shader_state *shader);
290 
291 struct d3d12_shader_selector *
292 d3d12_create_compute_shader(struct d3d12_context *ctx,
293                             const struct pipe_compute_state *shader);
294 
295 void
296 d3d12_shader_free(struct d3d12_shader_selector *shader);
297 
298 void
299 d3d12_select_shader_variants(struct d3d12_context *ctx,
300                              const struct pipe_draw_info *dinfo);
301 
302 void
303 d3d12_select_compute_shader_variants(struct d3d12_context *ctx,
304                                      const struct pipe_grid_info *info);
305 
306 void
307 d3d12_gs_variant_cache_init(struct d3d12_context *ctx);
308 
309 void
310 d3d12_gs_variant_cache_destroy(struct d3d12_context *ctx);
311 
312 struct d3d12_shader_selector *
313 d3d12_get_gs_variant(struct d3d12_context *ctx, struct d3d12_gs_variant_key *key);
314 
315 void
316 d3d12_tcs_variant_cache_init(struct d3d12_context *ctx);
317 
318 void
319 d3d12_tcs_variant_cache_destroy(struct d3d12_context *ctx);
320 
321 struct d3d12_shader_selector *
322 d3d12_get_tcs_variant(struct d3d12_context *ctx, struct d3d12_tcs_variant_key *key);
323 
324 unsigned
325 missing_dual_src_outputs(struct d3d12_context* ctx);
326 
327 bool
328 has_flat_varyings(struct d3d12_context* ctx);
329 
330 bool
331 d3d12_compare_varying_info(const struct d3d12_varying_info *expect, const struct d3d12_varying_info *have);
332 
333 bool
334 manual_depth_range(struct d3d12_context* ctx);
335 
336 #ifdef __cplusplus
337 }
338 #endif
339 
340 #endif
341