i965/vec4: Simplify opt_reduce_swizzle() using the swizzle utils.
[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.emit_buffer_surface_state(
72 brw, &stage_state->surf_offset[
73 prog_data->binding_table.shader_time_start],
74 brw->shader_time.bo, 0, BRW_SURFACEFORMAT_RAW,
75 brw->shader_time.bo->size, 1, true);
76 }
77
78 uint32_t *bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
79 prog_data->binding_table.size_bytes, 32,
80 &stage_state->bind_bo_offset);
81
82 /* BRW_NEW_SURFACES and BRW_NEW_*_CONSTBUF */
83 memcpy(bind, stage_state->surf_offset,
84 prog_data->binding_table.size_bytes);
85 }
86
87 brw->state.dirty.brw |= brw_new_binding_table;
88
89 if (brw->gen >= 7) {
90 BEGIN_BATCH(2);
91 OUT_BATCH(packet_name << 16 | (2 - 2));
92 OUT_BATCH(stage_state->bind_bo_offset);
93 ADVANCE_BATCH();
94 }
95 }
96
97 /**
98 * State atoms which upload the binding table for a particular shader stage.
99 * @{
100 */
101
102 /** Upload the VS binding table. */
103 static void
104 brw_vs_upload_binding_table(struct brw_context *brw)
105 {
106 brw_upload_binding_table(brw,
107 _3DSTATE_BINDING_TABLE_POINTERS_VS,
108 BRW_NEW_VS_BINDING_TABLE, &brw->vs.base);
109 }
110
111 const struct brw_tracked_state brw_vs_binding_table = {
112 .dirty = {
113 .mesa = 0,
114 .brw = BRW_NEW_BATCH |
115 BRW_NEW_VS_CONSTBUF |
116 BRW_NEW_VS_PROG_DATA |
117 BRW_NEW_SURFACES,
118 },
119 .emit = brw_vs_upload_binding_table,
120 };
121
122
123 /** Upload the PS binding table. */
124 static void
125 brw_upload_wm_binding_table(struct brw_context *brw)
126 {
127 brw_upload_binding_table(brw,
128 _3DSTATE_BINDING_TABLE_POINTERS_PS,
129 BRW_NEW_PS_BINDING_TABLE, &brw->wm.base);
130 }
131
132 const struct brw_tracked_state brw_wm_binding_table = {
133 .dirty = {
134 .mesa = 0,
135 .brw = BRW_NEW_BATCH |
136 BRW_NEW_FS_PROG_DATA |
137 BRW_NEW_SURFACES,
138 },
139 .emit = brw_upload_wm_binding_table,
140 };
141
142 /** Upload the GS binding table (if GS is active). */
143 static void
144 brw_gs_upload_binding_table(struct brw_context *brw)
145 {
146 /* If there's no GS, skip changing anything. */
147 if (brw->geometry_program == NULL)
148 return;
149
150 brw_upload_binding_table(brw,
151 _3DSTATE_BINDING_TABLE_POINTERS_GS,
152 BRW_NEW_GS_BINDING_TABLE, &brw->gs.base);
153 }
154
155 const struct brw_tracked_state brw_gs_binding_table = {
156 .dirty = {
157 .mesa = 0,
158 .brw = BRW_NEW_BATCH |
159 BRW_NEW_GS_CONSTBUF |
160 BRW_NEW_GS_PROG_DATA |
161 BRW_NEW_SURFACES,
162 },
163 .emit = brw_gs_upload_binding_table,
164 };
165
166 /** @} */
167
168 /**
169 * State atoms which emit 3DSTATE packets to update the binding table pointers.
170 * @{
171 */
172
173 /**
174 * (Gen4-5) Upload the binding table pointers for all shader stages.
175 *
176 * The binding table pointers are relative to the surface state base address,
177 * which points at the batchbuffer containing the streamed batch state.
178 */
179 static void
180 gen4_upload_binding_table_pointers(struct brw_context *brw)
181 {
182 BEGIN_BATCH(6);
183 OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS << 16 | (6 - 2));
184 OUT_BATCH(brw->vs.base.bind_bo_offset);
185 OUT_BATCH(0); /* gs */
186 OUT_BATCH(0); /* clip */
187 OUT_BATCH(0); /* sf */
188 OUT_BATCH(brw->wm.base.bind_bo_offset);
189 ADVANCE_BATCH();
190 }
191
192 const struct brw_tracked_state brw_binding_table_pointers = {
193 .dirty = {
194 .mesa = 0,
195 .brw = BRW_NEW_BATCH |
196 BRW_NEW_GS_BINDING_TABLE |
197 BRW_NEW_PS_BINDING_TABLE |
198 BRW_NEW_STATE_BASE_ADDRESS |
199 BRW_NEW_VS_BINDING_TABLE,
200 },
201 .emit = gen4_upload_binding_table_pointers,
202 };
203
204 /**
205 * (Sandybridge Only) Upload the binding table pointers for all shader stages.
206 *
207 * The binding table pointers are relative to the surface state base address,
208 * which points at the batchbuffer containing the streamed batch state.
209 */
210 static void
211 gen6_upload_binding_table_pointers(struct brw_context *brw)
212 {
213 BEGIN_BATCH(4);
214 OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS << 16 |
215 GEN6_BINDING_TABLE_MODIFY_VS |
216 GEN6_BINDING_TABLE_MODIFY_GS |
217 GEN6_BINDING_TABLE_MODIFY_PS |
218 (4 - 2));
219 OUT_BATCH(brw->vs.base.bind_bo_offset); /* vs */
220 if (brw->ff_gs.prog_active)
221 OUT_BATCH(brw->ff_gs.bind_bo_offset); /* gs */
222 else
223 OUT_BATCH(brw->gs.base.bind_bo_offset); /* gs */
224 OUT_BATCH(brw->wm.base.bind_bo_offset); /* wm/ps */
225 ADVANCE_BATCH();
226 }
227
228 const struct brw_tracked_state gen6_binding_table_pointers = {
229 .dirty = {
230 .mesa = 0,
231 .brw = BRW_NEW_BATCH |
232 BRW_NEW_GS_BINDING_TABLE |
233 BRW_NEW_PS_BINDING_TABLE |
234 BRW_NEW_STATE_BASE_ADDRESS |
235 BRW_NEW_VS_BINDING_TABLE,
236 },
237 .emit = gen6_upload_binding_table_pointers,
238 };
239
240 /** @} */