1 /*
2  * Copyright © 2012 Intel 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 BLORP_PRIV_H
25 #define BLORP_PRIV_H
26 
27 #include <stdint.h>
28 
29 #include "compiler/nir/nir.h"
30 #include "compiler/brw_compiler.h"
31 
32 #include "blorp.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * Binding table indices used by BLORP.
40  */
41 enum {
42    BLORP_RENDERBUFFER_BT_INDEX,
43    BLORP_TEXTURE_BT_INDEX,
44    BLORP_NUM_BT_ENTRIES
45 };
46 
47 struct brw_blorp_surface_info
48 {
49    bool enabled;
50 
51    struct isl_surf surf;
52    struct blorp_address addr;
53 
54    struct isl_surf aux_surf;
55    struct blorp_address aux_addr;
56    enum isl_aux_usage aux_usage;
57 
58    union isl_color_value clear_color;
59    struct blorp_address clear_color_addr;
60 
61    struct isl_view view;
62 
63    /* Z offset into a 3-D texture or slice of a 2-D array texture. */
64    float z_offset;
65 
66    uint32_t tile_x_sa, tile_y_sa;
67 };
68 
69 void
70 brw_blorp_surface_info_init(struct blorp_context *blorp,
71                             struct brw_blorp_surface_info *info,
72                             const struct blorp_surf *surf,
73                             unsigned int level, float layer,
74                             enum isl_format format, bool is_render_target);
75 void
76 blorp_surf_convert_to_single_slice(const struct isl_device *isl_dev,
77                                    struct brw_blorp_surface_info *info);
78 void
79 surf_fake_rgb_with_red(const struct isl_device *isl_dev,
80                        struct brw_blorp_surface_info *info);
81 void
82 blorp_surf_convert_to_uncompressed(const struct isl_device *isl_dev,
83                                    struct brw_blorp_surface_info *info,
84                                    uint32_t *x, uint32_t *y,
85                                    uint32_t *width, uint32_t *height);
86 void
87 blorp_surf_fake_interleaved_msaa(const struct isl_device *isl_dev,
88                                  struct brw_blorp_surface_info *info);
89 void
90 blorp_surf_retile_w_to_y(const struct isl_device *isl_dev,
91                          struct brw_blorp_surface_info *info);
92 
93 
94 struct brw_blorp_coord_transform
95 {
96    float multiplier;
97    float offset;
98 };
99 
100 /**
101  * Bounding rectangle telling pixel discard which pixels are not to be
102  * touched. This is needed in when surfaces are configured as something else
103  * what they really are:
104  *
105  *    - writing W-tiled stencil as Y-tiled
106  *    - writing interleaved multisampled as single sampled.
107  *
108  * See blorp_nir_discard_if_outside_rect().
109  */
110 struct brw_blorp_discard_rect
111 {
112    uint32_t x0;
113    uint32_t x1;
114    uint32_t y0;
115    uint32_t y1;
116 };
117 
118 /**
119  * Grid needed for blended and scaled blits of integer formats, see
120  * blorp_nir_manual_blend_bilinear().
121  */
122 struct brw_blorp_rect_grid
123 {
124    float x1;
125    float y1;
126    float pad[2];
127 };
128 
129 struct blorp_surf_offset {
130    uint32_t x;
131    uint32_t y;
132 };
133 
134 struct brw_blorp_wm_inputs
135 {
136    uint32_t clear_color[4];
137 
138    struct brw_blorp_discard_rect discard_rect;
139    struct brw_blorp_rect_grid rect_grid;
140    struct brw_blorp_coord_transform coord_transform[2];
141 
142    struct blorp_surf_offset src_offset;
143    struct blorp_surf_offset dst_offset;
144 
145    /* (1/width, 1/height) for the source surface */
146    float src_inv_size[2];
147 
148    /* Minimum layer setting works for all the textures types but texture_3d
149     * for which the setting has no effect. Use the z-coordinate instead.
150     */
151    float src_z;
152 
153    /* Pad out to an integral number of registers */
154    uint32_t pad[1];
155 };
156 
157 #define BLORP_CREATE_NIR_INPUT(shader, name, type) ({ \
158    nir_variable *input = nir_variable_create((shader), nir_var_shader_in, \
159                                              type, #name); \
160    if ((shader)->info.stage == MESA_SHADER_FRAGMENT) \
161       input->data.interpolation = INTERP_MODE_FLAT; \
162    input->data.location = VARYING_SLOT_VAR0 + \
163       offsetof(struct brw_blorp_wm_inputs, name) / (4 * sizeof(float)); \
164    input->data.location_frac = \
165       (offsetof(struct brw_blorp_wm_inputs, name) / sizeof(float)) % 4; \
166    input; \
167 })
168 
169 struct blorp_vs_inputs {
170    uint32_t base_layer;
171    uint32_t _instance_id; /* Set in hardware by SGVS */
172    uint32_t pad[2];
173 };
174 
175 static inline unsigned
brw_blorp_get_urb_length(const struct brw_wm_prog_data * prog_data)176 brw_blorp_get_urb_length(const struct brw_wm_prog_data *prog_data)
177 {
178    if (prog_data == NULL)
179       return 1;
180 
181    /* From the BSpec: 3D Pipeline - Strips and Fans - 3DSTATE_SBE
182     *
183     * read_length = ceiling((max_source_attr+1)/2)
184     */
185    return MAX2((prog_data->num_varying_inputs + 1) / 2, 1);
186 }
187 
188 struct blorp_params
189 {
190    uint32_t x0;
191    uint32_t y0;
192    uint32_t x1;
193    uint32_t y1;
194    float z;
195    uint8_t stencil_mask;
196    uint8_t stencil_ref;
197    struct brw_blorp_surface_info depth;
198    struct brw_blorp_surface_info stencil;
199    uint32_t depth_format;
200    struct brw_blorp_surface_info src;
201    struct brw_blorp_surface_info dst;
202    enum isl_aux_op hiz_op;
203    bool full_surface_hiz_op;
204    enum isl_aux_op fast_clear_op;
205    bool color_write_disable[4];
206    struct brw_blorp_wm_inputs wm_inputs;
207    struct blorp_vs_inputs vs_inputs;
208    bool dst_clear_color_as_input;
209    unsigned num_samples;
210    unsigned num_draw_buffers;
211    unsigned num_layers;
212    uint32_t vs_prog_kernel;
213    struct brw_vs_prog_data *vs_prog_data;
214    uint32_t sf_prog_kernel;
215    struct brw_sf_prog_data *sf_prog_data;
216    uint32_t wm_prog_kernel;
217    struct brw_wm_prog_data *wm_prog_data;
218 
219    bool use_pre_baked_binding_table;
220    uint32_t pre_baked_binding_table_offset;
221 };
222 
223 void blorp_params_init(struct blorp_params *params);
224 
225 enum blorp_shader_type {
226    BLORP_SHADER_TYPE_COPY,
227    BLORP_SHADER_TYPE_BLIT,
228    BLORP_SHADER_TYPE_CLEAR,
229    BLORP_SHADER_TYPE_MCS_PARTIAL_RESOLVE,
230    BLORP_SHADER_TYPE_LAYER_OFFSET_VS,
231    BLORP_SHADER_TYPE_GEN4_SF,
232 };
233 
234 struct brw_blorp_blit_prog_key
235 {
236    enum blorp_shader_type shader_type; /* Must be BLORP_SHADER_TYPE_BLIT */
237 
238    /* Number of samples per pixel that have been configured in the surface
239     * state for texturing from.
240     */
241    unsigned tex_samples;
242 
243    /* MSAA layout that has been configured in the surface state for texturing
244     * from.
245     */
246    enum isl_msaa_layout tex_layout;
247 
248    enum isl_aux_usage tex_aux_usage;
249 
250    /* Actual number of samples per pixel in the source image. */
251    unsigned src_samples;
252 
253    /* Actual MSAA layout used by the source image. */
254    enum isl_msaa_layout src_layout;
255 
256    /* The swizzle to apply to the source in the shader */
257    struct isl_swizzle src_swizzle;
258 
259    /* The format of the source if format-specific workarounds are needed
260     * and 0 (ISL_FORMAT_R32G32B32A32_FLOAT) if the destination is natively
261     * renderable.
262     */
263    enum isl_format src_format;
264 
265    /* True if the source requires normalized coordinates */
266    bool src_coords_normalized;
267 
268    /* Number of samples per pixel that have been configured in the render
269     * target.
270     */
271    unsigned rt_samples;
272 
273    /* MSAA layout that has been configured in the render target. */
274    enum isl_msaa_layout rt_layout;
275 
276    /* Actual number of samples per pixel in the destination image. */
277    unsigned dst_samples;
278 
279    /* Actual MSAA layout used by the destination image. */
280    enum isl_msaa_layout dst_layout;
281 
282    /* The swizzle to apply to the destination in the shader */
283    struct isl_swizzle dst_swizzle;
284 
285    /* The format of the destination if format-specific workarounds are needed
286     * and 0 (ISL_FORMAT_R32G32B32A32_FLOAT) if the destination is natively
287     * renderable.
288     */
289    enum isl_format dst_format;
290 
291    /* Whether or not the format workarounds are a bitcast operation */
292    bool format_bit_cast;
293 
294    /** True if we need to perform SINT -> UINT clamping. */
295    bool sint32_to_uint;
296 
297    /** True if we need to perform UINT -> SINT clamping. */
298    bool uint32_to_sint;
299 
300    /* Type of the data to be read from the texture (one of
301     * nir_type_(int|uint|float)).
302     */
303    nir_alu_type texture_data_type;
304 
305    /* True if the source image is W tiled.  If true, the surface state for the
306     * source image must be configured as Y tiled, and tex_samples must be 0.
307     */
308    bool src_tiled_w;
309 
310    /* True if the destination image is W tiled.  If true, the surface state
311     * for the render target must be configured as Y tiled, and rt_samples must
312     * be 0.
313     */
314    bool dst_tiled_w;
315 
316    /* True if the destination is an RGB format.  If true, the surface state
317     * for the render target must be configured as red with three times the
318     * normal width.  We need to do this because you cannot render to
319     * non-power-of-two formats.
320     */
321    bool dst_rgb;
322 
323    isl_surf_usage_flags_t dst_usage;
324 
325    enum blorp_filter filter;
326 
327    /* True if the rectangle being sent through the rendering pipeline might be
328     * larger than the destination rectangle, so the WM program should kill any
329     * pixels that are outside the destination rectangle.
330     */
331    bool use_kill;
332 
333    /**
334     * True if the WM program should be run in MSDISPMODE_PERSAMPLE with more
335     * than one sample per pixel.
336     */
337    bool persample_msaa_dispatch;
338 
339    /* True if this blit operation may involve intratile offsets on the source.
340     * In this case, we need to add the offset before texturing.
341     */
342    bool need_src_offset;
343 
344    /* True if this blit operation may involve intratile offsets on the
345     * destination.  In this case, we need to add the offset to gl_FragCoord.
346     */
347    bool need_dst_offset;
348 
349    /* Scale factors between the pixel grid and the grid of samples. We're
350     * using grid of samples for bilinear filetring in multisample scaled blits.
351     */
352    float x_scale;
353    float y_scale;
354 };
355 
356 /**
357  * \name BLORP internals
358  * \{
359  *
360  * Used internally by gen6_blorp_exec() and gen7_blorp_exec().
361  */
362 
363 void brw_blorp_init_wm_prog_key(struct brw_wm_prog_key *wm_key);
364 
365 const char *blorp_shader_type_to_name(enum blorp_shader_type type);
366 
367 const unsigned *
368 blorp_compile_fs(struct blorp_context *blorp, void *mem_ctx,
369                  struct nir_shader *nir,
370                  struct brw_wm_prog_key *wm_key,
371                  bool use_repclear,
372                  struct brw_wm_prog_data *wm_prog_data);
373 
374 const unsigned *
375 blorp_compile_vs(struct blorp_context *blorp, void *mem_ctx,
376                  struct nir_shader *nir,
377                  struct brw_vs_prog_data *vs_prog_data);
378 
379 bool
380 blorp_ensure_sf_program(struct blorp_batch *batch,
381                         struct blorp_params *params);
382 
383 /** \} */
384 
385 #ifdef __cplusplus
386 } /* end extern "C" */
387 #endif /* __cplusplus */
388 
389 #endif /* BLORP_PRIV_H */
390