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