2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "main/context.h"
34 #include "main/blend.h"
35 #include "main/mtypes.h"
36 #include "main/samplerobj.h"
37 #include "program/prog_parameter.h"
39 #include "intel_mipmap_tree.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_tex.h"
42 #include "intel_fbo.h"
43 #include "intel_buffer_objects.h"
45 #include "brw_context.h"
46 #include "brw_state.h"
47 #include "brw_defines.h"
51 translate_tex_target(GLenum target
)
55 case GL_TEXTURE_1D_ARRAY_EXT
:
56 return BRW_SURFACE_1D
;
58 case GL_TEXTURE_RECTANGLE_NV
:
59 return BRW_SURFACE_2D
;
62 case GL_TEXTURE_2D_ARRAY_EXT
:
63 case GL_TEXTURE_EXTERNAL_OES
:
64 case GL_TEXTURE_2D_MULTISAMPLE
:
65 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
66 return BRW_SURFACE_2D
;
69 return BRW_SURFACE_3D
;
71 case GL_TEXTURE_CUBE_MAP
:
72 case GL_TEXTURE_CUBE_MAP_ARRAY
:
73 return BRW_SURFACE_CUBE
;
82 brw_get_surface_tiling_bits(uint32_t tiling
)
86 return BRW_SURFACE_TILED
;
88 return BRW_SURFACE_TILED
| BRW_SURFACE_TILED_Y
;
96 brw_get_surface_num_multisamples(unsigned num_samples
)
99 return BRW_SURFACE_MULTISAMPLECOUNT_4
;
101 return BRW_SURFACE_MULTISAMPLECOUNT_1
;
106 * Compute the combination of DEPTH_TEXTURE_MODE and EXT_texture_swizzle
110 brw_get_texture_swizzle(const struct gl_context
*ctx
,
111 const struct gl_texture_object
*t
)
113 const struct gl_texture_image
*img
= t
->Image
[0][t
->BaseLevel
];
115 int swizzles
[SWIZZLE_NIL
+ 1] = {
125 if (img
->_BaseFormat
== GL_DEPTH_COMPONENT
||
126 img
->_BaseFormat
== GL_DEPTH_STENCIL
) {
127 GLenum depth_mode
= t
->DepthMode
;
129 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
130 * with depth component data specified with a sized internal format.
131 * Otherwise, it's left at the old default, GL_LUMINANCE.
133 if (_mesa_is_gles3(ctx
) &&
134 img
->InternalFormat
!= GL_DEPTH_COMPONENT
&&
135 img
->InternalFormat
!= GL_DEPTH_STENCIL
) {
139 switch (depth_mode
) {
141 swizzles
[0] = SWIZZLE_ZERO
;
142 swizzles
[1] = SWIZZLE_ZERO
;
143 swizzles
[2] = SWIZZLE_ZERO
;
144 swizzles
[3] = SWIZZLE_X
;
147 swizzles
[0] = SWIZZLE_X
;
148 swizzles
[1] = SWIZZLE_X
;
149 swizzles
[2] = SWIZZLE_X
;
150 swizzles
[3] = SWIZZLE_ONE
;
153 swizzles
[0] = SWIZZLE_X
;
154 swizzles
[1] = SWIZZLE_X
;
155 swizzles
[2] = SWIZZLE_X
;
156 swizzles
[3] = SWIZZLE_X
;
159 swizzles
[0] = SWIZZLE_X
;
160 swizzles
[1] = SWIZZLE_ZERO
;
161 swizzles
[2] = SWIZZLE_ZERO
;
162 swizzles
[3] = SWIZZLE_ONE
;
167 /* If the texture's format is alpha-only, force R, G, and B to
168 * 0.0. Similarly, if the texture's format has no alpha channel,
169 * force the alpha value read to 1.0. This allows for the
170 * implementation to use an RGBA texture for any of these formats
171 * without leaking any unexpected values.
173 switch (img
->_BaseFormat
) {
175 swizzles
[0] = SWIZZLE_ZERO
;
176 swizzles
[1] = SWIZZLE_ZERO
;
177 swizzles
[2] = SWIZZLE_ZERO
;
182 if (_mesa_get_format_bits(img
->TexFormat
, GL_ALPHA_BITS
) > 0)
183 swizzles
[3] = SWIZZLE_ONE
;
187 return MAKE_SWIZZLE4(swizzles
[GET_SWZ(t
->_Swizzle
, 0)],
188 swizzles
[GET_SWZ(t
->_Swizzle
, 1)],
189 swizzles
[GET_SWZ(t
->_Swizzle
, 2)],
190 swizzles
[GET_SWZ(t
->_Swizzle
, 3)]);
194 gen4_emit_buffer_surface_state(struct brw_context
*brw
,
195 uint32_t *out_offset
,
197 unsigned buffer_offset
,
198 unsigned surface_format
,
199 unsigned buffer_size
,
202 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
203 6 * 4, 32, out_offset
);
204 memset(surf
, 0, 6 * 4);
206 surf
[0] = BRW_SURFACE_BUFFER
<< BRW_SURFACE_TYPE_SHIFT
|
207 surface_format
<< BRW_SURFACE_FORMAT_SHIFT
|
208 (brw
->gen
>= 6 ? BRW_SURFACE_RC_READ_WRITE
: 0);
209 surf
[1] = (bo
? bo
->offset
: 0) + buffer_offset
; /* reloc */
210 surf
[2] = (buffer_size
& 0x7f) << BRW_SURFACE_WIDTH_SHIFT
|
211 ((buffer_size
>> 7) & 0x1fff) << BRW_SURFACE_HEIGHT_SHIFT
;
212 surf
[3] = ((buffer_size
>> 20) & 0x7f) << BRW_SURFACE_DEPTH_SHIFT
|
213 (pitch
- 1) << BRW_SURFACE_PITCH_SHIFT
;
215 /* Emit relocation to surface contents. The 965 PRM, Volume 4, section
216 * 5.1.2 "Data Cache" says: "the data cache does not exist as a separate
217 * physical cache. It is mapped in hardware to the sampler cache."
220 drm_intel_bo_emit_reloc(brw
->batch
.bo
, *out_offset
+ 4,
222 I915_GEM_DOMAIN_SAMPLER
, 0);
227 brw_update_buffer_texture_surface(struct gl_context
*ctx
,
229 uint32_t *surf_offset
)
231 struct brw_context
*brw
= brw_context(ctx
);
232 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[unit
]._Current
;
233 struct intel_buffer_object
*intel_obj
=
234 intel_buffer_object(tObj
->BufferObject
);
235 drm_intel_bo
*bo
= intel_obj
? intel_obj
->buffer
: NULL
;
236 gl_format format
= tObj
->_BufferObjectFormat
;
237 uint32_t brw_format
= brw_format_for_mesa_format(format
);
238 int texel_size
= _mesa_get_format_bytes(format
);
239 int w
= intel_obj
? intel_obj
->Base
.Size
/ texel_size
: 0;
241 if (brw_format
== 0 && format
!= MESA_FORMAT_RGBA_FLOAT32
) {
242 _mesa_problem(NULL
, "bad format %s for texture buffer\n",
243 _mesa_get_format_name(format
));
246 gen4_emit_buffer_surface_state(brw
, surf_offset
, bo
, 0,
252 brw_update_texture_surface(struct gl_context
*ctx
,
254 uint32_t *surf_offset
,
257 struct brw_context
*brw
= brw_context(ctx
);
258 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[unit
]._Current
;
259 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
260 struct intel_mipmap_tree
*mt
= intelObj
->mt
;
261 struct gl_sampler_object
*sampler
= _mesa_get_samplerobj(ctx
, unit
);
264 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
265 brw_update_buffer_texture_surface(ctx
, unit
, surf_offset
);
269 surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
270 6 * 4, 32, surf_offset
);
272 (void) for_gather
; /* no w/a to apply for this gen */
274 surf
[0] = (translate_tex_target(tObj
->Target
) << BRW_SURFACE_TYPE_SHIFT
|
275 BRW_SURFACE_MIPMAPLAYOUT_BELOW
<< BRW_SURFACE_MIPLAYOUT_SHIFT
|
276 BRW_SURFACE_CUBEFACE_ENABLES
|
277 (translate_tex_format(brw
,
280 sampler
->sRGBDecode
) <<
281 BRW_SURFACE_FORMAT_SHIFT
));
283 surf
[1] = intelObj
->mt
->region
->bo
->offset
+ intelObj
->mt
->offset
; /* reloc */
285 surf
[2] = ((intelObj
->_MaxLevel
- tObj
->BaseLevel
) << BRW_SURFACE_LOD_SHIFT
|
286 (mt
->logical_width0
- 1) << BRW_SURFACE_WIDTH_SHIFT
|
287 (mt
->logical_height0
- 1) << BRW_SURFACE_HEIGHT_SHIFT
);
289 surf
[3] = (brw_get_surface_tiling_bits(intelObj
->mt
->region
->tiling
) |
290 (mt
->logical_depth0
- 1) << BRW_SURFACE_DEPTH_SHIFT
|
291 (intelObj
->mt
->region
->pitch
- 1) <<
292 BRW_SURFACE_PITCH_SHIFT
);
294 surf
[4] = (brw_get_surface_num_multisamples(intelObj
->mt
->num_samples
) |
295 SET_FIELD(tObj
->BaseLevel
- mt
->first_level
, BRW_SURFACE_MIN_LOD
));
297 surf
[5] = mt
->align_h
== 4 ? BRW_SURFACE_VERTICAL_ALIGN_ENABLE
: 0;
299 /* Emit relocation to surface contents */
300 drm_intel_bo_emit_reloc(brw
->batch
.bo
,
302 intelObj
->mt
->region
->bo
,
303 surf
[1] - intelObj
->mt
->region
->bo
->offset
,
304 I915_GEM_DOMAIN_SAMPLER
, 0);
308 * Create the constant buffer surface. Vertex/fragment shader constants will be
309 * read from this buffer with Data Port Read instructions/messages.
312 brw_create_constant_surface(struct brw_context
*brw
,
316 uint32_t *out_offset
,
319 uint32_t stride
= dword_pitch
? 4 : 16;
320 uint32_t elements
= ALIGN(size
, stride
) / stride
;
322 gen4_emit_buffer_surface_state(brw
, out_offset
, bo
, offset
,
323 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT
,
328 * Set up a binding table entry for use by stream output logic (transform
331 * buffer_size_minus_1 must me less than BRW_MAX_NUM_BUFFER_ENTRIES.
334 brw_update_sol_surface(struct brw_context
*brw
,
335 struct gl_buffer_object
*buffer_obj
,
336 uint32_t *out_offset
, unsigned num_vector_components
,
337 unsigned stride_dwords
, unsigned offset_dwords
)
339 struct intel_buffer_object
*intel_bo
= intel_buffer_object(buffer_obj
);
340 drm_intel_bo
*bo
= intel_bufferobj_buffer(brw
, intel_bo
, INTEL_WRITE_PART
);
341 uint32_t *surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
, 6 * 4, 32,
343 uint32_t pitch_minus_1
= 4*stride_dwords
- 1;
344 uint32_t offset_bytes
= 4 * offset_dwords
;
345 size_t size_dwords
= buffer_obj
->Size
/ 4;
346 uint32_t buffer_size_minus_1
, width
, height
, depth
, surface_format
;
348 /* FIXME: can we rely on core Mesa to ensure that the buffer isn't
349 * too big to map using a single binding table entry?
351 assert((size_dwords
- offset_dwords
) / stride_dwords
352 <= BRW_MAX_NUM_BUFFER_ENTRIES
);
354 if (size_dwords
> offset_dwords
+ num_vector_components
) {
355 /* There is room for at least 1 transform feedback output in the buffer.
356 * Compute the number of additional transform feedback outputs the
357 * buffer has room for.
359 buffer_size_minus_1
=
360 (size_dwords
- offset_dwords
- num_vector_components
) / stride_dwords
;
362 /* There isn't even room for a single transform feedback output in the
363 * buffer. We can't configure the binding table entry to prevent output
364 * entirely; we'll have to rely on the geometry shader to detect
365 * overflow. But to minimize the damage in case of a bug, set up the
366 * binding table entry to just allow a single output.
368 buffer_size_minus_1
= 0;
370 width
= buffer_size_minus_1
& 0x7f;
371 height
= (buffer_size_minus_1
& 0xfff80) >> 7;
372 depth
= (buffer_size_minus_1
& 0x7f00000) >> 20;
374 switch (num_vector_components
) {
376 surface_format
= BRW_SURFACEFORMAT_R32_FLOAT
;
379 surface_format
= BRW_SURFACEFORMAT_R32G32_FLOAT
;
382 surface_format
= BRW_SURFACEFORMAT_R32G32B32_FLOAT
;
385 surface_format
= BRW_SURFACEFORMAT_R32G32B32A32_FLOAT
;
388 assert(!"Invalid vector size for transform feedback output");
389 surface_format
= BRW_SURFACEFORMAT_R32_FLOAT
;
393 surf
[0] = BRW_SURFACE_BUFFER
<< BRW_SURFACE_TYPE_SHIFT
|
394 BRW_SURFACE_MIPMAPLAYOUT_BELOW
<< BRW_SURFACE_MIPLAYOUT_SHIFT
|
395 surface_format
<< BRW_SURFACE_FORMAT_SHIFT
|
396 BRW_SURFACE_RC_READ_WRITE
;
397 surf
[1] = bo
->offset
+ offset_bytes
; /* reloc */
398 surf
[2] = (width
<< BRW_SURFACE_WIDTH_SHIFT
|
399 height
<< BRW_SURFACE_HEIGHT_SHIFT
);
400 surf
[3] = (depth
<< BRW_SURFACE_DEPTH_SHIFT
|
401 pitch_minus_1
<< BRW_SURFACE_PITCH_SHIFT
);
405 /* Emit relocation to surface contents. */
406 drm_intel_bo_emit_reloc(brw
->batch
.bo
,
409 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
);
412 /* Creates a new WM constant buffer reflecting the current fragment program's
413 * constants, if needed by the fragment program.
415 * Otherwise, constants go through the CURBEs using the brw_constant_buffer
419 brw_upload_wm_pull_constants(struct brw_context
*brw
)
421 struct gl_context
*ctx
= &brw
->ctx
;
422 /* BRW_NEW_FRAGMENT_PROGRAM */
423 struct brw_fragment_program
*fp
=
424 (struct brw_fragment_program
*) brw
->fragment_program
;
425 struct gl_program_parameter_list
*params
= fp
->program
.Base
.Parameters
;
426 const int size
= brw
->wm
.prog_data
->nr_pull_params
* sizeof(float);
427 const int surf_index
=
428 brw
->wm
.prog_data
->base
.binding_table
.pull_constants_start
;
432 _mesa_load_state_parameters(ctx
, params
);
434 /* CACHE_NEW_WM_PROG */
435 if (brw
->wm
.prog_data
->nr_pull_params
== 0) {
436 if (brw
->wm
.base
.const_bo
) {
437 drm_intel_bo_unreference(brw
->wm
.base
.const_bo
);
438 brw
->wm
.base
.const_bo
= NULL
;
439 brw
->wm
.base
.surf_offset
[surf_index
] = 0;
440 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
445 drm_intel_bo_unreference(brw
->wm
.base
.const_bo
);
446 brw
->wm
.base
.const_bo
= drm_intel_bo_alloc(brw
->bufmgr
, "WM const bo",
449 /* _NEW_PROGRAM_CONSTANTS */
450 drm_intel_gem_bo_map_gtt(brw
->wm
.base
.const_bo
);
451 constants
= brw
->wm
.base
.const_bo
->virtual;
452 for (i
= 0; i
< brw
->wm
.prog_data
->nr_pull_params
; i
++) {
453 constants
[i
] = *brw
->wm
.prog_data
->pull_param
[i
];
455 drm_intel_gem_bo_unmap_gtt(brw
->wm
.base
.const_bo
);
457 brw
->vtbl
.create_constant_surface(brw
, brw
->wm
.base
.const_bo
, 0, size
,
458 &brw
->wm
.base
.surf_offset
[surf_index
],
461 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
464 const struct brw_tracked_state brw_wm_pull_constants
= {
466 .mesa
= (_NEW_PROGRAM_CONSTANTS
),
467 .brw
= (BRW_NEW_BATCH
| BRW_NEW_FRAGMENT_PROGRAM
),
468 .cache
= CACHE_NEW_WM_PROG
,
470 .emit
= brw_upload_wm_pull_constants
,
474 brw_update_null_renderbuffer_surface(struct brw_context
*brw
, unsigned int unit
)
476 /* From the Sandy bridge PRM, Vol4 Part1 p71 (Surface Type: Programming
479 * A null surface will be used in instances where an actual surface is
480 * not bound. When a write message is generated to a null surface, no
481 * actual surface is written to. When a read message (including any
482 * sampling engine message) is generated to a null surface, the result
483 * is all zeros. Note that a null surface type is allowed to be used
484 * with all messages, even if it is not specificially indicated as
485 * supported. All of the remaining fields in surface state are ignored
486 * for null surfaces, with the following exceptions:
488 * - [DevSNB+]: Width, Height, Depth, and LOD fields must match the
489 * depth buffer’s corresponding state for all render target surfaces,
492 * - Surface Format must be R8G8B8A8_UNORM.
494 struct gl_context
*ctx
= &brw
->ctx
;
496 unsigned surface_type
= BRW_SURFACE_NULL
;
497 drm_intel_bo
*bo
= NULL
;
498 unsigned pitch_minus_1
= 0;
499 uint32_t multisampling_state
= 0;
500 uint32_t surf_index
=
501 brw
->wm
.prog_data
->binding_table
.render_target_start
+ unit
;
504 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
506 surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
, 6 * 4, 32,
507 &brw
->wm
.base
.surf_offset
[surf_index
]);
509 if (fb
->Visual
.samples
> 1) {
510 /* On Gen6, null render targets seem to cause GPU hangs when
511 * multisampling. So work around this problem by rendering into dummy
514 * To decrease the amount of memory needed by the workaround buffer, we
515 * set its pitch to 128 bytes (the width of a Y tile). This means that
516 * the amount of memory needed for the workaround buffer is
517 * (width_in_tiles + height_in_tiles - 1) tiles.
519 * Note that since the workaround buffer will be interpreted by the
520 * hardware as an interleaved multisampled buffer, we need to compute
521 * width_in_tiles and height_in_tiles by dividing the width and height
522 * by 16 rather than the normal Y-tile size of 32.
524 unsigned width_in_tiles
= ALIGN(fb
->Width
, 16) / 16;
525 unsigned height_in_tiles
= ALIGN(fb
->Height
, 16) / 16;
526 unsigned size_needed
= (width_in_tiles
+ height_in_tiles
- 1) * 4096;
527 brw_get_scratch_bo(brw
, &brw
->wm
.multisampled_null_render_target_bo
,
529 bo
= brw
->wm
.multisampled_null_render_target_bo
;
530 surface_type
= BRW_SURFACE_2D
;
532 multisampling_state
=
533 brw_get_surface_num_multisamples(fb
->Visual
.samples
);
536 surf
[0] = (surface_type
<< BRW_SURFACE_TYPE_SHIFT
|
537 BRW_SURFACEFORMAT_B8G8R8A8_UNORM
<< BRW_SURFACE_FORMAT_SHIFT
);
539 surf
[0] |= (1 << BRW_SURFACE_WRITEDISABLE_R_SHIFT
|
540 1 << BRW_SURFACE_WRITEDISABLE_G_SHIFT
|
541 1 << BRW_SURFACE_WRITEDISABLE_B_SHIFT
|
542 1 << BRW_SURFACE_WRITEDISABLE_A_SHIFT
);
544 surf
[1] = bo
? bo
->offset
: 0;
545 surf
[2] = ((fb
->Width
- 1) << BRW_SURFACE_WIDTH_SHIFT
|
546 (fb
->Height
- 1) << BRW_SURFACE_HEIGHT_SHIFT
);
548 /* From Sandy bridge PRM, Vol4 Part1 p82 (Tiled Surface: Programming
551 * If Surface Type is SURFTYPE_NULL, this field must be TRUE
553 surf
[3] = (BRW_SURFACE_TILED
| BRW_SURFACE_TILED_Y
|
554 pitch_minus_1
<< BRW_SURFACE_PITCH_SHIFT
);
555 surf
[4] = multisampling_state
;
559 drm_intel_bo_emit_reloc(brw
->batch
.bo
,
560 brw
->wm
.base
.surf_offset
[surf_index
] + 4,
562 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
);
567 * Sets up a surface state structure to point at the given region.
568 * While it is only used for the front/back buffer currently, it should be
569 * usable for further buffers when doing ARB_draw_buffer support.
572 brw_update_renderbuffer_surface(struct brw_context
*brw
,
573 struct gl_renderbuffer
*rb
,
577 struct gl_context
*ctx
= &brw
->ctx
;
578 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
579 struct intel_mipmap_tree
*mt
= irb
->mt
;
580 struct intel_region
*region
;
582 uint32_t tile_x
, tile_y
;
585 gl_format rb_format
= _mesa_get_render_format(ctx
, intel_rb_format(irb
));
586 uint32_t surf_index
=
587 brw
->wm
.prog_data
->binding_table
.render_target_start
+ unit
;
591 if (rb
->TexImage
&& !brw
->has_surface_tile_offset
) {
592 intel_renderbuffer_get_tile_offsets(irb
, &tile_x
, &tile_y
);
594 if (tile_x
!= 0 || tile_y
!= 0) {
595 /* Original gen4 hardware couldn't draw to a non-tile-aligned
596 * destination in a miptree unless you actually setup your renderbuffer
597 * as a miptree and used the fragile lod/array_index/etc. controls to
598 * select the image. So, instead, we just make a new single-level
599 * miptree and render into that.
601 intel_renderbuffer_move_to_temp(brw
, irb
, false);
606 intel_miptree_used_for_rendering(irb
->mt
);
608 region
= irb
->mt
->region
;
610 surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
, 6 * 4, 32,
611 &brw
->wm
.base
.surf_offset
[surf_index
]);
613 format
= brw
->render_target_format
[rb_format
];
614 if (unlikely(!brw
->format_supported_as_render_target
[rb_format
])) {
615 _mesa_problem(ctx
, "%s: renderbuffer format %s unsupported\n",
616 __FUNCTION__
, _mesa_get_format_name(rb_format
));
619 surf
[0] = (BRW_SURFACE_2D
<< BRW_SURFACE_TYPE_SHIFT
|
620 format
<< BRW_SURFACE_FORMAT_SHIFT
);
623 surf
[1] = (intel_renderbuffer_get_tile_offsets(irb
, &tile_x
, &tile_y
) +
626 surf
[2] = ((rb
->Width
- 1) << BRW_SURFACE_WIDTH_SHIFT
|
627 (rb
->Height
- 1) << BRW_SURFACE_HEIGHT_SHIFT
);
629 surf
[3] = (brw_get_surface_tiling_bits(region
->tiling
) |
630 (region
->pitch
- 1) << BRW_SURFACE_PITCH_SHIFT
);
632 surf
[4] = brw_get_surface_num_multisamples(mt
->num_samples
);
634 assert(brw
->has_surface_tile_offset
|| (tile_x
== 0 && tile_y
== 0));
635 /* Note that the low bits of these fields are missing, so
636 * there's the possibility of getting in trouble.
638 assert(tile_x
% 4 == 0);
639 assert(tile_y
% 2 == 0);
640 surf
[5] = ((tile_x
/ 4) << BRW_SURFACE_X_OFFSET_SHIFT
|
641 (tile_y
/ 2) << BRW_SURFACE_Y_OFFSET_SHIFT
|
642 (mt
->align_h
== 4 ? BRW_SURFACE_VERTICAL_ALIGN_ENABLE
: 0));
646 if (!ctx
->Color
.ColorLogicOpEnabled
&&
647 (ctx
->Color
.BlendEnabled
& (1 << unit
)))
648 surf
[0] |= BRW_SURFACE_BLEND_ENABLED
;
650 if (!ctx
->Color
.ColorMask
[unit
][0])
651 surf
[0] |= 1 << BRW_SURFACE_WRITEDISABLE_R_SHIFT
;
652 if (!ctx
->Color
.ColorMask
[unit
][1])
653 surf
[0] |= 1 << BRW_SURFACE_WRITEDISABLE_G_SHIFT
;
654 if (!ctx
->Color
.ColorMask
[unit
][2])
655 surf
[0] |= 1 << BRW_SURFACE_WRITEDISABLE_B_SHIFT
;
657 /* As mentioned above, disable writes to the alpha component when the
658 * renderbuffer is XRGB.
660 if (ctx
->DrawBuffer
->Visual
.alphaBits
== 0 ||
661 !ctx
->Color
.ColorMask
[unit
][3]) {
662 surf
[0] |= 1 << BRW_SURFACE_WRITEDISABLE_A_SHIFT
;
666 drm_intel_bo_emit_reloc(brw
->batch
.bo
,
667 brw
->wm
.base
.surf_offset
[surf_index
] + 4,
669 surf
[1] - region
->bo
->offset
,
670 I915_GEM_DOMAIN_RENDER
,
671 I915_GEM_DOMAIN_RENDER
);
675 * Construct SURFACE_STATE objects for renderbuffers/draw buffers.
678 brw_update_renderbuffer_surfaces(struct brw_context
*brw
)
680 struct gl_context
*ctx
= &brw
->ctx
;
683 /* _NEW_BUFFERS | _NEW_COLOR */
684 /* Update surfaces for drawing buffers */
685 if (ctx
->DrawBuffer
->_NumColorDrawBuffers
>= 1) {
686 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
687 if (intel_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[i
])) {
688 brw
->vtbl
.update_renderbuffer_surface(brw
, ctx
->DrawBuffer
->_ColorDrawBuffers
[i
],
689 ctx
->DrawBuffer
->Layered
, i
);
691 brw
->vtbl
.update_null_renderbuffer_surface(brw
, i
);
695 brw
->vtbl
.update_null_renderbuffer_surface(brw
, 0);
697 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
700 const struct brw_tracked_state brw_renderbuffer_surfaces
= {
702 .mesa
= (_NEW_COLOR
|
704 .brw
= BRW_NEW_BATCH
,
707 .emit
= brw_update_renderbuffer_surfaces
,
710 const struct brw_tracked_state gen6_renderbuffer_surfaces
= {
712 .mesa
= _NEW_BUFFERS
,
713 .brw
= BRW_NEW_BATCH
,
716 .emit
= brw_update_renderbuffer_surfaces
,
721 update_stage_texture_surfaces(struct brw_context
*brw
,
722 const struct gl_program
*prog
,
723 struct brw_stage_state
*stage_state
,
729 struct gl_context
*ctx
= &brw
->ctx
;
731 uint32_t *surf_offset
= stage_state
->surf_offset
;
733 surf_offset
+= stage_state
->prog_data
->binding_table
.gather_texture_start
;
735 surf_offset
+= stage_state
->prog_data
->binding_table
.texture_start
;
737 unsigned num_samplers
= _mesa_fls(prog
->SamplersUsed
);
738 for (unsigned s
= 0; s
< num_samplers
; s
++) {
741 if (prog
->SamplersUsed
& (1 << s
)) {
742 const unsigned unit
= prog
->SamplerUnits
[s
];
745 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
) {
746 brw
->vtbl
.update_texture_surface(ctx
, unit
, surf_offset
+ s
, for_gather
);
754 * Construct SURFACE_STATE objects for enabled textures.
757 brw_update_texture_surfaces(struct brw_context
*brw
)
759 /* BRW_NEW_VERTEX_PROGRAM */
760 struct gl_program
*vs
= (struct gl_program
*) brw
->vertex_program
;
762 /* BRW_NEW_GEOMETRY_PROGRAM */
763 struct gl_program
*gs
= (struct gl_program
*) brw
->geometry_program
;
765 /* BRW_NEW_FRAGMENT_PROGRAM */
766 struct gl_program
*fs
= (struct gl_program
*) brw
->fragment_program
;
769 update_stage_texture_surfaces(brw
, vs
, &brw
->vs
.base
, false);
770 update_stage_texture_surfaces(brw
, gs
, &brw
->gs
.base
, false);
771 update_stage_texture_surfaces(brw
, fs
, &brw
->wm
.base
, false);
773 /* emit alternate set of surface state for gather. this
774 * allows the surface format to be overriden for only the
775 * gather4 messages. */
776 if (vs
&& vs
->UsesGather
)
777 update_stage_texture_surfaces(brw
, vs
, &brw
->vs
.base
, true);
778 if (gs
&& gs
->UsesGather
)
779 update_stage_texture_surfaces(brw
, gs
, &brw
->gs
.base
, true);
780 if (fs
&& fs
->UsesGather
)
781 update_stage_texture_surfaces(brw
, fs
, &brw
->wm
.base
, true);
783 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
786 const struct brw_tracked_state brw_texture_surfaces
= {
788 .mesa
= _NEW_TEXTURE
,
789 .brw
= BRW_NEW_BATCH
|
790 BRW_NEW_VERTEX_PROGRAM
|
791 BRW_NEW_GEOMETRY_PROGRAM
|
792 BRW_NEW_FRAGMENT_PROGRAM
,
795 .emit
= brw_update_texture_surfaces
,
799 brw_upload_ubo_surfaces(struct brw_context
*brw
,
800 struct gl_shader
*shader
,
801 struct brw_stage_state
*stage_state
,
802 struct brw_stage_prog_data
*prog_data
)
804 struct gl_context
*ctx
= &brw
->ctx
;
809 uint32_t *surf_offsets
=
810 &stage_state
->surf_offset
[prog_data
->binding_table
.ubo_start
];
812 for (int i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
813 struct gl_uniform_buffer_binding
*binding
;
814 struct intel_buffer_object
*intel_bo
;
816 binding
= &ctx
->UniformBufferBindings
[shader
->UniformBlocks
[i
].Binding
];
817 intel_bo
= intel_buffer_object(binding
->BufferObject
);
818 drm_intel_bo
*bo
= intel_bufferobj_buffer(brw
, intel_bo
, INTEL_READ
);
820 /* Because behavior for referencing outside of the binding's size in the
821 * glBindBufferRange case is undefined, we can just bind the whole buffer
822 * glBindBufferBase wants and be a correct implementation.
824 brw
->vtbl
.create_constant_surface(brw
, bo
, binding
->Offset
,
825 bo
->size
- binding
->Offset
,
827 shader
->Type
== GL_FRAGMENT_SHADER
);
830 if (shader
->NumUniformBlocks
)
831 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
835 brw_upload_wm_ubo_surfaces(struct brw_context
*brw
)
837 struct gl_context
*ctx
= &brw
->ctx
;
839 struct gl_shader_program
*prog
= ctx
->Shader
._CurrentFragmentProgram
;
844 /* CACHE_NEW_WM_PROG */
845 brw_upload_ubo_surfaces(brw
, prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
],
846 &brw
->wm
.base
, &brw
->wm
.prog_data
->base
);
849 const struct brw_tracked_state brw_wm_ubo_surfaces
= {
851 .mesa
= _NEW_PROGRAM
,
852 .brw
= BRW_NEW_BATCH
| BRW_NEW_UNIFORM_BUFFER
,
853 .cache
= CACHE_NEW_WM_PROG
,
855 .emit
= brw_upload_wm_ubo_surfaces
,
859 gen4_init_vtable_surface_functions(struct brw_context
*brw
)
861 brw
->vtbl
.update_texture_surface
= brw_update_texture_surface
;
862 brw
->vtbl
.update_renderbuffer_surface
= brw_update_renderbuffer_surface
;
863 brw
->vtbl
.update_null_renderbuffer_surface
=
864 brw_update_null_renderbuffer_surface
;
865 brw
->vtbl
.create_constant_surface
= brw_create_constant_surface
;