Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / softpipe / sp_fs_sse.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
25 *
26 **************************************************************************/
27
28
29 #include "sp_context.h"
30 #include "sp_state.h"
31 #include "sp_fs.h"
32 #include "sp_headers.h"
33
34
35 #include "pipe/p_state.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_memory.h"
38 #include "pipe/p_inlines.h"
39 #include "tgsi/tgsi_exec.h"
40 #include "tgsi/tgsi_sse2.h"
41
42
43 #if defined(PIPE_ARCH_X86) && defined(PIPE_ARCH_SSE)
44
45 #include "rtasm/rtasm_x86sse.h"
46
47 /* Surely this should be defined somewhere in a tgsi header:
48 */
49 typedef void (PIPE_CDECL *codegen_function)(
50 const struct tgsi_exec_vector *input,
51 struct tgsi_exec_vector *output,
52 const float (*constant)[4],
53 struct tgsi_exec_vector *temporary,
54 const struct tgsi_interp_coef *coef,
55 float (*immediates)[4]
56 //, const struct tgsi_exec_vector *quadPos
57 );
58
59
60 struct sp_sse_fragment_shader {
61 struct sp_fragment_shader base;
62 struct x86_function sse2_program;
63 codegen_function func;
64 float immediates[TGSI_EXEC_NUM_IMMEDIATES][4];
65 };
66
67
68
69 static void
70 fs_sse_prepare( const struct sp_fragment_shader *base,
71 struct tgsi_exec_machine *machine,
72 struct tgsi_sampler *samplers )
73 {
74 }
75
76
77 /* TODO: codegenerate the whole run function, skip this wrapper.
78 * TODO: break dependency on tgsi_exec_machine struct
79 * TODO: push Position calculation into the generated shader
80 * TODO: process >1 quad at a time
81 */
82 static unsigned
83 fs_sse_run( const struct sp_fragment_shader *base,
84 struct tgsi_exec_machine *machine,
85 struct quad_header *quad )
86 {
87 struct sp_sse_fragment_shader *shader = (struct sp_sse_fragment_shader *) base;
88
89 /* Compute X, Y, Z, W vals for this quad -- place in temp[0] for now */
90 sp_setup_pos_vector(quad->posCoef,
91 (float)quad->input.x0, (float)quad->input.y0,
92 machine->Temps);
93
94 /* init kill mask */
95 tgsi_set_kill_mask(machine, 0x0);
96 tgsi_set_exec_mask(machine, 1, 1, 1, 1);
97
98 shader->func( machine->Inputs,
99 machine->Outputs,
100 machine->Consts,
101 machine->Temps,
102 machine->InterpCoefs,
103 shader->immediates
104 // , &machine->QuadPos
105 );
106
107 return ~(machine->Temps[TGSI_EXEC_TEMP_KILMASK_I].xyzw[TGSI_EXEC_TEMP_KILMASK_C].u[0]);
108 }
109
110
111 static void
112 fs_sse_delete( struct sp_fragment_shader *base )
113 {
114 struct sp_sse_fragment_shader *shader = (struct sp_sse_fragment_shader *) base;
115
116 x86_release_func( &shader->sse2_program );
117 FREE(shader);
118 }
119
120
121 struct sp_fragment_shader *
122 softpipe_create_fs_sse(struct softpipe_context *softpipe,
123 const struct pipe_shader_state *templ)
124 {
125 struct sp_sse_fragment_shader *shader;
126
127 if (!softpipe->use_sse)
128 return NULL;
129
130 shader = CALLOC_STRUCT(sp_sse_fragment_shader);
131 if (!shader)
132 return NULL;
133
134 x86_init_func( &shader->sse2_program );
135
136 if (!tgsi_emit_sse2( templ->tokens, &shader->sse2_program,
137 shader->immediates, FALSE )) {
138 FREE(shader);
139 return NULL;
140 }
141
142 shader->func = (codegen_function) x86_get_func( &shader->sse2_program );
143 if (!shader->func) {
144 x86_release_func( &shader->sse2_program );
145 FREE(shader);
146 return NULL;
147 }
148
149 shader->base.shader = *templ;
150 shader->base.prepare = fs_sse_prepare;
151 shader->base.run = fs_sse_run;
152 shader->base.delete = fs_sse_delete;
153
154 return &shader->base;
155 }
156
157
158 #else
159
160 /* Maybe put this varient in the header file.
161 */
162 struct sp_fragment_shader *
163 softpipe_create_fs_sse(struct softpipe_context *softpipe,
164 const struct pipe_shader_state *templ)
165 {
166 return NULL;
167 }
168
169 #endif