Merge branch 'nouveau-gallium-0.1' into darktama-gallium-0.1
[mesa.git] / src / mesa / pipe / llvm / llvm_entry.c
1 /* clang --emit-llvm llvm_builtins.c |llvm-as |opt -std-compile-opts |llvm2cpp -for=Shader -gen-module -funcname=createBaseShader */
2 /**************************************************************************
3 *
4 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29 /*
30 * Authors:
31 * Zack Rusin zack@tungstengraphics.com
32 */
33
34 /* clang --emit-llvm llvm_builtins.c |llvm-as |opt -std-compile-opts |llvm-dis */
35 typedef __attribute__(( ocu_vector_type(4) )) float float4;
36
37 #if 0
38 //clang doesn't suppoer "struct->member" notation yet
39 struct vertex_header {
40 unsigned clipmask:12;
41 unsigned edgeflag:1;
42 unsigned pad:3;
43 unsigned vertex_id:16;
44
45 float clip[4];
46
47 float data[][4];
48 };
49
50 inline float
51 dot4(float4 a, float4 b)
52 {
53 float4 c = a*b;
54 return c.x + c.y + c.z + c.w;
55 }
56
57 inline unsigned
58 compute_clipmask(float4 clip, float4 (*plane), unsigned nr)
59 {
60 unsigned mask = 0;
61 unsigned i;
62
63 for (i = 0; i < nr; i++) {
64 if (dot4(clip, plane[i]) < 0)
65 mask |= (1<<i);
66 }
67
68 return mask;
69 }
70
71 inline void collect_results(float4 *results, struct vertex_header *vOut,
72 float4 *planes, int nr_planes,
73 float4 scale, float4 trans,
74 int num_attribs)
75 {
76 /* store results */
77 unsigned slot;
78 float x, y, z, w;
79
80 /* Handle attr[0] (position) specially:
81 */
82 float4 res0 = results[0];
83 float *clip = vOut->clip;
84 x = clip[0] = res0.x;
85 y = clip[1] = res0.y;
86 z = clip[2] = res0.z;
87 w = clip[3] = res0.w;
88
89 vOut->clipmask = compute_clipmask(res0, planes, nr_planes);
90 vOut->edgeflag = 1;
91
92 /* divide by w */
93 w = 1.0f / w;
94 x *= w;
95 y *= w;
96 z *= w;
97 res0.x = x; res0.y = y; res0.z = z; res0.w = 1;
98
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;
105
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.
110 */
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;
117
118 printf("output %d: %f %f %f %f\n", slot,
119 vOut->data[slot][0],
120 vOut->data[slot][1],
121 vOut->data[slot][2],
122 vOut->data[slot][3]);
123 }
124 }
125 #endif
126
127 void from_array(float4 (*res)[16], float (*ainputs)[16][4],
128 int count, int num_attribs)
129 {
130 for (int i = 0; i < count; ++i) {
131 for (int j = 0; j < num_attribs; ++j) {
132 float4 vec;
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];
137 res[i][j] = vec;
138 }
139 }
140 }
141
142 void from_consts(float4 *res, float (*ainputs)[4],
143 int count)
144 {
145 for (int i = 0; i < count; ++i) {
146 float4 vec;
147 vec.x = ainputs[i][0];
148 vec.y = ainputs[i][1];
149 vec.z = ainputs[i][2];
150 vec.w = ainputs[i][3];
151 res[i] = vec;
152 }
153 }
154
155 void to_array(float (*dests)[4], float4 *in, int num_attribs)
156 {
157 for (int i = 0; i < num_attribs; ++i) {
158 float *rd = dests[i];
159 float4 ri = in[i];
160 rd[0] = ri.x;
161 rd[1] = ri.y;
162 rd[2] = ri.z;
163 rd[3] = ri.w;
164 }
165 }
166
167 extern void execute_shader(float4 dests[16], float4 inputs[16],
168 float4 consts[32], float4 temps[128]);
169
170 void run_vertex_shader(float (*ainputs)[16][4],
171 float (*dests)[16][4],
172 float (*aconsts)[4],
173 int num_vertices,
174 int num_inputs,
175 int num_attribs,
176 int num_consts)
177 {
178 float4 inputs[16*32*4][16];
179 float4 consts[32];
180 float4 results[16*32*4][16];
181 float4 temps[128];//MAX_PROGRAM_TEMPS
182
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);
192 }
193 }
194
195
196 struct pipe_sampler_state;
197 struct softpipe_tile_cache;
198
199 #define NUM_CHANNELS 4 /* R,G,B,A */
200 #define QUAD_SIZE 4 /* 4 pixel/quad */
201
202 struct tgsi_sampler
203 {
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],
210 float lodbias,
211 float rgba[NUM_CHANNELS][QUAD_SIZE]);
212 void *pipe; /*XXX temporary*/
213 struct softpipe_tile_cache *cache;
214 };
215
216 int run_fragment_shader(float x, float y,
217 float (*dests)[16][4],
218 float (*ainputs)[16][4],
219 int num_inputs,
220 float (*aconsts)[4],
221 int num_consts,
222 struct tgsi_sampler *samplers,
223 unsigned *sampler_units)
224 {
225 float4 inputs[4][16];
226 float4 consts[32];
227 float4 results[4][16];
228 float4 temps[128];//MAX_PROGRAM_TEMPS
229 int kilmask = 0;
230
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);
239 }
240 return ~kilmask;
241 }
242