i965/skl: Don't use ALL_SLICES_AT_EACH_LOD
[mesa.git] / src / mesa / drivers / dri / i965 / gen8_surface_state.c
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 #include "main/blend.h"
25 #include "main/mtypes.h"
26 #include "main/samplerobj.h"
27 #include "main/texformat.h"
28 #include "main/teximage.h"
29 #include "program/prog_parameter.h"
30
31 #include "intel_mipmap_tree.h"
32 #include "intel_batchbuffer.h"
33 #include "intel_tex.h"
34 #include "intel_fbo.h"
35 #include "intel_buffer_objects.h"
36
37 #include "brw_context.h"
38 #include "brw_state.h"
39 #include "brw_defines.h"
40 #include "brw_wm.h"
41
42 /**
43 * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
44 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
45 *
46 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
47 * 0 1 2 3 4 5
48 * 4 5 6 7 0 1
49 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
50 *
51 * which is simply adding 4 then modding by 8 (or anding with 7).
52 */
53 static unsigned
54 swizzle_to_scs(unsigned swizzle)
55 {
56 return (swizzle + 4) & 7;
57 }
58
59 static uint32_t
60 surface_tiling_mode(uint32_t tiling)
61 {
62 switch (tiling) {
63 case I915_TILING_X:
64 return GEN8_SURFACE_TILING_X;
65 case I915_TILING_Y:
66 return GEN8_SURFACE_TILING_Y;
67 default:
68 return GEN8_SURFACE_TILING_NONE;
69 }
70 }
71
72 static unsigned
73 vertical_alignment(struct intel_mipmap_tree *mt)
74 {
75 switch (mt->align_h) {
76 case 4:
77 return GEN8_SURFACE_VALIGN_4;
78 case 8:
79 return GEN8_SURFACE_VALIGN_8;
80 case 16:
81 return GEN8_SURFACE_VALIGN_16;
82 default:
83 unreachable("Unsupported vertical surface alignment.");
84 }
85 }
86
87 static unsigned
88 horizontal_alignment(struct intel_mipmap_tree *mt)
89 {
90 switch (mt->align_w) {
91 case 4:
92 return GEN8_SURFACE_HALIGN_4;
93 case 8:
94 return GEN8_SURFACE_HALIGN_8;
95 case 16:
96 return GEN8_SURFACE_HALIGN_16;
97 default:
98 unreachable("Unsupported horizontal surface alignment.");
99 }
100 }
101
102 static uint32_t *
103 allocate_surface_state(struct brw_context *brw, uint32_t *out_offset)
104 {
105 int dwords = brw->gen >= 9 ? 16 : 13;
106 uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
107 dwords * 4, 64, out_offset);
108 memset(surf, 0, dwords * 4);
109 return surf;
110 }
111
112 static void
113 gen8_emit_buffer_surface_state(struct brw_context *brw,
114 uint32_t *out_offset,
115 drm_intel_bo *bo,
116 unsigned buffer_offset,
117 unsigned surface_format,
118 unsigned buffer_size,
119 unsigned pitch,
120 bool rw)
121 {
122 const unsigned mocs = brw->gen >= 9 ? SKL_MOCS_WB : BDW_MOCS_WB;
123 uint32_t *surf = allocate_surface_state(brw, out_offset);
124
125 surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |
126 surface_format << BRW_SURFACE_FORMAT_SHIFT |
127 BRW_SURFACE_RC_READ_WRITE;
128 surf[1] = SET_FIELD(mocs, GEN8_SURFACE_MOCS);
129
130 surf[2] = SET_FIELD((buffer_size - 1) & 0x7f, GEN7_SURFACE_WIDTH) |
131 SET_FIELD(((buffer_size - 1) >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT);
132 if (surface_format == BRW_SURFACEFORMAT_RAW)
133 surf[3] = SET_FIELD(((buffer_size - 1) >> 21) & 0x3ff, BRW_SURFACE_DEPTH);
134 else
135 surf[3] = SET_FIELD(((buffer_size - 1) >> 21) & 0x3f, BRW_SURFACE_DEPTH);
136 surf[3] |= (pitch - 1);
137 surf[7] = SET_FIELD(HSW_SCS_RED, GEN7_SURFACE_SCS_R) |
138 SET_FIELD(HSW_SCS_GREEN, GEN7_SURFACE_SCS_G) |
139 SET_FIELD(HSW_SCS_BLUE, GEN7_SURFACE_SCS_B) |
140 SET_FIELD(HSW_SCS_ALPHA, GEN7_SURFACE_SCS_A);
141 /* reloc */
142 *((uint64_t *) &surf[8]) = (bo ? bo->offset64 : 0) + buffer_offset;
143
144 /* Emit relocation to surface contents. */
145 if (bo) {
146 drm_intel_bo_emit_reloc(brw->batch.bo, *out_offset + 8 * 4,
147 bo, buffer_offset, I915_GEM_DOMAIN_SAMPLER,
148 rw ? I915_GEM_DOMAIN_SAMPLER : 0);
149 }
150 }
151
152 static void
153 gen8_update_texture_surface(struct gl_context *ctx,
154 unsigned unit,
155 uint32_t *surf_offset,
156 bool for_gather)
157 {
158 struct brw_context *brw = brw_context(ctx);
159 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
160 struct intel_texture_object *intelObj = intel_texture_object(tObj);
161 struct intel_mipmap_tree *mt = intelObj->mt;
162 struct gl_texture_image *firstImage = tObj->Image[0][tObj->BaseLevel];
163 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
164 struct intel_mipmap_tree *aux_mt = NULL;
165 uint32_t aux_mode = 0;
166 mesa_format format = intelObj->_Format;
167 uint32_t mocs_wb = brw->gen >= 9 ? SKL_MOCS_WB : BDW_MOCS_WB;
168
169 if (tObj->Target == GL_TEXTURE_BUFFER) {
170 brw_update_buffer_texture_surface(ctx, unit, surf_offset);
171 return;
172 }
173
174 if (tObj->StencilSampling && firstImage->_BaseFormat == GL_DEPTH_STENCIL) {
175 mt = mt->stencil_mt;
176 format = MESA_FORMAT_S_UINT8;
177 }
178
179 unsigned tiling_mode, pitch;
180 if (format == MESA_FORMAT_S_UINT8) {
181 tiling_mode = GEN8_SURFACE_TILING_W;
182 pitch = 2 * mt->pitch;
183 } else {
184 tiling_mode = surface_tiling_mode(mt->tiling);
185 pitch = mt->pitch;
186 }
187
188 if (mt->mcs_mt) {
189 aux_mt = mt->mcs_mt;
190 aux_mode = GEN8_SURFACE_AUX_MODE_MCS;
191 }
192
193 /* If this is a view with restricted NumLayers, then our effective depth
194 * is not just the miptree depth.
195 */
196 uint32_t effective_depth =
197 (tObj->Immutable && tObj->Target != GL_TEXTURE_3D) ? tObj->NumLayers
198 : mt->logical_depth0;
199
200 uint32_t tex_format = translate_tex_format(brw, format, sampler->sRGBDecode);
201
202 uint32_t *surf = allocate_surface_state(brw, surf_offset);
203
204 surf[0] = translate_tex_target(tObj->Target) << BRW_SURFACE_TYPE_SHIFT |
205 tex_format << BRW_SURFACE_FORMAT_SHIFT |
206 vertical_alignment(mt) |
207 horizontal_alignment(mt) |
208 tiling_mode;
209
210 if (tObj->Target == GL_TEXTURE_CUBE_MAP ||
211 tObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
212 surf[0] |= BRW_SURFACE_CUBEFACE_ENABLES;
213 }
214
215 if (_mesa_is_array_texture(tObj->Target) ||
216 tObj->Target == GL_TEXTURE_CUBE_MAP)
217 surf[0] |= GEN8_SURFACE_IS_ARRAY;
218
219 surf[1] = SET_FIELD(mocs_wb, GEN8_SURFACE_MOCS) | mt->qpitch >> 2;
220
221 surf[2] = SET_FIELD(mt->logical_width0 - 1, GEN7_SURFACE_WIDTH) |
222 SET_FIELD(mt->logical_height0 - 1, GEN7_SURFACE_HEIGHT);
223
224 surf[3] = SET_FIELD(effective_depth - 1, BRW_SURFACE_DEPTH) | (pitch - 1);
225
226 surf[4] = gen7_surface_msaa_bits(mt->num_samples, mt->msaa_layout) |
227 SET_FIELD(tObj->MinLayer, GEN7_SURFACE_MIN_ARRAY_ELEMENT) |
228 SET_FIELD(effective_depth - 1,
229 GEN7_SURFACE_RENDER_TARGET_VIEW_EXTENT);
230
231 surf[5] = SET_FIELD(tObj->MinLevel + tObj->BaseLevel - mt->first_level,
232 GEN7_SURFACE_MIN_LOD) |
233 (intelObj->_MaxLevel - tObj->BaseLevel); /* mip count */
234
235 if (aux_mt) {
236 surf[6] = SET_FIELD(mt->qpitch / 4, GEN8_SURFACE_AUX_QPITCH) |
237 SET_FIELD((aux_mt->pitch / 128) - 1, GEN8_SURFACE_AUX_PITCH) |
238 aux_mode;
239 } else {
240 surf[6] = 0;
241 }
242
243 /* Handling GL_ALPHA as a surface format override breaks 1.30+ style
244 * texturing functions that return a float, as our code generation always
245 * selects the .x channel (which would always be 0).
246 */
247 const bool alpha_depth = tObj->DepthMode == GL_ALPHA &&
248 (firstImage->_BaseFormat == GL_DEPTH_COMPONENT ||
249 firstImage->_BaseFormat == GL_DEPTH_STENCIL);
250
251 surf[7] = mt->fast_clear_color_value;
252
253 const int swizzle =
254 unlikely(alpha_depth) ? SWIZZLE_XYZW : brw_get_texture_swizzle(ctx, tObj);
255 surf[7] |=
256 SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 0)), GEN7_SURFACE_SCS_R) |
257 SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 1)), GEN7_SURFACE_SCS_G) |
258 SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 2)), GEN7_SURFACE_SCS_B) |
259 SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 3)), GEN7_SURFACE_SCS_A);
260
261 *((uint64_t *) &surf[8]) = mt->bo->offset64 + mt->offset; /* reloc */
262
263 if (aux_mt) {
264 *((uint64_t *) &surf[10]) = aux_mt->bo->offset64;
265 drm_intel_bo_emit_reloc(brw->batch.bo, *surf_offset + 10 * 4,
266 aux_mt->bo, 0,
267 I915_GEM_DOMAIN_SAMPLER, 0);
268 } else {
269 surf[10] = 0;
270 surf[11] = 0;
271 }
272 surf[12] = 0;
273
274 /* Emit relocation to surface contents */
275 drm_intel_bo_emit_reloc(brw->batch.bo,
276 *surf_offset + 8 * 4,
277 mt->bo,
278 mt->offset,
279 I915_GEM_DOMAIN_SAMPLER, 0);
280 }
281
282 /**
283 * Creates a null surface.
284 *
285 * This is used when the shader doesn't write to any color output. An FB
286 * write to target 0 will still be emitted, because that's how the thread is
287 * terminated (and computed depth is returned), so we need to have the
288 * hardware discard the target 0 color output..
289 */
290 static void
291 gen8_emit_null_surface_state(struct brw_context *brw,
292 unsigned width,
293 unsigned height,
294 unsigned samples,
295 uint32_t *out_offset)
296 {
297 uint32_t *surf = allocate_surface_state(brw, out_offset);
298
299 surf[0] = BRW_SURFACE_NULL << BRW_SURFACE_TYPE_SHIFT |
300 BRW_SURFACEFORMAT_B8G8R8A8_UNORM << BRW_SURFACE_FORMAT_SHIFT |
301 GEN8_SURFACE_TILING_Y;
302 surf[2] = SET_FIELD(width - 1, GEN7_SURFACE_WIDTH) |
303 SET_FIELD(height - 1, GEN7_SURFACE_HEIGHT);
304 }
305
306 /**
307 * Sets up a surface state structure to point at the given region.
308 * While it is only used for the front/back buffer currently, it should be
309 * usable for further buffers when doing ARB_draw_buffer support.
310 */
311 static void
312 gen8_update_renderbuffer_surface(struct brw_context *brw,
313 struct gl_renderbuffer *rb,
314 bool layered,
315 unsigned unit)
316 {
317 struct gl_context *ctx = &brw->ctx;
318 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
319 struct intel_mipmap_tree *mt = irb->mt;
320 struct intel_mipmap_tree *aux_mt = NULL;
321 uint32_t aux_mode = 0;
322 unsigned width = mt->logical_width0;
323 unsigned height = mt->logical_height0;
324 unsigned pitch = mt->pitch;
325 uint32_t tiling = mt->tiling;
326 uint32_t format = 0;
327 uint32_t surf_type;
328 bool is_array = false;
329 int depth = MAX2(irb->layer_count, 1);
330 const int min_array_element = (mt->format == MESA_FORMAT_S_UINT8) ?
331 irb->mt_layer : (irb->mt_layer / MAX2(mt->num_samples, 1));
332 GLenum gl_target =
333 rb->TexImage ? rb->TexImage->TexObject->Target : GL_TEXTURE_2D;
334 uint32_t surf_index =
335 brw->wm.prog_data->binding_table.render_target_start + unit;
336 /* FINISHME: Use PTE MOCS on Skylake. */
337 uint32_t mocs = brw->gen >= 9 ? SKL_MOCS_WT : BDW_MOCS_PTE;
338
339 intel_miptree_used_for_rendering(mt);
340
341 switch (gl_target) {
342 case GL_TEXTURE_CUBE_MAP_ARRAY:
343 case GL_TEXTURE_CUBE_MAP:
344 surf_type = BRW_SURFACE_2D;
345 is_array = true;
346 depth *= 6;
347 break;
348 case GL_TEXTURE_3D:
349 depth = MAX2(irb->mt->logical_depth0, 1);
350 /* fallthrough */
351 default:
352 surf_type = translate_tex_target(gl_target);
353 is_array = _mesa_tex_target_is_array(gl_target);
354 break;
355 }
356
357 /* _NEW_BUFFERS */
358 /* Render targets can't use IMS layout. Stencil in turn gets configured as
359 * single sampled and indexed manually by the program.
360 */
361 if (mt->format == MESA_FORMAT_S_UINT8) {
362 brw_configure_w_tiled(mt, true, &width, &height, &pitch,
363 &tiling, &format);
364 } else {
365 assert(mt->msaa_layout != INTEL_MSAA_LAYOUT_IMS);
366 assert(brw_render_target_supported(brw, rb));
367 mesa_format rb_format = _mesa_get_render_format(ctx,
368 intel_rb_format(irb));
369 format = brw->render_target_format[rb_format];
370 if (unlikely(!brw->format_supported_as_render_target[rb_format]))
371 _mesa_problem(ctx, "%s: renderbuffer format %s unsupported\n",
372 __func__, _mesa_get_format_name(rb_format));
373 }
374
375 if (mt->mcs_mt) {
376 aux_mt = mt->mcs_mt;
377 aux_mode = GEN8_SURFACE_AUX_MODE_MCS;
378 }
379
380 uint32_t *surf =
381 allocate_surface_state(brw, &brw->wm.base.surf_offset[surf_index]);
382
383 surf[0] = (surf_type << BRW_SURFACE_TYPE_SHIFT) |
384 (is_array ? GEN7_SURFACE_IS_ARRAY : 0) |
385 (format << BRW_SURFACE_FORMAT_SHIFT) |
386 vertical_alignment(mt) |
387 horizontal_alignment(mt) |
388 surface_tiling_mode(tiling);
389
390 surf[1] = SET_FIELD(mocs, GEN8_SURFACE_MOCS) | mt->qpitch >> 2;
391
392 surf[2] = SET_FIELD(width - 1, GEN7_SURFACE_WIDTH) |
393 SET_FIELD(height - 1, GEN7_SURFACE_HEIGHT);
394
395 surf[3] = (depth - 1) << BRW_SURFACE_DEPTH_SHIFT |
396 (pitch - 1); /* Surface Pitch */
397
398 surf[4] = min_array_element << GEN7_SURFACE_MIN_ARRAY_ELEMENT_SHIFT |
399 (depth - 1) << GEN7_SURFACE_RENDER_TARGET_VIEW_EXTENT_SHIFT;
400
401 if (mt->format != MESA_FORMAT_S_UINT8)
402 surf[4] |= gen7_surface_msaa_bits(mt->num_samples, mt->msaa_layout);
403
404 surf[5] = irb->mt_level - irb->mt->first_level;
405
406 if (aux_mt) {
407 surf[6] = SET_FIELD(mt->qpitch / 4, GEN8_SURFACE_AUX_QPITCH) |
408 SET_FIELD((aux_mt->pitch / 128) - 1, GEN8_SURFACE_AUX_PITCH) |
409 aux_mode;
410 } else {
411 surf[6] = 0;
412 }
413
414 surf[7] = mt->fast_clear_color_value |
415 SET_FIELD(HSW_SCS_RED, GEN7_SURFACE_SCS_R) |
416 SET_FIELD(HSW_SCS_GREEN, GEN7_SURFACE_SCS_G) |
417 SET_FIELD(HSW_SCS_BLUE, GEN7_SURFACE_SCS_B) |
418 SET_FIELD(HSW_SCS_ALPHA, GEN7_SURFACE_SCS_A);
419
420 assert(mt->offset % mt->cpp == 0);
421 *((uint64_t *) &surf[8]) = mt->bo->offset64 + mt->offset; /* reloc */
422
423 if (aux_mt) {
424 *((uint64_t *) &surf[10]) = aux_mt->bo->offset64;
425 drm_intel_bo_emit_reloc(brw->batch.bo,
426 brw->wm.base.surf_offset[surf_index] + 10 * 4,
427 aux_mt->bo, 0,
428 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER);
429 } else {
430 surf[10] = 0;
431 surf[11] = 0;
432 }
433 surf[12] = 0;
434
435 drm_intel_bo_emit_reloc(brw->batch.bo,
436 brw->wm.base.surf_offset[surf_index] + 8 * 4,
437 mt->bo,
438 mt->offset,
439 I915_GEM_DOMAIN_RENDER,
440 I915_GEM_DOMAIN_RENDER);
441 }
442
443 void
444 gen8_init_vtable_surface_functions(struct brw_context *brw)
445 {
446 brw->vtbl.update_texture_surface = gen8_update_texture_surface;
447 brw->vtbl.update_renderbuffer_surface = gen8_update_renderbuffer_surface;
448 brw->vtbl.emit_null_surface_state = gen8_emit_null_surface_state;
449 brw->vtbl.emit_buffer_surface_state = gen8_emit_buffer_surface_state;
450 }