2 * Copyright © 2013 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
25 * \file brw_binding_tables.c
27 * State atoms which upload the "binding table" for each shader stage.
29 * Binding tables map a numeric "surface index" to the SURFACE_STATE structure
30 * for a currently bound surface. This allows SEND messages (such as sampler
31 * or data port messages) to refer to a particular surface by number, rather
34 * The binding table is stored as a (sparse) array of SURFACE_STATE entries;
35 * surface indexes are simply indexes into the array. The ordering of the
36 * entries is entirely left up to software; see the SURF_INDEX_* macros in
37 * brw_context.h to see our current layout.
40 #include "main/mtypes.h"
42 #include "brw_context.h"
43 #include "brw_defines.h"
44 #include "brw_state.h"
45 #include "intel_batchbuffer.h"
47 static const GLuint stage_to_bt_edit
[MESA_SHADER_FRAGMENT
+ 1] = {
48 _3DSTATE_BINDING_TABLE_EDIT_VS
,
49 _3DSTATE_BINDING_TABLE_EDIT_GS
,
50 _3DSTATE_BINDING_TABLE_EDIT_PS
,
54 * Upload a shader stage's binding table as indirect state.
56 * This copies brw_stage_state::surf_offset[] into the indirect state section
57 * of the batchbuffer (allocated by brw_state_batch()).
60 brw_upload_binding_table(struct brw_context
*brw
,
62 GLbitfield brw_new_binding_table
,
63 const struct brw_stage_prog_data
*prog_data
,
64 struct brw_stage_state
*stage_state
)
66 if (prog_data
->binding_table
.size_bytes
== 0) {
67 /* There are no surfaces; skip making the binding table altogether. */
68 if (stage_state
->bind_bo_offset
== 0 && brw
->gen
< 9)
71 stage_state
->bind_bo_offset
= 0;
73 /* Upload a new binding table. */
74 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
75 brw
->vtbl
.emit_buffer_surface_state(
76 brw
, &stage_state
->surf_offset
[
77 prog_data
->binding_table
.shader_time_start
],
78 brw
->shader_time
.bo
, 0, BRW_SURFACEFORMAT_RAW
,
79 brw
->shader_time
.bo
->size
, 1, true);
82 uint32_t *bind
= brw_state_batch(brw
, AUB_TRACE_BINDING_TABLE
,
83 prog_data
->binding_table
.size_bytes
, 32,
84 &stage_state
->bind_bo_offset
);
86 /* BRW_NEW_SURFACES and BRW_NEW_*_CONSTBUF */
87 memcpy(bind
, stage_state
->surf_offset
,
88 prog_data
->binding_table
.size_bytes
);
91 brw
->ctx
.NewDriverState
|= brw_new_binding_table
;
95 OUT_BATCH(packet_name
<< 16 | (2 - 2));
96 OUT_BATCH(stage_state
->bind_bo_offset
);
102 * State atoms which upload the binding table for a particular shader stage.
106 /** Upload the VS binding table. */
108 brw_vs_upload_binding_table(struct brw_context
*brw
)
110 /* BRW_NEW_VS_PROG_DATA */
111 const struct brw_stage_prog_data
*prog_data
= brw
->vs
.base
.prog_data
;
112 brw_upload_binding_table(brw
,
113 _3DSTATE_BINDING_TABLE_POINTERS_VS
,
114 BRW_NEW_VS_BINDING_TABLE
, prog_data
,
118 const struct brw_tracked_state brw_vs_binding_table
= {
121 .brw
= BRW_NEW_BATCH
|
122 BRW_NEW_VS_CONSTBUF
|
123 BRW_NEW_VS_PROG_DATA
|
126 .emit
= brw_vs_upload_binding_table
,
130 /** Upload the PS binding table. */
132 brw_upload_wm_binding_table(struct brw_context
*brw
)
134 /* BRW_NEW_FS_PROG_DATA */
135 const struct brw_stage_prog_data
*prog_data
= brw
->wm
.base
.prog_data
;
136 brw_upload_binding_table(brw
,
137 _3DSTATE_BINDING_TABLE_POINTERS_PS
,
138 BRW_NEW_PS_BINDING_TABLE
, prog_data
,
142 const struct brw_tracked_state brw_wm_binding_table
= {
145 .brw
= BRW_NEW_BATCH
|
146 BRW_NEW_FS_PROG_DATA
|
149 .emit
= brw_upload_wm_binding_table
,
152 /** Upload the GS binding table (if GS is active). */
154 brw_gs_upload_binding_table(struct brw_context
*brw
)
156 /* If there's no GS, skip changing anything. */
157 if (brw
->geometry_program
== NULL
)
160 /* BRW_NEW_GS_PROG_DATA */
161 const struct brw_stage_prog_data
*prog_data
= brw
->gs
.base
.prog_data
;
162 brw_upload_binding_table(brw
,
163 _3DSTATE_BINDING_TABLE_POINTERS_GS
,
164 BRW_NEW_GS_BINDING_TABLE
, prog_data
,
168 const struct brw_tracked_state brw_gs_binding_table
= {
171 .brw
= BRW_NEW_BATCH
|
172 BRW_NEW_GS_CONSTBUF
|
173 BRW_NEW_GS_PROG_DATA
|
176 .emit
= brw_gs_upload_binding_table
,
180 * Edit a single entry in a hardware-generated binding table
183 gen7_edit_hw_binding_table_entry(struct brw_context
*brw
,
184 gl_shader_stage stage
,
186 uint32_t surf_offset
)
188 assert(stage
<= MESA_SHADER_FRAGMENT
);
190 uint32_t dw2
= SET_FIELD(index
, BRW_BINDING_TABLE_INDEX
) |
191 (brw
->gen
>= 8 ? GEN8_SURFACE_STATE_EDIT(surf_offset
) :
192 HSW_SURFACE_STATE_EDIT(surf_offset
));
195 OUT_BATCH(stage_to_bt_edit
[stage
] << 16 | (3 - 2));
196 OUT_BATCH(BRW_BINDING_TABLE_EDIT_TARGET_ALL
);
202 * Upload a whole hardware binding table for the given stage.
204 * Takes an array of surface offsets and the number of binding table
208 gen7_update_binding_table_from_array(struct brw_context
*brw
,
209 gl_shader_stage stage
,
210 const uint32_t* binding_table
,
214 assert(stage
<= MESA_SHADER_FRAGMENT
);
216 BEGIN_BATCH(num_surfaces
+ 2);
217 OUT_BATCH(stage_to_bt_edit
[stage
] << 16 | num_surfaces
);
218 OUT_BATCH(BRW_BINDING_TABLE_EDIT_TARGET_ALL
);
219 for (int i
= 0; i
< num_surfaces
; i
++) {
220 dw2
= SET_FIELD(i
, BRW_BINDING_TABLE_INDEX
) |
221 (brw
->gen
>= 8 ? GEN8_SURFACE_STATE_EDIT(binding_table
[i
]) :
222 HSW_SURFACE_STATE_EDIT(binding_table
[i
]));
229 * Disable hardware binding table support, falling back to the
230 * older software-generated binding table mechanism.
233 gen7_disable_hw_binding_tables(struct brw_context
*brw
)
235 if (!brw
->use_resource_streamer
)
237 /* From the Haswell PRM, Volume 7: 3D Media GPGPU,
238 * 3DSTATE_BINDING_TABLE_POOL_ALLOC > Programming Note:
240 * "When switching between HW and SW binding table generation, SW must
241 * issue a state cache invalidate."
243 brw_emit_pipe_control_flush(brw
, PIPE_CONTROL_STATE_CACHE_INVALIDATE
);
245 int pkt_len
= brw
->gen
>= 8 ? 4 : 3;
247 BEGIN_BATCH(pkt_len
);
248 OUT_BATCH(_3DSTATE_BINDING_TABLE_POOL_ALLOC
<< 16 | (pkt_len
- 2));
254 OUT_BATCH(HSW_BT_POOL_ALLOC_MUST_BE_ONE
);
261 * Enable hardware binding tables and set up the binding table pool.
264 gen7_enable_hw_binding_tables(struct brw_context
*brw
)
266 if (!brw
->use_resource_streamer
)
269 if (!brw
->hw_bt_pool
.bo
) {
270 /* We use a single re-usable buffer object for the lifetime of the
271 * context and size it to maximum allowed binding tables that can be
272 * programmed per batch:
274 * From the Haswell PRM, Volume 7: 3D Media GPGPU,
275 * 3DSTATE_BINDING_TABLE_POOL_ALLOC > Programming Note:
276 * "A maximum of 16,383 Binding tables are allowed in any batch buffer"
278 static const int max_size
= 16383 * 4;
279 brw
->hw_bt_pool
.bo
= drm_intel_bo_alloc(brw
->bufmgr
, "hw_bt",
281 brw
->hw_bt_pool
.next_offset
= 0;
284 /* From the Haswell PRM, Volume 7: 3D Media GPGPU,
285 * 3DSTATE_BINDING_TABLE_POOL_ALLOC > Programming Note:
287 * "When switching between HW and SW binding table generation, SW must
288 * issue a state cache invalidate."
290 brw_emit_pipe_control_flush(brw
, PIPE_CONTROL_STATE_CACHE_INVALIDATE
);
292 int pkt_len
= brw
->gen
>= 8 ? 4 : 3;
293 uint32_t dw1
= BRW_HW_BINDING_TABLE_ENABLE
;
294 if (brw
->is_haswell
) {
295 dw1
|= SET_FIELD(GEN7_MOCS_L3
, GEN7_HW_BT_POOL_MOCS
) |
296 HSW_BT_POOL_ALLOC_MUST_BE_ONE
;
297 } else if (brw
->gen
>= 8) {
301 BEGIN_BATCH(pkt_len
);
302 OUT_BATCH(_3DSTATE_BINDING_TABLE_POOL_ALLOC
<< 16 | (pkt_len
- 2));
304 OUT_RELOC64(brw
->hw_bt_pool
.bo
, I915_GEM_DOMAIN_SAMPLER
, 0, dw1
);
305 OUT_BATCH(brw
->hw_bt_pool
.bo
->size
);
307 OUT_RELOC(brw
->hw_bt_pool
.bo
, I915_GEM_DOMAIN_SAMPLER
, 0, dw1
);
308 OUT_RELOC(brw
->hw_bt_pool
.bo
, I915_GEM_DOMAIN_SAMPLER
, 0,
309 brw
->hw_bt_pool
.bo
->size
);
315 gen7_reset_hw_bt_pool_offsets(struct brw_context
*brw
)
317 brw
->hw_bt_pool
.next_offset
= 0;
320 const struct brw_tracked_state gen7_hw_binding_tables
= {
323 .brw
= BRW_NEW_BATCH
,
325 .emit
= gen7_enable_hw_binding_tables
331 * State atoms which emit 3DSTATE packets to update the binding table pointers.
336 * (Gen4-5) Upload the binding table pointers for all shader stages.
338 * The binding table pointers are relative to the surface state base address,
339 * which points at the batchbuffer containing the streamed batch state.
342 gen4_upload_binding_table_pointers(struct brw_context
*brw
)
345 OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS
<< 16 | (6 - 2));
346 OUT_BATCH(brw
->vs
.base
.bind_bo_offset
);
347 OUT_BATCH(0); /* gs */
348 OUT_BATCH(0); /* clip */
349 OUT_BATCH(0); /* sf */
350 OUT_BATCH(brw
->wm
.base
.bind_bo_offset
);
354 const struct brw_tracked_state brw_binding_table_pointers
= {
357 .brw
= BRW_NEW_BATCH
|
358 BRW_NEW_GS_BINDING_TABLE
|
359 BRW_NEW_PS_BINDING_TABLE
|
360 BRW_NEW_STATE_BASE_ADDRESS
|
361 BRW_NEW_VS_BINDING_TABLE
,
363 .emit
= gen4_upload_binding_table_pointers
,
367 * (Sandybridge Only) Upload the binding table pointers for all shader stages.
369 * The binding table pointers are relative to the surface state base address,
370 * which points at the batchbuffer containing the streamed batch state.
373 gen6_upload_binding_table_pointers(struct brw_context
*brw
)
376 OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS
<< 16 |
377 GEN6_BINDING_TABLE_MODIFY_VS
|
378 GEN6_BINDING_TABLE_MODIFY_GS
|
379 GEN6_BINDING_TABLE_MODIFY_PS
|
381 OUT_BATCH(brw
->vs
.base
.bind_bo_offset
); /* vs */
382 if (brw
->ff_gs
.prog_active
)
383 OUT_BATCH(brw
->ff_gs
.bind_bo_offset
); /* gs */
385 OUT_BATCH(brw
->gs
.base
.bind_bo_offset
); /* gs */
386 OUT_BATCH(brw
->wm
.base
.bind_bo_offset
); /* wm/ps */
390 const struct brw_tracked_state gen6_binding_table_pointers
= {
393 .brw
= BRW_NEW_BATCH
|
394 BRW_NEW_GS_BINDING_TABLE
|
395 BRW_NEW_PS_BINDING_TABLE
|
396 BRW_NEW_STATE_BASE_ADDRESS
|
397 BRW_NEW_VS_BINDING_TABLE
,
399 .emit
= gen6_upload_binding_table_pointers
,