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