Fix wrong comment.
[mesa.git] / src / mesa / drivers / dri / r300 / r300_vertexprog.c
1 #include "glheader.h"
2 #include "macros.h"
3 #include "enums.h"
4
5 #include "program.h"
6 #include "r300_context.h"
7 #include "nvvertprog.h"
8
9 static void r300BindProgram(GLcontext *ctx, GLenum target, struct program *prog)
10 {
11 fprintf(stderr, "r300BindProgram\n");
12 }
13
14
15 static struct program *r300NewProgram(GLcontext *ctx, GLenum target, GLuint id)
16 {
17 r300ContextPtr rmesa = R300_CONTEXT(ctx);
18 struct r300_vertex_program *vp;
19 struct fragment_program *fp;
20 struct ati_fragment_shader *afs;
21
22 fprintf(stderr, "r300NewProgram, target=%d, id=%d\n", target, id);
23
24 switch(target){
25 case GL_VERTEX_PROGRAM_ARB:
26 fprintf(stderr, "vertex prog\n");
27 vp=malloc(sizeof(*vp));
28 memset(vp, 0, sizeof(*vp));
29
30 /* note that vp points to mesa_program since its first on the struct
31 */
32 return _mesa_init_vertex_program(ctx, &vp->mesa_program, target, id);
33
34 case GL_FRAGMENT_PROGRAM_ARB:
35 fprintf(stderr, "fragment prog\n");
36 fp=malloc(sizeof(*fp));
37 memset(fp, 0, sizeof(*fp));
38
39 return _mesa_init_fragment_program(ctx, fp, target, id);
40 case GL_FRAGMENT_PROGRAM_NV:
41 fprintf(stderr, "nv fragment prog\n");
42 fp=malloc(sizeof(*fp));
43 memset(fp, 0, sizeof(*fp));
44
45 return _mesa_init_fragment_program(ctx, fp, target, id);
46
47 case GL_FRAGMENT_SHADER_ATI:
48 fprintf(stderr, "ati fragment prog\n");
49 afs=malloc(sizeof(*afs));
50 memset(afs, 0, sizeof(*afs));
51
52 return _mesa_init_ati_fragment_shader(ctx, afs, target, id);
53
54 default:
55 return NULL;
56 }
57
58 }
59
60
61 static void r300DeleteProgram(GLcontext *ctx, struct program *prog)
62 {
63 fprintf(stderr, "r300DeleteProgram\n");
64
65 /* check that not active */
66 _mesa_delete_program(ctx, prog);
67 }
68
69 static void r300ProgramStringNotify(GLcontext *ctx, GLenum target,
70 struct program *prog)
71 {
72 struct r300_vertex_program *vp=(void *)prog;
73
74 fprintf(stderr, "r300ProgramStringNotify\n");
75 /* XXX: There is still something wrong as mesa doesnt call r300IsProgramNative at all */
76 r300IsProgramNative(ctx, target, prog);
77
78 switch(target) {
79 case GL_VERTEX_PROGRAM_ARB:
80 vp->translated=GL_FALSE;
81 break;
82 }
83
84 }
85
86 #define SCALAR_FLAG (1<<31)
87 #define FLAG_MASK (1<<31)
88 #define OPN(operator, ip, op) {#operator, VP_OPCODE_##operator, ip, op}
89 struct{
90 char *name;
91 int opcode;
92 unsigned long ip; /* number of input operands and flags */
93 unsigned long op;
94 }op_names[]={
95 OPN(ABS, 1, 1),
96 OPN(ADD, 2, 1),
97 OPN(ARL, 1, 1|SCALAR_FLAG),
98 OPN(DP3, 2, 3|SCALAR_FLAG),
99 OPN(DP4, 2, 3|SCALAR_FLAG),
100 OPN(DPH, 2, 3|SCALAR_FLAG),
101 OPN(DST, 2, 1),
102 OPN(EX2, 1|SCALAR_FLAG, 4|SCALAR_FLAG),
103 OPN(EXP, 1|SCALAR_FLAG, 1),
104 OPN(FLR, 1, 1),
105 OPN(FRC, 1, 1),
106 OPN(LG2, 1|SCALAR_FLAG, 4|SCALAR_FLAG),
107 OPN(LIT, 1, 1),
108 OPN(LOG, 1|SCALAR_FLAG, 1),
109 OPN(MAD, 3, 1),
110 OPN(MAX, 2, 1),
111 OPN(MIN, 2, 1),
112 OPN(MOV, 1, 1),
113 OPN(MUL, 2, 1),
114 OPN(POW, 2|SCALAR_FLAG, 4|SCALAR_FLAG),
115 OPN(RCP, 1|SCALAR_FLAG, 4|SCALAR_FLAG),
116 OPN(RSQ, 1|SCALAR_FLAG, 4|SCALAR_FLAG),
117 OPN(SGE, 2, 1),
118 OPN(SLT, 2, 1),
119 OPN(SUB, 2, 1),
120 OPN(SWZ, 1, 1),
121 OPN(XPD, 2, 1),
122 OPN(RCC, 0, 0), //extra
123 OPN(PRINT, 0, 0),
124 OPN(END, 0, 0),
125 };
126 #undef OPN
127 #define OPN(rf) {#rf, PROGRAM_##rf}
128
129 struct{
130 char *name;
131 int id;
132 }register_file_names[]={
133 OPN(TEMPORARY),
134 OPN(INPUT),
135 OPN(OUTPUT),
136 OPN(LOCAL_PARAM),
137 OPN(ENV_PARAM),
138 OPN(NAMED_PARAM),
139 OPN(STATE_VAR),
140 OPN(WRITE_ONLY),
141 OPN(ADDRESS),
142 };
143
144 char *dst_mask_names[4]={ "X", "Y", "Z", "W" };
145
146 /* from vertex program spec:
147 Instruction Inputs Output Description
148 ----------- ------ ------ --------------------------------
149 ABS v v absolute value
150 ADD v,v v add
151 ARL v a address register load
152 DP3 v,v ssss 3-component dot product
153 DP4 v,v ssss 4-component dot product
154 DPH v,v ssss homogeneous dot product
155 DST v,v v distance vector
156 EX2 s ssss exponential base 2
157 EXP s v exponential base 2 (approximate)
158 FLR v v floor
159 FRC v v fraction
160 LG2 s ssss logarithm base 2
161 LIT v v compute light coefficients
162 LOG s v logarithm base 2 (approximate)
163 MAD v,v,v v multiply and add
164 MAX v,v v maximum
165 MIN v,v v minimum
166 MOV v v move
167 MUL v,v v multiply
168 POW s,s ssss exponentiate
169 RCP s ssss reciprocal
170 RSQ s ssss reciprocal square root
171 SGE v,v v set on greater than or equal
172 SLT v,v v set on less than
173 SUB v,v v subtract
174 SWZ v v extended swizzle
175 XPD v,v v cross product
176 */
177
178 void dump_program_params(struct vertex_program *vp){
179 int i;
180 int pi;
181
182 fprintf(stderr, "NumInstructions=%d\n", vp->Base.NumInstructions);
183 fprintf(stderr, "NumTemporaries=%d\n", vp->Base.NumTemporaries);
184 fprintf(stderr, "NumParameters=%d\n", vp->Base.NumParameters);
185 fprintf(stderr, "NumAttributes=%d\n", vp->Base.NumAttributes);
186 fprintf(stderr, "NumAddressRegs=%d\n", vp->Base.NumAddressRegs);
187
188 for(pi=0; pi < vp->Base.NumParameters; pi++){
189 fprintf(stderr, "{ ");
190 for(i=0; i < 4; i++)
191 fprintf(stderr, "%f ", vp->Base.LocalParams[pi][i]);
192 fprintf(stderr, "}\n");
193 }
194
195 for(pi=0; pi < vp->Parameters->NumParameters; pi++){
196 fprintf(stderr, "param %02d:", pi);
197
198 switch(vp->Parameters->Parameters[pi].Type){
199
200 case NAMED_PARAMETER:
201 fprintf(stderr, "%s", vp->Parameters->Parameters[pi].Name);
202 fprintf(stderr, "(NAMED_PARAMETER)");
203 break;
204
205 case CONSTANT:
206 fprintf(stderr, "(CONSTANT)");
207 break;
208
209 case STATE:
210 fprintf(stderr, "(STATE)\n");
211 /* fetch state info */
212 continue;
213 break;
214
215 }
216
217 fprintf(stderr, "{ ");
218 for(i=0; i < 4; i++)
219 fprintf(stderr, "%f ", vp->Parameters->Parameters[pi].Values[i]);
220 fprintf(stderr, "}\n");
221
222 }
223 }
224
225 static GLboolean r300IsProgramNative(GLcontext *ctx, GLenum target,
226 struct program *prog)
227 {
228 struct vertex_program *vp=(void *)prog;
229 struct vp_instruction *vpi;
230 int i, operand_index;
231 int operator_index;
232
233 fprintf(stderr, "r300IsProgramNative\n");
234 //exit(0);
235
236 dump_program_params(vp);
237
238 vpi=vp->Instructions;
239
240 for(;; vpi++){
241 if(vpi->Opcode == VP_OPCODE_END)
242 break;
243
244 for(i=0; i < sizeof(op_names) / sizeof(*op_names); i++){
245 if(vpi->Opcode == op_names[i].opcode){
246 fprintf(stderr, "%s ", op_names[i].name);
247 break;
248 }
249 }
250 operator_index=i;
251
252 for(i=0; i < sizeof(register_file_names) / sizeof(*register_file_names); i++){
253 if(vpi->DstReg.File == register_file_names[i].id){
254 fprintf(stderr, "%s ", register_file_names[i].name);
255 break;
256 }
257 }
258
259 fprintf(stderr, "%d.", vpi->DstReg.Index);
260
261 for(i=0; i < 4; i++)
262 if(vpi->DstReg.WriteMask[i])
263 fprintf(stderr, "%s", dst_mask_names[i]);
264 fprintf(stderr, " ");
265
266 for(operand_index=0; operand_index < op_names[operator_index].ip & (~FLAG_MASK);
267 operand_index++){
268
269 if(vpi->SrcReg[operand_index].Negate)
270 fprintf(stderr, "-");
271
272 for(i=0; i < sizeof(register_file_names) / sizeof(*register_file_names); i++){
273 if(vpi->SrcReg[operand_index].File == register_file_names[i].id){
274 fprintf(stderr, "%s ", register_file_names[i].name);
275 break;
276 }
277 }
278 fprintf(stderr, "%d.", vpi->SrcReg[operand_index].Index);
279
280 for(i=0; i < 4; i++)
281 fprintf(stderr, "%s", dst_mask_names[vpi->SrcReg[operand_index].Swizzle[i]]);
282
283 if(operand_index+1 < op_names[operator_index].ip & (~FLAG_MASK) )
284 fprintf(stderr, ",");
285 }
286 fprintf(stderr, "\n");
287 //op_names[i].ip
288 //op_names[i].op
289 }
290 return 1;
291 }
292
293 /* This is misnamed and shouldnt be here since fragment programs use these functions too */
294 void r300InitVertexProgFuncs(struct dd_function_table *functions)
295 {
296 #if 1
297 functions->NewProgram=r300NewProgram;
298 functions->BindProgram=r300BindProgram;
299 functions->DeleteProgram=r300DeleteProgram;
300 functions->ProgramStringNotify=r300ProgramStringNotify;
301 functions->IsProgramNative=r300IsProgramNative;
302 #endif
303 }