2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2010 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
27 * \file ir_to_mesa.cpp
29 * Translates the IR to ARB_fragment_program text if possible,
34 #include "main/compiler.h"
36 #include "ir_visitor.h"
37 #include "ir_print_visitor.h"
38 #include "ir_expression_flattening.h"
39 #include "glsl_types.h"
40 #include "glsl_parser_extras.h"
41 #include "../glsl/program.h"
42 #include "ir_optimization.h"
46 #include "main/mtypes.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/uniforms.h"
50 #include "program/hash_table.h"
51 #include "program/prog_instruction.h"
52 #include "program/prog_optimize.h"
53 #include "program/prog_print.h"
54 #include "program/program.h"
55 #include "program/prog_uniform.h"
56 #include "program/prog_parameter.h"
59 static int swizzle_for_size(int size
);
62 * This struct is a corresponding struct to Mesa prog_src_register, with
65 typedef struct ir_to_mesa_src_reg
{
66 ir_to_mesa_src_reg(int file
, int index
, const glsl_type
*type
)
70 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
71 this->swizzle
= swizzle_for_size(type
->vector_elements
);
73 this->swizzle
= SWIZZLE_XYZW
;
80 this->file
= PROGRAM_UNDEFINED
;
87 int file
; /**< PROGRAM_* from Mesa */
88 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
89 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
90 int negate
; /**< NEGATE_XYZW mask from mesa */
91 /** Register index should be offset by the integer in this reg. */
92 ir_to_mesa_src_reg
*reladdr
;
95 typedef struct ir_to_mesa_dst_reg
{
96 int file
; /**< PROGRAM_* from Mesa */
97 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
98 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
100 /** Register index should be offset by the integer in this reg. */
101 ir_to_mesa_src_reg
*reladdr
;
102 } ir_to_mesa_dst_reg
;
104 extern ir_to_mesa_src_reg ir_to_mesa_undef
;
106 class ir_to_mesa_instruction
: public exec_node
{
108 /* Callers of this talloc-based new need not call delete. It's
109 * easier to just talloc_free 'ctx' (or any of its ancestors). */
110 static void* operator new(size_t size
, void *ctx
)
114 node
= talloc_zero_size(ctx
, size
);
115 assert(node
!= NULL
);
121 ir_to_mesa_dst_reg dst_reg
;
122 ir_to_mesa_src_reg src_reg
[3];
123 /** Pointer to the ir source this tree came from for debugging */
125 GLboolean cond_update
;
126 int sampler
; /**< sampler index */
127 int tex_target
; /**< One of TEXTURE_*_INDEX */
128 GLboolean tex_shadow
;
130 class function_entry
*function
; /* Set on OPCODE_CAL or OPCODE_BGNSUB */
133 class variable_storage
: public exec_node
{
135 variable_storage(ir_variable
*var
, int file
, int index
)
136 : file(file
), index(index
), var(var
)
143 ir_variable
*var
; /* variable that maps to this, if any */
146 class function_entry
: public exec_node
{
148 ir_function_signature
*sig
;
151 * identifier of this function signature used by the program.
153 * At the point that Mesa instructions for function calls are
154 * generated, we don't know the address of the first instruction of
155 * the function body. So we make the BranchTarget that is called a
156 * small integer and rewrite them during set_branchtargets().
161 * Pointer to first instruction of the function body.
163 * Set during function body emits after main() is processed.
165 ir_to_mesa_instruction
*bgn_inst
;
168 * Index of the first instruction of the function body in actual
171 * Set after convertion from ir_to_mesa_instruction to prog_instruction.
175 /** Storage for the return value. */
176 ir_to_mesa_src_reg return_reg
;
179 class ir_to_mesa_visitor
: public ir_visitor
{
181 ir_to_mesa_visitor();
182 ~ir_to_mesa_visitor();
184 function_entry
*current_function
;
187 struct gl_program
*prog
;
188 struct gl_shader_program
*shader_program
;
192 variable_storage
*find_variable_storage(ir_variable
*var
);
194 function_entry
*get_function_signature(ir_function_signature
*sig
);
196 ir_to_mesa_src_reg
get_temp(const glsl_type
*type
);
197 void reladdr_to_temp(ir_instruction
*ir
,
198 ir_to_mesa_src_reg
*reg
, int *num_reladdr
);
200 struct ir_to_mesa_src_reg
src_reg_for_float(float val
);
203 * \name Visit methods
205 * As typical for the visitor pattern, there must be one \c visit method for
206 * each concrete subclass of \c ir_instruction. Virtual base classes within
207 * the hierarchy should not have \c visit methods.
210 virtual void visit(ir_variable
*);
211 virtual void visit(ir_loop
*);
212 virtual void visit(ir_loop_jump
*);
213 virtual void visit(ir_function_signature
*);
214 virtual void visit(ir_function
*);
215 virtual void visit(ir_expression
*);
216 virtual void visit(ir_swizzle
*);
217 virtual void visit(ir_dereference_variable
*);
218 virtual void visit(ir_dereference_array
*);
219 virtual void visit(ir_dereference_record
*);
220 virtual void visit(ir_assignment
*);
221 virtual void visit(ir_constant
*);
222 virtual void visit(ir_call
*);
223 virtual void visit(ir_return
*);
224 virtual void visit(ir_discard
*);
225 virtual void visit(ir_texture
*);
226 virtual void visit(ir_if
*);
229 struct ir_to_mesa_src_reg result
;
231 /** List of variable_storage */
234 /** List of function_entry */
235 exec_list function_signatures
;
236 int next_signature_id
;
238 /** List of ir_to_mesa_instruction */
239 exec_list instructions
;
241 ir_to_mesa_instruction
*ir_to_mesa_emit_op0(ir_instruction
*ir
,
242 enum prog_opcode op
);
244 ir_to_mesa_instruction
*ir_to_mesa_emit_op1(ir_instruction
*ir
,
246 ir_to_mesa_dst_reg dst
,
247 ir_to_mesa_src_reg src0
);
249 ir_to_mesa_instruction
*ir_to_mesa_emit_op2(ir_instruction
*ir
,
251 ir_to_mesa_dst_reg dst
,
252 ir_to_mesa_src_reg src0
,
253 ir_to_mesa_src_reg src1
);
255 ir_to_mesa_instruction
*ir_to_mesa_emit_op3(ir_instruction
*ir
,
257 ir_to_mesa_dst_reg dst
,
258 ir_to_mesa_src_reg src0
,
259 ir_to_mesa_src_reg src1
,
260 ir_to_mesa_src_reg src2
);
262 void ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
264 ir_to_mesa_dst_reg dst
,
265 ir_to_mesa_src_reg src0
);
267 void ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
269 ir_to_mesa_dst_reg dst
,
270 ir_to_mesa_src_reg src0
,
271 ir_to_mesa_src_reg src1
);
273 GLboolean
try_emit_mad(ir_expression
*ir
,
276 int get_sampler_uniform_value(ir_dereference
*deref
);
281 ir_to_mesa_src_reg ir_to_mesa_undef
= ir_to_mesa_src_reg(PROGRAM_UNDEFINED
, 0, NULL
);
283 ir_to_mesa_dst_reg ir_to_mesa_undef_dst
= {
284 PROGRAM_UNDEFINED
, 0, SWIZZLE_NOOP
, COND_TR
, NULL
,
287 ir_to_mesa_dst_reg ir_to_mesa_address_reg
= {
288 PROGRAM_ADDRESS
, 0, WRITEMASK_X
, COND_TR
, NULL
291 static void fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
293 static void fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
297 prog
->InfoLog
= talloc_vasprintf_append(prog
->InfoLog
, fmt
, args
);
300 prog
->LinkStatus
= GL_FALSE
;
303 static int swizzle_for_size(int size
)
305 int size_swizzles
[4] = {
306 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
307 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
308 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
309 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
312 return size_swizzles
[size
- 1];
315 ir_to_mesa_instruction
*
316 ir_to_mesa_visitor::ir_to_mesa_emit_op3(ir_instruction
*ir
,
318 ir_to_mesa_dst_reg dst
,
319 ir_to_mesa_src_reg src0
,
320 ir_to_mesa_src_reg src1
,
321 ir_to_mesa_src_reg src2
)
323 ir_to_mesa_instruction
*inst
= new(mem_ctx
) ir_to_mesa_instruction();
326 /* If we have to do relative addressing, we want to load the ARL
327 * reg directly for one of the regs, and preload the other reladdr
328 * sources into temps.
330 num_reladdr
+= dst
.reladdr
!= NULL
;
331 num_reladdr
+= src0
.reladdr
!= NULL
;
332 num_reladdr
+= src1
.reladdr
!= NULL
;
333 num_reladdr
+= src2
.reladdr
!= NULL
;
335 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
336 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
337 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
340 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
,
345 assert(num_reladdr
== 0);
349 inst
->src_reg
[0] = src0
;
350 inst
->src_reg
[1] = src1
;
351 inst
->src_reg
[2] = src2
;
354 inst
->function
= NULL
;
356 this->instructions
.push_tail(inst
);
362 ir_to_mesa_instruction
*
363 ir_to_mesa_visitor::ir_to_mesa_emit_op2(ir_instruction
*ir
,
365 ir_to_mesa_dst_reg dst
,
366 ir_to_mesa_src_reg src0
,
367 ir_to_mesa_src_reg src1
)
369 return ir_to_mesa_emit_op3(ir
, op
, dst
, src0
, src1
, ir_to_mesa_undef
);
372 ir_to_mesa_instruction
*
373 ir_to_mesa_visitor::ir_to_mesa_emit_op1(ir_instruction
*ir
,
375 ir_to_mesa_dst_reg dst
,
376 ir_to_mesa_src_reg src0
)
378 assert(dst
.writemask
!= 0);
379 return ir_to_mesa_emit_op3(ir
, op
, dst
,
380 src0
, ir_to_mesa_undef
, ir_to_mesa_undef
);
383 ir_to_mesa_instruction
*
384 ir_to_mesa_visitor::ir_to_mesa_emit_op0(ir_instruction
*ir
,
387 return ir_to_mesa_emit_op3(ir
, op
, ir_to_mesa_undef_dst
,
393 inline ir_to_mesa_dst_reg
394 ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg reg
)
396 ir_to_mesa_dst_reg dst_reg
;
398 dst_reg
.file
= reg
.file
;
399 dst_reg
.index
= reg
.index
;
400 dst_reg
.writemask
= WRITEMASK_XYZW
;
401 dst_reg
.cond_mask
= COND_TR
;
402 dst_reg
.reladdr
= reg
.reladdr
;
407 inline ir_to_mesa_src_reg
408 ir_to_mesa_src_reg_from_dst(ir_to_mesa_dst_reg reg
)
410 return ir_to_mesa_src_reg(reg
.file
, reg
.index
, NULL
);
414 * Emits Mesa scalar opcodes to produce unique answers across channels.
416 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
417 * channel determines the result across all channels. So to do a vec4
418 * of this operation, we want to emit a scalar per source channel used
419 * to produce dest channels.
422 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
424 ir_to_mesa_dst_reg dst
,
425 ir_to_mesa_src_reg orig_src0
,
426 ir_to_mesa_src_reg orig_src1
)
429 int done_mask
= ~dst
.writemask
;
431 /* Mesa RCP is a scalar operation splatting results to all channels,
432 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
435 for (i
= 0; i
< 4; i
++) {
436 GLuint this_mask
= (1 << i
);
437 ir_to_mesa_instruction
*inst
;
438 ir_to_mesa_src_reg src0
= orig_src0
;
439 ir_to_mesa_src_reg src1
= orig_src1
;
441 if (done_mask
& this_mask
)
444 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
445 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
446 for (j
= i
+ 1; j
< 4; j
++) {
447 if (!(done_mask
& (1 << j
)) &&
448 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
449 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
450 this_mask
|= (1 << j
);
453 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
454 src0_swiz
, src0_swiz
);
455 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
456 src1_swiz
, src1_swiz
);
458 inst
= ir_to_mesa_emit_op2(ir
, op
,
462 inst
->dst_reg
.writemask
= this_mask
;
463 done_mask
|= this_mask
;
468 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
470 ir_to_mesa_dst_reg dst
,
471 ir_to_mesa_src_reg src0
)
473 ir_to_mesa_src_reg undef
= ir_to_mesa_undef
;
475 undef
.swizzle
= SWIZZLE_XXXX
;
477 ir_to_mesa_emit_scalar_op2(ir
, op
, dst
, src0
, undef
);
480 struct ir_to_mesa_src_reg
481 ir_to_mesa_visitor::src_reg_for_float(float val
)
483 ir_to_mesa_src_reg
src_reg(PROGRAM_CONSTANT
, -1, NULL
);
485 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
486 &val
, 1, &src_reg
.swizzle
);
492 type_size(const struct glsl_type
*type
)
497 switch (type
->base_type
) {
500 case GLSL_TYPE_FLOAT
:
502 if (type
->is_matrix()) {
503 return type
->matrix_columns
;
505 /* Regardless of size of vector, it gets a vec4. This is bad
506 * packing for things like floats, but otherwise arrays become a
507 * mess. Hopefully a later pass over the code can pack scalars
508 * down if appropriate.
512 case GLSL_TYPE_ARRAY
:
513 return type_size(type
->fields
.array
) * type
->length
;
514 case GLSL_TYPE_STRUCT
:
516 for (i
= 0; i
< type
->length
; i
++) {
517 size
+= type_size(type
->fields
.structure
[i
].type
);
520 case GLSL_TYPE_SAMPLER
:
521 /* Samplers take up one slot in UNIFORMS[], but they're baked in
532 * In the initial pass of codegen, we assign temporary numbers to
533 * intermediate results. (not SSA -- variable assignments will reuse
534 * storage). Actual register allocation for the Mesa VM occurs in a
535 * pass over the Mesa IR later.
538 ir_to_mesa_visitor::get_temp(const glsl_type
*type
)
540 ir_to_mesa_src_reg src_reg
;
544 src_reg
.file
= PROGRAM_TEMPORARY
;
545 src_reg
.index
= next_temp
;
546 src_reg
.reladdr
= NULL
;
547 next_temp
+= type_size(type
);
549 if (type
->is_array() || type
->is_record()) {
550 src_reg
.swizzle
= SWIZZLE_NOOP
;
552 for (i
= 0; i
< type
->vector_elements
; i
++)
555 swizzle
[i
] = type
->vector_elements
- 1;
556 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
557 swizzle
[2], swizzle
[3]);
565 ir_to_mesa_visitor::find_variable_storage(ir_variable
*var
)
568 variable_storage
*entry
;
570 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
571 entry
= (variable_storage
*)iter
.get();
573 if (entry
->var
== var
)
580 struct statevar_element
{
582 int tokens
[STATE_LENGTH
];
587 static struct statevar_element gl_DepthRange_elements
[] = {
588 {"near", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_XXXX
},
589 {"far", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_YYYY
},
590 {"diff", {STATE_DEPTH_RANGE
, 0, 0}, SWIZZLE_ZZZZ
},
593 static struct statevar_element gl_ClipPlane_elements
[] = {
594 {NULL
, {STATE_CLIPPLANE
, 0, 0}, SWIZZLE_XYZW
}
597 static struct statevar_element gl_Point_elements
[] = {
598 {"size", {STATE_POINT_SIZE
}, SWIZZLE_XXXX
},
599 {"sizeMin", {STATE_POINT_SIZE
}, SWIZZLE_YYYY
},
600 {"sizeMax", {STATE_POINT_SIZE
}, SWIZZLE_ZZZZ
},
601 {"fadeThresholdSize", {STATE_POINT_SIZE
}, SWIZZLE_WWWW
},
602 {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_XXXX
},
603 {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_YYYY
},
604 {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION
}, SWIZZLE_ZZZZ
},
607 static struct statevar_element gl_FrontMaterial_elements
[] = {
608 {"emission", {STATE_MATERIAL
, 0, STATE_EMISSION
}, SWIZZLE_XYZW
},
609 {"ambient", {STATE_MATERIAL
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
610 {"diffuse", {STATE_MATERIAL
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
611 {"specular", {STATE_MATERIAL
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
612 {"shininess", {STATE_MATERIAL
, 0, STATE_SHININESS
}, SWIZZLE_XXXX
},
615 static struct statevar_element gl_BackMaterial_elements
[] = {
616 {"emission", {STATE_MATERIAL
, 1, STATE_EMISSION
}, SWIZZLE_XYZW
},
617 {"ambient", {STATE_MATERIAL
, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
618 {"diffuse", {STATE_MATERIAL
, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
619 {"specular", {STATE_MATERIAL
, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
620 {"shininess", {STATE_MATERIAL
, 1, STATE_SHININESS
}, SWIZZLE_XXXX
},
623 static struct statevar_element gl_LightSource_elements
[] = {
624 {"ambient", {STATE_LIGHT
, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
625 {"diffuse", {STATE_LIGHT
, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
626 {"specular", {STATE_LIGHT
, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
627 {"position", {STATE_LIGHT
, 0, STATE_POSITION
}, SWIZZLE_XYZW
},
628 {"halfVector", {STATE_LIGHT
, 0, STATE_HALF_VECTOR
}, SWIZZLE_XYZW
},
629 {"spotDirection", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_XYZW
},
630 {"spotCosCutoff", {STATE_LIGHT
, 0, STATE_SPOT_DIRECTION
}, SWIZZLE_WWWW
},
631 {"spotCutoff", {STATE_LIGHT
, 0, STATE_SPOT_CUTOFF
}, SWIZZLE_XXXX
},
632 {"spotExponent", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_WWWW
},
633 {"constantAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_XXXX
},
634 {"linearAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_YYYY
},
635 {"quadraticAttenuation", {STATE_LIGHT
, 0, STATE_ATTENUATION
}, SWIZZLE_ZZZZ
},
638 static struct statevar_element gl_LightModel_elements
[] = {
639 {"ambient", {STATE_LIGHTMODEL_AMBIENT
, 0}, SWIZZLE_XYZW
},
642 static struct statevar_element gl_FrontLightModelProduct_elements
[] = {
643 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 0}, SWIZZLE_XYZW
},
646 static struct statevar_element gl_BackLightModelProduct_elements
[] = {
647 {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR
, 1}, SWIZZLE_XYZW
},
650 static struct statevar_element gl_FrontLightProduct_elements
[] = {
651 {"ambient", {STATE_LIGHTPROD
, 0, 0, STATE_AMBIENT
}, SWIZZLE_XYZW
},
652 {"diffuse", {STATE_LIGHTPROD
, 0, 0, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
653 {"specular", {STATE_LIGHTPROD
, 0, 0, STATE_SPECULAR
}, SWIZZLE_XYZW
},
656 static struct statevar_element gl_BackLightProduct_elements
[] = {
657 {"ambient", {STATE_LIGHTPROD
, 0, 1, STATE_AMBIENT
}, SWIZZLE_XYZW
},
658 {"diffuse", {STATE_LIGHTPROD
, 0, 1, STATE_DIFFUSE
}, SWIZZLE_XYZW
},
659 {"specular", {STATE_LIGHTPROD
, 0, 1, STATE_SPECULAR
}, SWIZZLE_XYZW
},
662 static struct statevar_element gl_TextureEnvColor_elements
[] = {
663 {NULL
, {STATE_TEXENV_COLOR
, 0}, SWIZZLE_XYZW
},
666 static struct statevar_element gl_EyePlaneS_elements
[] = {
667 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_S
}, SWIZZLE_XYZW
},
670 static struct statevar_element gl_EyePlaneT_elements
[] = {
671 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_T
}, SWIZZLE_XYZW
},
674 static struct statevar_element gl_EyePlaneR_elements
[] = {
675 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_R
}, SWIZZLE_XYZW
},
678 static struct statevar_element gl_EyePlaneQ_elements
[] = {
679 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_EYE_Q
}, SWIZZLE_XYZW
},
682 static struct statevar_element gl_ObjectPlaneS_elements
[] = {
683 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_S
}, SWIZZLE_XYZW
},
686 static struct statevar_element gl_ObjectPlaneT_elements
[] = {
687 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_T
}, SWIZZLE_XYZW
},
690 static struct statevar_element gl_ObjectPlaneR_elements
[] = {
691 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_R
}, SWIZZLE_XYZW
},
694 static struct statevar_element gl_ObjectPlaneQ_elements
[] = {
695 {NULL
, {STATE_TEXGEN
, 0, STATE_TEXGEN_OBJECT_Q
}, SWIZZLE_XYZW
},
698 static struct statevar_element gl_Fog_elements
[] = {
699 {"color", {STATE_FOG_COLOR
}, SWIZZLE_XYZW
},
700 {"density", {STATE_FOG_PARAMS
}, SWIZZLE_XXXX
},
701 {"start", {STATE_FOG_PARAMS
}, SWIZZLE_YYYY
},
702 {"end", {STATE_FOG_PARAMS
}, SWIZZLE_ZZZZ
},
703 {"scale", {STATE_FOG_PARAMS
}, SWIZZLE_WWWW
},
706 #define MATRIX(name, statevar, modifier) \
707 static struct statevar_element name ## _elements[] = { \
708 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
709 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
710 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
711 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
714 MATRIX(gl_ModelViewMatrix
,
715 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
716 MATRIX(gl_ModelViewMatrixInverse
,
717 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
718 MATRIX(gl_ModelViewMatrixTranspose
,
719 STATE_MODELVIEW_MATRIX
, 0);
720 MATRIX(gl_ModelViewMatrixInverseTranspose
,
721 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
723 MATRIX(gl_ProjectionMatrix
,
724 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
725 MATRIX(gl_ProjectionMatrixInverse
,
726 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
727 MATRIX(gl_ProjectionMatrixTranspose
,
728 STATE_PROJECTION_MATRIX
, 0);
729 MATRIX(gl_ProjectionMatrixInverseTranspose
,
730 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
732 MATRIX(gl_ModelViewProjectionMatrix
,
733 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
734 MATRIX(gl_ModelViewProjectionMatrixInverse
,
735 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
736 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
737 STATE_MVP_MATRIX
, 0);
738 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
739 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
741 MATRIX(gl_TextureMatrix
,
742 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
743 MATRIX(gl_TextureMatrixInverse
,
744 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
745 MATRIX(gl_TextureMatrixTranspose
,
746 STATE_TEXTURE_MATRIX
, 0);
747 MATRIX(gl_TextureMatrixInverseTranspose
,
748 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
750 static struct statevar_element gl_NormalMatrix_elements
[] = {
751 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
753 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
755 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
761 #define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
763 static const struct statevar
{
765 struct statevar_element
*elements
;
766 unsigned int num_elements
;
768 STATEVAR(gl_DepthRange
),
769 STATEVAR(gl_ClipPlane
),
771 STATEVAR(gl_FrontMaterial
),
772 STATEVAR(gl_BackMaterial
),
773 STATEVAR(gl_LightSource
),
774 STATEVAR(gl_LightModel
),
775 STATEVAR(gl_FrontLightModelProduct
),
776 STATEVAR(gl_BackLightModelProduct
),
777 STATEVAR(gl_FrontLightProduct
),
778 STATEVAR(gl_BackLightProduct
),
779 STATEVAR(gl_TextureEnvColor
),
780 STATEVAR(gl_EyePlaneS
),
781 STATEVAR(gl_EyePlaneT
),
782 STATEVAR(gl_EyePlaneR
),
783 STATEVAR(gl_EyePlaneQ
),
784 STATEVAR(gl_ObjectPlaneS
),
785 STATEVAR(gl_ObjectPlaneT
),
786 STATEVAR(gl_ObjectPlaneR
),
787 STATEVAR(gl_ObjectPlaneQ
),
790 STATEVAR(gl_ModelViewMatrix
),
791 STATEVAR(gl_ModelViewMatrixInverse
),
792 STATEVAR(gl_ModelViewMatrixTranspose
),
793 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
795 STATEVAR(gl_ProjectionMatrix
),
796 STATEVAR(gl_ProjectionMatrixInverse
),
797 STATEVAR(gl_ProjectionMatrixTranspose
),
798 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
800 STATEVAR(gl_ModelViewProjectionMatrix
),
801 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
802 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
803 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
805 STATEVAR(gl_TextureMatrix
),
806 STATEVAR(gl_TextureMatrixInverse
),
807 STATEVAR(gl_TextureMatrixTranspose
),
808 STATEVAR(gl_TextureMatrixInverseTranspose
),
810 STATEVAR(gl_NormalMatrix
),
814 ir_to_mesa_visitor::visit(ir_variable
*ir
)
816 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
817 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
819 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
820 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
823 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
826 for (i
= 0; i
< Elements(statevars
); i
++) {
827 if (strcmp(ir
->name
, statevars
[i
].name
) == 0)
831 if (i
== Elements(statevars
)) {
832 fail_link(this->shader_program
,
833 "Failed to find builtin uniform `%s'\n", ir
->name
);
837 const struct statevar
*statevar
= &statevars
[i
];
840 if (ir
->type
->is_array()) {
841 array_count
= ir
->type
->length
;
846 /* Check if this statevar's setup in the STATE file exactly
847 * matches how we'll want to reference it as a
848 * struct/array/whatever. If not, then we need to move it into
849 * temporary storage and hope that it'll get copy-propagated
852 for (i
= 0; i
< statevar
->num_elements
; i
++) {
853 if (statevar
->elements
[i
].swizzle
!= SWIZZLE_XYZW
) {
858 struct variable_storage
*storage
;
859 ir_to_mesa_dst_reg dst
;
860 if (i
== statevar
->num_elements
) {
861 /* We'll set the index later. */
862 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
863 this->variables
.push_tail(storage
);
865 dst
= ir_to_mesa_undef_dst
;
867 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
869 this->variables
.push_tail(storage
);
870 this->next_temp
+= type_size(ir
->type
);
872 dst
= ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg(PROGRAM_TEMPORARY
,
878 for (int a
= 0; a
< array_count
; a
++) {
879 for (unsigned int i
= 0; i
< statevar
->num_elements
; i
++) {
880 struct statevar_element
*element
= &statevar
->elements
[i
];
881 int tokens
[STATE_LENGTH
];
883 memcpy(tokens
, element
->tokens
, sizeof(element
->tokens
));
884 if (ir
->type
->is_array()) {
888 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
889 (gl_state_index
*)tokens
);
891 if (storage
->file
== PROGRAM_STATE_VAR
) {
892 if (storage
->index
== -1) {
893 storage
->index
= index
;
895 assert(index
== ((int)storage
->index
+
896 a
* statevar
->num_elements
+ i
));
899 ir_to_mesa_src_reg
src(PROGRAM_STATE_VAR
, index
, NULL
);
900 src
.swizzle
= element
->swizzle
;
901 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, dst
, src
);
902 /* even a float takes up a whole vec4 reg in a struct/array. */
907 if (storage
->file
== PROGRAM_TEMPORARY
&&
908 dst
.index
!= storage
->index
+ type_size(ir
->type
)) {
909 fail_link(this->shader_program
,
910 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
911 ir
->name
, dst
.index
- storage
->index
,
912 type_size(ir
->type
));
918 ir_to_mesa_visitor::visit(ir_loop
*ir
)
920 ir_dereference_variable
*counter
= NULL
;
922 if (ir
->counter
!= NULL
)
923 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
925 if (ir
->from
!= NULL
) {
926 assert(ir
->counter
!= NULL
);
928 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
934 ir_to_mesa_emit_op0(NULL
, OPCODE_BGNLOOP
);
938 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
940 ir_if
*if_stmt
= new(ir
) ir_if(e
);
942 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
944 if_stmt
->then_instructions
.push_tail(brk
);
946 if_stmt
->accept(this);
953 visit_exec_list(&ir
->body_instructions
, this);
957 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
958 counter
, ir
->increment
);
960 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
967 ir_to_mesa_emit_op0(NULL
, OPCODE_ENDLOOP
);
971 ir_to_mesa_visitor::visit(ir_loop_jump
*ir
)
974 case ir_loop_jump::jump_break
:
975 ir_to_mesa_emit_op0(NULL
, OPCODE_BRK
);
977 case ir_loop_jump::jump_continue
:
978 ir_to_mesa_emit_op0(NULL
, OPCODE_CONT
);
985 ir_to_mesa_visitor::visit(ir_function_signature
*ir
)
992 ir_to_mesa_visitor::visit(ir_function
*ir
)
994 /* Ignore function bodies other than main() -- we shouldn't see calls to
995 * them since they should all be inlined before we get to ir_to_mesa.
997 if (strcmp(ir
->name
, "main") == 0) {
998 const ir_function_signature
*sig
;
1001 sig
= ir
->matching_signature(&empty
);
1005 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1006 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1014 ir_to_mesa_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1016 int nonmul_operand
= 1 - mul_operand
;
1017 ir_to_mesa_src_reg a
, b
, c
;
1019 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1020 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1023 expr
->operands
[0]->accept(this);
1025 expr
->operands
[1]->accept(this);
1027 ir
->operands
[nonmul_operand
]->accept(this);
1030 this->result
= get_temp(ir
->type
);
1031 ir_to_mesa_emit_op3(ir
, OPCODE_MAD
,
1032 ir_to_mesa_dst_reg_from_src(this->result
), a
, b
, c
);
1038 ir_to_mesa_visitor::reladdr_to_temp(ir_instruction
*ir
,
1039 ir_to_mesa_src_reg
*reg
, int *num_reladdr
)
1044 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
, *reg
->reladdr
);
1046 if (*num_reladdr
!= 1) {
1047 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
1049 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
,
1050 ir_to_mesa_dst_reg_from_src(temp
), *reg
);
1058 ir_to_mesa_visitor::visit(ir_expression
*ir
)
1060 unsigned int operand
;
1061 struct ir_to_mesa_src_reg op
[2];
1062 struct ir_to_mesa_src_reg result_src
;
1063 struct ir_to_mesa_dst_reg result_dst
;
1064 const glsl_type
*vec4_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 4, 1);
1065 const glsl_type
*vec3_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 3, 1);
1066 const glsl_type
*vec2_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 2, 1);
1068 /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
1070 if (ir
->operation
== ir_binop_add
) {
1071 if (try_emit_mad(ir
, 1))
1073 if (try_emit_mad(ir
, 0))
1077 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1078 this->result
.file
= PROGRAM_UNDEFINED
;
1079 ir
->operands
[operand
]->accept(this);
1080 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1082 printf("Failed to get tree for expression operand:\n");
1083 ir
->operands
[operand
]->accept(&v
);
1086 op
[operand
] = this->result
;
1088 /* Matrix expression operands should have been broken down to vector
1089 * operations already.
1091 assert(!ir
->operands
[operand
]->type
->is_matrix());
1094 this->result
.file
= PROGRAM_UNDEFINED
;
1096 /* Storage for our result. Ideally for an assignment we'd be using
1097 * the actual storage for the result here, instead.
1099 result_src
= get_temp(ir
->type
);
1100 /* convenience for the emit functions below. */
1101 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
1102 /* Limit writes to the channels that will be used by result_src later.
1103 * This does limit this temp's use as a temporary for multi-instruction
1106 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1108 switch (ir
->operation
) {
1109 case ir_unop_logic_not
:
1110 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
,
1111 op
[0], src_reg_for_float(0.0));
1114 op
[0].negate
= ~op
[0].negate
;
1118 ir_to_mesa_emit_op1(ir
, OPCODE_ABS
, result_dst
, op
[0]);
1121 ir_to_mesa_emit_op1(ir
, OPCODE_SSG
, result_dst
, op
[0]);
1124 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RCP
, result_dst
, op
[0]);
1128 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_EX2
, result_dst
, op
[0]);
1132 assert(!"not reached: should be handled by ir_explog_to_explog2");
1135 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LG2
, result_dst
, op
[0]);
1138 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_SIN
, result_dst
, op
[0]);
1141 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_COS
, result_dst
, op
[0]);
1145 ir_to_mesa_emit_op1(ir
, OPCODE_DDX
, result_dst
, op
[0]);
1148 ir_to_mesa_emit_op1(ir
, OPCODE_DDY
, result_dst
, op
[0]);
1152 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1155 ir_to_mesa_emit_op2(ir
, OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1159 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1162 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1164 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1168 ir_to_mesa_emit_op2(ir
, OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1170 case ir_binop_greater
:
1171 ir_to_mesa_emit_op2(ir
, OPCODE_SGT
, result_dst
, op
[0], op
[1]);
1173 case ir_binop_lequal
:
1174 ir_to_mesa_emit_op2(ir
, OPCODE_SLE
, result_dst
, op
[0], op
[1]);
1176 case ir_binop_gequal
:
1177 ir_to_mesa_emit_op2(ir
, OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1179 case ir_binop_equal
:
1180 /* "==" operator producing a scalar boolean. */
1181 if (ir
->operands
[0]->type
->is_vector() ||
1182 ir
->operands
[1]->type
->is_vector()) {
1183 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
1184 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1185 ir_to_mesa_dst_reg_from_src(temp
), op
[0], op
[1]);
1186 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, temp
, temp
);
1187 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
,
1188 result_dst
, result_src
, src_reg_for_float(0.0));
1190 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1193 case ir_binop_nequal
:
1194 /* "!=" operator producing a scalar boolean. */
1195 if (ir
->operands
[0]->type
->is_vector() ||
1196 ir
->operands
[1]->type
->is_vector()) {
1197 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
1198 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1199 ir_to_mesa_dst_reg_from_src(temp
), op
[0], op
[1]);
1200 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, temp
, temp
);
1201 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1202 result_dst
, result_src
, src_reg_for_float(0.0));
1204 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1209 switch (ir
->operands
[0]->type
->vector_elements
) {
1211 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, op
[0], op
[0]);
1214 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
, result_dst
, op
[0], op
[0]);
1217 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
, result_dst
, op
[0], op
[0]);
1220 assert(!"unreached: ir_unop_any of non-bvec");
1223 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1224 result_dst
, result_src
, src_reg_for_float(0.0));
1227 case ir_binop_logic_xor
:
1228 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1231 case ir_binop_logic_or
:
1232 /* This could be a saturated add and skip the SNE. */
1233 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
,
1237 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1239 result_src
, src_reg_for_float(0.0));
1242 case ir_binop_logic_and
:
1243 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1244 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1250 if (ir
->operands
[0]->type
== vec4_type
) {
1251 assert(ir
->operands
[1]->type
== vec4_type
);
1252 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
,
1255 } else if (ir
->operands
[0]->type
== vec3_type
) {
1256 assert(ir
->operands
[1]->type
== vec3_type
);
1257 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
,
1260 } else if (ir
->operands
[0]->type
== vec2_type
) {
1261 assert(ir
->operands
[1]->type
== vec2_type
);
1262 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
,
1268 case ir_binop_cross
:
1269 ir_to_mesa_emit_op2(ir
, OPCODE_XPD
, result_dst
, op
[0], op
[1]);
1273 /* sqrt(x) = x * rsq(x). */
1274 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
1275 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1276 /* For incoming channels <= 0, set the result to 0. */
1277 op
[0].negate
= ~op
[0].negate
;
1278 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, result_dst
,
1279 op
[0], result_src
, src_reg_for_float(0.0));
1282 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
1287 /* Mesa IR lacks types, ints are stored as truncated floats. */
1291 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1295 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
,
1296 op
[0], src_reg_for_float(0.0));
1299 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1302 op
[0].negate
= ~op
[0].negate
;
1303 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1304 result_src
.negate
= ~result_src
.negate
;
1307 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1310 ir_to_mesa_emit_op1(ir
, OPCODE_FRC
, result_dst
, op
[0]);
1314 ir_to_mesa_emit_op2(ir
, OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1317 ir_to_mesa_emit_op2(ir
, OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1320 ir_to_mesa_emit_scalar_op2(ir
, OPCODE_POW
, result_dst
, op
[0], op
[1]);
1323 case ir_unop_bit_not
:
1325 case ir_binop_lshift
:
1326 case ir_binop_rshift
:
1327 case ir_binop_bit_and
:
1328 case ir_binop_bit_xor
:
1329 case ir_binop_bit_or
:
1330 assert(!"GLSL 1.30 features unsupported");
1334 this->result
= result_src
;
1339 ir_to_mesa_visitor::visit(ir_swizzle
*ir
)
1341 ir_to_mesa_src_reg src_reg
;
1345 /* Note that this is only swizzles in expressions, not those on the left
1346 * hand side of an assignment, which do write masking. See ir_assignment
1350 ir
->val
->accept(this);
1351 src_reg
= this->result
;
1352 assert(src_reg
.file
!= PROGRAM_UNDEFINED
);
1354 for (i
= 0; i
< 4; i
++) {
1355 if (i
< ir
->type
->vector_elements
) {
1358 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.x
);
1361 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.y
);
1364 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.z
);
1367 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.w
);
1371 /* If the type is smaller than a vec4, replicate the last
1374 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1378 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0],
1383 this->result
= src_reg
;
1387 ir_to_mesa_visitor::visit(ir_dereference_variable
*ir
)
1389 variable_storage
*entry
= find_variable_storage(ir
->var
);
1392 switch (ir
->var
->mode
) {
1393 case ir_var_uniform
:
1394 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_UNIFORM
,
1396 this->variables
.push_tail(entry
);
1401 /* The linker assigns locations for varyings and attributes,
1402 * including deprecated builtins (like gl_Color), user-assign
1403 * generic attributes (glBindVertexLocation), and
1404 * user-defined varyings.
1406 * FINISHME: We would hit this path for function arguments. Fix!
1408 assert(ir
->var
->location
!= -1);
1409 if (ir
->var
->mode
== ir_var_in
||
1410 ir
->var
->mode
== ir_var_inout
) {
1411 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1415 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1416 ir
->var
->location
>= VERT_ATTRIB_GENERIC0
) {
1417 _mesa_add_attribute(prog
->Attributes
,
1419 _mesa_sizeof_glsl_type(ir
->var
->type
->gl_type
),
1420 ir
->var
->type
->gl_type
,
1421 ir
->var
->location
- VERT_ATTRIB_GENERIC0
);
1424 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1431 case ir_var_temporary
:
1432 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_TEMPORARY
,
1434 this->variables
.push_tail(entry
);
1436 next_temp
+= type_size(ir
->var
->type
);
1441 printf("Failed to make storage for %s\n", ir
->var
->name
);
1446 this->result
= ir_to_mesa_src_reg(entry
->file
, entry
->index
, ir
->var
->type
);
1450 ir_to_mesa_visitor::visit(ir_dereference_array
*ir
)
1453 ir_to_mesa_src_reg src_reg
;
1454 int element_size
= type_size(ir
->type
);
1456 index
= ir
->array_index
->constant_expression_value();
1458 ir
->array
->accept(this);
1459 src_reg
= this->result
;
1462 src_reg
.index
+= index
->value
.i
[0] * element_size
;
1464 ir_to_mesa_src_reg array_base
= this->result
;
1465 /* Variable index array dereference. It eats the "vec4" of the
1466 * base of the array and an index that offsets the Mesa register
1469 ir
->array_index
->accept(this);
1471 ir_to_mesa_src_reg index_reg
;
1473 if (element_size
== 1) {
1474 index_reg
= this->result
;
1476 index_reg
= get_temp(glsl_type::float_type
);
1478 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1479 ir_to_mesa_dst_reg_from_src(index_reg
),
1480 this->result
, src_reg_for_float(element_size
));
1483 src_reg
.reladdr
= talloc(mem_ctx
, ir_to_mesa_src_reg
);
1484 memcpy(src_reg
.reladdr
, &index_reg
, sizeof(index_reg
));
1487 /* If the type is smaller than a vec4, replicate the last channel out. */
1488 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1489 src_reg
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1491 src_reg
.swizzle
= SWIZZLE_NOOP
;
1493 this->result
= src_reg
;
1497 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
1500 const glsl_type
*struct_type
= ir
->record
->type
;
1503 ir
->record
->accept(this);
1505 for (i
= 0; i
< struct_type
->length
; i
++) {
1506 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1508 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1510 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1511 this->result
.index
+= offset
;
1515 * We want to be careful in assignment setup to hit the actual storage
1516 * instead of potentially using a temporary like we might with the
1517 * ir_dereference handler.
1519 static struct ir_to_mesa_dst_reg
1520 get_assignment_lhs(ir_dereference
*ir
, ir_to_mesa_visitor
*v
)
1522 /* The LHS must be a dereference. If the LHS is a variable indexed array
1523 * access of a vector, it must be separated into a series conditional moves
1524 * before reaching this point (see ir_vec_index_to_cond_assign).
1526 assert(ir
->as_dereference());
1527 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1529 assert(!deref_array
->array
->type
->is_vector());
1532 /* Use the rvalue deref handler for the most part. We'll ignore
1533 * swizzles in it and write swizzles using writemask, though.
1536 return ir_to_mesa_dst_reg_from_src(v
->result
);
1540 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1542 struct ir_to_mesa_dst_reg l
;
1543 struct ir_to_mesa_src_reg r
;
1546 ir
->rhs
->accept(this);
1549 l
= get_assignment_lhs(ir
->lhs
, this);
1551 /* FINISHME: This should really set to the correct maximal writemask for each
1552 * FINISHME: component written (in the loops below). This case can only
1553 * FINISHME: occur for matrices, arrays, and structures.
1555 if (ir
->write_mask
== 0) {
1556 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1557 l
.writemask
= WRITEMASK_XYZW
;
1558 } else if (ir
->lhs
->type
->is_scalar()) {
1559 /* FINISHME: This hack makes writing to gl_FragData, which lives in the
1560 * FINISHME: W component of fragment shader output zero, work correctly.
1562 l
.writemask
= WRITEMASK_XYZW
;
1564 assert(ir
->lhs
->type
->is_vector());
1565 l
.writemask
= ir
->write_mask
;
1568 assert(l
.file
!= PROGRAM_UNDEFINED
);
1569 assert(r
.file
!= PROGRAM_UNDEFINED
);
1571 if (ir
->condition
) {
1572 ir_to_mesa_src_reg condition
;
1574 ir
->condition
->accept(this);
1575 condition
= this->result
;
1577 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves,
1578 * and the condition we produced is 0.0 or 1.0. By flipping the
1579 * sign, we can choose which value OPCODE_CMP produces without
1580 * an extra computing the condition.
1582 condition
.negate
= ~condition
.negate
;
1583 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1584 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, l
,
1585 condition
, r
, ir_to_mesa_src_reg_from_dst(l
));
1590 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1591 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1600 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1602 ir_to_mesa_src_reg src_reg
;
1603 GLfloat stack_vals
[4] = { 0 };
1604 GLfloat
*values
= stack_vals
;
1607 /* Unfortunately, 4 floats is all we can get into
1608 * _mesa_add_unnamed_constant. So, make a temp to store an
1609 * aggregate constant and move each constant value into it. If we
1610 * get lucky, copy propagation will eliminate the extra moves.
1613 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1614 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1615 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1617 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1618 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1619 int size
= type_size(field_value
->type
);
1623 field_value
->accept(this);
1624 src_reg
= this->result
;
1626 for (i
= 0; i
< (unsigned int)size
; i
++) {
1627 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1633 this->result
= temp_base
;
1637 if (ir
->type
->is_array()) {
1638 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1639 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1640 int size
= type_size(ir
->type
->fields
.array
);
1644 for (i
= 0; i
< ir
->type
->length
; i
++) {
1645 ir
->array_elements
[i
]->accept(this);
1646 src_reg
= this->result
;
1647 for (int j
= 0; j
< size
; j
++) {
1648 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1654 this->result
= temp_base
;
1658 if (ir
->type
->is_matrix()) {
1659 ir_to_mesa_src_reg mat
= get_temp(ir
->type
);
1660 ir_to_mesa_dst_reg mat_column
= ir_to_mesa_dst_reg_from_src(mat
);
1662 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1663 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
1664 values
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1666 src_reg
= ir_to_mesa_src_reg(PROGRAM_CONSTANT
, -1, NULL
);
1667 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1669 ir
->type
->vector_elements
,
1671 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, mat_column
, src_reg
);
1680 src_reg
.file
= PROGRAM_CONSTANT
;
1681 switch (ir
->type
->base_type
) {
1682 case GLSL_TYPE_FLOAT
:
1683 values
= &ir
->value
.f
[0];
1685 case GLSL_TYPE_UINT
:
1686 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1687 values
[i
] = ir
->value
.u
[i
];
1691 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1692 values
[i
] = ir
->value
.i
[i
];
1695 case GLSL_TYPE_BOOL
:
1696 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1697 values
[i
] = ir
->value
.b
[i
];
1701 assert(!"Non-float/uint/int/bool constant");
1704 this->result
= ir_to_mesa_src_reg(PROGRAM_CONSTANT
, -1, ir
->type
);
1705 this->result
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1707 ir
->type
->vector_elements
,
1708 &this->result
.swizzle
);
1712 ir_to_mesa_visitor::get_function_signature(ir_function_signature
*sig
)
1714 function_entry
*entry
;
1716 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
1717 entry
= (function_entry
*)iter
.get();
1719 if (entry
->sig
== sig
)
1723 entry
= talloc(mem_ctx
, function_entry
);
1725 entry
->sig_id
= this->next_signature_id
++;
1726 entry
->bgn_inst
= NULL
;
1728 /* Allocate storage for all the parameters. */
1729 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1730 ir_variable
*param
= (ir_variable
*)iter
.get();
1731 variable_storage
*storage
;
1733 storage
= find_variable_storage(param
);
1736 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
1738 this->variables
.push_tail(storage
);
1740 this->next_temp
+= type_size(param
->type
);
1743 if (!sig
->return_type
->is_void()) {
1744 entry
->return_reg
= get_temp(sig
->return_type
);
1746 entry
->return_reg
= ir_to_mesa_undef
;
1749 this->function_signatures
.push_tail(entry
);
1754 ir_to_mesa_visitor::visit(ir_call
*ir
)
1756 ir_to_mesa_instruction
*call_inst
;
1757 ir_function_signature
*sig
= ir
->get_callee();
1758 function_entry
*entry
= get_function_signature(sig
);
1761 /* Process in parameters. */
1762 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
1763 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1764 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1765 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1767 if (param
->mode
== ir_var_in
||
1768 param
->mode
== ir_var_inout
) {
1769 variable_storage
*storage
= find_variable_storage(param
);
1772 param_rval
->accept(this);
1773 ir_to_mesa_src_reg r
= this->result
;
1775 ir_to_mesa_dst_reg l
;
1776 l
.file
= storage
->file
;
1777 l
.index
= storage
->index
;
1779 l
.writemask
= WRITEMASK_XYZW
;
1780 l
.cond_mask
= COND_TR
;
1782 for (i
= 0; i
< type_size(param
->type
); i
++) {
1783 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1791 assert(!sig_iter
.has_next());
1793 /* Emit call instruction */
1794 call_inst
= ir_to_mesa_emit_op1(ir
, OPCODE_CAL
,
1795 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
1796 call_inst
->function
= entry
;
1798 /* Process out parameters. */
1799 sig_iter
= sig
->parameters
.iterator();
1800 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1801 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1802 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1804 if (param
->mode
== ir_var_out
||
1805 param
->mode
== ir_var_inout
) {
1806 variable_storage
*storage
= find_variable_storage(param
);
1809 ir_to_mesa_src_reg r
;
1810 r
.file
= storage
->file
;
1811 r
.index
= storage
->index
;
1813 r
.swizzle
= SWIZZLE_NOOP
;
1816 param_rval
->accept(this);
1817 ir_to_mesa_dst_reg l
= ir_to_mesa_dst_reg_from_src(this->result
);
1819 for (i
= 0; i
< type_size(param
->type
); i
++) {
1820 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1828 assert(!sig_iter
.has_next());
1830 /* Process return value. */
1831 this->result
= entry
->return_reg
;
1834 class get_sampler_name
: public ir_hierarchical_visitor
1837 get_sampler_name(ir_to_mesa_visitor
*mesa
, ir_dereference
*last
)
1839 this->mem_ctx
= mesa
->mem_ctx
;
1846 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
1848 this->name
= ir
->var
->name
;
1849 return visit_continue
;
1852 virtual ir_visitor_status
visit_leave(ir_dereference_record
*ir
)
1854 this->name
= talloc_asprintf(mem_ctx
, "%s.%s", name
, ir
->field
);
1855 return visit_continue
;
1858 virtual ir_visitor_status
visit_leave(ir_dereference_array
*ir
)
1860 ir_constant
*index
= ir
->array_index
->as_constant();
1864 i
= index
->value
.i
[0];
1866 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
1867 * while GLSL 1.30 requires that the array indices be
1868 * constant integer expressions. We don't expect any driver
1869 * to actually work with a really variable array index, so
1870 * all that would work would be an unrolled loop counter that ends
1871 * up being constant above.
1873 mesa
->shader_program
->InfoLog
=
1874 talloc_asprintf_append(mesa
->shader_program
->InfoLog
,
1875 "warning: Variable sampler array index "
1876 "unsupported.\nThis feature of the language "
1877 "was removed in GLSL 1.20 and is unlikely "
1878 "to be supported for 1.10 in Mesa.\n");
1882 this->name
= talloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
1886 return visit_continue
;
1889 ir_to_mesa_visitor
*mesa
;
1893 ir_dereference
*last
;
1897 ir_to_mesa_visitor::get_sampler_uniform_value(ir_dereference
*sampler
)
1899 get_sampler_name
getname(this, sampler
);
1901 sampler
->accept(&getname
);
1903 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
1907 fail_link(this->shader_program
,
1908 "failed to find sampler named %s.\n", getname
.name
);
1912 index
+= getname
.offset
;
1914 return this->prog
->Parameters
->ParameterValues
[index
][0];
1918 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1920 ir_to_mesa_src_reg result_src
, coord
, lod_info
, projector
;
1921 ir_to_mesa_dst_reg result_dst
, coord_dst
;
1922 ir_to_mesa_instruction
*inst
= NULL
;
1923 prog_opcode opcode
= OPCODE_NOP
;
1925 ir
->coordinate
->accept(this);
1927 /* Put our coords in a temp. We'll need to modify them for shadow,
1928 * projection, or LOD, so the only case we'd use it as is is if
1929 * we're doing plain old texturing. Mesa IR optimization should
1930 * handle cleaning up our mess in that case.
1932 coord
= get_temp(glsl_type::vec4_type
);
1933 coord_dst
= ir_to_mesa_dst_reg_from_src(coord
);
1934 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
,
1937 if (ir
->projector
) {
1938 ir
->projector
->accept(this);
1939 projector
= this->result
;
1942 /* Storage for our result. Ideally for an assignment we'd be using
1943 * the actual storage for the result here, instead.
1945 result_src
= get_temp(glsl_type::vec4_type
);
1946 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
1950 opcode
= OPCODE_TEX
;
1953 opcode
= OPCODE_TXB
;
1954 ir
->lod_info
.bias
->accept(this);
1955 lod_info
= this->result
;
1958 opcode
= OPCODE_TXL
;
1959 ir
->lod_info
.lod
->accept(this);
1960 lod_info
= this->result
;
1964 assert(!"GLSL 1.30 features unsupported");
1968 if (ir
->projector
) {
1969 if (opcode
== OPCODE_TEX
) {
1970 /* Slot the projector in as the last component of the coord. */
1971 coord_dst
.writemask
= WRITEMASK_W
;
1972 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, projector
);
1973 coord_dst
.writemask
= WRITEMASK_XYZW
;
1974 opcode
= OPCODE_TXP
;
1976 ir_to_mesa_src_reg coord_w
= coord
;
1977 coord_w
.swizzle
= SWIZZLE_WWWW
;
1979 /* For the other TEX opcodes there's no projective version
1980 * since the last slot is taken up by lod info. Do the
1981 * projective divide now.
1983 coord_dst
.writemask
= WRITEMASK_W
;
1984 ir_to_mesa_emit_op1(ir
, OPCODE_RCP
, coord_dst
, projector
);
1986 coord_dst
.writemask
= WRITEMASK_XYZ
;
1987 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, coord_dst
, coord
, coord_w
);
1989 coord_dst
.writemask
= WRITEMASK_XYZW
;
1990 coord
.swizzle
= SWIZZLE_XYZW
;
1994 if (ir
->shadow_comparitor
) {
1995 /* Slot the shadow value in as the second to last component of the
1998 ir
->shadow_comparitor
->accept(this);
1999 coord_dst
.writemask
= WRITEMASK_Z
;
2000 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, this->result
);
2001 coord_dst
.writemask
= WRITEMASK_XYZW
;
2004 if (opcode
== OPCODE_TXL
|| opcode
== OPCODE_TXB
) {
2005 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2006 coord_dst
.writemask
= WRITEMASK_W
;
2007 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, lod_info
);
2008 coord_dst
.writemask
= WRITEMASK_XYZW
;
2011 inst
= ir_to_mesa_emit_op1(ir
, opcode
, result_dst
, coord
);
2013 if (ir
->shadow_comparitor
)
2014 inst
->tex_shadow
= GL_TRUE
;
2016 inst
->sampler
= get_sampler_uniform_value(ir
->sampler
);
2018 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2020 switch (sampler_type
->sampler_dimensionality
) {
2021 case GLSL_SAMPLER_DIM_1D
:
2022 inst
->tex_target
= (sampler_type
->sampler_array
)
2023 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2025 case GLSL_SAMPLER_DIM_2D
:
2026 inst
->tex_target
= (sampler_type
->sampler_array
)
2027 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2029 case GLSL_SAMPLER_DIM_3D
:
2030 inst
->tex_target
= TEXTURE_3D_INDEX
;
2032 case GLSL_SAMPLER_DIM_CUBE
:
2033 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2035 case GLSL_SAMPLER_DIM_RECT
:
2036 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2038 case GLSL_SAMPLER_DIM_BUF
:
2039 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2042 assert(!"Should not get here.");
2045 this->result
= result_src
;
2049 ir_to_mesa_visitor::visit(ir_return
*ir
)
2051 if (ir
->get_value()) {
2052 ir_to_mesa_dst_reg l
;
2055 assert(current_function
);
2057 ir
->get_value()->accept(this);
2058 ir_to_mesa_src_reg r
= this->result
;
2060 l
= ir_to_mesa_dst_reg_from_src(current_function
->return_reg
);
2062 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2063 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
2069 ir_to_mesa_emit_op0(ir
, OPCODE_RET
);
2073 ir_to_mesa_visitor::visit(ir_discard
*ir
)
2075 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2077 assert(ir
->condition
== NULL
); /* FINISHME */
2079 ir_to_mesa_emit_op0(ir
, OPCODE_KIL_NV
);
2080 fp
->UsesKill
= GL_TRUE
;
2084 ir_to_mesa_visitor::visit(ir_if
*ir
)
2086 ir_to_mesa_instruction
*cond_inst
, *if_inst
, *else_inst
= NULL
;
2087 ir_to_mesa_instruction
*prev_inst
;
2089 prev_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2091 ir
->condition
->accept(this);
2092 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2094 if (ctx
->Shader
.EmitCondCodes
) {
2095 cond_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2097 /* See if we actually generated any instruction for generating
2098 * the condition. If not, then cook up a move to a temp so we
2099 * have something to set cond_update on.
2101 if (cond_inst
== prev_inst
) {
2102 ir_to_mesa_src_reg temp
= get_temp(glsl_type::bool_type
);
2103 cond_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_MOV
,
2104 ir_to_mesa_dst_reg_from_src(temp
),
2107 cond_inst
->cond_update
= GL_TRUE
;
2109 if_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_IF
);
2110 if_inst
->dst_reg
.cond_mask
= COND_NE
;
2112 if_inst
= ir_to_mesa_emit_op1(ir
->condition
,
2113 OPCODE_IF
, ir_to_mesa_undef_dst
,
2117 this->instructions
.push_tail(if_inst
);
2119 visit_exec_list(&ir
->then_instructions
, this);
2121 if (!ir
->else_instructions
.is_empty()) {
2122 else_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_ELSE
);
2123 visit_exec_list(&ir
->else_instructions
, this);
2126 if_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_ENDIF
,
2127 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
2130 ir_to_mesa_visitor::ir_to_mesa_visitor()
2132 result
.file
= PROGRAM_UNDEFINED
;
2134 next_signature_id
= 1;
2135 current_function
= NULL
;
2136 mem_ctx
= talloc_new(NULL
);
2139 ir_to_mesa_visitor::~ir_to_mesa_visitor()
2141 talloc_free(mem_ctx
);
2144 static struct prog_src_register
2145 mesa_src_reg_from_ir_src_reg(ir_to_mesa_src_reg reg
)
2147 struct prog_src_register mesa_reg
;
2149 mesa_reg
.File
= reg
.file
;
2150 assert(reg
.index
< (1 << INST_INDEX_BITS
) - 1);
2151 mesa_reg
.Index
= reg
.index
;
2152 mesa_reg
.Swizzle
= reg
.swizzle
;
2153 mesa_reg
.RelAddr
= reg
.reladdr
!= NULL
;
2154 mesa_reg
.Negate
= reg
.negate
;
2156 mesa_reg
.HasIndex2
= GL_FALSE
;
2157 mesa_reg
.RelAddr2
= 0;
2158 mesa_reg
.Index2
= 0;
2164 set_branchtargets(ir_to_mesa_visitor
*v
,
2165 struct prog_instruction
*mesa_instructions
,
2166 int num_instructions
)
2168 int if_count
= 0, loop_count
= 0;
2169 int *if_stack
, *loop_stack
;
2170 int if_stack_pos
= 0, loop_stack_pos
= 0;
2173 for (i
= 0; i
< num_instructions
; i
++) {
2174 switch (mesa_instructions
[i
].Opcode
) {
2178 case OPCODE_BGNLOOP
:
2183 mesa_instructions
[i
].BranchTarget
= -1;
2190 if_stack
= talloc_zero_array(v
->mem_ctx
, int, if_count
);
2191 loop_stack
= talloc_zero_array(v
->mem_ctx
, int, loop_count
);
2193 for (i
= 0; i
< num_instructions
; i
++) {
2194 switch (mesa_instructions
[i
].Opcode
) {
2196 if_stack
[if_stack_pos
] = i
;
2200 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2201 if_stack
[if_stack_pos
- 1] = i
;
2204 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2207 case OPCODE_BGNLOOP
:
2208 loop_stack
[loop_stack_pos
] = i
;
2211 case OPCODE_ENDLOOP
:
2213 /* Rewrite any breaks/conts at this nesting level (haven't
2214 * already had a BranchTarget assigned) to point to the end
2217 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
2218 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
2219 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
2220 if (mesa_instructions
[j
].BranchTarget
== -1) {
2221 mesa_instructions
[j
].BranchTarget
= i
;
2225 /* The loop ends point at each other. */
2226 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
2227 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
2230 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
2231 function_entry
*entry
= (function_entry
*)iter
.get();
2233 if (entry
->sig_id
== mesa_instructions
[i
].BranchTarget
) {
2234 mesa_instructions
[i
].BranchTarget
= entry
->inst
;
2246 print_program(struct prog_instruction
*mesa_instructions
,
2247 ir_instruction
**mesa_instruction_annotation
,
2248 int num_instructions
)
2250 ir_instruction
*last_ir
= NULL
;
2254 for (i
= 0; i
< num_instructions
; i
++) {
2255 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
2256 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
2258 fprintf(stdout
, "%3d: ", i
);
2260 if (last_ir
!= ir
&& ir
) {
2263 for (j
= 0; j
< indent
; j
++) {
2264 fprintf(stdout
, " ");
2270 fprintf(stdout
, " "); /* line number spacing. */
2273 indent
= _mesa_fprint_instruction_opt(stdout
, mesa_inst
, indent
,
2274 PROG_PRINT_DEBUG
, NULL
);
2279 count_resources(struct gl_program
*prog
)
2283 prog
->SamplersUsed
= 0;
2285 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
2286 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
2288 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
2289 prog
->SamplerTargets
[inst
->TexSrcUnit
] =
2290 (gl_texture_index
)inst
->TexSrcTarget
;
2291 prog
->SamplersUsed
|= 1 << inst
->TexSrcUnit
;
2292 if (inst
->TexShadow
) {
2293 prog
->ShadowSamplers
|= 1 << inst
->TexSrcUnit
;
2298 _mesa_update_shader_textures_used(prog
);
2301 struct uniform_sort
{
2302 struct gl_uniform
*u
;
2306 /* The shader_program->Uniforms list is almost sorted in increasing
2307 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2308 * uniforms shared between targets. We need to add parameters in
2309 * increasing order for the targets.
2312 sort_uniforms(const void *a
, const void *b
)
2314 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2315 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2317 return u1
->pos
- u2
->pos
;
2320 /* Add the uniforms to the parameters. The linker chose locations
2321 * in our parameters lists (which weren't created yet), which the
2322 * uniforms code will use to poke values into our parameters list
2323 * when uniforms are updated.
2326 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2327 struct gl_shader
*shader
,
2328 struct gl_program
*prog
)
2331 unsigned int next_sampler
= 0, num_uniforms
= 0;
2332 struct uniform_sort
*sorted_uniforms
;
2334 sorted_uniforms
= talloc_array(NULL
, struct uniform_sort
,
2335 shader_program
->Uniforms
->NumUniforms
);
2337 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2338 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2339 int parameter_index
= -1;
2341 switch (shader
->Type
) {
2342 case GL_VERTEX_SHADER
:
2343 parameter_index
= uniform
->VertPos
;
2345 case GL_FRAGMENT_SHADER
:
2346 parameter_index
= uniform
->FragPos
;
2348 case GL_GEOMETRY_SHADER
:
2349 parameter_index
= uniform
->GeomPos
;
2353 /* Only add uniforms used in our target. */
2354 if (parameter_index
!= -1) {
2355 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2356 sorted_uniforms
[num_uniforms
].u
= uniform
;
2361 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2364 for (i
= 0; i
< num_uniforms
; i
++) {
2365 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2366 int parameter_index
= sorted_uniforms
[i
].pos
;
2367 const glsl_type
*type
= uniform
->Type
;
2370 if (type
->is_vector() ||
2371 type
->is_scalar()) {
2372 size
= type
->vector_elements
;
2374 size
= type_size(type
) * 4;
2377 gl_register_file file
;
2378 if (type
->is_sampler() ||
2379 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2380 file
= PROGRAM_SAMPLER
;
2382 file
= PROGRAM_UNIFORM
;
2385 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2389 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2390 uniform
->Name
, size
, type
->gl_type
,
2393 /* Sampler uniform values are stored in prog->SamplerUnits,
2394 * and the entry in that array is selected by this index we
2395 * store in ParameterValues[].
2397 if (file
== PROGRAM_SAMPLER
) {
2398 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2399 prog
->Parameters
->ParameterValues
[index
+ j
][0] = next_sampler
++;
2402 /* The location chosen in the Parameters list here (returned
2403 * from _mesa_add_uniform) has to match what the linker chose.
2405 if (index
!= parameter_index
) {
2406 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2407 "failed (%d vs %d)\n",
2408 uniform
->Name
, index
, parameter_index
);
2413 talloc_free(sorted_uniforms
);
2417 set_uniform_initializer(GLcontext
*ctx
, void *mem_ctx
,
2418 struct gl_shader_program
*shader_program
,
2419 const char *name
, const glsl_type
*type
,
2422 if (type
->is_record()) {
2423 ir_constant
*field_constant
;
2425 field_constant
= (ir_constant
*)val
->components
.get_head();
2427 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2428 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2429 const char *field_name
= talloc_asprintf(mem_ctx
, "%s.%s", name
,
2430 type
->fields
.structure
[i
].name
);
2431 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2432 field_type
, field_constant
);
2433 field_constant
= (ir_constant
*)field_constant
->next
;
2438 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2441 fail_link(shader_program
,
2442 "Couldn't find uniform for initializer %s\n", name
);
2446 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2447 ir_constant
*element
;
2448 const glsl_type
*element_type
;
2449 if (type
->is_array()) {
2450 element
= val
->array_elements
[i
];
2451 element_type
= type
->fields
.array
;
2454 element_type
= type
;
2459 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2460 int *conv
= talloc_array(mem_ctx
, int, element_type
->components());
2461 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2462 conv
[j
] = element
->value
.b
[j
];
2464 values
= (void *)conv
;
2465 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2466 element_type
->vector_elements
,
2469 values
= &element
->value
;
2472 if (element_type
->is_matrix()) {
2473 _mesa_uniform_matrix(ctx
, shader_program
,
2474 element_type
->matrix_columns
,
2475 element_type
->vector_elements
,
2476 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2477 loc
+= element_type
->matrix_columns
;
2479 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2480 values
, element_type
->gl_type
);
2481 loc
+= type_size(element_type
);
2487 set_uniform_initializers(GLcontext
*ctx
,
2488 struct gl_shader_program
*shader_program
)
2490 void *mem_ctx
= NULL
;
2492 for (unsigned int i
= 0; i
< shader_program
->_NumLinkedShaders
; i
++) {
2493 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2494 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2495 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2496 ir_variable
*var
= ir
->as_variable();
2498 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2502 mem_ctx
= talloc_new(NULL
);
2504 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2505 var
->type
, var
->constant_value
);
2509 talloc_free(mem_ctx
);
2513 get_mesa_program(GLcontext
*ctx
, struct gl_shader_program
*shader_program
,
2514 struct gl_shader
*shader
)
2516 ir_to_mesa_visitor v
;
2517 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
2518 ir_instruction
**mesa_instruction_annotation
;
2520 struct gl_program
*prog
;
2522 const char *target_string
;
2525 switch (shader
->Type
) {
2526 case GL_VERTEX_SHADER
:
2527 target
= GL_VERTEX_PROGRAM_ARB
;
2528 target_string
= "vertex";
2530 case GL_FRAGMENT_SHADER
:
2531 target
= GL_FRAGMENT_PROGRAM_ARB
;
2532 target_string
= "fragment";
2535 assert(!"should not be reached");
2539 validate_ir_tree(shader
->ir
);
2541 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
2544 prog
->Parameters
= _mesa_new_parameter_list();
2545 prog
->Varying
= _mesa_new_parameter_list();
2546 prog
->Attributes
= _mesa_new_parameter_list();
2549 v
.shader_program
= shader_program
;
2551 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
2553 /* Emit Mesa IR for main(). */
2554 visit_exec_list(shader
->ir
, &v
);
2555 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_END
);
2557 /* Now emit bodies for any functions that were used. */
2559 progress
= GL_FALSE
;
2561 foreach_iter(exec_list_iterator
, iter
, v
.function_signatures
) {
2562 function_entry
*entry
= (function_entry
*)iter
.get();
2564 if (!entry
->bgn_inst
) {
2565 v
.current_function
= entry
;
2567 entry
->bgn_inst
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_BGNSUB
);
2568 entry
->bgn_inst
->function
= entry
;
2570 visit_exec_list(&entry
->sig
->body
, &v
);
2572 ir_to_mesa_instruction
*last
;
2573 last
= (ir_to_mesa_instruction
*)v
.instructions
.get_tail();
2574 if (last
->op
!= OPCODE_RET
)
2575 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_RET
);
2577 ir_to_mesa_instruction
*end
;
2578 end
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_ENDSUB
);
2579 end
->function
= entry
;
2586 prog
->NumTemporaries
= v
.next_temp
;
2588 int num_instructions
= 0;
2589 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2594 (struct prog_instruction
*)calloc(num_instructions
,
2595 sizeof(*mesa_instructions
));
2596 mesa_instruction_annotation
= talloc_array(v
.mem_ctx
, ir_instruction
*,
2599 mesa_inst
= mesa_instructions
;
2601 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2602 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2604 mesa_inst
->Opcode
= inst
->op
;
2605 mesa_inst
->CondUpdate
= inst
->cond_update
;
2606 mesa_inst
->DstReg
.File
= inst
->dst_reg
.file
;
2607 mesa_inst
->DstReg
.Index
= inst
->dst_reg
.index
;
2608 mesa_inst
->DstReg
.CondMask
= inst
->dst_reg
.cond_mask
;
2609 mesa_inst
->DstReg
.WriteMask
= inst
->dst_reg
.writemask
;
2610 mesa_inst
->DstReg
.RelAddr
= inst
->dst_reg
.reladdr
!= NULL
;
2611 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[0]);
2612 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[1]);
2613 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[2]);
2614 mesa_inst
->TexSrcUnit
= inst
->sampler
;
2615 mesa_inst
->TexSrcTarget
= inst
->tex_target
;
2616 mesa_inst
->TexShadow
= inst
->tex_shadow
;
2617 mesa_instruction_annotation
[i
] = inst
->ir
;
2619 /* Set IndirectRegisterFiles. */
2620 if (mesa_inst
->DstReg
.RelAddr
)
2621 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->DstReg
.File
;
2623 for (unsigned src
= 0; src
< 3; src
++)
2624 if (mesa_inst
->SrcReg
[src
].RelAddr
)
2625 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->SrcReg
[src
].File
;
2627 if (ctx
->Shader
.EmitNoIfs
&& mesa_inst
->Opcode
== OPCODE_IF
) {
2628 fail_link(shader_program
, "Couldn't flatten if statement\n");
2631 switch (mesa_inst
->Opcode
) {
2633 inst
->function
->inst
= i
;
2634 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2637 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2640 mesa_inst
->BranchTarget
= inst
->function
->sig_id
; /* rewritten later */
2643 prog
->NumAddressRegs
= 1;
2653 set_branchtargets(&v
, mesa_instructions
, num_instructions
);
2655 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2657 printf("GLSL IR for linked %s program %d:\n", target_string
,
2658 shader_program
->Name
);
2659 _mesa_print_ir(shader
->ir
, NULL
);
2662 printf("Mesa IR for linked %s program %d:\n", target_string
,
2663 shader_program
->Name
);
2664 print_program(mesa_instructions
, mesa_instruction_annotation
,
2668 prog
->Instructions
= mesa_instructions
;
2669 prog
->NumInstructions
= num_instructions
;
2671 do_set_program_inouts(shader
->ir
, prog
);
2672 count_resources(prog
);
2674 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
2676 if ((ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2677 _mesa_optimize_program(ctx
, prog
);
2685 _mesa_ir_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
2687 assert(shader
->CompileStatus
);
2694 _mesa_ir_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
2696 assert(prog
->LinkStatus
);
2698 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2700 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
2706 do_mat_op_to_vec(ir
);
2707 do_mod_to_fract(ir
);
2708 do_div_to_mul_rcp(ir
);
2709 do_explog_to_explog2(ir
);
2711 progress
= do_common_optimization(ir
, true) || progress
;
2713 if (ctx
->Shader
.EmitNoIfs
)
2714 progress
= do_if_to_cond_assign(ir
) || progress
;
2716 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
2719 validate_ir_tree(ir
);
2722 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2723 struct gl_program
*linked_prog
;
2726 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
2728 switch (prog
->_LinkedShaders
[i
]->Type
) {
2729 case GL_VERTEX_SHADER
:
2730 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
2731 (struct gl_vertex_program
*)linked_prog
);
2732 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
2735 case GL_FRAGMENT_SHADER
:
2736 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
2737 (struct gl_fragment_program
*)linked_prog
);
2738 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
2745 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
2752 _mesa_glsl_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
2754 struct _mesa_glsl_parse_state
*state
=
2755 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
2757 const char *source
= shader
->Source
;
2758 /* Check if the user called glCompileShader without first calling
2759 * glShaderSource. This should fail to compile, but not raise a GL_ERROR.
2761 if (source
== NULL
) {
2762 shader
->CompileStatus
= GL_FALSE
;
2766 state
->error
= preprocess(state
, &source
, &state
->info_log
,
2769 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2770 printf("GLSL source for shader %d:\n", shader
->Name
);
2771 printf("%s\n", shader
->Source
);
2774 if (!state
->error
) {
2775 _mesa_glsl_lexer_ctor(state
, source
);
2776 _mesa_glsl_parse(state
);
2777 _mesa_glsl_lexer_dtor(state
);
2780 talloc_free(shader
->ir
);
2781 shader
->ir
= new(shader
) exec_list
;
2782 if (!state
->error
&& !state
->translation_unit
.is_empty())
2783 _mesa_ast_to_hir(shader
->ir
, state
);
2785 if (!state
->error
&& !shader
->ir
->is_empty()) {
2786 validate_ir_tree(shader
->ir
);
2788 /* Do some optimization at compile time to reduce shader IR size
2789 * and reduce later work if the same shader is linked multiple times
2791 while (do_common_optimization(shader
->ir
, false))
2794 validate_ir_tree(shader
->ir
);
2797 shader
->symbols
= state
->symbols
;
2799 shader
->CompileStatus
= !state
->error
;
2800 shader
->InfoLog
= state
->info_log
;
2801 shader
->Version
= state
->language_version
;
2802 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
2803 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
2804 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
2806 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2807 _mesa_write_shader_to_file(shader
);
2810 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2811 if (shader
->CompileStatus
) {
2812 printf("GLSL IR for shader %d:\n", shader
->Name
);
2813 _mesa_print_ir(shader
->ir
, NULL
);
2816 printf("GLSL shader %d failed to compile.\n", shader
->Name
);
2818 if (shader
->InfoLog
&& shader
->InfoLog
[0] != 0) {
2819 printf("GLSL shader %d info log:\n", shader
->Name
);
2820 printf("%s\n", shader
->InfoLog
);
2824 /* Retain any live IR, but trash the rest. */
2825 reparent_ir(shader
->ir
, shader
->ir
);
2829 if (shader
->CompileStatus
) {
2830 if (!ctx
->Driver
.CompileShader(ctx
, shader
))
2831 shader
->CompileStatus
= GL_FALSE
;
2836 _mesa_glsl_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
2840 _mesa_clear_shader_program_data(ctx
, prog
);
2842 prog
->LinkStatus
= GL_TRUE
;
2844 for (i
= 0; i
< prog
->NumShaders
; i
++) {
2845 if (!prog
->Shaders
[i
]->CompileStatus
) {
2846 fail_link(prog
, "linking with uncompiled shader");
2847 prog
->LinkStatus
= GL_FALSE
;
2851 prog
->Varying
= _mesa_new_parameter_list();
2852 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
2853 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
2855 if (prog
->LinkStatus
) {
2856 link_shaders(ctx
, prog
);
2859 if (prog
->LinkStatus
) {
2860 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
2861 prog
->LinkStatus
= GL_FALSE
;
2865 set_uniform_initializers(ctx
, prog
);
2867 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2868 if (!prog
->LinkStatus
) {
2869 printf("GLSL shader program %d failed to link\n", prog
->Name
);
2872 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
2873 printf("GLSL shader program %d info log:\n", prog
->Name
);
2874 printf("%s\n", prog
->InfoLog
);