2 * Copyright © 2011 Intel Corporation
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:
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
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
23 #include "main/mtypes.h"
24 #include "main/samplerobj.h"
25 #include "program/prog_parameter.h"
27 #include "intel_mipmap_tree.h"
28 #include "intel_batchbuffer.h"
29 #include "intel_tex.h"
30 #include "intel_fbo.h"
31 #include "intel_buffer_objects.h"
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
39 gen7_set_surface_tiling(struct gen7_surface_state *surf, uint32_t tiling)
42 case I915_TILING_NONE:
43 surf->ss0.tiled_surface = 0;
44 surf->ss0.tile_walk = 0;
47 surf->ss0.tiled_surface = 1;
48 surf->ss0.tile_walk = BRW_TILEWALK_XMAJOR;
51 surf->ss0.tiled_surface = 1;
52 surf->ss0.tile_walk = BRW_TILEWALK_YMAJOR;
59 gen7_set_surface_msaa(struct gen7_surface_state *surf, unsigned num_samples,
60 enum intel_msaa_layout layout)
63 surf->ss4.num_multisamples = GEN7_SURFACE_MULTISAMPLECOUNT_8;
64 else if (num_samples > 0)
65 surf->ss4.num_multisamples = GEN7_SURFACE_MULTISAMPLECOUNT_4;
67 surf->ss4.num_multisamples = GEN7_SURFACE_MULTISAMPLECOUNT_1;
69 surf->ss4.multisampled_surface_storage_format =
70 layout == INTEL_MSAA_LAYOUT_IMS ?
71 GEN7_SURFACE_MSFMT_DEPTH_STENCIL :
72 GEN7_SURFACE_MSFMT_MSS;
77 gen7_set_surface_mcs_info(struct brw_context *brw,
78 struct gen7_surface_state *surf,
80 const struct intel_mipmap_tree *mcs_mt,
81 bool is_render_target)
83 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
85 * "The MCS surface must be stored as Tile Y."
87 assert(mcs_mt->region->tiling == I915_TILING_Y);
89 /* Compute the pitch in units of tiles. To do this we need to divide the
90 * pitch in bytes by 128, since a single Y-tile is 128 bytes wide.
92 unsigned pitch_bytes = mcs_mt->region->pitch * mcs_mt->cpp;
93 unsigned pitch_tiles = pitch_bytes / 128;
95 /* The upper 20 bits of surface state DWORD 6 are the upper 20 bits of the
96 * GPU address of the MCS buffer; the lower 12 bits contain other control
97 * information. Since buffer addresses are always on 4k boundaries (and
98 * thus have their lower 12 bits zero), we can use an ordinary reloc to do
99 * the necessary address translation.
101 assert ((mcs_mt->region->bo->offset & 0xfff) == 0);
102 surf->ss6.mcs_enabled.mcs_enable = 1;
103 surf->ss6.mcs_enabled.mcs_surface_pitch = pitch_tiles - 1;
104 surf->ss6.mcs_enabled.mcs_base_address = mcs_mt->region->bo->offset >> 12;
105 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
107 offsetof(struct gen7_surface_state, ss6),
109 surf->ss6.raw_data & 0xfff,
110 is_render_target ? I915_GEM_DOMAIN_RENDER
111 : I915_GEM_DOMAIN_SAMPLER,
112 is_render_target ? I915_GEM_DOMAIN_RENDER : 0);
117 gen7_check_surface_setup(struct gen7_surface_state *surf,
118 bool is_render_target)
120 bool is_multisampled =
121 surf->ss4.num_multisamples != GEN7_SURFACE_MULTISAMPLECOUNT_1;
122 /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
123 * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Surface Array
126 * If Multisampled Surface Storage Format is MSFMT_MSS and Number of
127 * Multisamples is not MULTISAMPLECOUNT_1, this field must be set to
130 if (surf->ss4.multisampled_surface_storage_format == GEN7_SURFACE_MSFMT_MSS
132 assert(surf->ss0.surface_array_spacing == GEN7_SURFACE_ARYSPC_LOD0);
134 /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
135 * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
136 * Surface Storage Format:
138 * All multisampled render target surfaces must have this field set to
143 * This field is ignored if Number of Multisamples is MULTISAMPLECOUNT_1.
145 if (is_render_target && is_multisampled) {
146 assert(surf->ss4.multisampled_surface_storage_format ==
147 GEN7_SURFACE_MSFMT_MSS);
150 /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
151 * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
152 * Surface Storage Format:
154 * If the surface’s Number of Multisamples is MULTISAMPLECOUNT_8, Width
155 * is >= 8192 (meaning the actual surface width is >= 8193 pixels), this
156 * field must be set to MSFMT_MSS.
158 if (surf->ss4.num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_8 &&
159 surf->ss2.width >= 8192) {
160 assert(surf->ss4.multisampled_surface_storage_format ==
161 GEN7_SURFACE_MSFMT_MSS);
164 /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
165 * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
166 * Surface Storage Format:
168 * If the surface’s Number of Multisamples is MULTISAMPLECOUNT_8,
169 * ((Depth+1) * (Height+1)) is > 4,194,304, OR if the surface’s Number of
170 * Multisamples is MULTISAMPLECOUNT_4, ((Depth+1) * (Height+1)) is >
171 * 8,388,608, this field must be set to MSFMT_DEPTH_STENCIL.This field
172 * must be set to MSFMT_DEPTH_STENCIL if Surface Format is one of the
173 * following: I24X8_UNORM, L24X8_UNORM, A24X8_UNORM, or
174 * R24_UNORM_X8_TYPELESS.
178 * This field is ignored if Number of Multisamples is MULTISAMPLECOUNT_1.
180 uint32_t depth = surf->ss3.depth + 1;
181 uint32_t height = surf->ss2.height + 1;
182 if (surf->ss4.num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_8 &&
183 depth * height > 4194304) {
184 assert(surf->ss4.multisampled_surface_storage_format ==
185 GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
187 if (surf->ss4.num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_4 &&
188 depth * height > 8388608) {
189 assert(surf->ss4.multisampled_surface_storage_format ==
190 GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
192 if (is_multisampled) {
193 switch (surf->ss0.surface_format) {
194 case BRW_SURFACEFORMAT_I24X8_UNORM:
195 case BRW_SURFACEFORMAT_L24X8_UNORM:
196 case BRW_SURFACEFORMAT_A24X8_UNORM:
197 case BRW_SURFACEFORMAT_R24_UNORM_X8_TYPELESS:
198 assert(surf->ss4.multisampled_surface_storage_format ==
199 GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
206 gen7_update_buffer_texture_surface(struct gl_context *ctx, GLuint unit)
208 struct brw_context *brw = brw_context(ctx);
209 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
210 const GLuint surf_index = SURF_INDEX_TEXTURE(unit);
211 struct gen7_surface_state *surf;
212 struct intel_buffer_object *intel_obj =
213 intel_buffer_object(tObj->BufferObject);
214 drm_intel_bo *bo = intel_obj ? intel_obj->buffer : NULL;
215 gl_format format = tObj->_BufferObjectFormat;
216 int texel_size = _mesa_get_format_bytes(format);
218 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
219 sizeof(*surf), 32, &brw->wm.surf_offset[surf_index]);
220 memset(surf, 0, sizeof(*surf));
222 surf->ss0.surface_type = BRW_SURFACE_BUFFER;
223 surf->ss0.surface_format = brw_format_for_mesa_format(format);
225 surf->ss0.render_cache_read_write = 1;
227 if (surf->ss0.surface_format == 0 && format != MESA_FORMAT_RGBA_FLOAT32) {
228 _mesa_problem(NULL, "bad format %s for texture buffer\n",
229 _mesa_get_format_name(format));
233 surf->ss1.base_addr = bo->offset; /* reloc */
235 /* Emit relocation to surface contents. Section 5.1.1 of the gen4
236 * bspec ("Data Cache") says that the data cache does not exist as
237 * a separate cache and is just the sampler cache.
239 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
240 (brw->wm.surf_offset[surf_index] +
241 offsetof(struct gen7_surface_state, ss1)),
243 I915_GEM_DOMAIN_SAMPLER, 0);
245 int w = intel_obj->Base.Size / texel_size;
246 surf->ss2.width = w & 0x7f; /* bits 6:0 of size or width */
247 surf->ss2.height = (w >> 7) & 0x1fff; /* bits 19:7 of size or width */
248 surf->ss3.depth = (w >> 20) & 0x7f; /* bits 26:20 of size or width */
249 surf->ss3.pitch = texel_size - 1;
251 surf->ss1.base_addr = 0;
253 surf->ss2.height = 0;
258 gen7_set_surface_tiling(surf, I915_TILING_NONE);
260 gen7_check_surface_setup(surf, false /* is_render_target */);
264 gen7_update_texture_surface(struct gl_context *ctx, GLuint unit)
266 struct brw_context *brw = brw_context(ctx);
267 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
268 struct intel_texture_object *intelObj = intel_texture_object(tObj);
269 struct intel_mipmap_tree *mt = intelObj->mt;
270 struct gl_texture_image *firstImage = tObj->Image[0][tObj->BaseLevel];
271 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
272 const GLuint surf_index = SURF_INDEX_TEXTURE(unit);
273 struct gen7_surface_state *surf;
274 int width, height, depth;
276 if (tObj->Target == GL_TEXTURE_BUFFER) {
277 gen7_update_buffer_texture_surface(ctx, unit);
281 /* We don't support MSAA for textures. */
282 assert(!mt->array_spacing_lod0);
283 assert(mt->num_samples == 0);
285 intel_miptree_get_dimensions_for_image(firstImage, &width, &height, &depth);
287 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
288 sizeof(*surf), 32, &brw->wm.surf_offset[surf_index]);
289 memset(surf, 0, sizeof(*surf));
291 if (mt->align_h == 4)
292 surf->ss0.vertical_alignment = 1;
293 if (mt->align_w == 8)
294 surf->ss0.horizontal_alignment = 1;
296 surf->ss0.surface_type = translate_tex_target(tObj->Target);
297 surf->ss0.surface_format = translate_tex_format(mt->format,
298 firstImage->InternalFormat,
300 sampler->sRGBDecode);
301 if (tObj->Target == GL_TEXTURE_CUBE_MAP) {
302 surf->ss0.cube_pos_x = 1;
303 surf->ss0.cube_pos_y = 1;
304 surf->ss0.cube_pos_z = 1;
305 surf->ss0.cube_neg_x = 1;
306 surf->ss0.cube_neg_y = 1;
307 surf->ss0.cube_neg_z = 1;
310 surf->ss0.is_array = depth > 1 && tObj->Target != GL_TEXTURE_3D;
312 gen7_set_surface_tiling(surf, intelObj->mt->region->tiling);
314 /* ss0 remaining fields:
315 * - vert_line_stride (exists on gen6 but we ignore it)
316 * - vert_line_stride_ofs (exists on gen6 but we ignore it)
317 * - surface_array_spacing
318 * - render_cache_read_write (exists on gen6 but ignored here)
321 surf->ss1.base_addr =
322 intelObj->mt->region->bo->offset + intelObj->mt->offset; /* reloc */
324 surf->ss2.width = width - 1;
325 surf->ss2.height = height - 1;
327 surf->ss3.pitch = (intelObj->mt->region->pitch * intelObj->mt->cpp) - 1;
328 surf->ss3.depth = depth - 1;
332 surf->ss5.mip_count = intelObj->_MaxLevel - tObj->BaseLevel;
333 surf->ss5.min_lod = 0;
335 /* ss5 remaining fields:
336 * - x_offset (N/A for textures?)
341 if (brw->intel.is_haswell) {
342 surf->ss7.shader_chanel_select_r = HSW_SCS_RED;
343 surf->ss7.shader_chanel_select_g = HSW_SCS_GREEN;
344 surf->ss7.shader_chanel_select_b = HSW_SCS_BLUE;
345 surf->ss7.shader_chanel_select_a = HSW_SCS_ALPHA;
348 /* Emit relocation to surface contents */
349 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
350 brw->wm.surf_offset[surf_index] +
351 offsetof(struct gen7_surface_state, ss1),
352 intelObj->mt->region->bo, intelObj->mt->offset,
353 I915_GEM_DOMAIN_SAMPLER, 0);
355 gen7_check_surface_setup(surf, false /* is_render_target */);
359 * Create the constant buffer surface. Vertex/fragment shader constants will
360 * be read from this buffer with Data Port Read instructions/messages.
363 gen7_create_constant_surface(struct brw_context *brw,
366 uint32_t *out_offset)
368 const GLint w = width - 1;
369 struct gen7_surface_state *surf;
371 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
372 sizeof(*surf), 32, out_offset);
373 memset(surf, 0, sizeof(*surf));
375 surf->ss0.surface_type = BRW_SURFACE_BUFFER;
376 surf->ss0.surface_format = BRW_SURFACEFORMAT_R32G32B32A32_FLOAT;
378 surf->ss0.render_cache_read_write = 1;
381 surf->ss1.base_addr = bo->offset; /* reloc */
383 surf->ss2.width = w & 0x7f; /* bits 6:0 of size or width */
384 surf->ss2.height = (w >> 7) & 0x1fff; /* bits 19:7 of size or width */
385 surf->ss3.depth = (w >> 20) & 0x7f; /* bits 26:20 of size or width */
386 surf->ss3.pitch = (16 - 1); /* stride between samples */
387 gen7_set_surface_tiling(surf, I915_TILING_NONE); /* tiling now allowed */
389 if (brw->intel.is_haswell) {
390 surf->ss7.shader_chanel_select_r = HSW_SCS_RED;
391 surf->ss7.shader_chanel_select_g = HSW_SCS_GREEN;
392 surf->ss7.shader_chanel_select_b = HSW_SCS_BLUE;
393 surf->ss7.shader_chanel_select_a = HSW_SCS_ALPHA;
396 /* Emit relocation to surface contents. Section 5.1.1 of the gen4
397 * bspec ("Data Cache") says that the data cache does not exist as
398 * a separate cache and is just the sampler cache.
400 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
402 offsetof(struct gen7_surface_state, ss1)),
404 I915_GEM_DOMAIN_SAMPLER, 0);
406 gen7_check_surface_setup(surf, false /* is_render_target */);
410 gen7_update_null_renderbuffer_surface(struct brw_context *brw, unsigned unit)
412 struct gen7_surface_state *surf;
414 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
415 sizeof(*surf), 32, &brw->wm.surf_offset[unit]);
416 memset(surf, 0, sizeof(*surf));
418 surf->ss0.surface_type = BRW_SURFACE_NULL;
419 surf->ss0.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
421 gen7_check_surface_setup(surf, true /* is_render_target */);
425 * Sets up a surface state structure to point at the given region.
426 * While it is only used for the front/back buffer currently, it should be
427 * usable for further buffers when doing ARB_draw_buffer support.
430 gen7_update_renderbuffer_surface(struct brw_context *brw,
431 struct gl_renderbuffer *rb,
434 struct intel_context *intel = &brw->intel;
435 struct gl_context *ctx = &intel->ctx;
436 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
437 struct intel_region *region = irb->mt->region;
438 struct gen7_surface_state *surf;
439 uint32_t tile_x, tile_y;
440 gl_format rb_format = intel_rb_format(irb);
442 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
443 sizeof(*surf), 32, &brw->wm.surf_offset[unit]);
444 memset(surf, 0, sizeof(*surf));
446 /* Render targets can't use IMS layout */
447 assert(irb->mt->msaa_layout != INTEL_MSAA_LAYOUT_IMS);
449 if (irb->mt->align_h == 4)
450 surf->ss0.vertical_alignment = 1;
451 if (irb->mt->align_w == 8)
452 surf->ss0.horizontal_alignment = 1;
455 case MESA_FORMAT_SARGB8:
458 * Without GL_EXT_framebuffer_sRGB we shouldn't bind sRGB surfaces to the
459 * blend/update as sRGB.
461 if (ctx->Color.sRGBEnabled)
462 surf->ss0.surface_format = brw_format_for_mesa_format(rb_format);
464 surf->ss0.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
467 assert(brw_render_target_supported(intel, rb));
468 surf->ss0.surface_format = brw->render_target_format[rb_format];
469 if (unlikely(!brw->format_supported_as_render_target[rb_format])) {
470 _mesa_problem(ctx, "%s: renderbuffer format %s unsupported\n",
471 __FUNCTION__, _mesa_get_format_name(rb_format));
476 surf->ss0.surface_type = BRW_SURFACE_2D;
477 surf->ss0.surface_array_spacing = irb->mt->array_spacing_lod0 ?
478 GEN7_SURFACE_ARYSPC_LOD0 : GEN7_SURFACE_ARYSPC_FULL;
481 surf->ss1.base_addr = intel_renderbuffer_tile_offsets(irb, &tile_x, &tile_y);
482 surf->ss1.base_addr += region->bo->offset; /* reloc */
484 assert(brw->has_surface_tile_offset);
485 /* Note that the low bits of these fields are missing, so
486 * there's the possibility of getting in trouble.
488 assert(tile_x % 4 == 0);
489 assert(tile_y % 2 == 0);
490 surf->ss5.x_offset = tile_x / 4;
491 surf->ss5.y_offset = tile_y / 2;
493 surf->ss2.width = rb->Width - 1;
494 surf->ss2.height = rb->Height - 1;
495 gen7_set_surface_tiling(surf, region->tiling);
496 surf->ss3.pitch = (region->pitch * region->cpp) - 1;
498 gen7_set_surface_msaa(surf, irb->mt->num_samples, irb->mt->msaa_layout);
500 if (irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
501 gen7_set_surface_mcs_info(brw, surf, brw->wm.surf_offset[unit],
502 irb->mt->mcs_mt, true /* is_render_target */);
505 if (intel->is_haswell) {
506 surf->ss7.shader_chanel_select_r = HSW_SCS_RED;
507 surf->ss7.shader_chanel_select_g = HSW_SCS_GREEN;
508 surf->ss7.shader_chanel_select_b = HSW_SCS_BLUE;
509 surf->ss7.shader_chanel_select_a = HSW_SCS_ALPHA;
512 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
513 brw->wm.surf_offset[unit] +
514 offsetof(struct gen7_surface_state, ss1),
516 surf->ss1.base_addr - region->bo->offset,
517 I915_GEM_DOMAIN_RENDER,
518 I915_GEM_DOMAIN_RENDER);
520 gen7_check_surface_setup(surf, true /* is_render_target */);
524 gen7_init_vtable_surface_functions(struct brw_context *brw)
526 struct intel_context *intel = &brw->intel;
528 intel->vtbl.update_texture_surface = gen7_update_texture_surface;
529 intel->vtbl.update_renderbuffer_surface = gen7_update_renderbuffer_surface;
530 intel->vtbl.update_null_renderbuffer_surface =
531 gen7_update_null_renderbuffer_surface;
532 intel->vtbl.create_constant_surface = gen7_create_constant_surface;