2 * Copyright (C) 2009 Maciej Cencora <m.cencora@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 * Fragment program compiler. Perform transformations on the intermediate
32 * representation until the program is in a form where we can translate
33 * it more or less directly into machine-readable form.
35 * \author Ben Skeggs <darktama@iinet.net.au>
36 * \author Jerome Glisse <j.glisse@gmail.com>
39 #include "r300_fragprog_common.h"
41 #include "shader/program.h"
42 #include "shader/prog_parameter.h"
43 #include "shader/prog_print.h"
45 #include "compiler/radeon_compiler.h"
47 #include "radeon_mesa_to_rc.h"
48 #include "r300_state.h"
51 static GLuint
build_dtm(GLuint depthmode
)
55 case GL_LUMINANCE
: return 0;
56 case GL_INTENSITY
: return 1;
57 case GL_ALPHA
: return 2;
61 static GLuint
build_func(GLuint comparefunc
)
63 return comparefunc
- GL_NEVER
;
67 * Collect all external state that is relevant for compiling the given
70 static void build_state(
72 struct gl_fragment_program
*fp
,
73 struct r300_fragment_program_external_state
*state
)
77 _mesa_bzero(state
, sizeof(*state
));
79 for(unit
= 0; unit
< 16; ++unit
) {
80 if (fp
->Base
.ShadowSamplers
& (1 << unit
)) {
81 struct gl_texture_object
* tex
= r300
->radeon
.glCtx
->Texture
.Unit
[unit
]._Current
;
83 state
->unit
[unit
].depth_texture_mode
= build_dtm(tex
->DepthMode
);
84 state
->unit
[unit
].texture_compare_func
= build_func(tex
->CompareFunc
);
91 * Transform the program to support fragment.position.
93 * Introduce a small fragment at the start of the program that will be
94 * the only code that directly reads the FRAG_ATTRIB_WPOS input.
95 * All other code pieces that reference that input will be rewritten
96 * to read from a newly allocated temporary.
99 static void insert_WPOS_trailer(struct r300_fragment_program_compiler
*compiler
, struct r300_fragment_program
* fp
)
103 fp
->wpos_attr
= FRAG_ATTRIB_MAX
;
104 if (!(compiler
->Base
.Program
.InputsRead
& FRAG_BIT_WPOS
)) {
108 for (i
= FRAG_ATTRIB_TEX0
; i
<= FRAG_ATTRIB_TEX7
; ++i
)
110 if (!(compiler
->Base
.Program
.InputsRead
& (1 << i
))) {
116 /* No free texcoord found, fall-back to software rendering */
117 if (fp
->wpos_attr
== FRAG_ATTRIB_MAX
)
119 compiler
->Base
.Error
= 1;
123 rc_transform_fragment_wpos(&compiler
->Base
, FRAG_ATTRIB_WPOS
, fp
->wpos_attr
);
127 * Rewrite fragment.fogcoord to use a texture coordinate slot.
128 * Note that fogcoord is forced into an X001 pattern, and this enforcement
131 * See also the counterpart rewriting for vertex programs.
133 static void rewriteFog(struct r300_fragment_program_compiler
*compiler
, struct r300_fragment_program
* fp
)
135 struct rc_src_register src
;
138 fp
->fog_attr
= FRAG_ATTRIB_MAX
;
139 if (!(compiler
->Base
.Program
.InputsRead
& FRAG_BIT_FOGC
)) {
143 for (i
= FRAG_ATTRIB_TEX0
; i
<= FRAG_ATTRIB_TEX7
; ++i
)
145 if (!(compiler
->Base
.Program
.InputsRead
& (1 << i
))) {
151 /* No free texcoord found, fall-back to software rendering */
152 if (fp
->fog_attr
== FRAG_ATTRIB_MAX
)
154 compiler
->Base
.Error
= 1;
158 memset(&src
, 0, sizeof(src
));
159 src
.File
= RC_FILE_INPUT
;
160 src
.Index
= fp
->fog_attr
;
161 src
.Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
162 rc_move_input(&compiler
->Base
, FRAG_ATTRIB_FOGC
, src
);
167 * Reserve hardware temporary registers for the program inputs.
169 * @note This allocation is performed explicitly, because the order of inputs
170 * is determined by the RS hardware.
172 static void allocate_hw_inputs(
173 struct r300_fragment_program_compiler
* c
,
174 void (*allocate
)(void * data
, unsigned input
, unsigned hwreg
),
177 GLuint InputsRead
= c
->Base
.Program
.InputsRead
;
182 if (InputsRead
& FRAG_BIT_COL0
)
183 allocate(mydata
, FRAG_ATTRIB_COL0
, hwindex
++);
184 InputsRead
&= ~FRAG_BIT_COL0
;
186 /* Secondary color */
187 if (InputsRead
& FRAG_BIT_COL1
)
188 allocate(mydata
, FRAG_ATTRIB_COL1
, hwindex
++);
189 InputsRead
&= ~FRAG_BIT_COL1
;
192 for (i
= 0; i
< 8; i
++) {
193 if (InputsRead
& (FRAG_BIT_TEX0
<< i
))
194 allocate(mydata
, FRAG_ATTRIB_TEX0
+i
, hwindex
++);
196 InputsRead
&= ~FRAG_BITS_TEX_ANY
;
198 /* Fogcoords treated as a texcoord */
199 if (InputsRead
& FRAG_BIT_FOGC
)
200 allocate(mydata
, FRAG_ATTRIB_FOGC
, hwindex
++);
201 InputsRead
&= ~FRAG_BIT_FOGC
;
203 /* fragment position treated as a texcoord */
204 if (InputsRead
& FRAG_BIT_WPOS
)
205 allocate(mydata
, FRAG_ATTRIB_WPOS
, hwindex
++);
206 InputsRead
&= ~FRAG_BIT_WPOS
;
210 rc_error(&c
->Base
, "Don't know how to handle inputs 0x%x\n", InputsRead
);
214 static void translate_fragment_program(GLcontext
*ctx
, struct r300_fragment_program_cont
*cont
, struct r300_fragment_program
*fp
)
216 r300ContextPtr r300
= R300_CONTEXT(ctx
);
217 struct r300_fragment_program_compiler compiler
;
219 rc_init(&compiler
.Base
);
220 compiler
.Base
.Debug
= (RADEON_DEBUG
& RADEON_PIXEL
) ? GL_TRUE
: GL_FALSE
;
222 compiler
.code
= &fp
->code
;
223 compiler
.state
= fp
->state
;
224 compiler
.is_r500
= (r300
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
) ? GL_TRUE
: GL_FALSE
;
225 compiler
.OutputDepth
= FRAG_RESULT_DEPTH
;
226 compiler
.OutputColor
= FRAG_RESULT_COLOR
;
227 compiler
.AllocateHwInputs
= &allocate_hw_inputs
;
229 if (compiler
.Base
.Debug
) {
231 _mesa_printf("Fragment Program: Initial program:\n");
232 _mesa_print_program(&cont
->Base
.Base
);
236 radeon_mesa_to_rc_program(&compiler
.Base
, &cont
->Base
.Base
);
238 insert_WPOS_trailer(&compiler
, fp
);
240 rewriteFog(&compiler
, fp
);
242 r3xx_compile_fragment_program(&compiler
);
244 if (compiler
.is_r500
) {
245 /* We need to support the non-KMS DRM interface, which
246 * artificially limits the number of instructions and
247 * constants which are available to us.
249 * See also the comment in r300_context.c where we
250 * set the MAX_NATIVE_xxx values.
252 if (fp
->code
.code
.r500
.inst_end
>= 255 || fp
->code
.constants
.Count
> 255)
253 rc_error(&compiler
.Base
, "Program is too big (upgrade to r300g to avoid this limitation).\n");
256 fp
->error
= compiler
.Base
.Error
;
258 fp
->InputsRead
= compiler
.Base
.Program
.InputsRead
;
260 rc_destroy(&compiler
.Base
);
263 struct r300_fragment_program
*r300SelectAndTranslateFragmentShader(GLcontext
*ctx
)
265 r300ContextPtr r300
= R300_CONTEXT(ctx
);
266 struct r300_fragment_program_cont
*fp_list
;
267 struct r300_fragment_program
*fp
;
268 struct r300_fragment_program_external_state state
;
270 fp_list
= (struct r300_fragment_program_cont
*)ctx
->FragmentProgram
._Current
;
271 build_state(r300
, ctx
->FragmentProgram
._Current
, &state
);
275 if (_mesa_memcmp(&fp
->state
, &state
, sizeof(state
)) == 0) {
276 return r300
->selected_fp
= fp
;
281 fp
= _mesa_calloc(sizeof(struct r300_fragment_program
));
285 fp
->next
= fp_list
->progs
;
288 translate_fragment_program(ctx
, fp_list
, fp
);
290 return r300
->selected_fp
= fp
;