1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <[email protected]>
31 * Brian Paul
32 */
33
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "util/ralloc.h"
37 #include "pipe/p_shader_tokens.h"
38 #include "pipe/p_context.h"
39 #include "nir/nir_to_tgsi.h"
40
41 #include "draw_private.h"
42 #include "draw_context.h"
43 #include "draw_vs.h"
44
45 #include "tgsi/tgsi_parse.h"
46 #include "tgsi/tgsi_scan.h"
47 #include "tgsi/tgsi_exec.h"
48
49
50 struct exec_vertex_shader {
51 struct draw_vertex_shader base;
52 struct tgsi_exec_machine *machine;
53 };
54
55
56 static struct exec_vertex_shader *
exec_vertex_shader(struct draw_vertex_shader * vs)57 exec_vertex_shader(struct draw_vertex_shader *vs)
58 {
59 return (struct exec_vertex_shader *)vs;
60 }
61
62
63 /* Not required for run_linear.
64 */
65 static void
vs_exec_prepare(struct draw_vertex_shader * shader,struct draw_context * draw)66 vs_exec_prepare(struct draw_vertex_shader *shader,
67 struct draw_context *draw)
68 {
69 struct exec_vertex_shader *evs = exec_vertex_shader(shader);
70
71 assert(!draw->llvm);
72 /* Specify the vertex program to interpret/execute.
73 * Avoid rebinding when possible.
74 */
75 if (evs->machine->Tokens != shader->state.tokens) {
76 tgsi_exec_machine_bind_shader(evs->machine,
77 shader->state.tokens,
78 draw->vs.tgsi.sampler,
79 draw->vs.tgsi.image,
80 draw->vs.tgsi.buffer);
81 }
82 }
83
84
85
86 /**
87 * Simplified vertex shader interface for the pt paths. Given the
88 * complexity of code-generating all the above operations together,
89 * it's time to try doing all the other stuff separately.
90 */
91 static void
vs_exec_run_linear(struct draw_vertex_shader * shader,const float (* input)[4],float (* output)[4],const struct draw_buffer_info * constants,unsigned count,unsigned input_stride,unsigned output_stride,const unsigned * fetch_elts)92 vs_exec_run_linear(struct draw_vertex_shader *shader,
93 const float (*input)[4],
94 float (*output)[4],
95 const struct draw_buffer_info *constants,
96 unsigned count,
97 unsigned input_stride,
98 unsigned output_stride,
99 const unsigned *fetch_elts)
100 {
101 struct exec_vertex_shader *evs = exec_vertex_shader(shader);
102 struct tgsi_exec_machine *machine = evs->machine;
103 unsigned int i, j;
104 unsigned slot;
105 bool clamp_vertex_color = shader->draw->rasterizer->clamp_vertex_color;
106
107 assert(!shader->draw->llvm);
108 tgsi_exec_set_constant_buffers(machine, PIPE_MAX_CONSTANT_BUFFERS,
109 (const struct tgsi_exec_consts_info *)constants);
110
111 if (shader->info.uses_instanceid) {
112 unsigned i = machine->SysSemanticToIndex[TGSI_SEMANTIC_INSTANCEID];
113 assert(i < ARRAY_SIZE(machine->SystemValue));
114 for (j = 0; j < TGSI_QUAD_SIZE; j++)
115 machine->SystemValue[i].xyzw[0].i[j] = shader->draw->instance_id;
116 }
117
118 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
119 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
120
121 /* Swizzle inputs.
122 */
123 for (j = 0; j < max_vertices; j++) {
124 #if 0
125 debug_printf("%d) Input vert:\n", i + j);
126 for (slot = 0; slot < shader->info.num_inputs; slot++) {
127 debug_printf("\t%d: %f %f %f %f\n", slot,
128 input[slot][0],
129 input[slot][1],
130 input[slot][2],
131 input[slot][3]);
132 }
133 #endif
134 int basevertex = shader->draw->pt.user.eltSize ? shader->draw->pt.user.eltBias : shader->draw->start_index;
135
136 if (shader->info.uses_vertexid) {
137 unsigned vid = machine->SysSemanticToIndex[TGSI_SEMANTIC_VERTEXID];
138 assert(vid < ARRAY_SIZE(machine->SystemValue));
139 machine->SystemValue[vid].xyzw[0].i[j] = fetch_elts ? fetch_elts[i + j] : (i + j + basevertex);
140 }
141 if (shader->info.uses_basevertex) {
142 unsigned vid = machine->SysSemanticToIndex[TGSI_SEMANTIC_BASEVERTEX];
143 assert(vid < ARRAY_SIZE(machine->SystemValue));
144 machine->SystemValue[vid].xyzw[0].i[j] = basevertex;
145 }
146 if (shader->info.uses_vertexid_nobase) {
147 unsigned vid = machine->SysSemanticToIndex[TGSI_SEMANTIC_VERTEXID_NOBASE];
148 assert(vid < ARRAY_SIZE(machine->SystemValue));
149 machine->SystemValue[vid].xyzw[0].i[j] = fetch_elts ? (fetch_elts[i + j] - basevertex) : (i + j);
150 }
151
152 for (slot = 0; slot < shader->info.num_inputs; slot++) {
153 #if 0
154 assert(!util_is_inf_or_nan(input[slot][0]));
155 assert(!util_is_inf_or_nan(input[slot][1]));
156 assert(!util_is_inf_or_nan(input[slot][2]));
157 assert(!util_is_inf_or_nan(input[slot][3]));
158 #endif
159 machine->Inputs[slot].xyzw[0].f[j] = input[slot][0];
160 machine->Inputs[slot].xyzw[1].f[j] = input[slot][1];
161 machine->Inputs[slot].xyzw[2].f[j] = input[slot][2];
162 machine->Inputs[slot].xyzw[3].f[j] = input[slot][3];
163 }
164
165 input = (const float (*)[4])((const char *)input + input_stride);
166 }
167
168 machine->NonHelperMask = (1 << max_vertices) - 1;
169 /* run interpreter */
170 tgsi_exec_machine_run(machine, 0);
171
172 /* Unswizzle all output results.
173 */
174 for (j = 0; j < max_vertices; j++) {
175 for (slot = 0; slot < shader->info.num_outputs; slot++) {
176 enum tgsi_semantic name = shader->info.output_semantic_name[slot];
177 if (clamp_vertex_color &&
178 (name == TGSI_SEMANTIC_COLOR || name == TGSI_SEMANTIC_BCOLOR)) {
179 output[slot][0] = SATURATE(machine->Outputs[slot].xyzw[0].f[j]);
180 output[slot][1] = SATURATE(machine->Outputs[slot].xyzw[1].f[j]);
181 output[slot][2] = SATURATE(machine->Outputs[slot].xyzw[2].f[j]);
182 output[slot][3] = SATURATE(machine->Outputs[slot].xyzw[3].f[j]);
183 } else {
184 output[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
185 output[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
186 output[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
187 output[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
188 }
189 }
190
191 #if 0
192 debug_printf("%d) Post xform vert:\n", i + j);
193 for (slot = 0; slot < shader->info.num_outputs; slot++) {
194 debug_printf("\t%d: %f %f %f %f\n", slot,
195 output[slot][0],
196 output[slot][1],
197 output[slot][2],
198 output[slot][3]);
199 assert(!util_is_inf_or_nan(output[slot][0]));
200 }
201 #endif
202
203 output = (float (*)[4])((char *)output + output_stride);
204 }
205 }
206 }
207
208
209 static void
vs_exec_delete(struct draw_vertex_shader * dvs)210 vs_exec_delete(struct draw_vertex_shader *dvs)
211 {
212 FREE((void*) dvs->state.tokens);
213 FREE(dvs);
214 }
215
216
217 struct draw_vertex_shader *
draw_create_vs_exec(struct draw_context * draw,const struct pipe_shader_state * state)218 draw_create_vs_exec(struct draw_context *draw,
219 const struct pipe_shader_state *state)
220 {
221 struct exec_vertex_shader *vs = CALLOC_STRUCT(exec_vertex_shader);
222
223 if (!vs)
224 return NULL;
225
226 if (state->type == PIPE_SHADER_IR_NIR) {
227 vs->base.state.type = PIPE_SHADER_IR_TGSI;
228 vs->base.state.tokens = nir_to_tgsi(state->ir.nir, draw->pipe->screen);
229 } else {
230 assert(state->type == PIPE_SHADER_IR_TGSI);
231 vs->base.state.type = state->type;
232
233 /* we need to keep a local copy of the tokens */
234 vs->base.state.tokens = tgsi_dup_tokens(state->tokens);
235 if (!vs->base.state.tokens) {
236 FREE(vs);
237 return NULL;
238 }
239 }
240
241 tgsi_scan_shader(vs->base.state.tokens, &vs->base.info);
242
243 vs->base.state.stream_output = state->stream_output;
244 vs->base.draw = draw;
245 vs->base.prepare = vs_exec_prepare;
246 vs->base.run_linear = vs_exec_run_linear;
247 vs->base.delete = vs_exec_delete;
248 vs->base.create_variant = draw_vs_create_variant_generic;
249 vs->machine = draw->vs.tgsi.machine;
250
251 return &vs->base;
252 }
253