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 **************************************************************************/
29 * Execute fragment shader using runtime SSE code generation.
32 #include "sp_context.h"
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"
44 #if defined(PIPE_ARCH_X86)
46 #include "rtasm/rtasm_x86sse.h"
51 * Subclass of sp_fragment_shader
53 struct sp_sse_fragment_shader
55 struct sp_fragment_shader base
;
56 struct x86_function sse2_program
;
57 tgsi_sse2_fs_function func
;
58 float immediates
[TGSI_EXEC_NUM_IMMEDIATES
][4];
63 static INLINE
struct sp_sse_fragment_shader
*
64 sp_sse_fragment_shader(const struct sp_fragment_shader
*base
)
66 return (struct sp_sse_fragment_shader
*) base
;
71 fs_sse_prepare( const struct sp_fragment_shader
*base
,
72 struct tgsi_exec_machine
*machine
,
73 struct tgsi_sampler
**samplers
)
75 machine
->Samplers
= samplers
;
79 /* TODO: codegenerate the whole run function, skip this wrapper.
80 * TODO: break dependency on tgsi_exec_machine struct
81 * TODO: push Position calculation into the generated shader
82 * TODO: process >1 quad at a time
85 fs_sse_run( const struct sp_fragment_shader
*base
,
86 struct tgsi_exec_machine
*machine
,
87 struct quad_header
*quad
)
89 struct sp_sse_fragment_shader
*shader
= sp_sse_fragment_shader(base
);
91 /* Compute X, Y, Z, W vals for this quad -- place in temp[0] for now */
92 sp_setup_pos_vector(quad
->posCoef
,
93 (float)quad
->input
.x0
, (float)quad
->input
.y0
,
97 tgsi_set_kill_mask(machine
, 0x0);
98 tgsi_set_exec_mask(machine
, 1, 1, 1, 1);
100 shader
->func( machine
,
102 (const float (*)[4])shader
->immediates
,
104 // , &machine->QuadPos
107 quad
->inout
.mask
&= ~(machine
->Temps
[TGSI_EXEC_TEMP_KILMASK_I
].xyzw
[TGSI_EXEC_TEMP_KILMASK_C
].u
[0]);
108 if (quad
->inout
.mask
== 0)
114 const ubyte
*sem_name
= shader
->base
.info
.output_semantic_name
;
115 const ubyte
*sem_index
= shader
->base
.info
.output_semantic_index
;
116 const uint n
= shader
->base
.info
.num_outputs
;
118 for (i
= 0; i
< n
; i
++) {
119 switch (sem_name
[i
]) {
120 case TGSI_SEMANTIC_COLOR
:
122 uint cbuf
= sem_index
[i
];
123 memcpy(quad
->output
.color
[cbuf
],
124 &machine
->Outputs
[i
].xyzw
[0].f
[0],
125 sizeof(quad
->output
.color
[0]) );
128 case TGSI_SEMANTIC_POSITION
:
131 for (j
= 0; j
< 4; j
++) {
132 quad
->output
.depth
[j
] = machine
->Outputs
[0].xyzw
[2].f
[j
];
145 fs_sse_delete( struct sp_fragment_shader
*base
)
147 struct sp_sse_fragment_shader
*shader
= sp_sse_fragment_shader(base
);
149 x86_release_func( &shader
->sse2_program
);
154 struct sp_fragment_shader
*
155 softpipe_create_fs_sse(struct softpipe_context
*softpipe
,
156 const struct pipe_shader_state
*templ
)
158 struct sp_sse_fragment_shader
*shader
;
160 if (!softpipe
->use_sse
)
163 shader
= CALLOC_STRUCT(sp_sse_fragment_shader
);
167 x86_init_func( &shader
->sse2_program
);
169 if (!tgsi_emit_sse2( templ
->tokens
, &shader
->sse2_program
,
170 shader
->immediates
, FALSE
)) {
175 shader
->func
= (tgsi_sse2_fs_function
) x86_get_func( &shader
->sse2_program
);
177 x86_release_func( &shader
->sse2_program
);
182 shader
->base
.shader
.tokens
= NULL
; /* don't hold reference to templ->tokens */
183 shader
->base
.prepare
= fs_sse_prepare
;
184 shader
->base
.run
= fs_sse_run
;
185 shader
->base
.delete = fs_sse_delete
;
187 return &shader
->base
;
193 /* Maybe put this variant in the header file.
195 struct sp_fragment_shader
*
196 softpipe_create_fs_sse(struct softpipe_context
*softpipe
,
197 const struct pipe_shader_state
*templ
)