• 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 "dxil_nir_lower_int_samplers.h"
28 
29 #include "pipe/p_defines.h"
30 #include "pipe/p_state.h"
31 
32 #include "compiler/shader_info.h"
33 #include "program/prog_statevars.h"
34 
35 #include "nir.h"
36 
37 struct pipe_screen;
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 enum d3d12_state_var {
44    D3D12_STATE_VAR_Y_FLIP = 0,
45    D3D12_STATE_VAR_PT_SPRITE,
46    D3D12_STATE_VAR_DRAW_PARAMS,
47    D3D12_STATE_VAR_DEPTH_TRANSFORM,
48    D3D12_STATE_VAR_DEFAULT_INNER_TESS_LEVEL,
49    D3D12_STATE_VAR_DEFAULT_OUTER_TESS_LEVEL,
50    D3D12_STATE_VAR_PATCH_VERTICES_IN,
51    D3D12_MAX_GRAPHICS_STATE_VARS,
52 
53    D3D12_STATE_VAR_NUM_WORKGROUPS = 0,
54    D3D12_STATE_VAR_TRANSFORM_GENERIC0,
55    D3D12_MAX_COMPUTE_STATE_VARS,
56 
57    D3D12_MAX_STATE_VARS = MAX2(D3D12_MAX_GRAPHICS_STATE_VARS, D3D12_MAX_COMPUTE_STATE_VARS)
58 };
59 
60 #define D3D12_MAX_POINT_SIZE 255.0f
61 
62 const void *
63 d3d12_get_compiler_options(struct pipe_screen *screen,
64                            enum pipe_shader_ir ir,
65                            enum pipe_shader_type shader);
66 
67 struct d3d12_varying_info {
68    struct {
69       const struct glsl_type *types[4];
70       uint8_t location_frac_mask:2;
71       uint8_t patch:1;
72       struct {
73          unsigned interpolation:3;   // INTERP_MODE_COUNT = 5
74          unsigned driver_location:6; // VARYING_SLOT_MAX = 64
75          unsigned compact:1;
76       } vars[4];
77    } slots[VARYING_SLOT_MAX];
78    uint64_t mask;
79 };
80 
81 struct d3d12_image_format_conversion_info {
82    enum pipe_format view_format, emulated_format;
83 };
84 
85 struct d3d12_shader_key {
86    uint32_t hash;
87    enum pipe_shader_type stage;
88 
89    struct d3d12_varying_info required_varying_inputs;
90    struct d3d12_varying_info required_varying_outputs;
91    uint64_t next_varying_inputs;
92    uint64_t prev_varying_outputs;
93    unsigned last_vertex_processing_stage : 1;
94    unsigned invert_depth : 16;
95    unsigned halfz : 1;
96    unsigned samples_int_textures : 1;
97    unsigned input_clip_size : 4;
98    unsigned tex_saturate_s : PIPE_MAX_SAMPLERS;
99    unsigned tex_saturate_r : PIPE_MAX_SAMPLERS;
100    unsigned tex_saturate_t : PIPE_MAX_SAMPLERS;
101 
102    struct {
103       unsigned needs_format_emulation:1;
104       enum pipe_format format_conversion[PIPE_MAX_ATTRIBS];
105    } vs;
106 
107    struct {
108       unsigned sprite_coord_enable:24;
109       unsigned sprite_origin_upper_left:1;
110       unsigned point_pos_stream_out:1;
111       unsigned writes_psize:1;
112       unsigned point_size_per_vertex:1;
113       unsigned aa_point:1;
114       unsigned stream_output_factor:3;
115       unsigned primitive_id:1;
116       unsigned triangle_strip:1;
117    } gs;
118 
119    struct {
120       unsigned primitive_mode:2;
121       unsigned ccw:1;
122       unsigned point_mode:1;
123       unsigned spacing:2;
124       unsigned patch_vertices_in:5;
125       struct d3d12_varying_info required_patch_outputs;
126       uint32_t next_patch_inputs;
127    } hs;
128 
129    struct {
130       unsigned tcs_vertices_out;
131       struct d3d12_varying_info required_patch_inputs;
132       uint32_t prev_patch_outputs;
133    } ds;
134 
135    struct {
136       unsigned missing_dual_src_outputs : 2;
137       unsigned frag_result_color_lowering : 4;
138       unsigned cast_to_uint : 1;
139       unsigned cast_to_int : 1;
140       unsigned provoking_vertex : 2;
141       unsigned manual_depth_range : 1;
142       unsigned polygon_stipple : 1;
143       unsigned remap_front_facing : 1;
144       unsigned multisample_disabled : 1;
145    } fs;
146 
147    struct {
148       unsigned workgroup_size[3];
149    } cs;
150 
151    int n_texture_states;
152    dxil_wrap_sampler_state tex_wrap_states[PIPE_MAX_SHADER_SAMPLER_VIEWS];
153    dxil_texture_swizzle_state swizzle_state[PIPE_MAX_SHADER_SAMPLER_VIEWS];
154    enum compare_func sampler_compare_funcs[PIPE_MAX_SHADER_SAMPLER_VIEWS];
155 
156    int n_images;
157    struct d3d12_image_format_conversion_info image_format_conversion[PIPE_MAX_SHADER_IMAGES];
158 };
159 
160 struct d3d12_shader {
161    void *bytecode;
162    size_t bytecode_length;
163 
164    nir_shader *nir;
165 
166    struct {
167       unsigned binding;
168    } cb_bindings[PIPE_MAX_CONSTANT_BUFFERS];
169    size_t num_cb_bindings;
170 
171    struct {
172       enum d3d12_state_var var;
173       unsigned offset;
174    } state_vars[D3D12_MAX_STATE_VARS];
175    unsigned num_state_vars;
176    size_t state_vars_size;
177    bool state_vars_used;
178 
179    struct {
180       uint32_t dimension;
181    } srv_bindings[PIPE_MAX_SHADER_SAMPLER_VIEWS];
182    size_t begin_srv_binding;
183    size_t end_srv_binding;
184 
185    struct {
186       enum pipe_format format;
187       uint32_t dimension;
188    } uav_bindings[PIPE_MAX_SHADER_IMAGES];
189 
190    bool has_default_ubo0;
191    unsigned pstipple_binding;
192 
193    struct d3d12_shader_key key;
194    struct d3d12_shader *next_variant;
195 };
196 
197 struct d3d12_gs_variant_key
198 {
199    unsigned passthrough:1;
200    unsigned provoking_vertex:3;
201    unsigned alternate_tri:1;
202    unsigned fill_mode:2;
203    unsigned cull_mode:2;
204    unsigned has_front_face:1;
205    unsigned front_ccw:1;
206    unsigned edge_flag_fix:1;
207    unsigned flatshade_first:1;
208    uint64_t flat_varyings;
209    struct d3d12_varying_info varyings;
210 };
211 
212 struct d3d12_tcs_variant_key
213 {
214    unsigned vertices_out;
215    struct d3d12_varying_info varyings;
216 };
217 
218 struct d3d12_shader_selector {
219    enum pipe_shader_type stage;
220    nir_shader *initial;
221    struct d3d12_shader *first;
222    struct d3d12_shader *current;
223 
224    struct pipe_stream_output_info so_info;
225 
226    unsigned samples_int_textures:1;
227    unsigned compare_with_lod_bias_grad:1;
228    unsigned workgroup_size_variable:1;
229 
230    bool is_variant;
231    union {
232       struct d3d12_gs_variant_key gs_key;
233       struct d3d12_tcs_variant_key tcs_key;
234    };
235 };
236 
237 struct d3d12_context;
238 
239 struct d3d12_shader_selector *
240 d3d12_create_shader(struct d3d12_context *ctx,
241                     enum pipe_shader_type stage,
242                     const struct pipe_shader_state *shader);
243 
244 struct d3d12_shader_selector *
245 d3d12_create_compute_shader(struct d3d12_context *ctx,
246                             const struct pipe_compute_state *shader);
247 
248 void
249 d3d12_shader_free(struct d3d12_shader_selector *shader);
250 
251 void
252 d3d12_select_shader_variants(struct d3d12_context *ctx,
253                              const struct pipe_draw_info *dinfo);
254 
255 void
256 d3d12_select_compute_shader_variants(struct d3d12_context *ctx,
257                                      const struct pipe_grid_info *info);
258 
259 void
260 d3d12_gs_variant_cache_init(struct d3d12_context *ctx);
261 
262 void
263 d3d12_gs_variant_cache_destroy(struct d3d12_context *ctx);
264 
265 struct d3d12_shader_selector *
266 d3d12_get_gs_variant(struct d3d12_context *ctx, struct d3d12_gs_variant_key *key);
267 
268 void
269 d3d12_tcs_variant_cache_init(struct d3d12_context *ctx);
270 
271 void
272 d3d12_tcs_variant_cache_destroy(struct d3d12_context *ctx);
273 
274 struct d3d12_shader_selector *
275 d3d12_get_tcs_variant(struct d3d12_context *ctx, struct d3d12_tcs_variant_key *key);
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif
282