i965: Move brw_upload_pull_constants to gen6_constant_state.c
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_constant_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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_context.h"
25 #include "brw_state.h"
26 #include "brw_defines.h"
27 #include "intel_batchbuffer.h"
28 #include "intel_buffer_objects.h"
29 #include "program/prog_parameter.h"
30
31 /**
32 * Creates a streamed BO containing the push constants for the VS or GS on
33 * gen6+.
34 *
35 * Push constants are constant values (such as GLSL uniforms) that are
36 * pre-loaded into a shader stage's register space at thread spawn time.
37 *
38 * Not all GLSL uniforms will be uploaded as push constants: The hardware has
39 * a limitation of 32 or 64 EU registers (256 or 512 floats) per stage to be
40 * uploaded as push constants, while GL 4.4 requires at least 1024 components
41 * to be usable for the VS. Plus, currently we always use pull constants
42 * instead of push constants when doing variable-index array access.
43 *
44 * See brw_curbe.c for the equivalent gen4/5 code.
45 */
46 void
47 gen6_upload_push_constants(struct brw_context *brw,
48 const struct gl_program *prog,
49 const struct brw_stage_prog_data *prog_data,
50 struct brw_stage_state *stage_state)
51 {
52 const struct gen_device_info *devinfo = &brw->screen->devinfo;
53 struct gl_context *ctx = &brw->ctx;
54
55 if (prog_data->nr_params == 0) {
56 stage_state->push_const_size = 0;
57 } else {
58 /* Updates the ParamaterValues[i] pointers for all parameters of the
59 * basic type of PROGRAM_STATE_VAR.
60 */
61 /* XXX: Should this happen somewhere before to get our state flag set? */
62 if (prog)
63 _mesa_load_state_parameters(ctx, prog->Parameters);
64
65 int i;
66 const int size = prog_data->nr_params * sizeof(gl_constant_value);
67 gl_constant_value *param;
68 if (devinfo->gen >= 8 || devinfo->is_haswell) {
69 param = intel_upload_space(brw, size, 32,
70 &stage_state->push_const_bo,
71 &stage_state->push_const_offset);
72 } else {
73 param = brw_state_batch(brw, size, 32,
74 &stage_state->push_const_offset);
75 }
76
77 STATIC_ASSERT(sizeof(gl_constant_value) == sizeof(float));
78
79 /* _NEW_PROGRAM_CONSTANTS
80 *
81 * Also _NEW_TRANSFORM -- we may reference clip planes other than as a
82 * side effect of dereferencing uniforms, so _NEW_PROGRAM_CONSTANTS
83 * wouldn't be set for them.
84 */
85 for (i = 0; i < prog_data->nr_params; i++) {
86 param[i] = *prog_data->param[i];
87 }
88
89 if (0) {
90 fprintf(stderr, "%s constants:\n",
91 _mesa_shader_stage_to_string(stage_state->stage));
92 for (i = 0; i < prog_data->nr_params; i++) {
93 if ((i & 7) == 0)
94 fprintf(stderr, "g%d: ",
95 prog_data->dispatch_grf_start_reg + i / 8);
96 fprintf(stderr, "%8f ", param[i].f);
97 if ((i & 7) == 7)
98 fprintf(stderr, "\n");
99 }
100 if ((i & 7) != 0)
101 fprintf(stderr, "\n");
102 fprintf(stderr, "\n");
103 }
104
105 stage_state->push_const_size = ALIGN(prog_data->nr_params, 8) / 8;
106 /* We can only push 32 registers of constants at a time. */
107
108 /* From the SNB PRM (vol2, part 1, section 3.2.1.4: 3DSTATE_CONSTANT_VS:
109 *
110 * "The sum of all four read length fields (each incremented to
111 * represent the actual read length) must be less than or equal to
112 * 32"
113 *
114 * From the IVB PRM (vol2, part 1, section 3.2.1.3: 3DSTATE_CONSTANT_VS:
115 *
116 * "The sum of all four read length fields must be less than or
117 * equal to the size of 64"
118 *
119 * The other shader stages all match the VS's limits.
120 */
121 assert(stage_state->push_const_size <= 32);
122 }
123
124 stage_state->push_constants_dirty = true;
125 }
126
127
128 /**
129 * Creates a temporary BO containing the pull constant data for the shader
130 * stage, and the SURFACE_STATE struct that points at it.
131 *
132 * Pull constants are GLSL uniforms (and other constant data) beyond what we
133 * could fit as push constants, or that have variable-index array access
134 * (which is easiest to support using pull constants, and avoids filling
135 * register space with mostly-unused data).
136 *
137 * Compare this path to brw_curbe.c for gen4/5 push constants, and
138 * gen6_vs_state.c for gen6+ push constants.
139 */
140 void
141 brw_upload_pull_constants(struct brw_context *brw,
142 GLbitfield64 brw_new_constbuf,
143 const struct gl_program *prog,
144 struct brw_stage_state *stage_state,
145 const struct brw_stage_prog_data *prog_data)
146 {
147 unsigned i;
148 uint32_t surf_index = prog_data->binding_table.pull_constants_start;
149
150 if (!prog_data->nr_pull_params) {
151 if (stage_state->surf_offset[surf_index]) {
152 stage_state->surf_offset[surf_index] = 0;
153 brw->ctx.NewDriverState |= brw_new_constbuf;
154 }
155 return;
156 }
157
158 /* Updates the ParamaterValues[i] pointers for all parameters of the
159 * basic type of PROGRAM_STATE_VAR.
160 */
161 _mesa_load_state_parameters(&brw->ctx, prog->Parameters);
162
163 /* BRW_NEW_*_PROG_DATA | _NEW_PROGRAM_CONSTANTS */
164 uint32_t size = prog_data->nr_pull_params * 4;
165 struct brw_bo *const_bo = NULL;
166 uint32_t const_offset;
167 gl_constant_value *constants = intel_upload_space(brw, size, 64,
168 &const_bo, &const_offset);
169
170 STATIC_ASSERT(sizeof(gl_constant_value) == sizeof(float));
171
172 for (i = 0; i < prog_data->nr_pull_params; i++) {
173 constants[i] = *prog_data->pull_param[i];
174 }
175
176 if (0) {
177 for (i = 0; i < ALIGN(prog_data->nr_pull_params, 4) / 4; i++) {
178 const gl_constant_value *row = &constants[i * 4];
179 fprintf(stderr, "const surface %3d: %4.3f %4.3f %4.3f %4.3f\n",
180 i, row[0].f, row[1].f, row[2].f, row[3].f);
181 }
182 }
183
184 brw_create_constant_surface(brw, const_bo, const_offset, size,
185 &stage_state->surf_offset[surf_index]);
186 brw_bo_unreference(const_bo);
187
188 brw->ctx.NewDriverState |= brw_new_constbuf;
189 }