• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       } vars[4];
90    } slots[VARYING_SLOT_MAX];
91    uint64_t mask;
92    uint32_t hash;
93    uint32_t max;
94 };
95 
96 struct d3d12_image_format_conversion_info {
97    enum pipe_format view_format, emulated_format;
98 };
99 struct d3d12_image_format_conversion_info_arr {
100    int n_images;
101    struct d3d12_image_format_conversion_info* image_format_conversion;
102 };
103 
104 struct d3d12_shader_key {
105    uint32_t hash;
106    enum pipe_shader_type stage;
107 
108    struct d3d12_varying_info *required_varying_inputs;
109    struct d3d12_varying_info *required_varying_outputs;
110    uint64_t next_varying_inputs;
111    uint64_t prev_varying_outputs;
112    union {
113       struct {
114          unsigned last_vertex_processing_stage : 1;
115          unsigned invert_depth : 16;
116          unsigned halfz : 1;
117          unsigned samples_int_textures : 1;
118          unsigned input_clip_size : 4;
119       };
120       uint32_t common_all;
121    };
122    unsigned tex_saturate_s : PIPE_MAX_SAMPLERS;
123    unsigned tex_saturate_r : PIPE_MAX_SAMPLERS;
124    unsigned tex_saturate_t : PIPE_MAX_SAMPLERS;
125    union {
126       struct {
127          unsigned needs_format_emulation:1;
128          enum pipe_format format_conversion[PIPE_MAX_ATTRIBS];
129       } vs;
130 
131       union {
132          struct {
133             unsigned sprite_coord_enable:24;
134             unsigned sprite_origin_upper_left:1;
135             unsigned point_pos_stream_out:1;
136             unsigned writes_psize:1;
137             unsigned point_size_per_vertex:1;
138             unsigned aa_point:1;
139             unsigned stream_output_factor:3;
140             unsigned primitive_id:1;
141             unsigned triangle_strip:1;
142          };
143          uint64_t all;
144       } gs;
145 
146       struct {
147          union {
148             struct {
149                uint32_t next_patch_inputs;
150                unsigned primitive_mode:2;
151                unsigned ccw:1;
152                unsigned point_mode:1;
153                unsigned spacing:2;
154                unsigned patch_vertices_in:5;
155             };
156             uint64_t all;
157          };
158          struct d3d12_varying_info *required_patch_outputs;
159       } hs;
160 
161       struct {
162          unsigned tcs_vertices_out;
163          uint32_t prev_patch_outputs;
164          struct d3d12_varying_info *required_patch_inputs;
165       } ds;
166 
167       union {
168          struct {
169             unsigned missing_dual_src_outputs : 2;
170             unsigned frag_result_color_lowering : 4;
171             unsigned cast_to_uint : 1;
172             unsigned cast_to_int : 1;
173             unsigned provoking_vertex : 2;
174             unsigned manual_depth_range : 1;
175             unsigned polygon_stipple : 1;
176             unsigned remap_front_facing : 1;
177             unsigned multisample_disabled : 1;
178          };
179          unsigned short all;
180       } fs;
181 
182       struct {
183          unsigned workgroup_size[3];
184       } cs;
185    };
186 
187    int n_texture_states;
188    dxil_wrap_sampler_state *tex_wrap_states;
189    dxil_texture_swizzle_state swizzle_state[PIPE_MAX_SHADER_SAMPLER_VIEWS];
190    enum compare_func sampler_compare_funcs[PIPE_MAX_SHADER_SAMPLER_VIEWS];
191 
192    int n_images;
193    struct d3d12_image_format_conversion_info image_format_conversion[PIPE_MAX_SHADER_IMAGES];
194 };
195 
196 struct d3d12_shader {
197    void *bytecode;
198    size_t bytecode_length;
199 
200    nir_shader *nir;
201    struct d3d12_varying_info *output_vars_gs;
202    struct d3d12_varying_info *output_vars_fs;
203    struct d3d12_varying_info *output_vars_default;
204 
205    struct d3d12_varying_info *input_vars_vs;
206    struct d3d12_varying_info *input_vars_default;
207 
208    struct d3d12_varying_info *tess_eval_output_vars;
209    struct d3d12_varying_info *tess_ctrl_input_vars;
210 
211    struct {
212       unsigned binding;
213    } cb_bindings[PIPE_MAX_CONSTANT_BUFFERS];
214    size_t num_cb_bindings;
215 
216    struct {
217       enum d3d12_state_var var;
218       unsigned offset;
219    } state_vars[D3D12_MAX_STATE_VARS];
220    unsigned num_state_vars;
221    size_t state_vars_size;
222    bool state_vars_used;
223 
224    struct {
225       uint32_t dimension;
226    } srv_bindings[PIPE_MAX_SHADER_SAMPLER_VIEWS];
227    size_t begin_srv_binding;
228    size_t end_srv_binding;
229 
230    struct {
231       uint32_t dimension;
232    } uav_bindings[PIPE_MAX_SHADER_IMAGES];
233 
234    bool has_default_ubo0;
235    unsigned pstipple_binding;
236 
237    struct d3d12_shader_key key;
238    struct d3d12_shader *next_variant;
239 };
240 
241 struct d3d12_gs_variant_key
242 {
243    union {
244       struct {
245          unsigned passthrough:1;
246          unsigned provoking_vertex:3;
247          unsigned alternate_tri:1;
248          unsigned fill_mode:2;
249          unsigned cull_mode:2;
250          unsigned has_front_face:1;
251          unsigned front_ccw:1;
252          unsigned edge_flag_fix:1;
253          unsigned flatshade_first:1;
254       };
255       uint64_t all;
256    };
257    uint64_t flat_varyings;
258    struct d3d12_varying_info *varyings;
259 };
260 
261 struct d3d12_tcs_variant_key
262 {
263    unsigned vertices_out;
264    struct d3d12_varying_info *varyings;
265 };
266 
267 struct d3d12_shader_selector {
268    enum pipe_shader_type stage;
269    const nir_shader *initial;
270    struct d3d12_varying_info *initial_output_vars;
271 
272    struct d3d12_shader *first;
273    struct d3d12_shader *current;
274 
275    struct pipe_stream_output_info so_info;
276 
277    unsigned samples_int_textures:1;
278    unsigned compare_with_lod_bias_grad:1;
279    unsigned workgroup_size_variable:1;
280 
281    bool is_variant;
282    union {
283       struct d3d12_gs_variant_key gs_key;
284       struct d3d12_tcs_variant_key tcs_key;
285    };
286 };
287 
288 struct d3d12_shader_selector *
289 d3d12_create_shader(struct d3d12_context *ctx,
290                     enum pipe_shader_type stage,
291                     const struct pipe_shader_state *shader);
292 
293 struct d3d12_shader_selector *
294 d3d12_create_compute_shader(struct d3d12_context *ctx,
295                             const struct pipe_compute_state *shader);
296 
297 void
298 d3d12_shader_free(struct d3d12_shader_selector *shader);
299 
300 void
301 d3d12_select_shader_variants(struct d3d12_context *ctx,
302                              const struct pipe_draw_info *dinfo);
303 
304 void
305 d3d12_select_compute_shader_variants(struct d3d12_context *ctx,
306                                      const struct pipe_grid_info *info);
307 
308 void
309 d3d12_gs_variant_cache_init(struct d3d12_context *ctx);
310 
311 void
312 d3d12_gs_variant_cache_destroy(struct d3d12_context *ctx);
313 
314 struct d3d12_shader_selector *
315 d3d12_get_gs_variant(struct d3d12_context *ctx, struct d3d12_gs_variant_key *key);
316 
317 void
318 d3d12_tcs_variant_cache_init(struct d3d12_context *ctx);
319 
320 void
321 d3d12_tcs_variant_cache_destroy(struct d3d12_context *ctx);
322 
323 struct d3d12_shader_selector *
324 d3d12_get_tcs_variant(struct d3d12_context *ctx, struct d3d12_tcs_variant_key *key);
325 
326 unsigned
327 missing_dual_src_outputs(struct d3d12_context* ctx);
328 
329 bool
330 has_flat_varyings(struct d3d12_context* ctx);
331 
332 bool
333 d3d12_compare_varying_info(const struct d3d12_varying_info *expect, const struct d3d12_varying_info *have);
334 
335 bool
336 manual_depth_range(struct d3d12_context* ctx);
337 
338 #ifdef __cplusplus
339 }
340 #endif
341 
342 #endif
343