1 /* clang --emit-llvm llvm_builtins.c |llvm-as |opt -std-compile-opts |llvm2cpp -for=Shader -gen-module -funcname=createBaseShader */
2 /**************************************************************************
4 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
31 * Zack Rusin zack@tungstengraphics.com
34 /* clang --emit-llvm llvm_builtins.c |llvm-as |opt -std-compile-opts |llvm-dis */
35 typedef __attribute__(( ocu_vector_type(4) )) float float4
;
38 //clang doesn't suppoer "struct->member" notation yet
39 struct vertex_header
{
43 unsigned vertex_id
:16;
51 dot4(float4 a
, float4 b
)
54 return c
.x
+ c
.y
+ c
.z
+ c
.w
;
58 compute_clipmask(float4 clip
, float4 (*plane
), unsigned nr
)
63 for (i
= 0; i
< nr
; i
++) {
64 if (dot4(clip
, plane
[i
]) < 0)
71 inline void collect_results(float4
*results
, struct vertex_header
*vOut
,
72 float4
*planes
, int nr_planes
,
73 float4 scale
, float4 trans
,
80 /* Handle attr[0] (position) specially:
82 float4 res0
= results
[0];
83 float *clip
= vOut
->clip
;
89 vOut
->clipmask
= compute_clipmask(res0
, planes
, nr_planes
);
97 res0
.x
= x
; res0
.y
= y
; res0
.z
= z
; res0
.w
= 1;
99 /* Viewport mapping */
100 res0
= res0
* scale
+ trans
;
101 vOut
->data
[0][0] = res0
.x
;
102 vOut
->data
[0][1] = res0
.y
;
103 vOut
->data
[0][2] = res0
.z
;
104 vOut
->data
[0][3] = w
;
106 /* Remaining attributes are packed into sequential post-transform
107 * vertex attrib slots.
108 * Skip 0 since we just did it above.
109 * Subtract two because of the VERTEX_HEADER, CLIP_POS attribs.
111 for (slot
= 1; slot
< num_attribs
- 2; slot
++) {
112 float4 vec
= results
[slot
];
113 vOut
->data
[slot
][0] = vec
.x
;
114 vOut
->data
[slot
][1] = vec
.y
;
115 vOut
->data
[slot
][2] = vec
.z
;
116 vOut
->data
[slot
][3] = vec
.w
;
118 printf("output %d: %f %f %f %f\n", slot
,
122 vOut
->data
[slot
][3]);
127 void from_array(float4 (*res
)[16], float (*ainputs
)[16][4],
128 int count
, int num_attribs
)
130 for (int i
= 0; i
< count
; ++i
) {
131 for (int j
= 0; j
< num_attribs
; ++j
) {
133 vec
.x
= ainputs
[i
][j
][0];
134 vec
.y
= ainputs
[i
][j
][1];
135 vec
.z
= ainputs
[i
][j
][2];
136 vec
.w
= ainputs
[i
][j
][3];
142 void from_consts(float4
*res
, float (*ainputs
)[4],
145 for (int i
= 0; i
< count
; ++i
) {
147 vec
.x
= ainputs
[i
][0];
148 vec
.y
= ainputs
[i
][1];
149 vec
.z
= ainputs
[i
][2];
150 vec
.w
= ainputs
[i
][3];
155 void to_array(float (*dests
)[4], float4
*in
, int num_attribs
)
157 for (int i
= 0; i
< num_attribs
; ++i
) {
158 float *rd
= dests
[i
];
167 extern void execute_shader(float4 dests
[16], float4 inputs
[16],
168 float4 consts
[32], float4 temps
[128]);
170 void run_vertex_shader(float (*ainputs
)[16][4],
171 float (*dests
)[16][4],
178 float4 inputs
[16*32*4][16];
180 float4 results
[16*32*4][16];
181 float4 temps
[128];//MAX_PROGRAM_TEMPS
183 /*printf("XXX LLVM run_vertex_shader vertices = %d, inputs = %d, attribs = %d, consts = %d\n",
184 num_vertices, num_inputs, num_attribs, num_consts);*/
185 from_array(inputs
, ainputs
, num_vertices
, num_inputs
);
186 from_consts(consts
, aconsts
, num_consts
);
187 for (int i
= 0; i
< num_vertices
; ++i
) {
188 float4
*in
= inputs
[i
];
189 float4
*res
= results
[i
];
190 execute_shader(res
, in
, consts
, temps
);
191 to_array(dests
[i
], res
, num_attribs
);
196 struct pipe_sampler_state
;
197 struct softpipe_tile_cache
;
199 #define NUM_CHANNELS 4 /* R,G,B,A */
200 #define QUAD_SIZE 4 /* 4 pixel/quad */
204 const struct pipe_sampler_state
*state
;
205 /** Get samples for four fragments in a quad */
206 void (*get_samples
)(struct tgsi_sampler
*sampler
,
207 const float s
[QUAD_SIZE
],
208 const float t
[QUAD_SIZE
],
209 const float p
[QUAD_SIZE
],
211 float rgba
[NUM_CHANNELS
][QUAD_SIZE
]);
212 void *pipe
; /*XXX temporary*/
213 struct softpipe_tile_cache
*cache
;
216 int run_fragment_shader(float x
, float y
,
217 float (*dests
)[16][4],
218 float (*ainputs
)[16][4],
222 struct tgsi_sampler
*samplers
,
223 unsigned *sampler_units
)
225 float4 inputs
[4][16];
227 float4 results
[4][16];
228 float4 temps
[128];//MAX_PROGRAM_TEMPS
231 from_array(inputs
, ainputs
, 4, num_inputs
);
232 from_consts(consts
, aconsts
, num_consts
);
233 //printf("AAAAAAAAAAAAAAAAAAAAAAA FRAGMENT SHADER %f %f\n", x, y);
234 for (int i
= 0; i
< 4; ++i
) {
235 float4
*in
= inputs
[i
];
236 float4
*res
= results
[i
];
237 execute_shader(res
, in
, consts
, temps
);
238 to_array(dests
[i
], res
, 2);