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/blend.h"
25 #include "main/samplerobj.h"
26 #include "main/texformat.h"
27 #include "main/teximage.h"
28 #include "program/prog_parameter.h"
29 #include "program/prog_instruction.h"
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"
37 #include "brw_context.h"
38 #include "brw_state.h"
39 #include "brw_defines.h"
43 gen7_surface_tiling_mode(uint32_t tiling
)
47 return GEN7_SURFACE_TILING_X
;
49 return GEN7_SURFACE_TILING_Y
;
51 return GEN7_SURFACE_TILING_NONE
;
57 gen7_surface_msaa_bits(unsigned num_samples
, enum intel_msaa_layout layout
)
61 assert(num_samples
<= 16);
63 /* The SURFACE_MULTISAMPLECOUNT_X enums are simply log2(num_samples) << 3. */
64 ss4
|= (ffs(MAX2(num_samples
, 1)) - 1) << 3;
66 if (layout
== INTEL_MSAA_LAYOUT_IMS
)
67 ss4
|= GEN7_SURFACE_MSFMT_DEPTH_STENCIL
;
69 ss4
|= GEN7_SURFACE_MSFMT_MSS
;
76 gen7_set_surface_mcs_info(struct brw_context
*brw
,
79 const struct intel_mipmap_tree
*mcs_mt
,
80 bool is_render_target
)
82 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
84 * "The MCS surface must be stored as Tile Y."
86 assert(mcs_mt
->tiling
== I915_TILING_Y
);
88 /* Compute the pitch in units of tiles. To do this we need to divide the
89 * pitch in bytes by 128, since a single Y-tile is 128 bytes wide.
91 unsigned pitch_tiles
= mcs_mt
->pitch
/ 128;
93 /* The upper 20 bits of surface state DWORD 6 are the upper 20 bits of the
94 * GPU address of the MCS buffer; the lower 12 bits contain other control
95 * information. Since buffer addresses are always on 4k boundaries (and
96 * thus have their lower 12 bits zero), we can use an ordinary reloc to do
97 * the necessary address translation.
99 assert ((mcs_mt
->bo
->offset64
& 0xfff) == 0);
101 surf
[6] = GEN7_SURFACE_MCS_ENABLE
|
102 SET_FIELD(pitch_tiles
- 1, GEN7_SURFACE_MCS_PITCH
) |
103 mcs_mt
->bo
->offset64
;
105 drm_intel_bo_emit_reloc(brw
->batch
.bo
,
109 is_render_target
? I915_GEM_DOMAIN_RENDER
110 : I915_GEM_DOMAIN_SAMPLER
,
111 is_render_target
? I915_GEM_DOMAIN_RENDER
: 0);
116 gen7_check_surface_setup(uint32_t *surf
, bool is_render_target
)
118 unsigned num_multisamples
= surf
[4] & INTEL_MASK(5, 3);
119 unsigned multisampled_surface_storage_format
= surf
[4] & (1 << 6);
120 unsigned surface_array_spacing
= surf
[0] & (1 << 10);
121 bool is_multisampled
= num_multisamples
!= GEN7_SURFACE_MULTISAMPLECOUNT_1
;
123 (void) surface_array_spacing
;
125 /* From the Ivybridge PRM, Volume 4 Part 1, page 66 (RENDER_SURFACE_STATE
126 * dword 0 bit 10 "Surface Array Spacing" Programming Notes):
128 * If Multisampled Surface Storage Format is MSFMT_MSS and Number of
129 * Multisamples is not MULTISAMPLECOUNT_1, this field must be set to
132 if (multisampled_surface_storage_format
== GEN7_SURFACE_MSFMT_MSS
134 assert(surface_array_spacing
== GEN7_SURFACE_ARYSPC_LOD0
);
136 /* From the Ivybridge PRM, Volume 4 Part 1, page 72 (RENDER_SURFACE_STATE
137 * dword 4 bit 6 "Multisampled Surface Storage" Programming Notes):
139 * All multisampled render target surfaces must have this field set to
144 * This field is ignored if Number of Multisamples is MULTISAMPLECOUNT_1.
146 if (is_render_target
&& is_multisampled
) {
147 assert(multisampled_surface_storage_format
== GEN7_SURFACE_MSFMT_MSS
);
150 /* From the Ivybridge PRM, Volume 4 Part 1, page 72 (RENDER_SURFACE_STATE
151 * dword 4 bit 6 "Multisampled Surface Storage Format" Errata):
153 * If the surface’s Number of Multisamples is MULTISAMPLECOUNT_8, Width
154 * is >= 8192 (meaning the actual surface width is >= 8193 pixels), this
155 * field must be set to MSFMT_MSS.
157 uint32_t width
= GET_FIELD(surf
[2], GEN7_SURFACE_WIDTH
) + 1;
158 if (num_multisamples
== GEN7_SURFACE_MULTISAMPLECOUNT_8
&& width
>= 8193) {
159 assert(multisampled_surface_storage_format
== GEN7_SURFACE_MSFMT_MSS
);
162 /* From the Ivybridge PRM, Volume 4 Part 1, page 72 (RENDER_SURFACE_STATE
163 * dword 4 bit 6 "Multisampled Surface Storage Format" Errata):
165 * If the surface’s Number of Multisamples is MULTISAMPLECOUNT_8,
166 * ((Depth+1) * (Height+1)) is > 4,194,304, OR if the surface’s Number of
167 * Multisamples is MULTISAMPLECOUNT_4, ((Depth+1) * (Height+1)) is >
168 * 8,388,608, this field must be set to MSFMT_DEPTH_STENCIL.This field
169 * must be set to MSFMT_DEPTH_STENCIL if Surface Format is one of the
170 * following: I24X8_UNORM, L24X8_UNORM, A24X8_UNORM, or
171 * R24_UNORM_X8_TYPELESS.
173 * But also (from the Programming Notes):
175 * This field is ignored if Number of Multisamples is MULTISAMPLECOUNT_1.
177 uint32_t depth
= GET_FIELD(surf
[3], BRW_SURFACE_DEPTH
) + 1;
178 uint32_t height
= GET_FIELD(surf
[2], GEN7_SURFACE_HEIGHT
) + 1;
179 if (num_multisamples
== GEN7_SURFACE_MULTISAMPLECOUNT_8
&&
180 depth
* height
> 4194304) {
181 assert(multisampled_surface_storage_format
==
182 GEN7_SURFACE_MSFMT_DEPTH_STENCIL
);
184 if (num_multisamples
== GEN7_SURFACE_MULTISAMPLECOUNT_4
&&
185 depth
* height
> 8388608) {
186 assert(multisampled_surface_storage_format
==
187 GEN7_SURFACE_MSFMT_DEPTH_STENCIL
);
189 if (is_multisampled
) {
190 switch (GET_FIELD(surf
[0], BRW_SURFACE_FORMAT
)) {
191 case BRW_SURFACEFORMAT_I24X8_UNORM
:
192 case BRW_SURFACEFORMAT_L24X8_UNORM
:
193 case BRW_SURFACEFORMAT_A24X8_UNORM
:
194 case BRW_SURFACEFORMAT_R24_UNORM_X8_TYPELESS
:
195 assert(multisampled_surface_storage_format
==
196 GEN7_SURFACE_MSFMT_DEPTH_STENCIL
);
202 gen7_emit_buffer_surface_state(struct brw_context
*brw
,
203 uint32_t *out_offset
,
205 unsigned buffer_offset
,
206 unsigned surface_format
,
207 unsigned buffer_size
,
211 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
212 8 * 4, 32, out_offset
);
213 memset(surf
, 0, 8 * 4);
215 surf
[0] = BRW_SURFACE_BUFFER
<< BRW_SURFACE_TYPE_SHIFT
|
216 surface_format
<< BRW_SURFACE_FORMAT_SHIFT
|
217 BRW_SURFACE_RC_READ_WRITE
;
218 surf
[1] = (bo
? bo
->offset64
: 0) + buffer_offset
; /* reloc */
219 surf
[2] = SET_FIELD((buffer_size
- 1) & 0x7f, GEN7_SURFACE_WIDTH
) |
220 SET_FIELD(((buffer_size
- 1) >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT
);
221 if (surface_format
== BRW_SURFACEFORMAT_RAW
)
222 surf
[3] = SET_FIELD(((buffer_size
- 1) >> 21) & 0x3ff, BRW_SURFACE_DEPTH
);
224 surf
[3] = SET_FIELD(((buffer_size
- 1) >> 21) & 0x3f, BRW_SURFACE_DEPTH
);
225 surf
[3] |= (pitch
- 1);
227 surf
[5] = SET_FIELD(GEN7_MOCS_L3
, GEN7_SURFACE_MOCS
);
229 if (brw
->is_haswell
) {
230 surf
[7] |= (SET_FIELD(HSW_SCS_RED
, GEN7_SURFACE_SCS_R
) |
231 SET_FIELD(HSW_SCS_GREEN
, GEN7_SURFACE_SCS_G
) |
232 SET_FIELD(HSW_SCS_BLUE
, GEN7_SURFACE_SCS_B
) |
233 SET_FIELD(HSW_SCS_ALPHA
, GEN7_SURFACE_SCS_A
));
236 /* Emit relocation to surface contents */
238 drm_intel_bo_emit_reloc(brw
->batch
.bo
, *out_offset
+ 4,
239 bo
, buffer_offset
, I915_GEM_DOMAIN_SAMPLER
,
240 (rw
? I915_GEM_DOMAIN_SAMPLER
: 0));
243 gen7_check_surface_setup(surf
, false /* is_render_target */);
247 * Creates a null surface.
249 * This is used when the shader doesn't write to any color output. An FB
250 * write to target 0 will still be emitted, because that's how the thread is
251 * terminated (and computed depth is returned), so we need to have the
252 * hardware discard the target 0 color output..
255 gen7_emit_null_surface_state(struct brw_context
*brw
,
259 uint32_t *out_offset
)
261 /* From the Ivy bridge PRM, Vol4 Part1 p62 (Surface Type: Programming
264 * A null surface is used in instances where an actual surface is not
265 * bound. When a write message is generated to a null surface, no
266 * actual surface is written to. When a read message (including any
267 * sampling engine message) is generated to a null surface, the result
268 * is all zeros. Note that a null surface type is allowed to be used
269 * with all messages, even if it is not specificially indicated as
270 * supported. All of the remaining fields in surface state are ignored
271 * for null surfaces, with the following exceptions: Width, Height,
272 * Depth, LOD, and Render Target View Extent fields must match the
273 * depth buffer’s corresponding state for all render target surfaces,
276 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
, 8 * 4, 32,
278 memset(surf
, 0, 8 * 4);
280 /* From the Ivybridge PRM, Volume 4, Part 1, page 65,
281 * Tiled Surface: Programming Notes:
282 * "If Surface Type is SURFTYPE_NULL, this field must be TRUE."
284 surf
[0] = BRW_SURFACE_NULL
<< BRW_SURFACE_TYPE_SHIFT
|
285 BRW_SURFACEFORMAT_B8G8R8A8_UNORM
<< BRW_SURFACE_FORMAT_SHIFT
|
286 GEN7_SURFACE_TILING_Y
;
288 surf
[2] = SET_FIELD(width
- 1, GEN7_SURFACE_WIDTH
) |
289 SET_FIELD(height
- 1, GEN7_SURFACE_HEIGHT
);
291 gen7_check_surface_setup(surf
, true /* is_render_target */);
295 * Sets up a surface state structure to point at the given region.
296 * While it is only used for the front/back buffer currently, it should be
297 * usable for further buffers when doing ARB_draw_buffer support.
300 gen7_update_renderbuffer_surface(struct brw_context
*brw
,
301 struct gl_renderbuffer
*rb
,
302 bool layered
, unsigned unit
/* unused */,
305 struct gl_context
*ctx
= &brw
->ctx
;
306 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
307 struct intel_mipmap_tree
*mt
= irb
->mt
;
310 mesa_format rb_format
= _mesa_get_render_format(ctx
, intel_rb_format(irb
));
312 bool is_array
= false;
313 int depth
= MAX2(irb
->layer_count
, 1);
314 const uint8_t mocs
= GEN7_MOCS_L3
;
317 int min_array_element
= irb
->mt_layer
/ MAX2(mt
->num_samples
, 1);
319 GLenum gl_target
= rb
->TexImage
?
320 rb
->TexImage
->TexObject
->Target
: GL_TEXTURE_2D
;
322 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
, 8 * 4, 32,
324 memset(surf
, 0, 8 * 4);
326 intel_miptree_used_for_rendering(irb
->mt
);
328 /* Render targets can't use IMS layout */
329 assert(irb
->mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_IMS
);
331 assert(brw_render_target_supported(brw
, rb
));
332 format
= brw
->render_target_format
[rb_format
];
333 if (unlikely(!brw
->format_supported_as_render_target
[rb_format
])) {
334 _mesa_problem(ctx
, "%s: renderbuffer format %s unsupported\n",
335 __func__
, _mesa_get_format_name(rb_format
));
339 case GL_TEXTURE_CUBE_MAP_ARRAY
:
340 case GL_TEXTURE_CUBE_MAP
:
341 surftype
= BRW_SURFACE_2D
;
346 depth
= MAX2(irb
->mt
->logical_depth0
, 1);
349 surftype
= translate_tex_target(gl_target
);
350 is_array
= _mesa_is_array_texture(gl_target
);
354 surf
[0] = surftype
<< BRW_SURFACE_TYPE_SHIFT
|
355 format
<< BRW_SURFACE_FORMAT_SHIFT
|
356 (irb
->mt
->array_layout
== ALL_SLICES_AT_EACH_LOD
?
357 GEN7_SURFACE_ARYSPC_LOD0
: GEN7_SURFACE_ARYSPC_FULL
) |
358 gen7_surface_tiling_mode(mt
->tiling
);
360 if (irb
->mt
->valign
== 4)
361 surf
[0] |= GEN7_SURFACE_VALIGN_4
;
362 if (irb
->mt
->halign
== 8)
363 surf
[0] |= GEN7_SURFACE_HALIGN_8
;
366 surf
[0] |= GEN7_SURFACE_IS_ARRAY
;
369 assert(mt
->offset
% mt
->cpp
== 0);
370 surf
[1] = mt
->bo
->offset64
+ mt
->offset
;
372 assert(brw
->has_surface_tile_offset
);
374 surf
[5] = SET_FIELD(mocs
, GEN7_SURFACE_MOCS
) |
375 (irb
->mt_level
- irb
->mt
->first_level
);
377 surf
[2] = SET_FIELD(irb
->mt
->logical_width0
- 1, GEN7_SURFACE_WIDTH
) |
378 SET_FIELD(irb
->mt
->logical_height0
- 1, GEN7_SURFACE_HEIGHT
);
380 surf
[3] = ((depth
- 1) << BRW_SURFACE_DEPTH_SHIFT
) |
383 surf
[4] = gen7_surface_msaa_bits(irb
->mt
->num_samples
, irb
->mt
->msaa_layout
) |
384 min_array_element
<< GEN7_SURFACE_MIN_ARRAY_ELEMENT_SHIFT
|
385 (depth
- 1) << GEN7_SURFACE_RENDER_TARGET_VIEW_EXTENT_SHIFT
;
387 if (irb
->mt
->mcs_mt
) {
388 gen7_set_surface_mcs_info(brw
, surf
, offset
,
389 irb
->mt
->mcs_mt
, true /* is RT */);
392 surf
[7] = irb
->mt
->fast_clear_color_value
;
394 if (brw
->is_haswell
) {
395 surf
[7] |= (SET_FIELD(HSW_SCS_RED
, GEN7_SURFACE_SCS_R
) |
396 SET_FIELD(HSW_SCS_GREEN
, GEN7_SURFACE_SCS_G
) |
397 SET_FIELD(HSW_SCS_BLUE
, GEN7_SURFACE_SCS_B
) |
398 SET_FIELD(HSW_SCS_ALPHA
, GEN7_SURFACE_SCS_A
));
401 drm_intel_bo_emit_reloc(brw
->batch
.bo
,
404 surf
[1] - mt
->bo
->offset64
,
405 I915_GEM_DOMAIN_RENDER
,
406 I915_GEM_DOMAIN_RENDER
);
408 gen7_check_surface_setup(surf
, true /* is_render_target */);
414 gen7_init_vtable_surface_functions(struct brw_context
*brw
)
416 brw
->vtbl
.update_texture_surface
= brw_update_texture_surface
;
417 brw
->vtbl
.update_renderbuffer_surface
= gen7_update_renderbuffer_surface
;
418 brw
->vtbl
.emit_null_surface_state
= gen7_emit_null_surface_state
;
419 brw
->vtbl
.emit_buffer_surface_state
= gen7_emit_buffer_surface_state
;