2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 /* An attempt to hook s_fragprog_to_c.c up to libtcc.a to try &
26 * generate some real code.
28 * TCC isn't threadsafe, so it will need additional locking help if we
29 * end up using it as a backend in mesa.
39 #include "nvfragprog.h"
43 #include "s_nvfragprog.h"
44 #include "s_texture.h"
50 typedef int (*cfunc
)( void *ctx
,
51 const GLfloat (*local_param
)[4],
52 const GLfloat (*env_param
)[4],
53 const struct program_parameter
*state_param
,
54 const GLfloat (*interp
)[4],
55 GLfloat (*outputs
)[4]);
58 static cfunc current_func
;
59 static struct fragment_program
*current_program
;
60 static TCCState
*current_tcc_state
;
63 static void TEX( void *cc
, const float *texcoord
, int unit
, float *result
)
65 GLcontext
*ctx
= (GLcontext
*)cc
;
66 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
67 GLfloat lambda
= 1.0; /* hack */
70 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
71 1, (const GLfloat (*)[4]) texcoord
,
74 result
[0] = CHAN_TO_FLOAT(rgba
[0]);
75 result
[1] = CHAN_TO_FLOAT(rgba
[1]);
76 result
[2] = CHAN_TO_FLOAT(rgba
[2]);
77 result
[3] = CHAN_TO_FLOAT(rgba
[3]);
81 static void TXB( void *cc
, const float *texcoord
, int unit
, float *result
)
83 GLcontext
*ctx
= (GLcontext
*)cc
;
84 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
85 GLfloat lambda
= 1.0; /* hack */
88 /* texcoord[3] is the bias to add to lambda */
89 lambda
+= texcoord
[3];
92 /* Is it necessary to reset texcoord[3] to 1 at this point?
94 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
95 1, (const GLfloat (*)[4]) texcoord
,
98 result
[0] = CHAN_TO_FLOAT(rgba
[0]);
99 result
[1] = CHAN_TO_FLOAT(rgba
[1]);
100 result
[2] = CHAN_TO_FLOAT(rgba
[2]);
101 result
[3] = CHAN_TO_FLOAT(rgba
[3]);
105 static void TXP( void *cc
, const float *texcoord
, int unit
, float *result
)
107 /* I think that TEX needs to undo the perspective divide which has
108 * already occurred. In the meantime, TXP is correct to do this:
110 TEX( cc
, texcoord
, unit
, result
);
114 static cfunc
codegen( TCCState
*s
, const char *prog
, const char *fname
)
125 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
126 tcc_compile_string(s
, prog
);
128 /* tcc_add_dll("/usr/lib/libm.so"); */
130 tcc_add_symbol(s
, "TEX", (unsigned long)&TEX
);
131 tcc_add_symbol(s
, "TXB", (unsigned long)&TXB
);
132 tcc_add_symbol(s
, "TXP", (unsigned long)&TXP
);
136 tcc_get_symbol(s
, &val
, fname
);
140 /* TCC isn't threadsafe and even seems not to like having more than
141 * one TCCState created or used at any one time in a single threaded
142 * environment. So, this code is all for investigation only and can't
143 * currently be used in Mesa proper.
145 * I've taken some liberties with globals myself, now.
148 _swrast_execute_codegen_program( GLcontext
*ctx
,
149 const struct fragment_program
*program
, GLuint maxInst
,
150 struct fp_machine
*machine
, const struct sw_span
*span
,
153 if (program
!= current_program
) {
155 _swrast_translate_program( ctx
);
157 fprintf(stderr
, "%s: compiling:\n%s\n", __FUNCTION__
, program
->c_str
);
159 current_program
= program
;
160 current_func
= codegen( current_tcc_state
, program
->c_str
,
164 assert(current_func
);
166 return current_func( ctx
,
167 program
->Base
.LocalParams
,
168 (const GLfloat (*)[4])ctx
->FragmentProgram
.Parameters
,
169 program
->Parameters
->Parameters
,
170 (const GLfloat (*)[4])machine
->Inputs
,
177 _swrast_execute_codegen_program( GLcontext
*ctx
,
178 const struct fragment_program
*program
, GLuint maxInst
,
179 struct fp_machine
*machine
, const struct sw_span
*span
,
183 (void) program
; (void) maxInst
;
184 (void) machine
; (void) span
;