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