implement immediates and make them work
[mesa.git] / src / mesa / pipe / llvm / llvm_entry.c
1 /* clang --emit-llvm llvm_entry.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 void from_array(float4 (*res)[16], float (*ainputs)[16][4],
38 int count, int num_attribs)
39 {
40 for (int i = 0; i < count; ++i) {
41 for (int j = 0; j < num_attribs; ++j) {
42 float4 vec;
43 vec.x = ainputs[i][j][0];
44 vec.y = ainputs[i][j][1];
45 vec.z = ainputs[i][j][2];
46 vec.w = ainputs[i][j][3];
47 res[i][j] = vec;
48 }
49 }
50 }
51
52 void from_consts(float4 *res, float (*ainputs)[4],
53 int count)
54 {
55 for (int i = 0; i < count; ++i) {
56 float4 vec;
57 vec.x = ainputs[i][0];
58 vec.y = ainputs[i][1];
59 vec.z = ainputs[i][2];
60 vec.w = ainputs[i][3];
61 res[i] = vec;
62 }
63 }
64
65 void to_array(float (*dests)[4], float4 *in, int num_attribs)
66 {
67 for (int i = 0; i < num_attribs; ++i) {
68 float *rd = dests[i];
69 float4 ri = in[i];
70 rd[0] = ri.x;
71 rd[1] = ri.y;
72 rd[2] = ri.z;
73 rd[3] = ri.w;
74 }
75 }
76
77
78 struct ShaderInput
79 {
80 float4 *dests;
81 float4 *inputs;
82 float4 *temps;
83 float4 *consts;
84 int kilmask;
85 };
86
87 extern void execute_shader(struct ShaderInput *input);
88
89 void run_vertex_shader(void *inputs,
90 void *results,
91 float (*aconsts)[4],
92 int num_vertices,
93 int num_inputs,
94 int num_attribs,
95 int num_consts)
96 {
97 float4 consts[32];
98 float4 temps[128];//MAX_PROGRAM_TEMPS
99
100 struct ShaderInput args;
101 args.dests = results;
102 args.inputs = inputs;
103
104 /*printf("XXX LLVM run_vertex_shader vertices = %d, inputs = %d, attribs = %d, consts = %d\n",
105 num_vertices, num_inputs, num_attribs, num_consts);*/
106 from_consts(consts, aconsts, num_consts);
107 args.consts = consts;
108 args.temps = temps;
109
110 execute_shader(&args);
111 }
112
113
114 struct pipe_sampler_state;
115 struct softpipe_tile_cache;
116
117 #define NUM_CHANNELS 4 /* R,G,B,A */
118 #define QUAD_SIZE 4 /* 4 pixel/quad */
119
120 struct tgsi_sampler
121 {
122 const struct pipe_sampler_state *state;
123 /** Get samples for four fragments in a quad */
124 void (*get_samples)(struct tgsi_sampler *sampler,
125 const float s[QUAD_SIZE],
126 const float t[QUAD_SIZE],
127 const float p[QUAD_SIZE],
128 float lodbias,
129 float rgba[NUM_CHANNELS][QUAD_SIZE]);
130 void *pipe; /*XXX temporary*/
131 struct softpipe_tile_cache *cache;
132 };
133
134
135 int run_fragment_shader(float x, float y,
136 float4 (*results)[16],
137 float4 (*inputs)[16],
138 int num_inputs,
139 float (*aconsts)[4],
140 int num_consts,
141 struct tgsi_sampler *samplers)
142 {
143 float4 consts[32];
144 float4 temps[128];//MAX_PROGRAM_TEMPS
145 struct ShaderInput args;
146 int mask = 0;
147 args.kilmask = 0;
148
149 from_consts(consts, aconsts, num_consts);
150 args.consts = consts;
151 args.temps = temps;
152 //printf("AAAAAAAAAAAAAAAAAAAAAAA FRAGMENT SHADER %f %f\n", x, y);
153 for (int i = 0; i < 4; ++i) {
154 args.inputs = inputs[i];
155 args.dests = results[i];
156 mask = args.kilmask;
157 args.kilmask = 0;
158 execute_shader(&args);
159 args.kilmask = mask | (args.kilmask << i);
160 }
161 return ~args.kilmask;
162 }
163