3164f994dd4c1691db8e2fc2c055d5dc15d15e5b
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_wm_surface_state.c
1 /*
2 * Copyright © 2011 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 #include "main/mtypes.h"
24 #include "main/blend.h"
25 #include "main/samplerobj.h"
26 #include "main/texformat.h"
27 #include "program/prog_parameter.h"
28
29 #include "intel_mipmap_tree.h"
30 #include "intel_batchbuffer.h"
31 #include "intel_tex.h"
32 #include "intel_fbo.h"
33 #include "intel_buffer_objects.h"
34
35 #include "brw_context.h"
36 #include "brw_state.h"
37 #include "brw_defines.h"
38 #include "brw_wm.h"
39
40 /**
41 * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
42 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED)
43 */
44 static unsigned
45 swizzle_to_scs(GLenum swizzle)
46 {
47 switch (swizzle) {
48 case SWIZZLE_X:
49 return HSW_SCS_RED;
50 case SWIZZLE_Y:
51 return HSW_SCS_GREEN;
52 case SWIZZLE_Z:
53 return HSW_SCS_BLUE;
54 case SWIZZLE_W:
55 return HSW_SCS_ALPHA;
56 case SWIZZLE_ZERO:
57 return HSW_SCS_ZERO;
58 case SWIZZLE_ONE:
59 return HSW_SCS_ONE;
60 }
61
62 assert(!"Should not get here: invalid swizzle mode");
63 return HSW_SCS_ZERO;
64 }
65
66 uint32_t
67 gen7_surface_tiling_mode(uint32_t tiling)
68 {
69 switch (tiling) {
70 case I915_TILING_X:
71 return GEN7_SURFACE_TILING_X;
72 case I915_TILING_Y:
73 return GEN7_SURFACE_TILING_Y;
74 default:
75 return GEN7_SURFACE_TILING_NONE;
76 }
77 }
78
79
80 uint32_t
81 gen7_surface_msaa_bits(unsigned num_samples, enum intel_msaa_layout layout)
82 {
83 uint32_t ss4 = 0;
84
85 if (num_samples > 4)
86 ss4 |= GEN7_SURFACE_MULTISAMPLECOUNT_8;
87 else if (num_samples > 1)
88 ss4 |= GEN7_SURFACE_MULTISAMPLECOUNT_4;
89 else
90 ss4 |= GEN7_SURFACE_MULTISAMPLECOUNT_1;
91
92 if (layout == INTEL_MSAA_LAYOUT_IMS)
93 ss4 |= GEN7_SURFACE_MSFMT_DEPTH_STENCIL;
94 else
95 ss4 |= GEN7_SURFACE_MSFMT_MSS;
96
97 return ss4;
98 }
99
100
101 void
102 gen7_set_surface_mcs_info(struct brw_context *brw,
103 uint32_t *surf,
104 uint32_t surf_offset,
105 const struct intel_mipmap_tree *mcs_mt,
106 bool is_render_target)
107 {
108 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
109 *
110 * "The MCS surface must be stored as Tile Y."
111 */
112 assert(mcs_mt->region->tiling == I915_TILING_Y);
113
114 /* Compute the pitch in units of tiles. To do this we need to divide the
115 * pitch in bytes by 128, since a single Y-tile is 128 bytes wide.
116 */
117 unsigned pitch_tiles = mcs_mt->region->pitch / 128;
118
119 /* The upper 20 bits of surface state DWORD 6 are the upper 20 bits of the
120 * GPU address of the MCS buffer; the lower 12 bits contain other control
121 * information. Since buffer addresses are always on 4k boundaries (and
122 * thus have their lower 12 bits zero), we can use an ordinary reloc to do
123 * the necessary address translation.
124 */
125 assert ((mcs_mt->region->bo->offset & 0xfff) == 0);
126
127 surf[6] = GEN7_SURFACE_MCS_ENABLE |
128 SET_FIELD(pitch_tiles - 1, GEN7_SURFACE_MCS_PITCH) |
129 mcs_mt->region->bo->offset;
130
131 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
132 surf_offset + 6 * 4,
133 mcs_mt->region->bo,
134 surf[6] & 0xfff,
135 is_render_target ? I915_GEM_DOMAIN_RENDER
136 : I915_GEM_DOMAIN_SAMPLER,
137 is_render_target ? I915_GEM_DOMAIN_RENDER : 0);
138 }
139
140
141 void
142 gen7_check_surface_setup(uint32_t *surf, bool is_render_target)
143 {
144 unsigned num_multisamples = surf[4] & INTEL_MASK(5, 3);
145 unsigned multisampled_surface_storage_format = surf[4] & (1 << 6);
146 unsigned surface_array_spacing = surf[0] & (1 << 10);
147 bool is_multisampled = num_multisamples != GEN7_SURFACE_MULTISAMPLECOUNT_1;
148
149 (void) surface_array_spacing;
150
151 /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
152 * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Surface Array
153 * Spacing:
154 *
155 * If Multisampled Surface Storage Format is MSFMT_MSS and Number of
156 * Multisamples is not MULTISAMPLECOUNT_1, this field must be set to
157 * ARYSPC_LOD0.
158 */
159 if (multisampled_surface_storage_format == GEN7_SURFACE_MSFMT_MSS
160 && is_multisampled)
161 assert(surface_array_spacing == GEN7_SURFACE_ARYSPC_LOD0);
162
163 /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
164 * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
165 * Surface Storage Format:
166 *
167 * All multisampled render target surfaces must have this field set to
168 * MSFMT_MSS.
169 *
170 * But also:
171 *
172 * This field is ignored if Number of Multisamples is MULTISAMPLECOUNT_1.
173 */
174 if (is_render_target && is_multisampled) {
175 assert(multisampled_surface_storage_format == GEN7_SURFACE_MSFMT_MSS);
176 }
177
178 /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
179 * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
180 * Surface Storage Format:
181 *
182 * If the surface’s Number of Multisamples is MULTISAMPLECOUNT_8, Width
183 * is >= 8192 (meaning the actual surface width is >= 8193 pixels), this
184 * field must be set to MSFMT_MSS.
185 */
186 uint32_t width = GET_FIELD(surf[2], GEN7_SURFACE_WIDTH) + 1;
187 if (num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_8 && width >= 8193) {
188 assert(multisampled_surface_storage_format == GEN7_SURFACE_MSFMT_MSS);
189 }
190
191 /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
192 * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
193 * Surface Storage Format:
194 *
195 * If the surface’s Number of Multisamples is MULTISAMPLECOUNT_8,
196 * ((Depth+1) * (Height+1)) is > 4,194,304, OR if the surface’s Number of
197 * Multisamples is MULTISAMPLECOUNT_4, ((Depth+1) * (Height+1)) is >
198 * 8,388,608, this field must be set to MSFMT_DEPTH_STENCIL.This field
199 * must be set to MSFMT_DEPTH_STENCIL if Surface Format is one of the
200 * following: I24X8_UNORM, L24X8_UNORM, A24X8_UNORM, or
201 * R24_UNORM_X8_TYPELESS.
202 *
203 * But also:
204 *
205 * This field is ignored if Number of Multisamples is MULTISAMPLECOUNT_1.
206 */
207 uint32_t depth = GET_FIELD(surf[3], BRW_SURFACE_DEPTH) + 1;
208 uint32_t height = GET_FIELD(surf[2], GEN7_SURFACE_HEIGHT) + 1;
209 if (num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_8 &&
210 depth * height > 4194304) {
211 assert(multisampled_surface_storage_format ==
212 GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
213 }
214 if (num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_4 &&
215 depth * height > 8388608) {
216 assert(multisampled_surface_storage_format ==
217 GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
218 }
219 if (is_multisampled) {
220 switch (GET_FIELD(surf[0], BRW_SURFACE_FORMAT)) {
221 case BRW_SURFACEFORMAT_I24X8_UNORM:
222 case BRW_SURFACEFORMAT_L24X8_UNORM:
223 case BRW_SURFACEFORMAT_A24X8_UNORM:
224 case BRW_SURFACEFORMAT_R24_UNORM_X8_TYPELESS:
225 assert(multisampled_surface_storage_format ==
226 GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
227 }
228 }
229 }
230
231
232 static void
233 gen7_update_buffer_texture_surface(struct gl_context *ctx,
234 unsigned unit,
235 uint32_t *binding_table,
236 unsigned surf_index)
237 {
238 struct brw_context *brw = brw_context(ctx);
239 struct intel_context *intel = &brw->intel;
240 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
241 struct intel_buffer_object *intel_obj =
242 intel_buffer_object(tObj->BufferObject);
243 drm_intel_bo *bo = intel_obj ? intel_obj->buffer : NULL;
244 gl_format format = tObj->_BufferObjectFormat;
245
246 uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
247 8 * 4, 32, &binding_table[surf_index]);
248 memset(surf, 0, 8 * 4);
249
250 uint32_t surface_format = brw_format_for_mesa_format(format);
251 if (surface_format == 0 && format != MESA_FORMAT_RGBA_FLOAT32) {
252 _mesa_problem(NULL, "bad format %s for texture buffer\n",
253 _mesa_get_format_name(format));
254 }
255
256 surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |
257 surface_format << BRW_SURFACE_FORMAT_SHIFT |
258 BRW_SURFACE_RC_READ_WRITE;
259
260 if (bo) {
261 surf[1] = bo->offset; /* reloc */
262
263 /* Emit relocation to surface contents. Section 5.1.1 of the gen4
264 * bspec ("Data Cache") says that the data cache does not exist as
265 * a separate cache and is just the sampler cache.
266 */
267 drm_intel_bo_emit_reloc(intel->batch.bo,
268 binding_table[surf_index] + 4,
269 bo, 0,
270 I915_GEM_DOMAIN_SAMPLER, 0);
271
272 int texel_size = _mesa_get_format_bytes(format);
273 int w = intel_obj->Base.Size / texel_size;
274
275 /* note that these differ from GEN6 */
276 surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) | /* bits 6:0 of size */
277 SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT); /* 20:7 */
278 surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH) | /* bits 26:21 */
279 (texel_size - 1);
280 }
281
282 gen7_check_surface_setup(surf, false /* is_render_target */);
283 }
284
285 static void
286 gen7_update_texture_surface(struct gl_context *ctx,
287 unsigned unit,
288 uint32_t *binding_table,
289 unsigned surf_index)
290 {
291 struct brw_context *brw = brw_context(ctx);
292 struct intel_context *intel = &brw->intel;
293 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
294 struct intel_texture_object *intelObj = intel_texture_object(tObj);
295 struct intel_mipmap_tree *mt = intelObj->mt;
296 struct gl_texture_image *firstImage = tObj->Image[0][tObj->BaseLevel];
297 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
298 int width, height, depth;
299 uint32_t tile_x, tile_y;
300
301 if (tObj->Target == GL_TEXTURE_BUFFER) {
302 gen7_update_buffer_texture_surface(ctx, unit, binding_table, surf_index);
303 return;
304 }
305
306 intel_miptree_get_dimensions_for_image(firstImage, &width, &height, &depth);
307
308 uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
309 8 * 4, 32, &binding_table[surf_index]);
310 memset(surf, 0, 8 * 4);
311
312 uint32_t tex_format = translate_tex_format(intel,
313 mt->format,
314 firstImage->InternalFormat,
315 tObj->DepthMode,
316 sampler->sRGBDecode);
317
318 surf[0] = translate_tex_target(tObj->Target) << BRW_SURFACE_TYPE_SHIFT |
319 tex_format << BRW_SURFACE_FORMAT_SHIFT |
320 gen7_surface_tiling_mode(mt->region->tiling) |
321 BRW_SURFACE_CUBEFACE_ENABLES;
322
323 if (mt->align_h == 4)
324 surf[0] |= GEN7_SURFACE_VALIGN_4;
325 if (mt->align_w == 8)
326 surf[0] |= GEN7_SURFACE_HALIGN_8;
327
328 if (depth > 1 && tObj->Target != GL_TEXTURE_3D)
329 surf[0] |= GEN7_SURFACE_IS_ARRAY;
330
331 if (mt->array_spacing_lod0)
332 surf[0] |= GEN7_SURFACE_ARYSPC_LOD0;
333
334 surf[1] = mt->region->bo->offset + mt->offset; /* reloc */
335 surf[1] += intel_miptree_get_tile_offsets(intelObj->mt, firstImage->Level, 0,
336 &tile_x, &tile_y);
337
338 surf[2] = SET_FIELD(width - 1, GEN7_SURFACE_WIDTH) |
339 SET_FIELD(height - 1, GEN7_SURFACE_HEIGHT);
340 surf[3] = SET_FIELD(depth - 1, BRW_SURFACE_DEPTH) |
341 ((intelObj->mt->region->pitch) - 1);
342
343 surf[4] = gen7_surface_msaa_bits(mt->num_samples, mt->msaa_layout);
344
345 assert(brw->has_surface_tile_offset || (tile_x == 0 && tile_y == 0));
346 /* Note that the low bits of these fields are missing, so
347 * there's the possibility of getting in trouble.
348 */
349 surf[5] = ((tile_x / 4) << BRW_SURFACE_X_OFFSET_SHIFT |
350 (tile_y / 2) << BRW_SURFACE_Y_OFFSET_SHIFT |
351 /* mip count */
352 (intelObj->_MaxLevel - tObj->BaseLevel));
353
354 if (intel->is_haswell) {
355 /* Handling GL_ALPHA as a surface format override breaks 1.30+ style
356 * texturing functions that return a float, as our code generation always
357 * selects the .x channel (which would always be 0).
358 */
359 const bool alpha_depth = tObj->DepthMode == GL_ALPHA &&
360 (firstImage->_BaseFormat == GL_DEPTH_COMPONENT ||
361 firstImage->_BaseFormat == GL_DEPTH_STENCIL);
362
363 const int swizzle = unlikely(alpha_depth)
364 ? SWIZZLE_XYZW : brw_get_texture_swizzle(ctx, tObj);
365
366 surf[7] =
367 SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 0)), GEN7_SURFACE_SCS_R) |
368 SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 1)), GEN7_SURFACE_SCS_G) |
369 SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 2)), GEN7_SURFACE_SCS_B) |
370 SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 3)), GEN7_SURFACE_SCS_A);
371 }
372
373 /* Emit relocation to surface contents */
374 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
375 binding_table[surf_index] + 4,
376 intelObj->mt->region->bo,
377 surf[1] - intelObj->mt->region->bo->offset,
378 I915_GEM_DOMAIN_SAMPLER, 0);
379
380 gen7_check_surface_setup(surf, false /* is_render_target */);
381 }
382
383 /**
384 * Create the constant buffer surface. Vertex/fragment shader constants will
385 * be read from this buffer with Data Port Read instructions/messages.
386 */
387 static void
388 gen7_create_constant_surface(struct brw_context *brw,
389 drm_intel_bo *bo,
390 uint32_t offset,
391 uint32_t size,
392 uint32_t *out_offset,
393 bool dword_pitch)
394 {
395 struct intel_context *intel = &brw->intel;
396 uint32_t stride = dword_pitch ? 4 : 16;
397 uint32_t elements = ALIGN(size, stride) / stride;
398 const GLint w = elements - 1;
399
400 uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
401 8 * 4, 32, out_offset);
402 memset(surf, 0, 8 * 4);
403
404 surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |
405 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_SURFACE_FORMAT_SHIFT |
406 BRW_SURFACE_RC_READ_WRITE;
407
408 assert(bo);
409 surf[1] = bo->offset + offset; /* reloc */
410
411 /* note that these differ from GEN6 */
412 surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) |
413 SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT);
414 surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH) |
415 (stride - 1);
416
417 if (intel->is_haswell) {
418 surf[7] = SET_FIELD(HSW_SCS_RED, GEN7_SURFACE_SCS_R) |
419 SET_FIELD(HSW_SCS_GREEN, GEN7_SURFACE_SCS_G) |
420 SET_FIELD(HSW_SCS_BLUE, GEN7_SURFACE_SCS_B) |
421 SET_FIELD(HSW_SCS_ALPHA, GEN7_SURFACE_SCS_A);
422 }
423
424 /* Emit relocation to surface contents. Section 5.1.1 of the gen4
425 * bspec ("Data Cache") says that the data cache does not exist as
426 * a separate cache and is just the sampler cache.
427 */
428 drm_intel_bo_emit_reloc(intel->batch.bo,
429 *out_offset + 4,
430 bo, offset,
431 I915_GEM_DOMAIN_SAMPLER, 0);
432
433 gen7_check_surface_setup(surf, false /* is_render_target */);
434 }
435
436 /**
437 * Create a surface for shader time.
438 */
439 void
440 gen7_create_shader_time_surface(struct brw_context *brw, uint32_t *out_offset)
441 {
442 struct intel_context *intel = &brw->intel;
443 const int w = brw->shader_time.bo->size - 1;
444
445 uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
446 8 * 4, 32, out_offset);
447 memset(surf, 0, 8 * 4);
448
449 surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |
450 BRW_SURFACEFORMAT_RAW << BRW_SURFACE_FORMAT_SHIFT |
451 BRW_SURFACE_RC_READ_WRITE;
452
453 surf[1] = brw->shader_time.bo->offset; /* reloc */
454
455 /* note that these differ from GEN6 */
456 surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) |
457 SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT);
458 surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH);
459
460 /* Unlike texture or renderbuffer surfaces, we only do untyped operations
461 * on the shader_time surface, so there's no need to set HSW channel
462 * overrides.
463 */
464
465 /* Emit relocation to surface contents. Section 5.1.1 of the gen4
466 * bspec ("Data Cache") says that the data cache does not exist as
467 * a separate cache and is just the sampler cache.
468 */
469 drm_intel_bo_emit_reloc(intel->batch.bo,
470 *out_offset + 4,
471 brw->shader_time.bo, 0,
472 I915_GEM_DOMAIN_SAMPLER, 0);
473
474 gen7_check_surface_setup(surf, false /* is_render_target */);
475 }
476
477 static void
478 gen7_update_null_renderbuffer_surface(struct brw_context *brw, unsigned unit)
479 {
480 /* From the Ivy bridge PRM, Vol4 Part1 p62 (Surface Type: Programming
481 * Notes):
482 *
483 * A null surface is used in instances where an actual surface is not
484 * bound. When a write message is generated to a null surface, no
485 * actual surface is written to. When a read message (including any
486 * sampling engine message) is generated to a null surface, the result
487 * is all zeros. Note that a null surface type is allowed to be used
488 * with all messages, even if it is not specificially indicated as
489 * supported. All of the remaining fields in surface state are ignored
490 * for null surfaces, with the following exceptions: Width, Height,
491 * Depth, LOD, and Render Target View Extent fields must match the
492 * depth buffer’s corresponding state for all render target surfaces,
493 * including null.
494 */
495 struct intel_context *intel = &brw->intel;
496 struct gl_context *ctx = &intel->ctx;
497
498 /* _NEW_BUFFERS */
499 const struct gl_framebuffer *fb = ctx->DrawBuffer;
500
501 uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
502 8 * 4, 32, &brw->wm.surf_offset[unit]);
503 memset(surf, 0, 8 * 4);
504
505 /* From the Ivybridge PRM, Volume 4, Part 1, page 65,
506 * Tiled Surface: Programming Notes:
507 * "If Surface Type is SURFTYPE_NULL, this field must be TRUE."
508 */
509 surf[0] = BRW_SURFACE_NULL << BRW_SURFACE_TYPE_SHIFT |
510 BRW_SURFACEFORMAT_B8G8R8A8_UNORM << BRW_SURFACE_FORMAT_SHIFT |
511 GEN7_SURFACE_TILING_Y;
512
513 surf[2] = SET_FIELD(fb->Width - 1, GEN7_SURFACE_WIDTH) |
514 SET_FIELD(fb->Height - 1, GEN7_SURFACE_HEIGHT);
515
516 gen7_check_surface_setup(surf, true /* is_render_target */);
517 }
518
519 /**
520 * Sets up a surface state structure to point at the given region.
521 * While it is only used for the front/back buffer currently, it should be
522 * usable for further buffers when doing ARB_draw_buffer support.
523 */
524 static void
525 gen7_update_renderbuffer_surface(struct brw_context *brw,
526 struct gl_renderbuffer *rb,
527 bool layered,
528 unsigned int unit)
529 {
530 struct intel_context *intel = &brw->intel;
531 struct gl_context *ctx = &intel->ctx;
532 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
533 struct intel_region *region = irb->mt->region;
534 uint32_t format;
535 /* _NEW_BUFFERS */
536 gl_format rb_format = _mesa_get_render_format(ctx, intel_rb_format(irb));
537 uint32_t surftype;
538 bool is_array = false;
539 int depth = MAX2(rb->Depth, 1);
540 int min_array_element;
541 GLenum gl_target = rb->TexImage ?
542 rb->TexImage->TexObject->Target : GL_TEXTURE_2D;
543
544 uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
545 8 * 4, 32, &brw->wm.surf_offset[unit]);
546 memset(surf, 0, 8 * 4);
547
548 intel_miptree_used_for_rendering(irb->mt);
549
550 /* Render targets can't use IMS layout */
551 assert(irb->mt->msaa_layout != INTEL_MSAA_LAYOUT_IMS);
552
553 assert(brw_render_target_supported(intel, rb));
554 format = brw->render_target_format[rb_format];
555 if (unlikely(!brw->format_supported_as_render_target[rb_format])) {
556 _mesa_problem(ctx, "%s: renderbuffer format %s unsupported\n",
557 __FUNCTION__, _mesa_get_format_name(rb_format));
558 }
559
560 switch (gl_target) {
561 case GL_TEXTURE_CUBE_MAP_ARRAY:
562 case GL_TEXTURE_CUBE_MAP:
563 surftype = BRW_SURFACE_2D;
564 is_array = true;
565 depth *= 6;
566 break;
567 default:
568 surftype = translate_tex_target(gl_target);
569 is_array = _mesa_tex_target_is_array(gl_target);
570 break;
571 }
572
573 if (layered) {
574 min_array_element = 0;
575 } else if (irb->mt->num_samples > 1) {
576 min_array_element = irb->mt_layer / irb->mt->num_samples;
577 } else {
578 min_array_element = irb->mt_layer;
579 }
580
581 surf[0] = surftype << BRW_SURFACE_TYPE_SHIFT |
582 format << BRW_SURFACE_FORMAT_SHIFT |
583 (irb->mt->array_spacing_lod0 ? GEN7_SURFACE_ARYSPC_LOD0
584 : GEN7_SURFACE_ARYSPC_FULL) |
585 gen7_surface_tiling_mode(region->tiling);
586
587 if (irb->mt->align_h == 4)
588 surf[0] |= GEN7_SURFACE_VALIGN_4;
589 if (irb->mt->align_w == 8)
590 surf[0] |= GEN7_SURFACE_HALIGN_8;
591
592 if (is_array) {
593 surf[0] |= GEN7_SURFACE_IS_ARRAY;
594 }
595
596 surf[1] = region->bo->offset;
597
598 assert(brw->has_surface_tile_offset);
599
600 surf[5] = irb->mt_level;
601
602 surf[2] = SET_FIELD(irb->mt->logical_width0 - 1, GEN7_SURFACE_WIDTH) |
603 SET_FIELD(irb->mt->logical_height0 - 1, GEN7_SURFACE_HEIGHT);
604
605 surf[3] = ((depth - 1) << BRW_SURFACE_DEPTH_SHIFT) |
606 (region->pitch - 1);
607
608 surf[4] = gen7_surface_msaa_bits(irb->mt->num_samples, irb->mt->msaa_layout) |
609 min_array_element << GEN7_SURFACE_MIN_ARRAY_ELEMENT_SHIFT |
610 (depth - 1) << GEN7_SURFACE_RENDER_TARGET_VIEW_EXTENT_SHIFT;
611
612 if (irb->mt->mcs_mt) {
613 gen7_set_surface_mcs_info(brw, surf, brw->wm.surf_offset[unit],
614 irb->mt->mcs_mt, true /* is RT */);
615 }
616
617 if (intel->is_haswell) {
618 surf[7] = SET_FIELD(HSW_SCS_RED, GEN7_SURFACE_SCS_R) |
619 SET_FIELD(HSW_SCS_GREEN, GEN7_SURFACE_SCS_G) |
620 SET_FIELD(HSW_SCS_BLUE, GEN7_SURFACE_SCS_B) |
621 SET_FIELD(HSW_SCS_ALPHA, GEN7_SURFACE_SCS_A);
622 }
623
624 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
625 brw->wm.surf_offset[unit] + 4,
626 region->bo,
627 surf[1] - region->bo->offset,
628 I915_GEM_DOMAIN_RENDER,
629 I915_GEM_DOMAIN_RENDER);
630
631 gen7_check_surface_setup(surf, true /* is_render_target */);
632 }
633
634 void
635 gen7_init_vtable_surface_functions(struct brw_context *brw)
636 {
637 struct intel_context *intel = &brw->intel;
638
639 intel->vtbl.update_texture_surface = gen7_update_texture_surface;
640 intel->vtbl.update_renderbuffer_surface = gen7_update_renderbuffer_surface;
641 intel->vtbl.update_null_renderbuffer_surface =
642 gen7_update_null_renderbuffer_surface;
643 intel->vtbl.create_constant_surface = gen7_create_constant_surface;
644 }