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 LLVM code generation.
34 #include "sp_context.h"
38 #include "pipe/p_state.h"
39 #include "pipe/p_defines.h"
40 #include "util/u_memory.h"
41 #include "tgsi/tgsi_sse2.h"
46 * Subclass of sp_fragment_shader
48 struct sp_llvm_fragment_shader
50 struct sp_fragment_shader base
;
51 struct gallivm_prog
*llvm_prog
;
56 shade_quad_llvm(struct quad_stage
*qs
,
57 struct quad_header
*quad
)
59 struct quad_shade_stage
*qss
= quad_shade_stage(qs
);
60 struct softpipe_context
*softpipe
= qs
->softpipe
;
61 float dests
[4][16][4] ALIGN16_ATTRIB
;
62 float inputs
[4][16][4] ALIGN16_ATTRIB
;
63 const float fx
= (float) quad
->x0
;
64 const float fy
= (float) quad
->y0
;
65 struct gallivm_prog
*llvm
= qss
->llvm_prog
;
68 inputs
[1][0][0] = fx
+ 1.0f
;
70 inputs
[3][0][0] = fx
+ 1.0f
;
74 inputs
[2][0][1] = fy
+ 1.0f
;
75 inputs
[3][0][1] = fy
+ 1.0f
;
78 gallivm_prog_inputs_interpolate(llvm
, inputs
, quad
->coef
);
81 debug_printf("MASK = %d\n", quad
->mask
);
82 for (int i
= 0; i
< 4; ++i
) {
83 for (int j
= 0; j
< 2; ++j
) {
84 debug_printf("IN(%d,%d) [%f %f %f %f]\n", i
, j
,
85 inputs
[i
][j
][0], inputs
[i
][j
][1], inputs
[i
][j
][2], inputs
[i
][j
][3]);
91 gallivm_fragment_shader_exec(llvm
, fx
, fy
, dests
, inputs
,
92 softpipe
->mapped_constants
[PIPE_SHADER_FRAGMENT
],
95 debug_printf("OUT LLVM = 1[%f %f %f %f], 2[%f %f %f %f]\n",
96 dests
[0][0][0], dests
[0][0][1], dests
[0][0][2], dests
[0][0][3],
97 dests
[0][1][0], dests
[0][1][1], dests
[0][1][2], dests
[0][1][3]);
100 /* store result color */
101 if (qss
->colorOutSlot
>= 0) {
103 /* XXX need to handle multiple color outputs someday */
104 allvmrt(qss
->stage
.softpipe
->fs
->info
.output_semantic_name
[qss
->colorOutSlot
]
105 == TGSI_SEMANTIC_COLOR
);
106 for (i
= 0; i
< QUAD_SIZE
; ++i
) {
107 quad
->outputs
.color
[0][0][i
] = dests
[i
][qss
->colorOutSlot
][0];
108 quad
->outputs
.color
[0][1][i
] = dests
[i
][qss
->colorOutSlot
][1];
109 quad
->outputs
.color
[0][2][i
] = dests
[i
][qss
->colorOutSlot
][2];
110 quad
->outputs
.color
[0][3][i
] = dests
[i
][qss
->colorOutSlot
][3];
114 for (int i
= 0; i
< QUAD_SIZE
; ++i
) {
115 debug_printf("QLLVM%d(%d) [%f, %f, %f, %f]\n", i
, qss
->colorOutSlot
,
116 quad
->outputs
.color
[0][0][i
],
117 quad
->outputs
.color
[0][1][i
],
118 quad
->outputs
.color
[0][2][i
],
119 quad
->outputs
.color
[0][3][i
]);
124 if (qss
->depthOutSlot
>= 0) {
125 /* output[slot] is new Z */
127 for (i
= 0; i
< 4; i
++) {
128 quad
->outputs
.depth
[i
] = dests
[i
][0][2];
132 /* copy input Z (which was interpolated by the executor) to output Z */
134 for (i
= 0; i
< 4; i
++) {
135 quad
->outputs
.depth
[i
] = inputs
[i
][0][2];
139 debug_printf("D [%f, %f, %f, %f] mask = %d\n",
140 quad
->outputs
.depth
[0],
141 quad
->outputs
.depth
[1],
142 quad
->outputs
.depth
[2],
143 quad
->outputs
.depth
[3], quad
->mask
);
146 /* shader may cull fragments */
148 qs
->next
->run( qs
->next
, quad
);
154 run_llvm_fs( const struct sp_fragment_shader
*base
,
155 struct foo
*machine
)
161 delete_llvm_fs( struct sp_fragment_shader
*base
)
167 struct sp_fragment_shader
*
168 softpipe_create_fs_llvm(struct softpipe_context
*softpipe
,
169 const struct pipe_shader_state
*templ
)
171 struct sp_llvm_fragment_shader
*shader
= NULL
;
173 /* LLVM fragment shaders currently disabled:
175 state
= CALLOC_STRUCT(sp_llvm_shader_state
);
179 state
->llvm_prog
= 0;
181 if (!gallivm_global_cpu_engine()) {
182 gallivm_cpu_engine_create(state
->llvm_prog
);
185 gallivm_cpu_jit_compile(gallivm_global_cpu_engine(), state
->llvm_prog
);
188 shader
->base
.run
= run_llvm_fs
;
189 shader
->base
.delete = delete_llvm_fs
;
198 struct sp_fragment_shader
*
199 softpipe_create_fs_llvm(struct softpipe_context
*softpipe
,
200 const struct pipe_shader_state
*templ
)