2 * Copyright © 2012 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
24 #include "main/blend.h"
25 #include "main/mtypes.h"
26 #include "main/samplerobj.h"
27 #include "main/texformat.h"
28 #include "program/prog_parameter.h"
30 #include "intel_mipmap_tree.h"
31 #include "intel_batchbuffer.h"
32 #include "intel_tex.h"
33 #include "intel_fbo.h"
34 #include "intel_buffer_objects.h"
36 #include "brw_context.h"
37 #include "brw_state.h"
38 #include "brw_defines.h"
42 surface_tiling_mode(uint32_t tiling
)
46 return GEN8_SURFACE_TILING_X
;
48 return GEN8_SURFACE_TILING_Y
;
50 return GEN8_SURFACE_TILING_NONE
;
55 vertical_alignment(struct intel_mipmap_tree
*mt
)
57 switch (mt
->align_h
) {
59 return GEN8_SURFACE_VALIGN_4
;
61 return GEN8_SURFACE_VALIGN_8
;
63 return GEN8_SURFACE_VALIGN_16
;
65 assert(!"Unsupported vertical surface alignment.");
66 return GEN8_SURFACE_VALIGN_4
;
71 horizontal_alignment(struct intel_mipmap_tree
*mt
)
73 switch (mt
->align_w
) {
75 return GEN8_SURFACE_HALIGN_4
;
77 return GEN8_SURFACE_HALIGN_8
;
79 return GEN8_SURFACE_HALIGN_16
;
81 assert(!"Unsupported horizontal surface alignment.");
82 return GEN8_SURFACE_HALIGN_4
;
87 surface_num_multisamples(unsigned num_samples
)
89 assert(num_samples
>= 0 && num_samples
<= 16);
92 return GEN7_SURFACE_MULTISAMPLECOUNT_1
;
94 /* The SURFACE_MULTISAMPLECOUNT_X enums are simply log2(num_samples) << 3. */
95 return (ffs(num_samples
) - 1) << 3;
99 gen8_emit_buffer_surface_state(struct brw_context
*brw
,
100 uint32_t *out_offset
,
102 unsigned buffer_offset
,
103 unsigned surface_format
,
104 unsigned buffer_size
,
109 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
110 13 * 4, 64, out_offset
);
111 memset(surf
, 0, 13 * 4);
113 surf
[0] = BRW_SURFACE_BUFFER
<< BRW_SURFACE_TYPE_SHIFT
|
114 surface_format
<< BRW_SURFACE_FORMAT_SHIFT
|
115 BRW_SURFACE_RC_READ_WRITE
;
117 surf
[2] = SET_FIELD((buffer_size
- 1) & 0x7f, GEN7_SURFACE_WIDTH
) |
118 SET_FIELD(((buffer_size
- 1) >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT
);
119 surf
[3] = SET_FIELD(((buffer_size
- 1) >> 21) & 0x3f, BRW_SURFACE_DEPTH
) |
121 surf
[7] = SET_FIELD(HSW_SCS_RED
, GEN7_SURFACE_SCS_R
) |
122 SET_FIELD(HSW_SCS_GREEN
, GEN7_SURFACE_SCS_G
) |
123 SET_FIELD(HSW_SCS_BLUE
, GEN7_SURFACE_SCS_B
) |
124 SET_FIELD(HSW_SCS_ALPHA
, GEN7_SURFACE_SCS_A
);
126 *((uint64_t *) &surf
[8]) = (bo
? bo
->offset64
: 0) + buffer_offset
;
128 /* Emit relocation to surface contents. */
130 drm_intel_bo_emit_reloc(brw
->batch
.bo
, *out_offset
+ 8 * 4,
131 bo
, buffer_offset
, I915_GEM_DOMAIN_SAMPLER
,
132 rw
? I915_GEM_DOMAIN_SAMPLER
: 0);
137 gen8_update_texture_surface(struct gl_context
*ctx
,
139 uint32_t *surf_offset
,
142 struct brw_context
*brw
= brw_context(ctx
);
143 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[unit
]._Current
;
144 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
145 struct intel_mipmap_tree
*mt
= intelObj
->mt
;
146 struct gl_texture_image
*firstImage
= tObj
->Image
[0][tObj
->BaseLevel
];
147 struct gl_sampler_object
*sampler
= _mesa_get_samplerobj(ctx
, unit
);
149 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
150 brw_update_buffer_texture_surface(ctx
, unit
, surf_offset
);
154 uint32_t tex_format
= translate_tex_format(brw
,
156 sampler
->sRGBDecode
);
158 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
159 13 * 4, 64, surf_offset
);
161 surf
[0] = translate_tex_target(tObj
->Target
) << BRW_SURFACE_TYPE_SHIFT
|
162 tex_format
<< BRW_SURFACE_FORMAT_SHIFT
|
163 vertical_alignment(mt
) |
164 horizontal_alignment(mt
) |
165 surface_tiling_mode(mt
->region
->tiling
);
167 if (tObj
->Target
== GL_TEXTURE_CUBE_MAP
||
168 tObj
->Target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
169 surf
[0] |= BRW_SURFACE_CUBEFACE_ENABLES
;
172 if (mt
->logical_depth0
> 1 && tObj
->Target
!= GL_TEXTURE_3D
)
173 surf
[0] |= GEN8_SURFACE_IS_ARRAY
;
175 surf
[1] = mt
->qpitch
>> 2;
177 surf
[2] = SET_FIELD(mt
->logical_width0
- 1, GEN7_SURFACE_WIDTH
) |
178 SET_FIELD(mt
->logical_height0
- 1, GEN7_SURFACE_HEIGHT
);
180 surf
[3] = SET_FIELD(mt
->logical_depth0
- 1, BRW_SURFACE_DEPTH
) |
181 (mt
->region
->pitch
- 1);
183 surf
[4] = surface_num_multisamples(mt
->num_samples
);
185 surf
[5] = SET_FIELD(tObj
->BaseLevel
- mt
->first_level
, GEN7_SURFACE_MIN_LOD
) |
186 (intelObj
->_MaxLevel
- tObj
->BaseLevel
); /* mip count */
190 /* Handling GL_ALPHA as a surface format override breaks 1.30+ style
191 * texturing functions that return a float, as our code generation always
192 * selects the .x channel (which would always be 0).
194 const bool alpha_depth
= tObj
->DepthMode
== GL_ALPHA
&&
195 (firstImage
->_BaseFormat
== GL_DEPTH_COMPONENT
||
196 firstImage
->_BaseFormat
== GL_DEPTH_STENCIL
);
199 unlikely(alpha_depth
) ? SWIZZLE_XYZW
: brw_get_texture_swizzle(ctx
, tObj
);
201 SET_FIELD(brw_swizzle_to_scs(GET_SWZ(swizzle
, 0), false), GEN7_SURFACE_SCS_R
) |
202 SET_FIELD(brw_swizzle_to_scs(GET_SWZ(swizzle
, 1), false), GEN7_SURFACE_SCS_G
) |
203 SET_FIELD(brw_swizzle_to_scs(GET_SWZ(swizzle
, 2), false), GEN7_SURFACE_SCS_B
) |
204 SET_FIELD(brw_swizzle_to_scs(GET_SWZ(swizzle
, 3), false), GEN7_SURFACE_SCS_A
);
206 *((uint64_t *) &surf
[8]) = mt
->region
->bo
->offset64
+ mt
->offset
; /* reloc */
212 /* Emit relocation to surface contents */
213 drm_intel_bo_emit_reloc(brw
->batch
.bo
,
214 *surf_offset
+ 8 * 4,
217 I915_GEM_DOMAIN_SAMPLER
, 0);
221 * Create the constant buffer surface. Vertex/fragment shader constants will be
222 * read from this buffer with Data Port Read instructions/messages.
225 gen8_update_null_renderbuffer_surface(struct brw_context
*brw
, unsigned unit
)
227 struct gl_context
*ctx
= &brw
->ctx
;
230 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
231 uint32_t surf_index
=
232 brw
->wm
.prog_data
->binding_table
.render_target_start
+ unit
;
234 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
, 13 * 4, 64,
235 &brw
->wm
.base
.surf_offset
[surf_index
]);
236 memset(surf
, 0, 13 * 4);
238 surf
[0] = BRW_SURFACE_NULL
<< BRW_SURFACE_TYPE_SHIFT
|
239 BRW_SURFACEFORMAT_B8G8R8A8_UNORM
<< BRW_SURFACE_FORMAT_SHIFT
|
240 GEN8_SURFACE_TILING_Y
;
241 surf
[2] = SET_FIELD(fb
->Width
- 1, GEN7_SURFACE_WIDTH
) |
242 SET_FIELD(fb
->Height
- 1, GEN7_SURFACE_HEIGHT
);
246 * Sets up a surface state structure to point at the given region.
247 * While it is only used for the front/back buffer currently, it should be
248 * usable for further buffers when doing ARB_draw_buffer support.
251 gen8_update_renderbuffer_surface(struct brw_context
*brw
,
252 struct gl_renderbuffer
*rb
,
256 struct gl_context
*ctx
= &brw
->ctx
;
257 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
258 struct intel_mipmap_tree
*mt
= irb
->mt
;
259 struct intel_region
*region
= mt
->region
;
262 bool is_array
= false;
263 int depth
= MAX2(rb
->Depth
, 1);
264 int min_array_element
;
267 rb
->TexImage
? rb
->TexImage
->TexObject
->Target
: GL_TEXTURE_2D
;
269 uint32_t surf_index
=
270 brw
->wm
.prog_data
->binding_table
.render_target_start
+ unit
;
272 intel_miptree_used_for_rendering(mt
);
274 /* Render targets can't use IMS layout. */
275 assert(mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_IMS
);
278 case GL_TEXTURE_CUBE_MAP_ARRAY
:
279 case GL_TEXTURE_CUBE_MAP
:
280 surf_type
= BRW_SURFACE_2D
;
285 surf_type
= translate_tex_target(gl_target
);
286 is_array
= _mesa_tex_target_is_array(gl_target
);
291 min_array_element
= 0;
292 } else if (mt
->num_samples
> 1) {
293 min_array_element
= irb
->mt_layer
/ mt
->num_samples
;
295 min_array_element
= irb
->mt_layer
;
299 mesa_format rb_format
= _mesa_get_render_format(ctx
, intel_rb_format(irb
));
300 assert(brw_render_target_supported(brw
, rb
));
301 format
= brw
->render_target_format
[rb_format
];
302 if (unlikely(!brw
->format_supported_as_render_target
[rb_format
])) {
303 _mesa_problem(ctx
, "%s: renderbuffer format %s unsupported\n",
304 __FUNCTION__
, _mesa_get_format_name(rb_format
));
307 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
, 13 * 4, 64,
308 &brw
->wm
.base
.surf_offset
[surf_index
]);
310 surf
[0] = (surf_type
<< BRW_SURFACE_TYPE_SHIFT
) |
311 (is_array
? GEN7_SURFACE_IS_ARRAY
: 0) |
312 (format
<< BRW_SURFACE_FORMAT_SHIFT
) |
313 vertical_alignment(mt
) |
314 horizontal_alignment(mt
) |
315 surface_tiling_mode(region
->tiling
);
317 surf
[1] = mt
->qpitch
>> 2;
319 surf
[2] = SET_FIELD(mt
->logical_width0
- 1, GEN7_SURFACE_WIDTH
) |
320 SET_FIELD(mt
->logical_height0
- 1, GEN7_SURFACE_HEIGHT
);
322 surf
[3] = (depth
- 1) << BRW_SURFACE_DEPTH_SHIFT
|
323 (region
->pitch
- 1); /* Surface Pitch */
325 surf
[4] = surface_num_multisamples(mt
->num_samples
) |
326 min_array_element
<< GEN7_SURFACE_MIN_ARRAY_ELEMENT_SHIFT
|
327 (depth
- 1) << GEN7_SURFACE_RENDER_TARGET_VIEW_EXTENT_SHIFT
;
329 surf
[5] = irb
->mt_level
- irb
->mt
->first_level
;
331 surf
[6] = 0; /* Nothing of relevance. */
333 surf
[7] = mt
->fast_clear_color_value
|
334 SET_FIELD(HSW_SCS_RED
, GEN7_SURFACE_SCS_R
) |
335 SET_FIELD(HSW_SCS_GREEN
, GEN7_SURFACE_SCS_G
) |
336 SET_FIELD(HSW_SCS_BLUE
, GEN7_SURFACE_SCS_B
) |
337 SET_FIELD(HSW_SCS_ALPHA
, GEN7_SURFACE_SCS_A
);
339 *((uint64_t *) &surf
[8]) = region
->bo
->offset64
; /* reloc */
341 /* Nothing of relevance. */
346 drm_intel_bo_emit_reloc(brw
->batch
.bo
,
347 brw
->wm
.base
.surf_offset
[surf_index
] + 8 * 4,
350 I915_GEM_DOMAIN_RENDER
,
351 I915_GEM_DOMAIN_RENDER
);
355 gen8_init_vtable_surface_functions(struct brw_context
*brw
)
357 brw
->vtbl
.update_texture_surface
= gen8_update_texture_surface
;
358 brw
->vtbl
.update_renderbuffer_surface
= gen8_update_renderbuffer_surface
;
359 brw
->vtbl
.update_null_renderbuffer_surface
=
360 gen8_update_null_renderbuffer_surface
;
361 brw
->vtbl
.emit_buffer_surface_state
= gen8_emit_buffer_surface_state
;