i965: Convert brw->*_program into a brw->programs[i] array.
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_cs_state.c
1 /*
2 * Copyright © 2015 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "util/ralloc.h"
25 #include "brw_context.h"
26 #include "brw_defines.h"
27 #include "brw_cs.h"
28 #include "brw_wm.h"
29 #include "intel_mipmap_tree.h"
30 #include "intel_batchbuffer.h"
31 #include "brw_state.h"
32 #include "program/prog_statevars.h"
33 #include "compiler/glsl/ir_uniform.h"
34 #include "main/shaderapi.h"
35
36 /**
37 * Creates a region containing the push constants for the CS on gen7+.
38 *
39 * Push constants are constant values (such as GLSL uniforms) that are
40 * pre-loaded into a shader stage's register space at thread spawn time.
41 *
42 * For other stages, see brw_curbe.c:brw_upload_constant_buffer for the
43 * equivalent gen4/5 code and gen6_vs_state.c:gen6_upload_push_constants for
44 * gen6+.
45 */
46 static void
47 brw_upload_cs_push_constants(struct brw_context *brw,
48 const struct gl_program *prog,
49 const struct brw_cs_prog_data *cs_prog_data,
50 struct brw_stage_state *stage_state)
51 {
52 struct gl_context *ctx = &brw->ctx;
53 const struct brw_stage_prog_data *prog_data =
54 (struct brw_stage_prog_data*) cs_prog_data;
55
56 /* Updates the ParamaterValues[i] pointers for all parameters of the
57 * basic type of PROGRAM_STATE_VAR.
58 */
59 /* XXX: Should this happen somewhere before to get our state flag set? */
60 _mesa_load_state_parameters(ctx, prog->Parameters);
61
62 if (cs_prog_data->push.total.size == 0) {
63 stage_state->push_const_size = 0;
64 return;
65 }
66
67
68 gl_constant_value *param = (gl_constant_value*)
69 brw_state_batch(brw, ALIGN(cs_prog_data->push.total.size, 64),
70 64, &stage_state->push_const_offset);
71 assert(param);
72
73 STATIC_ASSERT(sizeof(gl_constant_value) == sizeof(float));
74
75 if (cs_prog_data->push.cross_thread.size > 0) {
76 gl_constant_value *param_copy = param;
77 assert(cs_prog_data->thread_local_id_index < 0 ||
78 cs_prog_data->thread_local_id_index >=
79 cs_prog_data->push.cross_thread.dwords);
80 for (unsigned i = 0;
81 i < cs_prog_data->push.cross_thread.dwords;
82 i++) {
83 param_copy[i] = *prog_data->param[i];
84 }
85 }
86
87 gl_constant_value thread_id;
88 if (cs_prog_data->push.per_thread.size > 0) {
89 for (unsigned t = 0; t < cs_prog_data->threads; t++) {
90 unsigned dst =
91 8 * (cs_prog_data->push.per_thread.regs * t +
92 cs_prog_data->push.cross_thread.regs);
93 unsigned src = cs_prog_data->push.cross_thread.dwords;
94 for ( ; src < prog_data->nr_params; src++, dst++) {
95 if (src != cs_prog_data->thread_local_id_index)
96 param[dst] = *prog_data->param[src];
97 else {
98 thread_id.u = t * cs_prog_data->simd_size;
99 param[dst] = thread_id;
100 }
101 }
102 }
103 }
104
105 stage_state->push_const_size =
106 cs_prog_data->push.cross_thread.regs +
107 cs_prog_data->push.per_thread.regs;
108 }
109
110
111 static void
112 gen7_upload_cs_push_constants(struct brw_context *brw)
113 {
114 struct brw_stage_state *stage_state = &brw->cs.base;
115
116 /* BRW_NEW_COMPUTE_PROGRAM */
117 const struct brw_program *cp =
118 (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
119
120 if (cp) {
121 /* BRW_NEW_CS_PROG_DATA */
122 struct brw_cs_prog_data *cs_prog_data =
123 brw_cs_prog_data(brw->cs.base.prog_data);
124
125 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_COMPUTE);
126 brw_upload_cs_push_constants(brw, &cp->program, cs_prog_data,
127 stage_state);
128 }
129 }
130
131 const struct brw_tracked_state gen7_cs_push_constants = {
132 .dirty = {
133 .mesa = _NEW_PROGRAM_CONSTANTS,
134 .brw = BRW_NEW_BATCH |
135 BRW_NEW_BLORP |
136 BRW_NEW_COMPUTE_PROGRAM |
137 BRW_NEW_CS_PROG_DATA,
138 },
139 .emit = gen7_upload_cs_push_constants,
140 };
141
142 /**
143 * Creates a new CS constant buffer reflecting the current CS program's
144 * constants, if needed by the CS program.
145 */
146 static void
147 brw_upload_cs_pull_constants(struct brw_context *brw)
148 {
149 struct brw_stage_state *stage_state = &brw->cs.base;
150
151 /* BRW_NEW_COMPUTE_PROGRAM */
152 struct brw_program *cp =
153 (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
154
155 /* BRW_NEW_CS_PROG_DATA */
156 const struct brw_stage_prog_data *prog_data = brw->cs.base.prog_data;
157
158 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_COMPUTE);
159 /* _NEW_PROGRAM_CONSTANTS */
160 brw_upload_pull_constants(brw, BRW_NEW_SURFACES, &cp->program,
161 stage_state, prog_data);
162 }
163
164 const struct brw_tracked_state brw_cs_pull_constants = {
165 .dirty = {
166 .mesa = _NEW_PROGRAM_CONSTANTS,
167 .brw = BRW_NEW_BATCH |
168 BRW_NEW_BLORP |
169 BRW_NEW_COMPUTE_PROGRAM |
170 BRW_NEW_CS_PROG_DATA,
171 },
172 .emit = brw_upload_cs_pull_constants,
173 };