1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © Microsoft Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef D3D12_COMPILER_H
25bf215546Sopenharmony_ci#define D3D12_COMPILER_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "dxil_nir_lower_int_samplers.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "pipe/p_defines.h"
30bf215546Sopenharmony_ci#include "pipe/p_state.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "compiler/shader_info.h"
33bf215546Sopenharmony_ci#include "program/prog_statevars.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "nir.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistruct pipe_screen;
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#ifdef __cplusplus
40bf215546Sopenharmony_ciextern "C" {
41bf215546Sopenharmony_ci#endif
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cienum d3d12_state_var {
44bf215546Sopenharmony_ci   D3D12_STATE_VAR_Y_FLIP = 0,
45bf215546Sopenharmony_ci   D3D12_STATE_VAR_PT_SPRITE,
46bf215546Sopenharmony_ci   D3D12_STATE_VAR_DRAW_PARAMS,
47bf215546Sopenharmony_ci   D3D12_STATE_VAR_DEPTH_TRANSFORM,
48bf215546Sopenharmony_ci   D3D12_STATE_VAR_DEFAULT_INNER_TESS_LEVEL,
49bf215546Sopenharmony_ci   D3D12_STATE_VAR_DEFAULT_OUTER_TESS_LEVEL,
50bf215546Sopenharmony_ci   D3D12_STATE_VAR_PATCH_VERTICES_IN,
51bf215546Sopenharmony_ci   D3D12_MAX_GRAPHICS_STATE_VARS,
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   D3D12_STATE_VAR_NUM_WORKGROUPS = 0,
54bf215546Sopenharmony_ci   D3D12_STATE_VAR_TRANSFORM_GENERIC0,
55bf215546Sopenharmony_ci   D3D12_MAX_COMPUTE_STATE_VARS,
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   D3D12_MAX_STATE_VARS = MAX2(D3D12_MAX_GRAPHICS_STATE_VARS, D3D12_MAX_COMPUTE_STATE_VARS)
58bf215546Sopenharmony_ci};
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci#define D3D12_MAX_POINT_SIZE 255.0f
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ciconst void *
63bf215546Sopenharmony_cid3d12_get_compiler_options(struct pipe_screen *screen,
64bf215546Sopenharmony_ci                           enum pipe_shader_ir ir,
65bf215546Sopenharmony_ci                           enum pipe_shader_type shader);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_cistruct d3d12_varying_info {
68bf215546Sopenharmony_ci   struct {
69bf215546Sopenharmony_ci      const struct glsl_type *types[4];
70bf215546Sopenharmony_ci      uint8_t location_frac_mask:2;
71bf215546Sopenharmony_ci      uint8_t patch:1;
72bf215546Sopenharmony_ci      struct {
73bf215546Sopenharmony_ci         unsigned interpolation:3;   // INTERP_MODE_COUNT = 5
74bf215546Sopenharmony_ci         unsigned driver_location:6; // VARYING_SLOT_MAX = 64
75bf215546Sopenharmony_ci         unsigned compact:1;
76bf215546Sopenharmony_ci      } vars[4];
77bf215546Sopenharmony_ci   } slots[VARYING_SLOT_MAX];
78bf215546Sopenharmony_ci   uint64_t mask;
79bf215546Sopenharmony_ci};
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_cistruct d3d12_image_format_conversion_info {
82bf215546Sopenharmony_ci   enum pipe_format view_format, emulated_format;
83bf215546Sopenharmony_ci};
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_cistruct d3d12_shader_key {
86bf215546Sopenharmony_ci   uint32_t hash;
87bf215546Sopenharmony_ci   enum pipe_shader_type stage;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   struct d3d12_varying_info required_varying_inputs;
90bf215546Sopenharmony_ci   struct d3d12_varying_info required_varying_outputs;
91bf215546Sopenharmony_ci   uint64_t next_varying_inputs;
92bf215546Sopenharmony_ci   uint64_t prev_varying_outputs;
93bf215546Sopenharmony_ci   unsigned last_vertex_processing_stage : 1;
94bf215546Sopenharmony_ci   unsigned invert_depth : 16;
95bf215546Sopenharmony_ci   unsigned halfz : 1;
96bf215546Sopenharmony_ci   unsigned samples_int_textures : 1;
97bf215546Sopenharmony_ci   unsigned input_clip_size : 4;
98bf215546Sopenharmony_ci   unsigned tex_saturate_s : PIPE_MAX_SAMPLERS;
99bf215546Sopenharmony_ci   unsigned tex_saturate_r : PIPE_MAX_SAMPLERS;
100bf215546Sopenharmony_ci   unsigned tex_saturate_t : PIPE_MAX_SAMPLERS;
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   struct {
103bf215546Sopenharmony_ci      unsigned needs_format_emulation:1;
104bf215546Sopenharmony_ci      enum pipe_format format_conversion[PIPE_MAX_ATTRIBS];
105bf215546Sopenharmony_ci   } vs;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   struct {
108bf215546Sopenharmony_ci      unsigned sprite_coord_enable:24;
109bf215546Sopenharmony_ci      unsigned sprite_origin_upper_left:1;
110bf215546Sopenharmony_ci      unsigned point_pos_stream_out:1;
111bf215546Sopenharmony_ci      unsigned writes_psize:1;
112bf215546Sopenharmony_ci      unsigned point_size_per_vertex:1;
113bf215546Sopenharmony_ci      unsigned aa_point:1;
114bf215546Sopenharmony_ci      unsigned stream_output_factor:3;
115bf215546Sopenharmony_ci      unsigned primitive_id:1;
116bf215546Sopenharmony_ci      unsigned triangle_strip:1;
117bf215546Sopenharmony_ci   } gs;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   struct {
120bf215546Sopenharmony_ci      unsigned primitive_mode:2;
121bf215546Sopenharmony_ci      unsigned ccw:1;
122bf215546Sopenharmony_ci      unsigned point_mode:1;
123bf215546Sopenharmony_ci      unsigned spacing:2;
124bf215546Sopenharmony_ci      unsigned patch_vertices_in:5;
125bf215546Sopenharmony_ci      struct d3d12_varying_info required_patch_outputs;
126bf215546Sopenharmony_ci      uint32_t next_patch_inputs;
127bf215546Sopenharmony_ci   } hs;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   struct {
130bf215546Sopenharmony_ci      unsigned tcs_vertices_out;
131bf215546Sopenharmony_ci      struct d3d12_varying_info required_patch_inputs;
132bf215546Sopenharmony_ci      uint32_t prev_patch_outputs;
133bf215546Sopenharmony_ci   } ds;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   struct {
136bf215546Sopenharmony_ci      unsigned missing_dual_src_outputs : 2;
137bf215546Sopenharmony_ci      unsigned frag_result_color_lowering : 4;
138bf215546Sopenharmony_ci      unsigned cast_to_uint : 1;
139bf215546Sopenharmony_ci      unsigned cast_to_int : 1;
140bf215546Sopenharmony_ci      unsigned provoking_vertex : 2;
141bf215546Sopenharmony_ci      unsigned manual_depth_range : 1;
142bf215546Sopenharmony_ci      unsigned polygon_stipple : 1;
143bf215546Sopenharmony_ci      unsigned remap_front_facing : 1;
144bf215546Sopenharmony_ci      unsigned multisample_disabled : 1;
145bf215546Sopenharmony_ci   } fs;
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   struct {
148bf215546Sopenharmony_ci      unsigned workgroup_size[3];
149bf215546Sopenharmony_ci   } cs;
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   int n_texture_states;
152bf215546Sopenharmony_ci   dxil_wrap_sampler_state tex_wrap_states[PIPE_MAX_SHADER_SAMPLER_VIEWS];
153bf215546Sopenharmony_ci   dxil_texture_swizzle_state swizzle_state[PIPE_MAX_SHADER_SAMPLER_VIEWS];
154bf215546Sopenharmony_ci   enum compare_func sampler_compare_funcs[PIPE_MAX_SHADER_SAMPLER_VIEWS];
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   int n_images;
157bf215546Sopenharmony_ci   struct d3d12_image_format_conversion_info image_format_conversion[PIPE_MAX_SHADER_IMAGES];
158bf215546Sopenharmony_ci};
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_cistruct d3d12_shader {
161bf215546Sopenharmony_ci   void *bytecode;
162bf215546Sopenharmony_ci   size_t bytecode_length;
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   nir_shader *nir;
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci   struct {
167bf215546Sopenharmony_ci      unsigned binding;
168bf215546Sopenharmony_ci   } cb_bindings[PIPE_MAX_CONSTANT_BUFFERS];
169bf215546Sopenharmony_ci   size_t num_cb_bindings;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   struct {
172bf215546Sopenharmony_ci      enum d3d12_state_var var;
173bf215546Sopenharmony_ci      unsigned offset;
174bf215546Sopenharmony_ci   } state_vars[D3D12_MAX_STATE_VARS];
175bf215546Sopenharmony_ci   unsigned num_state_vars;
176bf215546Sopenharmony_ci   size_t state_vars_size;
177bf215546Sopenharmony_ci   bool state_vars_used;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   struct {
180bf215546Sopenharmony_ci      uint32_t dimension;
181bf215546Sopenharmony_ci   } srv_bindings[PIPE_MAX_SHADER_SAMPLER_VIEWS];
182bf215546Sopenharmony_ci   size_t begin_srv_binding;
183bf215546Sopenharmony_ci   size_t end_srv_binding;
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   struct {
186bf215546Sopenharmony_ci      enum pipe_format format;
187bf215546Sopenharmony_ci      uint32_t dimension;
188bf215546Sopenharmony_ci   } uav_bindings[PIPE_MAX_SHADER_IMAGES];
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci   bool has_default_ubo0;
191bf215546Sopenharmony_ci   unsigned pstipple_binding;
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   struct d3d12_shader_key key;
194bf215546Sopenharmony_ci   struct d3d12_shader *next_variant;
195bf215546Sopenharmony_ci};
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_cistruct d3d12_gs_variant_key
198bf215546Sopenharmony_ci{
199bf215546Sopenharmony_ci   unsigned passthrough:1;
200bf215546Sopenharmony_ci   unsigned provoking_vertex:3;
201bf215546Sopenharmony_ci   unsigned alternate_tri:1;
202bf215546Sopenharmony_ci   unsigned fill_mode:2;
203bf215546Sopenharmony_ci   unsigned cull_mode:2;
204bf215546Sopenharmony_ci   unsigned has_front_face:1;
205bf215546Sopenharmony_ci   unsigned front_ccw:1;
206bf215546Sopenharmony_ci   unsigned edge_flag_fix:1;
207bf215546Sopenharmony_ci   unsigned flatshade_first:1;
208bf215546Sopenharmony_ci   uint64_t flat_varyings;
209bf215546Sopenharmony_ci   struct d3d12_varying_info varyings;
210bf215546Sopenharmony_ci};
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_cistruct d3d12_tcs_variant_key
213bf215546Sopenharmony_ci{
214bf215546Sopenharmony_ci   unsigned vertices_out;
215bf215546Sopenharmony_ci   struct d3d12_varying_info varyings;
216bf215546Sopenharmony_ci};
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_cistruct d3d12_shader_selector {
219bf215546Sopenharmony_ci   enum pipe_shader_type stage;
220bf215546Sopenharmony_ci   nir_shader *initial;
221bf215546Sopenharmony_ci   struct d3d12_shader *first;
222bf215546Sopenharmony_ci   struct d3d12_shader *current;
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci   struct pipe_stream_output_info so_info;
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci   unsigned samples_int_textures:1;
227bf215546Sopenharmony_ci   unsigned compare_with_lod_bias_grad:1;
228bf215546Sopenharmony_ci   unsigned workgroup_size_variable:1;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   bool is_variant;
231bf215546Sopenharmony_ci   union {
232bf215546Sopenharmony_ci      struct d3d12_gs_variant_key gs_key;
233bf215546Sopenharmony_ci      struct d3d12_tcs_variant_key tcs_key;
234bf215546Sopenharmony_ci   };
235bf215546Sopenharmony_ci};
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_cistruct d3d12_context;
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_cistruct d3d12_shader_selector *
240bf215546Sopenharmony_cid3d12_create_shader(struct d3d12_context *ctx,
241bf215546Sopenharmony_ci                    enum pipe_shader_type stage,
242bf215546Sopenharmony_ci                    const struct pipe_shader_state *shader);
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_cistruct d3d12_shader_selector *
245bf215546Sopenharmony_cid3d12_create_compute_shader(struct d3d12_context *ctx,
246bf215546Sopenharmony_ci                            const struct pipe_compute_state *shader);
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_civoid
249bf215546Sopenharmony_cid3d12_shader_free(struct d3d12_shader_selector *shader);
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_civoid
252bf215546Sopenharmony_cid3d12_select_shader_variants(struct d3d12_context *ctx,
253bf215546Sopenharmony_ci                             const struct pipe_draw_info *dinfo);
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_civoid
256bf215546Sopenharmony_cid3d12_select_compute_shader_variants(struct d3d12_context *ctx,
257bf215546Sopenharmony_ci                                     const struct pipe_grid_info *info);
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_civoid
260bf215546Sopenharmony_cid3d12_gs_variant_cache_init(struct d3d12_context *ctx);
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_civoid
263bf215546Sopenharmony_cid3d12_gs_variant_cache_destroy(struct d3d12_context *ctx);
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_cistruct d3d12_shader_selector *
266bf215546Sopenharmony_cid3d12_get_gs_variant(struct d3d12_context *ctx, struct d3d12_gs_variant_key *key);
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_civoid
269bf215546Sopenharmony_cid3d12_tcs_variant_cache_init(struct d3d12_context *ctx);
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_civoid
272bf215546Sopenharmony_cid3d12_tcs_variant_cache_destroy(struct d3d12_context *ctx);
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_cistruct d3d12_shader_selector *
275bf215546Sopenharmony_cid3d12_get_tcs_variant(struct d3d12_context *ctx, struct d3d12_tcs_variant_key *key);
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci#ifdef __cplusplus
278bf215546Sopenharmony_ci}
279bf215546Sopenharmony_ci#endif
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci#endif
282