i965: Rename CACHE_NEW_*_PROG to BRW_NEW_*_PROG_DATA.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_binding_tables.c
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * \file brw_binding_tables.c
26 *
27 * State atoms which upload the "binding table" for each shader stage.
28 *
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
32 * than by pointer.
33 *
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.
38 */
39
40 #include "main/mtypes.h"
41
42 #include "brw_context.h"
43 #include "brw_defines.h"
44 #include "brw_state.h"
45 #include "intel_batchbuffer.h"
46
47 /**
48 * Upload a shader stage's binding table as indirect state.
49 *
50 * This copies brw_stage_state::surf_offset[] into the indirect state section
51 * of the batchbuffer (allocated by brw_state_batch()).
52 */
53 static void
54 brw_upload_binding_table(struct brw_context *brw,
55 uint32_t packet_name,
56 GLbitfield brw_new_binding_table,
57 struct brw_stage_state *stage_state)
58 {
59 /* BRW_NEW_*_PROG_DATA */
60 struct brw_stage_prog_data *prog_data = stage_state->prog_data;
61
62 if (prog_data->binding_table.size_bytes == 0) {
63 /* There are no surfaces; skip making the binding table altogether. */
64 if (stage_state->bind_bo_offset == 0 && brw->gen < 9)
65 return;
66
67 stage_state->bind_bo_offset = 0;
68 } else {
69 /* Upload a new binding table. */
70 if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
71 brw->vtbl.create_raw_surface(
72 brw, brw->shader_time.bo, 0, brw->shader_time.bo->size,
73 &stage_state->surf_offset[prog_data->binding_table.shader_time_start], true);
74 }
75
76 uint32_t *bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
77 prog_data->binding_table.size_bytes, 32,
78 &stage_state->bind_bo_offset);
79
80 /* BRW_NEW_SURFACES and BRW_NEW_*_CONSTBUF */
81 memcpy(bind, stage_state->surf_offset,
82 prog_data->binding_table.size_bytes);
83 }
84
85 brw->state.dirty.brw |= brw_new_binding_table;
86
87 if (brw->gen >= 7) {
88 BEGIN_BATCH(2);
89 OUT_BATCH(packet_name << 16 | (2 - 2));
90 OUT_BATCH(stage_state->bind_bo_offset);
91 ADVANCE_BATCH();
92 }
93 }
94
95 /**
96 * State atoms which upload the binding table for a particular shader stage.
97 * @{
98 */
99
100 /** Upload the VS binding table. */
101 static void
102 brw_vs_upload_binding_table(struct brw_context *brw)
103 {
104 brw_upload_binding_table(brw,
105 _3DSTATE_BINDING_TABLE_POINTERS_VS,
106 BRW_NEW_VS_BINDING_TABLE, &brw->vs.base);
107 }
108
109 const struct brw_tracked_state brw_vs_binding_table = {
110 .dirty = {
111 .mesa = 0,
112 .brw = BRW_NEW_BATCH |
113 BRW_NEW_VS_CONSTBUF |
114 BRW_NEW_SURFACES,
115 .cache = BRW_NEW_VS_PROG_DATA
116 },
117 .emit = brw_vs_upload_binding_table,
118 };
119
120
121 /** Upload the PS binding table. */
122 static void
123 brw_upload_wm_binding_table(struct brw_context *brw)
124 {
125 brw_upload_binding_table(brw,
126 _3DSTATE_BINDING_TABLE_POINTERS_PS,
127 BRW_NEW_PS_BINDING_TABLE, &brw->wm.base);
128 }
129
130 const struct brw_tracked_state brw_wm_binding_table = {
131 .dirty = {
132 .mesa = 0,
133 .brw = BRW_NEW_BATCH |
134 BRW_NEW_SURFACES,
135 .cache = BRW_NEW_FS_PROG_DATA
136 },
137 .emit = brw_upload_wm_binding_table,
138 };
139
140 /** Upload the GS binding table (if GS is active). */
141 static void
142 brw_gs_upload_binding_table(struct brw_context *brw)
143 {
144 /* If there's no GS, skip changing anything. */
145 if (brw->geometry_program == NULL)
146 return;
147
148 brw_upload_binding_table(brw,
149 _3DSTATE_BINDING_TABLE_POINTERS_GS,
150 BRW_NEW_GS_BINDING_TABLE, &brw->gs.base);
151 }
152
153 const struct brw_tracked_state brw_gs_binding_table = {
154 .dirty = {
155 .mesa = 0,
156 .brw = BRW_NEW_BATCH |
157 BRW_NEW_GS_CONSTBUF |
158 BRW_NEW_SURFACES,
159 .cache = BRW_NEW_GS_PROG_DATA
160 },
161 .emit = brw_gs_upload_binding_table,
162 };
163
164 /** @} */
165
166 /**
167 * State atoms which emit 3DSTATE packets to update the binding table pointers.
168 * @{
169 */
170
171 /**
172 * (Gen4-5) Upload the binding table pointers for all shader stages.
173 *
174 * The binding table pointers are relative to the surface state base address,
175 * which points at the batchbuffer containing the streamed batch state.
176 */
177 static void
178 gen4_upload_binding_table_pointers(struct brw_context *brw)
179 {
180 BEGIN_BATCH(6);
181 OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS << 16 | (6 - 2));
182 OUT_BATCH(brw->vs.base.bind_bo_offset);
183 OUT_BATCH(0); /* gs */
184 OUT_BATCH(0); /* clip */
185 OUT_BATCH(0); /* sf */
186 OUT_BATCH(brw->wm.base.bind_bo_offset);
187 ADVANCE_BATCH();
188 }
189
190 const struct brw_tracked_state brw_binding_table_pointers = {
191 .dirty = {
192 .mesa = 0,
193 .brw = BRW_NEW_BATCH |
194 BRW_NEW_GS_BINDING_TABLE |
195 BRW_NEW_PS_BINDING_TABLE |
196 BRW_NEW_STATE_BASE_ADDRESS |
197 BRW_NEW_VS_BINDING_TABLE,
198 .cache = 0,
199 },
200 .emit = gen4_upload_binding_table_pointers,
201 };
202
203 /**
204 * (Sandybridge Only) Upload the binding table pointers for all shader stages.
205 *
206 * The binding table pointers are relative to the surface state base address,
207 * which points at the batchbuffer containing the streamed batch state.
208 */
209 static void
210 gen6_upload_binding_table_pointers(struct brw_context *brw)
211 {
212 BEGIN_BATCH(4);
213 OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS << 16 |
214 GEN6_BINDING_TABLE_MODIFY_VS |
215 GEN6_BINDING_TABLE_MODIFY_GS |
216 GEN6_BINDING_TABLE_MODIFY_PS |
217 (4 - 2));
218 OUT_BATCH(brw->vs.base.bind_bo_offset); /* vs */
219 if (brw->ff_gs.prog_active)
220 OUT_BATCH(brw->ff_gs.bind_bo_offset); /* gs */
221 else
222 OUT_BATCH(brw->gs.base.bind_bo_offset); /* gs */
223 OUT_BATCH(brw->wm.base.bind_bo_offset); /* wm/ps */
224 ADVANCE_BATCH();
225 }
226
227 const struct brw_tracked_state gen6_binding_table_pointers = {
228 .dirty = {
229 .mesa = 0,
230 .brw = BRW_NEW_BATCH |
231 BRW_NEW_GS_BINDING_TABLE |
232 BRW_NEW_PS_BINDING_TABLE |
233 BRW_NEW_STATE_BASE_ADDRESS |
234 BRW_NEW_VS_BINDING_TABLE,
235 .cache = 0,
236 },
237 .emit = gen6_upload_binding_table_pointers,
238 };
239
240 /** @} */