i965: Port push constant code to genxml.
[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 "program/prog_parameter.h"
29
30 /**
31 * Creates a streamed BO containing the push constants for the VS or GS on
32 * gen6+.
33 *
34 * Push constants are constant values (such as GLSL uniforms) that are
35 * pre-loaded into a shader stage's register space at thread spawn time.
36 *
37 * Not all GLSL uniforms will be uploaded as push constants: The hardware has
38 * a limitation of 32 or 64 EU registers (256 or 512 floats) per stage to be
39 * uploaded as push constants, while GL 4.4 requires at least 1024 components
40 * to be usable for the VS. Plus, currently we always use pull constants
41 * instead of push constants when doing variable-index array access.
42 *
43 * See brw_curbe.c for the equivalent gen4/5 code.
44 */
45 void
46 gen6_upload_push_constants(struct brw_context *brw,
47 const struct gl_program *prog,
48 const struct brw_stage_prog_data *prog_data,
49 struct brw_stage_state *stage_state)
50 {
51 struct gl_context *ctx = &brw->ctx;
52
53 if (prog_data->nr_params == 0) {
54 stage_state->push_const_size = 0;
55 } else {
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 if (prog)
61 _mesa_load_state_parameters(ctx, prog->Parameters);
62
63 gl_constant_value *param;
64 int i;
65
66 param = brw_state_batch(brw,
67 prog_data->nr_params * sizeof(gl_constant_value),
68 32, &stage_state->push_const_offset);
69
70 STATIC_ASSERT(sizeof(gl_constant_value) == sizeof(float));
71
72 /* _NEW_PROGRAM_CONSTANTS
73 *
74 * Also _NEW_TRANSFORM -- we may reference clip planes other than as a
75 * side effect of dereferencing uniforms, so _NEW_PROGRAM_CONSTANTS
76 * wouldn't be set for them.
77 */
78 for (i = 0; i < prog_data->nr_params; i++) {
79 param[i] = *prog_data->param[i];
80 }
81
82 if (0) {
83 fprintf(stderr, "%s constants:\n",
84 _mesa_shader_stage_to_string(stage_state->stage));
85 for (i = 0; i < prog_data->nr_params; i++) {
86 if ((i & 7) == 0)
87 fprintf(stderr, "g%d: ",
88 prog_data->dispatch_grf_start_reg + i / 8);
89 fprintf(stderr, "%8f ", param[i].f);
90 if ((i & 7) == 7)
91 fprintf(stderr, "\n");
92 }
93 if ((i & 7) != 0)
94 fprintf(stderr, "\n");
95 fprintf(stderr, "\n");
96 }
97
98 stage_state->push_const_size = ALIGN(prog_data->nr_params, 8) / 8;
99 /* We can only push 32 registers of constants at a time. */
100
101 /* From the SNB PRM (vol2, part 1, section 3.2.1.4: 3DSTATE_CONSTANT_VS:
102 *
103 * "The sum of all four read length fields (each incremented to
104 * represent the actual read length) must be less than or equal to
105 * 32"
106 *
107 * From the IVB PRM (vol2, part 1, section 3.2.1.3: 3DSTATE_CONSTANT_VS:
108 *
109 * "The sum of all four read length fields must be less than or
110 * equal to the size of 64"
111 *
112 * The other shader stages all match the VS's limits.
113 */
114 assert(stage_state->push_const_size <= 32);
115 }
116 }