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_config.h"
40 #if defined(PIPE_ARCH_X86)
42 #include "pipe/p_shader_tokens.h"
44 #include "draw_private.h"
45 #include "draw_context.h"
47 #include "rtasm/rtasm_cpu.h"
48 #include "rtasm/rtasm_x86sse.h"
49 #include "tgsi/tgsi_sse2.h"
50 #include "tgsi/tgsi_parse.h"
51 #include "tgsi/tgsi_exec.h"
53 #define SSE_MAX_VERTICES 4
56 struct draw_sse_vertex_shader {
57 struct draw_vertex_shader base;
58 struct x86_function sse2_program;
60 tgsi_sse2_vs_func func;
62 struct tgsi_exec_machine *machine;
67 vs_sse_prepare( struct draw_vertex_shader *base,
68 struct draw_context *draw )
70 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
71 struct tgsi_exec_machine *machine = shader->machine;
73 machine->Samplers = draw->vs.samplers;
75 if (base->info.uses_instanceid) {
76 unsigned i = machine->SysSemanticToIndex[TGSI_SEMANTIC_INSTANCEID];
77 assert(i < Elements(machine->SystemValue));
78 machine->SystemValue[i][0] = base->draw->instance_id;
84 /* Simplified vertex shader interface for the pt paths. Given the
85 * complexity of code-generating all the above operations together,
86 * it's time to try doing all the other stuff separately.
89 vs_sse_run_linear( struct draw_vertex_shader *base,
90 const float (*input)[4],
92 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
93 const unsigned const_size[PIPE_MAX_CONSTANT_BUFFERS],
95 unsigned input_stride,
96 unsigned output_stride )
98 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
99 struct tgsi_exec_machine *machine = shader->machine;
102 /* By default, execute all channels. XXX move this inside the loop
103 * below when we support shader conditionals/loops.
105 tgsi_set_exec_mask(machine, 1, 1, 1, 1);
107 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
108 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
110 if (max_vertices < 4) {
111 /* disable the unused execution channels */
112 tgsi_set_exec_mask(machine,
119 /* run compiled shader
121 shader->func(machine,
122 (const float (*)[4])constants[0],
123 shader->base.immediates,
125 base->info.num_inputs,
128 base->info.num_outputs,
131 input = (const float (*)[4])((const char *)input + input_stride * max_vertices);
132 output = (float (*)[4])((char *)output + output_stride * max_vertices);
140 vs_sse_delete( struct draw_vertex_shader *base )
142 struct draw_sse_vertex_shader *shader = (struct draw_sse_vertex_shader *)base;
144 x86_release_func( &shader->sse2_program );
146 align_free( (void *) shader->base.immediates );
148 FREE( (void*) shader->base.state.tokens );
153 struct draw_vertex_shader *
154 draw_create_vs_sse(struct draw_context *draw,
155 const struct pipe_shader_state *templ)
157 struct draw_sse_vertex_shader *vs;
159 if (!rtasm_cpu_has_sse2())
162 vs = CALLOC_STRUCT( draw_sse_vertex_shader );
166 /* we make a private copy of the tokens */
167 vs->base.state.tokens = tgsi_dup_tokens(templ->tokens);
168 if (!vs->base.state.tokens)
171 tgsi_scan_shader(templ->tokens, &vs->base.info);
173 vs->base.draw = draw;
175 vs->base.create_variant = draw_vs_create_variant_aos_sse;
177 vs->base.create_variant = draw_vs_create_variant_generic;
178 vs->base.prepare = vs_sse_prepare;
179 vs->base.run_linear = vs_sse_run_linear;
180 vs->base.delete = vs_sse_delete;
182 vs->base.immediates = align_malloc(TGSI_EXEC_NUM_IMMEDIATES * 4 *
185 vs->machine = draw->vs.machine;
187 x86_init_func( &vs->sse2_program );
189 if (!tgsi_emit_sse2( (struct tgsi_token *) vs->base.state.tokens,
191 (float (*)[4])vs->base.immediates,
195 vs->func = (tgsi_sse2_vs_func) x86_get_func( &vs->sse2_program );
204 debug_warning("tgsi_emit_sse2() failed, falling back to interpreter\n");
206 x86_release_func( &vs->sse2_program );
216 struct draw_vertex_shader *
217 draw_create_vs_sse( struct draw_context *draw,
218 const struct pipe_shader_state *templ )