1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "pipe/p_shader_tokens.h"
38 #include "draw_private.h"
39 #include "draw_context.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_scan.h"
44 #include "tgsi/tgsi_exec.h"
47 struct exec_vertex_shader
{
48 struct draw_vertex_shader base
;
49 struct tgsi_exec_machine
*machine
;
52 static struct exec_vertex_shader
*exec_vertex_shader( struct draw_vertex_shader
*vs
)
54 return (struct exec_vertex_shader
*)vs
;
58 /* Not required for run_linear.
61 vs_exec_prepare( struct draw_vertex_shader
*shader
,
62 struct draw_context
*draw
)
64 struct exec_vertex_shader
*evs
= exec_vertex_shader(shader
);
66 /* Specify the vertex program to interpret/execute.
67 * Avoid rebinding when possible.
69 if (evs
->machine
->Tokens
!= shader
->state
.tokens
) {
70 tgsi_exec_machine_bind_shader(evs
->machine
,
72 draw
->vs
.tgsi
.sampler
);
79 /* Simplified vertex shader interface for the pt paths. Given the
80 * complexity of code-generating all the above operations together,
81 * it's time to try doing all the other stuff separately.
84 vs_exec_run_linear( struct draw_vertex_shader
*shader
,
85 const float (*input
)[4],
87 const void *constants
[PIPE_MAX_CONSTANT_BUFFERS
],
88 const unsigned const_size
[PIPE_MAX_CONSTANT_BUFFERS
],
90 unsigned input_stride
,
91 unsigned output_stride
)
93 struct exec_vertex_shader
*evs
= exec_vertex_shader(shader
);
94 struct tgsi_exec_machine
*machine
= evs
->machine
;
97 boolean clamp_vertex_color
= shader
->draw
->rasterizer
->clamp_vertex_color
;
99 tgsi_exec_set_constant_buffers(machine
, PIPE_MAX_CONSTANT_BUFFERS
,
100 constants
, const_size
);
102 if (shader
->info
.uses_instanceid
) {
103 unsigned i
= machine
->SysSemanticToIndex
[TGSI_SEMANTIC_INSTANCEID
];
104 assert(i
< Elements(machine
->SystemValue
));
105 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
106 machine
->SystemValue
[i
].i
[j
] = shader
->draw
->instance_id
;
109 for (i
= 0; i
< count
; i
+= MAX_TGSI_VERTICES
) {
110 unsigned int max_vertices
= MIN2(MAX_TGSI_VERTICES
, count
- i
);
114 for (j
= 0; j
< max_vertices
; j
++) {
116 debug_printf("%d) Input vert:\n", i
+ j
);
117 for (slot
= 0; slot
< shader
->info
.num_inputs
; slot
++) {
118 debug_printf("\t%d: %f %f %f %f\n", slot
,
126 if (shader
->info
.uses_vertexid
) {
127 unsigned vid
= machine
->SysSemanticToIndex
[TGSI_SEMANTIC_VERTEXID
];
128 assert(vid
< Elements(machine
->SystemValue
));
129 machine
->SystemValue
[vid
].i
[j
] = i
+ j
;
132 for (slot
= 0; slot
< shader
->info
.num_inputs
; slot
++) {
134 assert(!util_is_inf_or_nan(input
[slot
][0]));
135 assert(!util_is_inf_or_nan(input
[slot
][1]));
136 assert(!util_is_inf_or_nan(input
[slot
][2]));
137 assert(!util_is_inf_or_nan(input
[slot
][3]));
139 machine
->Inputs
[slot
].xyzw
[0].f
[j
] = input
[slot
][0];
140 machine
->Inputs
[slot
].xyzw
[1].f
[j
] = input
[slot
][1];
141 machine
->Inputs
[slot
].xyzw
[2].f
[j
] = input
[slot
][2];
142 machine
->Inputs
[slot
].xyzw
[3].f
[j
] = input
[slot
][3];
145 input
= (const float (*)[4])((const char *)input
+ input_stride
);
148 tgsi_set_exec_mask(machine
,
154 /* run interpreter */
155 tgsi_exec_machine_run( machine
);
157 /* Unswizzle all output results.
159 for (j
= 0; j
< max_vertices
; j
++) {
160 for (slot
= 0; slot
< shader
->info
.num_outputs
; slot
++) {
161 unsigned name
= shader
->info
.output_semantic_name
[slot
];
162 if(clamp_vertex_color
&&
163 (name
== TGSI_SEMANTIC_COLOR
|| name
== TGSI_SEMANTIC_BCOLOR
))
165 output
[slot
][0] = CLAMP(machine
->Outputs
[slot
].xyzw
[0].f
[j
], 0.0f
, 1.0f
);
166 output
[slot
][1] = CLAMP(machine
->Outputs
[slot
].xyzw
[1].f
[j
], 0.0f
, 1.0f
);
167 output
[slot
][2] = CLAMP(machine
->Outputs
[slot
].xyzw
[2].f
[j
], 0.0f
, 1.0f
);
168 output
[slot
][3] = CLAMP(machine
->Outputs
[slot
].xyzw
[3].f
[j
], 0.0f
, 1.0f
);
170 else if (name
== TGSI_SEMANTIC_FOG
) {
171 output
[slot
][0] = machine
->Outputs
[slot
].xyzw
[0].f
[j
];
177 output
[slot
][0] = machine
->Outputs
[slot
].xyzw
[0].f
[j
];
178 output
[slot
][1] = machine
->Outputs
[slot
].xyzw
[1].f
[j
];
179 output
[slot
][2] = machine
->Outputs
[slot
].xyzw
[2].f
[j
];
180 output
[slot
][3] = machine
->Outputs
[slot
].xyzw
[3].f
[j
];
185 debug_printf("%d) Post xform vert:\n", i
+ j
);
186 for (slot
= 0; slot
< shader
->info
.num_outputs
; slot
++) {
187 debug_printf("\t%d: %f %f %f %f\n", slot
,
192 assert(!util_is_inf_or_nan(output
[slot
][0]));
196 output
= (float (*)[4])((char *)output
+ output_stride
);
206 vs_exec_delete( struct draw_vertex_shader
*dvs
)
208 FREE((void*) dvs
->state
.tokens
);
213 struct draw_vertex_shader
*
214 draw_create_vs_exec(struct draw_context
*draw
,
215 const struct pipe_shader_state
*state
)
217 struct exec_vertex_shader
*vs
= CALLOC_STRUCT( exec_vertex_shader
);
222 /* we make a private copy of the tokens */
223 vs
->base
.state
.tokens
= tgsi_dup_tokens(state
->tokens
);
224 if (!vs
->base
.state
.tokens
) {
229 tgsi_scan_shader(state
->tokens
, &vs
->base
.info
);
231 vs
->base
.state
.stream_output
= state
->stream_output
;
232 vs
->base
.draw
= draw
;
233 vs
->base
.prepare
= vs_exec_prepare
;
234 vs
->base
.run_linear
= vs_exec_run_linear
;
235 vs
->base
.delete = vs_exec_delete
;
236 vs
->base
.create_variant
= draw_vs_create_variant_generic
;
237 vs
->machine
= draw
->vs
.tgsi
.machine
;