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,
35 #include "ir_visitor.h"
36 #include "ir_print_visitor.h"
37 #include "ir_expression_flattening.h"
38 #include "glsl_types.h"
39 #include "glsl_parser_extras.h"
40 #include "../glsl/program.h"
41 #include "ir_optimization.h"
45 #include "main/mtypes.h"
46 #include "shader/prog_instruction.h"
47 #include "shader/prog_print.h"
48 #include "shader/program.h"
49 #include "shader/prog_uniform.h"
50 #include "shader/prog_parameter.h"
51 #include "shader/shader_api.h"
55 * This struct is a corresponding struct to Mesa prog_src_register, with
58 typedef struct ir_to_mesa_src_reg
{
59 int file
; /**< PROGRAM_* from Mesa */
60 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
61 GLuint swizzle
; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
62 int negate
; /**< NEGATE_XYZW mask from mesa */
63 bool reladdr
; /**< Register index should be offset by address reg. */
66 typedef struct ir_to_mesa_dst_reg
{
67 int file
; /**< PROGRAM_* from Mesa */
68 int index
; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
69 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
72 extern ir_to_mesa_src_reg ir_to_mesa_undef
;
74 class ir_to_mesa_instruction
: public exec_node
{
77 ir_to_mesa_dst_reg dst_reg
;
78 ir_to_mesa_src_reg src_reg
[3];
79 /** Pointer to the ir source this tree came from for debugging */
83 class temp_entry
: public exec_node
{
85 temp_entry(ir_variable
*var
, int file
, int index
)
86 : file(file
), index(index
), var(var
)
93 ir_variable
*var
; /* variable that maps to this, if any */
96 class ir_to_mesa_visitor
: public ir_visitor
{
101 struct gl_program
*prog
;
105 temp_entry
*find_variable_storage(ir_variable
*var
);
107 ir_to_mesa_src_reg
get_temp(const glsl_type
*type
);
109 struct ir_to_mesa_src_reg
src_reg_for_float(float val
);
112 * \name Visit methods
114 * As typical for the visitor pattern, there must be one \c visit method for
115 * each concrete subclass of \c ir_instruction. Virtual base classes within
116 * the hierarchy should not have \c visit methods.
119 virtual void visit(ir_variable
*);
120 virtual void visit(ir_loop
*);
121 virtual void visit(ir_loop_jump
*);
122 virtual void visit(ir_function_signature
*);
123 virtual void visit(ir_function
*);
124 virtual void visit(ir_expression
*);
125 virtual void visit(ir_swizzle
*);
126 virtual void visit(ir_dereference_variable
*);
127 virtual void visit(ir_dereference_array
*);
128 virtual void visit(ir_dereference_record
*);
129 virtual void visit(ir_assignment
*);
130 virtual void visit(ir_constant
*);
131 virtual void visit(ir_call
*);
132 virtual void visit(ir_return
*);
133 virtual void visit(ir_texture
*);
134 virtual void visit(ir_if
*);
137 struct ir_to_mesa_src_reg result
;
139 /** List of temp_entry */
140 exec_list variable_storage
;
142 /** List of ir_to_mesa_instruction */
143 exec_list instructions
;
145 ir_to_mesa_instruction
*ir_to_mesa_emit_op1(ir_instruction
*ir
,
147 ir_to_mesa_dst_reg dst
,
148 ir_to_mesa_src_reg src0
);
150 ir_to_mesa_instruction
*ir_to_mesa_emit_op2(ir_instruction
*ir
,
152 ir_to_mesa_dst_reg dst
,
153 ir_to_mesa_src_reg src0
,
154 ir_to_mesa_src_reg src1
);
156 ir_to_mesa_instruction
*ir_to_mesa_emit_op3(ir_instruction
*ir
,
158 ir_to_mesa_dst_reg dst
,
159 ir_to_mesa_src_reg src0
,
160 ir_to_mesa_src_reg src1
,
161 ir_to_mesa_src_reg src2
);
163 void ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
165 ir_to_mesa_dst_reg dst
,
166 ir_to_mesa_src_reg src0
);
171 ir_to_mesa_src_reg ir_to_mesa_undef
= {
172 PROGRAM_UNDEFINED
, 0, SWIZZLE_NOOP
, NEGATE_NONE
, false,
175 ir_to_mesa_dst_reg ir_to_mesa_undef_dst
= {
176 PROGRAM_UNDEFINED
, 0, SWIZZLE_NOOP
179 ir_to_mesa_dst_reg ir_to_mesa_address_reg
= {
180 PROGRAM_ADDRESS
, 0, WRITEMASK_X
183 static int swizzle_for_size(int size
)
185 int size_swizzles
[4] = {
186 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
187 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
188 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
189 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
192 return size_swizzles
[size
- 1];
195 /* This list should match up with builtin_variables.h */
196 static const struct {
200 } builtin_var_to_mesa_reg
[] = {
202 {"gl_Position", PROGRAM_OUTPUT
, VERT_RESULT_HPOS
},
203 {"gl_PointSize", PROGRAM_OUTPUT
, VERT_RESULT_PSIZ
},
206 {"gl_FragCoord", PROGRAM_INPUT
, FRAG_ATTRIB_WPOS
},
207 {"gl_FrontFacing", PROGRAM_INPUT
, FRAG_ATTRIB_FACE
},
208 {"gl_FragColor", PROGRAM_OUTPUT
, FRAG_ATTRIB_COL0
},
209 {"gl_FragDepth", PROGRAM_UNDEFINED
, FRAG_ATTRIB_WPOS
}, /* FINISHME: WPOS.z */
211 /* 110_deprecated_fs */
212 {"gl_Color", PROGRAM_INPUT
, FRAG_ATTRIB_COL0
},
213 {"gl_SecondaryColor", PROGRAM_INPUT
, FRAG_ATTRIB_COL1
},
214 {"gl_FogFragCoord", PROGRAM_INPUT
, FRAG_ATTRIB_FOGC
},
215 {"gl_TexCoord", PROGRAM_INPUT
, FRAG_ATTRIB_TEX0
}, /* array */
217 /* 110_deprecated_vs */
218 {"gl_Vertex", PROGRAM_INPUT
, VERT_ATTRIB_POS
},
219 {"gl_Normal", PROGRAM_INPUT
, VERT_ATTRIB_NORMAL
},
220 {"gl_Color", PROGRAM_INPUT
, VERT_ATTRIB_COLOR0
},
221 {"gl_SecondaryColor", PROGRAM_INPUT
, VERT_ATTRIB_COLOR1
},
222 {"gl_MultiTexCoord0", PROGRAM_INPUT
, VERT_ATTRIB_TEX0
},
223 {"gl_MultiTexCoord1", PROGRAM_INPUT
, VERT_ATTRIB_TEX1
},
224 {"gl_MultiTexCoord2", PROGRAM_INPUT
, VERT_ATTRIB_TEX2
},
225 {"gl_MultiTexCoord3", PROGRAM_INPUT
, VERT_ATTRIB_TEX3
},
226 {"gl_MultiTexCoord4", PROGRAM_INPUT
, VERT_ATTRIB_TEX4
},
227 {"gl_MultiTexCoord5", PROGRAM_INPUT
, VERT_ATTRIB_TEX5
},
228 {"gl_MultiTexCoord6", PROGRAM_INPUT
, VERT_ATTRIB_TEX6
},
229 {"gl_MultiTexCoord7", PROGRAM_INPUT
, VERT_ATTRIB_TEX7
},
230 {"gl_TexCoord", PROGRAM_OUTPUT
, VERT_RESULT_TEX0
}, /* array */
231 {"gl_FogCoord", PROGRAM_INPUT
, VERT_RESULT_FOGC
},
232 /*{"gl_ClipVertex", PROGRAM_OUTPUT, VERT_ATTRIB_FOGC},*/ /* FINISHME */
233 {"gl_FrontColor", PROGRAM_OUTPUT
, VERT_RESULT_COL0
},
234 {"gl_BackColor", PROGRAM_OUTPUT
, VERT_RESULT_BFC0
},
235 {"gl_FrontSecondaryColor", PROGRAM_OUTPUT
, VERT_RESULT_COL1
},
236 {"gl_BackSecondaryColor", PROGRAM_OUTPUT
, VERT_RESULT_BFC1
},
237 {"gl_FogFragCoord", PROGRAM_OUTPUT
, VERT_RESULT_FOGC
},
240 /*{"gl_VertexID", PROGRAM_INPUT, VERT_ATTRIB_FOGC},*/ /* FINISHME */
242 {"gl_FragData", PROGRAM_OUTPUT
, FRAG_RESULT_DATA0
}, /* array */
245 ir_to_mesa_instruction
*
246 ir_to_mesa_visitor::ir_to_mesa_emit_op3(ir_instruction
*ir
,
248 ir_to_mesa_dst_reg dst
,
249 ir_to_mesa_src_reg src0
,
250 ir_to_mesa_src_reg src1
,
251 ir_to_mesa_src_reg src2
)
253 ir_to_mesa_instruction
*inst
= new(mem_ctx
) ir_to_mesa_instruction();
257 inst
->src_reg
[0] = src0
;
258 inst
->src_reg
[1] = src1
;
259 inst
->src_reg
[2] = src2
;
262 this->instructions
.push_tail(inst
);
268 ir_to_mesa_instruction
*
269 ir_to_mesa_visitor::ir_to_mesa_emit_op2(ir_instruction
*ir
,
271 ir_to_mesa_dst_reg dst
,
272 ir_to_mesa_src_reg src0
,
273 ir_to_mesa_src_reg src1
)
275 return ir_to_mesa_emit_op3(ir
, op
, dst
, src0
, src1
, ir_to_mesa_undef
);
278 ir_to_mesa_instruction
*
279 ir_to_mesa_visitor::ir_to_mesa_emit_op1(ir_instruction
*ir
,
281 ir_to_mesa_dst_reg dst
,
282 ir_to_mesa_src_reg src0
)
284 return ir_to_mesa_emit_op3(ir
, op
, dst
,
285 src0
, ir_to_mesa_undef
, ir_to_mesa_undef
);
288 inline ir_to_mesa_dst_reg
289 ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg reg
)
291 ir_to_mesa_dst_reg dst_reg
;
293 dst_reg
.file
= reg
.file
;
294 dst_reg
.index
= reg
.index
;
295 dst_reg
.writemask
= WRITEMASK_XYZW
;
301 * Emits Mesa scalar opcodes to produce unique answers across channels.
303 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
304 * channel determines the result across all channels. So to do a vec4
305 * of this operation, we want to emit a scalar per source channel used
306 * to produce dest channels.
309 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
311 ir_to_mesa_dst_reg dst
,
312 ir_to_mesa_src_reg src0
)
315 int done_mask
= ~dst
.writemask
;
317 /* Mesa RCP is a scalar operation splatting results to all channels,
318 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
321 for (i
= 0; i
< 4; i
++) {
322 GLuint this_mask
= (1 << i
);
323 ir_to_mesa_instruction
*inst
;
324 ir_to_mesa_src_reg src
= src0
;
326 if (done_mask
& this_mask
)
329 GLuint src_swiz
= GET_SWZ(src
.swizzle
, i
);
330 for (j
= i
+ 1; j
< 4; j
++) {
331 if (!(done_mask
& (1 << j
)) && GET_SWZ(src
.swizzle
, j
) == src_swiz
) {
332 this_mask
|= (1 << j
);
335 src
.swizzle
= MAKE_SWIZZLE4(src_swiz
, src_swiz
,
338 inst
= ir_to_mesa_emit_op1(ir
, op
,
341 inst
->dst_reg
.writemask
= this_mask
;
342 done_mask
|= this_mask
;
346 struct ir_to_mesa_src_reg
347 ir_to_mesa_visitor::src_reg_for_float(float val
)
349 ir_to_mesa_src_reg src_reg
;
351 src_reg
.file
= PROGRAM_CONSTANT
;
352 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
353 &val
, 1, &src_reg
.swizzle
);
359 * In the initial pass of codegen, we assign temporary numbers to
360 * intermediate results. (not SSA -- variable assignments will reuse
361 * storage). Actual register allocation for the Mesa VM occurs in a
362 * pass over the Mesa IR later.
365 ir_to_mesa_visitor::get_temp(const glsl_type
*type
)
367 ir_to_mesa_src_reg src_reg
;
371 assert(!type
->is_array());
373 src_reg
.file
= PROGRAM_TEMPORARY
;
374 src_reg
.index
= type
->matrix_columns
;
375 src_reg
.reladdr
= false;
377 for (i
= 0; i
< type
->vector_elements
; i
++)
380 swizzle
[i
] = type
->vector_elements
- 1;
381 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
382 swizzle
[2], swizzle
[3]);
388 type_size(const struct glsl_type
*type
)
393 switch (type
->base_type
) {
396 case GLSL_TYPE_FLOAT
:
398 if (type
->is_matrix()) {
399 return 4; /* FINISHME: Not all matrices are 4x4. */
401 /* Regardless of size of vector, it gets a vec4. This is bad
402 * packing for things like floats, but otherwise arrays become a
403 * mess. Hopefully a later pass over the code can pack scalars
404 * down if appropriate.
408 case GLSL_TYPE_ARRAY
:
409 return type_size(type
->fields
.array
) * type
->length
;
410 case GLSL_TYPE_STRUCT
:
412 for (i
= 0; i
< type
->length
; i
++) {
413 size
+= type_size(type
->fields
.structure
[i
].type
);
422 ir_to_mesa_visitor::find_variable_storage(ir_variable
*var
)
427 foreach_iter(exec_list_iterator
, iter
, this->variable_storage
) {
428 entry
= (temp_entry
*)iter
.get();
430 if (entry
->var
== var
)
438 ir_to_mesa_visitor::visit(ir_variable
*ir
)
444 ir_to_mesa_visitor::visit(ir_loop
*ir
)
448 assert(!ir
->increment
);
449 assert(!ir
->counter
);
451 ir_to_mesa_emit_op1(NULL
, OPCODE_BGNLOOP
,
452 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
454 visit_exec_list(&ir
->body_instructions
, this);
456 ir_to_mesa_emit_op1(NULL
, OPCODE_ENDLOOP
,
457 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
461 ir_to_mesa_visitor::visit(ir_loop_jump
*ir
)
464 case ir_loop_jump::jump_break
:
465 ir_to_mesa_emit_op1(NULL
, OPCODE_BRK
,
466 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
468 case ir_loop_jump::jump_continue
:
469 ir_to_mesa_emit_op1(NULL
, OPCODE_CONT
,
470 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
477 ir_to_mesa_visitor::visit(ir_function_signature
*ir
)
484 ir_to_mesa_visitor::visit(ir_function
*ir
)
486 /* Ignore function bodies other than main() -- we shouldn't see calls to
487 * them since they should all be inlined before we get to ir_to_mesa.
489 if (strcmp(ir
->name
, "main") == 0) {
490 const ir_function_signature
*sig
;
493 sig
= ir
->matching_signature(&empty
);
497 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
498 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
506 ir_to_mesa_visitor::visit(ir_expression
*ir
)
508 unsigned int operand
;
509 struct ir_to_mesa_src_reg op
[2];
510 struct ir_to_mesa_src_reg result_src
;
511 struct ir_to_mesa_dst_reg result_dst
;
512 const glsl_type
*vec4_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 4, 1);
513 const glsl_type
*vec3_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 3, 1);
514 const glsl_type
*vec2_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 2, 1);
516 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
517 this->result
.file
= PROGRAM_UNDEFINED
;
518 ir
->operands
[operand
]->accept(this);
519 if (this->result
.file
== PROGRAM_UNDEFINED
) {
521 printf("Failed to get tree for expression operand:\n");
522 ir
->operands
[operand
]->accept(&v
);
525 op
[operand
] = this->result
;
527 /* Only expression implemented for matrices yet */
528 assert(!ir
->operands
[operand
]->type
->is_matrix() ||
529 ir
->operation
== ir_binop_mul
);
532 this->result
.file
= PROGRAM_UNDEFINED
;
534 /* Storage for our result. Ideally for an assignment we'd be using
535 * the actual storage for the result here, instead.
537 result_src
= get_temp(ir
->type
);
538 /* convenience for the emit functions below. */
539 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
540 /* Limit writes to the channels that will be used by result_src later.
541 * This does limit this temp's use as a temporary for multi-instruction
544 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
546 switch (ir
->operation
) {
547 case ir_unop_logic_not
:
548 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
,
549 op
[0], src_reg_for_float(0.0));
552 op
[0].negate
= ~op
[0].negate
;
556 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_EXP
, result_dst
, op
[0]);
559 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_EX2
, result_dst
, op
[0]);
562 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LOG
, result_dst
, op
[0]);
565 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LG2
, result_dst
, op
[0]);
568 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_SIN
, result_dst
, op
[0]);
571 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_COS
, result_dst
, op
[0]);
574 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
, result_dst
, op
[0], op
[1]);
577 ir_to_mesa_emit_op2(ir
, OPCODE_SUB
, result_dst
, op
[0], op
[1]);
580 if (ir
->operands
[0]->type
->is_matrix() &&
581 !ir
->operands
[1]->type
->is_matrix()) {
582 if (ir
->operands
[1]->type
->is_scalar()) {
583 ir_to_mesa_dst_reg dst_column
= result_dst
;
584 ir_to_mesa_src_reg src_column
= op
[0];
585 for (int i
= 0; i
< ir
->operands
[0]->type
->matrix_columns
; i
++) {
586 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
587 dst_column
, src_column
, op
[1]);
592 ir_to_mesa_src_reg src_column
= op
[0];
593 ir_to_mesa_src_reg src_chan
= op
[1];
594 assert(!ir
->operands
[1]->type
->is_matrix() ||
595 !"FINISHME: matrix * matrix");
596 for (int i
= 0; i
< ir
->operands
[0]->type
->matrix_columns
; i
++) {
597 src_chan
.swizzle
= MAKE_SWIZZLE4(i
, i
, i
, i
);
599 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
600 result_dst
, src_column
, src_chan
);
602 ir_to_mesa_emit_op3(ir
, OPCODE_MAD
,
603 result_dst
, src_column
, src_chan
,
610 assert(!ir
->operands
[0]->type
->is_matrix());
611 assert(!ir
->operands
[1]->type
->is_matrix());
612 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, op
[0], op
[1]);
616 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RCP
, result_dst
, op
[1]);
617 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, op
[0], result_src
);
621 ir_to_mesa_emit_op2(ir
, OPCODE_SLT
, result_dst
, op
[0], op
[1]);
623 case ir_binop_greater
:
624 ir_to_mesa_emit_op2(ir
, OPCODE_SGT
, result_dst
, op
[0], op
[1]);
626 case ir_binop_lequal
:
627 ir_to_mesa_emit_op2(ir
, OPCODE_SLE
, result_dst
, op
[0], op
[1]);
629 case ir_binop_gequal
:
630 ir_to_mesa_emit_op2(ir
, OPCODE_SGE
, result_dst
, op
[0], op
[1]);
633 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
635 case ir_binop_logic_xor
:
636 case ir_binop_nequal
:
637 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
640 case ir_binop_logic_or
:
641 /* This could be a saturated add and skip the SNE. */
642 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
,
646 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
648 result_src
, src_reg_for_float(0.0));
651 case ir_binop_logic_and
:
652 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
653 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
659 if (ir
->operands
[0]->type
== vec4_type
) {
660 assert(ir
->operands
[1]->type
== vec4_type
);
661 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
,
664 } else if (ir
->operands
[0]->type
== vec3_type
) {
665 assert(ir
->operands
[1]->type
== vec3_type
);
666 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
,
669 } else if (ir
->operands
[0]->type
== vec2_type
) {
670 assert(ir
->operands
[1]->type
== vec2_type
);
671 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
,
677 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
678 ir_to_mesa_emit_op1(ir
, OPCODE_RCP
, result_dst
, result_src
);
681 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
684 /* Mesa IR lacks types, ints are stored as truncated floats. */
688 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
691 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
,
692 result_src
, src_reg_for_float(0.0));
695 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
698 op
[0].negate
= ~op
[0].negate
;
699 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
700 result_src
.negate
= ~result_src
.negate
;
703 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
706 ir_to_mesa_emit_op2(ir
, OPCODE_MIN
, result_dst
, op
[0], op
[1]);
709 ir_to_mesa_emit_op2(ir
, OPCODE_MAX
, result_dst
, op
[0], op
[1]);
713 printf("Failed to get tree for expression:\n");
719 this->result
= result_src
;
724 ir_to_mesa_visitor::visit(ir_swizzle
*ir
)
726 ir_to_mesa_src_reg src_reg
;
730 /* Note that this is only swizzles in expressions, not those on the left
731 * hand side of an assignment, which do write masking. See ir_assignment
735 ir
->val
->accept(this);
736 src_reg
= this->result
;
737 assert(src_reg
.file
!= PROGRAM_UNDEFINED
);
739 for (i
= 0; i
< 4; i
++) {
740 if (i
< ir
->type
->vector_elements
) {
743 swizzle
[i
] = ir
->mask
.x
;
746 swizzle
[i
] = ir
->mask
.y
;
749 swizzle
[i
] = ir
->mask
.z
;
752 swizzle
[i
] = ir
->mask
.w
;
756 /* If the type is smaller than a vec4, replicate the last
759 swizzle
[i
] = ir
->type
->vector_elements
- 1;
763 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0],
768 this->result
= src_reg
;
772 get_builtin_matrix_ref(void *mem_ctx
, struct gl_program
*prog
, ir_variable
*var
)
775 * NOTE: The ARB_vertex_program extension specified that matrices get
776 * loaded in registers in row-major order. With GLSL, we want column-
777 * major order. So, we need to transpose all matrices here...
779 static const struct {
784 { "gl_ModelViewMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
},
785 { "gl_ModelViewMatrixInverse", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
},
786 { "gl_ModelViewMatrixTranspose", STATE_MODELVIEW_MATRIX
, 0 },
787 { "gl_ModelViewMatrixInverseTranspose", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
789 { "gl_ProjectionMatrix", STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
},
790 { "gl_ProjectionMatrixInverse", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
},
791 { "gl_ProjectionMatrixTranspose", STATE_PROJECTION_MATRIX
, 0 },
792 { "gl_ProjectionMatrixInverseTranspose", STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
},
794 { "gl_ModelViewProjectionMatrix", STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
},
795 { "gl_ModelViewProjectionMatrixInverse", STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
},
796 { "gl_ModelViewProjectionMatrixTranspose", STATE_MVP_MATRIX
, 0 },
797 { "gl_ModelViewProjectionMatrixInverseTranspose", STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
},
799 { "gl_TextureMatrix", STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
},
800 { "gl_TextureMatrixInverse", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
},
801 { "gl_TextureMatrixTranspose", STATE_TEXTURE_MATRIX
, 0 },
802 { "gl_TextureMatrixInverseTranspose", STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
},
804 { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
},
810 /* C++ gets angry when we try to use an int as a gl_state_index, so we use
811 * ints for gl_state_index. Make sure they're compatible.
813 assert(sizeof(gl_state_index
) == sizeof(int));
815 for (i
= 0; i
< Elements(matrices
); i
++) {
816 if (strcmp(var
->name
, matrices
[i
].name
) == 0) {
818 int last_pos
= -1, base_pos
= -1;
819 int tokens
[STATE_LENGTH
];
821 tokens
[0] = matrices
[i
].matrix
;
822 tokens
[1] = 0; /* array index! */
823 tokens
[4] = matrices
[i
].modifier
;
825 /* Add a ref for each column. It looks like the reason we do
826 * it this way is that _mesa_add_state_reference doesn't work
827 * for things that aren't vec4s, so the tokens[2]/tokens[3]
828 * range has to be equal.
830 for (j
= 0; j
< 4; j
++) {
833 int pos
= _mesa_add_state_reference(prog
->Parameters
,
834 (gl_state_index
*)tokens
);
835 assert(last_pos
== -1 || last_pos
== base_pos
+ j
);
840 entry
= new(mem_ctx
) temp_entry(var
,
852 ir_to_mesa_visitor::visit(ir_dereference_variable
*ir
)
854 ir_to_mesa_src_reg src_reg
;
855 temp_entry
*entry
= find_variable_storage(ir
->var
);
860 switch (ir
->var
->mode
) {
862 entry
= get_builtin_matrix_ref(this->mem_ctx
, this->prog
, ir
->var
);
866 /* FINISHME: Fix up uniform name for arrays and things */
867 assert(ir
->var
->type
->gl_type
!= 0 &&
868 ir
->var
->type
->gl_type
!= GL_INVALID_ENUM
);
869 loc
= _mesa_add_uniform(this->prog
->Parameters
,
871 type_size(ir
->var
->type
) * 4,
872 ir
->var
->type
->gl_type
,
874 /* Always mark the uniform used at this point. If it isn't
875 * used, dead code elimination should have nuked the decl already.
877 this->prog
->Parameters
->Parameters
[loc
].Used
= GL_TRUE
;
879 entry
= new(mem_ctx
) temp_entry(ir
->var
, PROGRAM_UNIFORM
, loc
);
880 this->variable_storage
.push_tail(entry
);
885 var_in
= (ir
->var
->mode
== ir_var_in
||
886 ir
->var
->mode
== ir_var_inout
);
888 for (i
= 0; i
< ARRAY_SIZE(builtin_var_to_mesa_reg
); i
++) {
889 bool in
= builtin_var_to_mesa_reg
[i
].file
== PROGRAM_INPUT
;
891 if (strcmp(ir
->var
->name
, builtin_var_to_mesa_reg
[i
].name
) == 0 &&
895 if (i
== ARRAY_SIZE(builtin_var_to_mesa_reg
)) {
896 printf("Failed to find builtin for %s variable %s\n",
897 var_in
? "in" : "out",
901 entry
= new(mem_ctx
) temp_entry(ir
->var
,
902 builtin_var_to_mesa_reg
[i
].file
,
903 builtin_var_to_mesa_reg
[i
].index
);
906 entry
= new(mem_ctx
) temp_entry(ir
->var
, PROGRAM_TEMPORARY
,
908 this->variable_storage
.push_tail(entry
);
910 next_temp
+= type_size(ir
->var
->type
);
915 printf("Failed to make storage for %s\n", ir
->var
->name
);
920 src_reg
.file
= entry
->file
;
921 src_reg
.index
= entry
->index
;
922 /* If the type is smaller than a vec4, replicate the last channel out. */
923 src_reg
.swizzle
= swizzle_for_size(ir
->var
->type
->vector_elements
);
924 src_reg
.reladdr
= false;
927 this->result
= src_reg
;
931 ir_to_mesa_visitor::visit(ir_dereference_array
*ir
)
934 ir_to_mesa_src_reg src_reg
;
936 index
= ir
->array_index
->constant_expression_value();
938 /* By the time we make it to this stage, matrices should be broken down
941 assert(!ir
->type
->is_matrix());
943 ir
->array
->accept(this);
944 src_reg
= this->result
;
946 if (src_reg
.file
== PROGRAM_INPUT
||
947 src_reg
.file
== PROGRAM_OUTPUT
) {
948 assert(index
); /* FINISHME: Handle variable indexing of builtins. */
950 src_reg
.index
+= index
->value
.i
[0];
953 src_reg
.index
+= index
->value
.i
[0];
955 ir_to_mesa_src_reg array_base
= this->result
;
956 /* Variable index array dereference. It eats the "vec4" of the
957 * base of the array and an index that offsets the Mesa register
960 ir
->array_index
->accept(this);
962 /* FINISHME: This doesn't work when we're trying to do the LHS
965 src_reg
.reladdr
= true;
966 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
,
969 this->result
= get_temp(ir
->type
);
970 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
,
971 ir_to_mesa_dst_reg_from_src(this->result
),
976 /* If the type is smaller than a vec4, replicate the last channel out. */
977 src_reg
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
979 this->result
= src_reg
;
983 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
986 const glsl_type
*struct_type
= ir
->record
->type
;
989 ir
->record
->accept(this);
991 for (i
= 0; i
< struct_type
->length
; i
++) {
992 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
994 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
996 this->result
.index
+= offset
;
1000 * We want to be careful in assignment setup to hit the actual storage
1001 * instead of potentially using a temporary like we might with the
1002 * ir_dereference handler.
1004 * Thanks to ir_swizzle_swizzle, and ir_vec_index_to_swizzle, we
1005 * should only see potentially one variable array index of a vector,
1006 * and one swizzle, before getting to actual vec4 storage. So handle
1007 * those, then go use ir_dereference to handle the rest.
1009 static struct ir_to_mesa_dst_reg
1010 get_assignment_lhs(ir_instruction
*ir
, ir_to_mesa_visitor
*v
)
1012 struct ir_to_mesa_dst_reg dst_reg
;
1013 ir_dereference
*deref
;
1016 /* Use the rvalue deref handler for the most part. We'll ignore
1017 * swizzles in it and write swizzles using writemask, though.
1020 dst_reg
= ir_to_mesa_dst_reg_from_src(v
->result
);
1022 if ((deref
= ir
->as_dereference())) {
1023 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1024 assert(!deref_array
|| deref_array
->array
->type
->is_array());
1027 } else if ((swiz
= ir
->as_swizzle())) {
1028 dst_reg
.writemask
= 0;
1029 if (swiz
->mask
.num_components
>= 1)
1030 dst_reg
.writemask
|= (1 << swiz
->mask
.x
);
1031 if (swiz
->mask
.num_components
>= 2)
1032 dst_reg
.writemask
|= (1 << swiz
->mask
.y
);
1033 if (swiz
->mask
.num_components
>= 3)
1034 dst_reg
.writemask
|= (1 << swiz
->mask
.z
);
1035 if (swiz
->mask
.num_components
>= 4)
1036 dst_reg
.writemask
|= (1 << swiz
->mask
.w
);
1043 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1045 struct ir_to_mesa_dst_reg l
;
1046 struct ir_to_mesa_src_reg r
;
1048 assert(!ir
->lhs
->type
->is_matrix());
1049 assert(!ir
->lhs
->type
->is_array());
1050 assert(ir
->lhs
->type
->base_type
!= GLSL_TYPE_STRUCT
);
1052 l
= get_assignment_lhs(ir
->lhs
, this);
1054 ir
->rhs
->accept(this);
1056 assert(l
.file
!= PROGRAM_UNDEFINED
);
1057 assert(r
.file
!= PROGRAM_UNDEFINED
);
1059 if (ir
->condition
) {
1060 ir_constant
*condition_constant
;
1062 condition_constant
= ir
->condition
->constant_expression_value();
1064 assert(condition_constant
&& condition_constant
->value
.b
[0]);
1067 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1072 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1074 ir_to_mesa_src_reg src_reg
;
1075 GLfloat stack_vals
[4];
1076 GLfloat
*values
= stack_vals
;
1079 if (ir
->type
->is_matrix() || ir
->type
->is_array()) {
1080 assert(!"FINISHME: array/matrix constants");
1083 src_reg
.file
= PROGRAM_CONSTANT
;
1084 switch (ir
->type
->base_type
) {
1085 case GLSL_TYPE_FLOAT
:
1086 values
= &ir
->value
.f
[0];
1088 case GLSL_TYPE_UINT
:
1089 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1090 values
[i
] = ir
->value
.u
[i
];
1094 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1095 values
[i
] = ir
->value
.i
[i
];
1098 case GLSL_TYPE_BOOL
:
1099 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1100 values
[i
] = ir
->value
.b
[i
];
1104 assert(!"Non-float/uint/int/bool constant");
1107 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1108 values
, ir
->type
->vector_elements
,
1110 src_reg
.reladdr
= false;
1113 this->result
= src_reg
;
1118 ir_to_mesa_visitor::visit(ir_call
*ir
)
1120 printf("Can't support call to %s\n", ir
->callee_name());
1126 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1130 ir
->coordinate
->accept(this);
1134 ir_to_mesa_visitor::visit(ir_return
*ir
)
1138 ir
->get_value()->accept(this);
1143 ir_to_mesa_visitor::visit(ir_if
*ir
)
1145 ir_to_mesa_instruction
*if_inst
, *else_inst
= NULL
;
1147 ir
->condition
->accept(this);
1148 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
1150 if_inst
= ir_to_mesa_emit_op1(ir
->condition
,
1151 OPCODE_IF
, ir_to_mesa_undef_dst
,
1154 this->instructions
.push_tail(if_inst
);
1156 visit_exec_list(&ir
->then_instructions
, this);
1158 if (!ir
->else_instructions
.is_empty()) {
1159 else_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_ELSE
,
1160 ir_to_mesa_undef_dst
,
1162 visit_exec_list(&ir
->then_instructions
, this);
1165 if_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_ENDIF
,
1166 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
1169 ir_to_mesa_visitor::ir_to_mesa_visitor()
1171 result
.file
= PROGRAM_UNDEFINED
;
1175 static struct prog_src_register
1176 mesa_src_reg_from_ir_src_reg(ir_to_mesa_src_reg reg
)
1178 struct prog_src_register mesa_reg
;
1180 mesa_reg
.File
= reg
.file
;
1181 assert(reg
.index
< (1 << INST_INDEX_BITS
) - 1);
1182 mesa_reg
.Index
= reg
.index
;
1183 mesa_reg
.Swizzle
= reg
.swizzle
;
1184 mesa_reg
.RelAddr
= reg
.reladdr
;
1190 set_branchtargets(struct prog_instruction
*mesa_instructions
,
1191 int num_instructions
)
1193 int if_count
= 0, loop_count
;
1194 int *if_stack
, *loop_stack
;
1195 int if_stack_pos
= 0, loop_stack_pos
= 0;
1198 for (i
= 0; i
< num_instructions
; i
++) {
1199 switch (mesa_instructions
[i
].Opcode
) {
1203 case OPCODE_BGNLOOP
:
1208 mesa_instructions
[i
].BranchTarget
= -1;
1215 if_stack
= (int *)calloc(if_count
, sizeof(*if_stack
));
1216 loop_stack
= (int *)calloc(loop_count
, sizeof(*loop_stack
));
1218 for (i
= 0; i
< num_instructions
; i
++) {
1219 switch (mesa_instructions
[i
].Opcode
) {
1221 if_stack
[if_stack_pos
] = i
;
1225 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
1226 if_stack
[if_stack_pos
- 1] = i
;
1229 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
1232 case OPCODE_BGNLOOP
:
1233 loop_stack
[loop_stack_pos
] = i
;
1236 case OPCODE_ENDLOOP
:
1238 /* Rewrite any breaks/conts at this nesting level (haven't
1239 * already had a BranchTarget assigned) to point to the end
1242 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
1243 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
1244 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
1245 if (mesa_instructions
[j
].BranchTarget
== -1) {
1246 mesa_instructions
[j
].BranchTarget
= i
;
1250 /* The loop ends point at each other. */
1251 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
1252 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
1262 print_program(struct prog_instruction
*mesa_instructions
,
1263 ir_instruction
**mesa_instruction_annotation
,
1264 int num_instructions
)
1266 ir_instruction
*last_ir
= NULL
;
1269 for (i
= 0; i
< num_instructions
; i
++) {
1270 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
1271 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
1273 if (last_ir
!= ir
&& ir
) {
1274 ir_print_visitor print
;
1280 _mesa_print_instruction(mesa_inst
);
1285 count_resources(struct gl_program
*prog
)
1287 prog
->InputsRead
= 0;
1288 prog
->OutputsWritten
= 0;
1291 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
1292 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
1295 switch (inst
->DstReg
.File
) {
1296 case PROGRAM_OUTPUT
:
1297 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->DstReg
.Index
);
1300 prog
->InputsRead
|= BITFIELD64_BIT(inst
->DstReg
.Index
);
1306 for (reg
= 0; reg
< _mesa_num_inst_src_regs(inst
->Opcode
); reg
++) {
1307 switch (inst
->SrcReg
[reg
].File
) {
1308 case PROGRAM_OUTPUT
:
1309 prog
->OutputsWritten
|= BITFIELD64_BIT(inst
->SrcReg
[reg
].Index
);
1312 prog
->InputsRead
|= BITFIELD64_BIT(inst
->SrcReg
[reg
].Index
);
1321 /* Each stage has some uniforms in its Parameters list. The Uniforms
1322 * list for the linked shader program has a pointer to these uniforms
1323 * in each of the stage's Parameters list, so that their values can be
1324 * updated when a uniform is set.
1327 link_uniforms_to_shared_uniform_list(struct gl_uniform_list
*uniforms
,
1328 struct gl_program
*prog
)
1332 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++) {
1333 const struct gl_program_parameter
*p
= prog
->Parameters
->Parameters
+ i
;
1335 if (p
->Type
== PROGRAM_UNIFORM
|| p
->Type
== PROGRAM_SAMPLER
) {
1336 struct gl_uniform
*uniform
=
1337 _mesa_append_uniform(uniforms
, p
->Name
, prog
->Target
, i
);
1339 uniform
->Initialized
= p
->Initialized
;
1345 get_mesa_program(GLcontext
*ctx
, void *mem_ctx
, struct glsl_shader
*shader
)
1347 ir_to_mesa_visitor v
;
1348 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
1349 ir_instruction
**mesa_instruction_annotation
;
1351 exec_list
*instructions
= &shader
->ir
;
1352 struct gl_program
*prog
;
1355 switch (shader
->Type
) {
1356 case GL_VERTEX_SHADER
: target
= GL_VERTEX_PROGRAM_ARB
; break;
1357 case GL_FRAGMENT_SHADER
: target
= GL_FRAGMENT_PROGRAM_ARB
; break;
1358 default: assert(!"should not be reached"); break;
1361 prog
= ctx
->Driver
.NewProgram(ctx
, target
, 1);
1364 prog
->Parameters
= _mesa_new_parameter_list();
1365 prog
->Varying
= _mesa_new_parameter_list();
1366 prog
->Attributes
= _mesa_new_parameter_list();
1370 v
.mem_ctx
= talloc_new(NULL
);
1371 visit_exec_list(instructions
, &v
);
1372 v
.ir_to_mesa_emit_op1(NULL
, OPCODE_END
,
1373 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
1375 prog
->NumTemporaries
= v
.next_temp
;
1377 int num_instructions
= 0;
1378 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
1383 (struct prog_instruction
*)calloc(num_instructions
,
1384 sizeof(*mesa_instructions
));
1385 mesa_instruction_annotation
= talloc_array(mem_ctx
, ir_instruction
*,
1388 mesa_inst
= mesa_instructions
;
1390 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
1391 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
1393 mesa_inst
->Opcode
= inst
->op
;
1394 mesa_inst
->DstReg
.File
= inst
->dst_reg
.file
;
1395 mesa_inst
->DstReg
.Index
= inst
->dst_reg
.index
;
1396 mesa_inst
->DstReg
.CondMask
= COND_TR
;
1397 mesa_inst
->DstReg
.WriteMask
= inst
->dst_reg
.writemask
;
1398 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[0]);
1399 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[1]);
1400 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[2]);
1401 mesa_instruction_annotation
[i
] = inst
->ir
;
1407 set_branchtargets(mesa_instructions
, num_instructions
);
1409 print_program(mesa_instructions
, mesa_instruction_annotation
,
1413 prog
->Instructions
= mesa_instructions
;
1414 prog
->NumInstructions
= num_instructions
;
1416 _mesa_reference_program(ctx
, &shader
->mesa_shader
->Program
, prog
);
1421 /* Takes a Mesa gl shader structure and compiles it, returning our Mesa-like
1422 * structure with the IR and such attached.
1424 static struct glsl_shader
*
1425 _mesa_get_glsl_shader(GLcontext
*ctx
, void *mem_ctx
, struct gl_shader
*sh
)
1427 struct glsl_shader
*shader
= talloc_zero(mem_ctx
, struct glsl_shader
);
1428 struct _mesa_glsl_parse_state
*state
;
1430 shader
->Type
= sh
->Type
;
1431 shader
->Name
= sh
->Name
;
1432 shader
->RefCount
= 1;
1433 shader
->Source
= sh
->Source
;
1434 shader
->SourceLen
= strlen(sh
->Source
);
1435 shader
->mesa_shader
= sh
;
1437 state
= talloc_zero(shader
, struct _mesa_glsl_parse_state
);
1438 switch (shader
->Type
) {
1439 case GL_VERTEX_SHADER
: state
->target
= vertex_shader
; break;
1440 case GL_FRAGMENT_SHADER
: state
->target
= fragment_shader
; break;
1441 case GL_GEOMETRY_SHADER
: state
->target
= geometry_shader
; break;
1444 state
->scanner
= NULL
;
1445 state
->translation_unit
.make_empty();
1446 state
->symbols
= new(mem_ctx
) glsl_symbol_table
;
1447 state
->info_log
= talloc_strdup(shader
, "");
1448 state
->error
= false;
1449 state
->temp_index
= 0;
1450 state
->loop_or_switch_nesting
= NULL
;
1451 state
->ARB_texture_rectangle_enable
= true;
1453 _mesa_glsl_lexer_ctor(state
, shader
->Source
);
1454 _mesa_glsl_parse(state
);
1455 _mesa_glsl_lexer_dtor(state
);
1457 shader
->ir
.make_empty();
1458 if (!state
->error
&& !state
->translation_unit
.is_empty())
1459 _mesa_ast_to_hir(&shader
->ir
, state
);
1461 /* Optimization passes */
1462 if (!state
->error
&& !shader
->ir
.is_empty()) {
1467 progress
= do_function_inlining(&shader
->ir
) || progress
;
1468 progress
= do_if_simplification(&shader
->ir
) || progress
;
1469 progress
= do_copy_propagation(&shader
->ir
) || progress
;
1470 progress
= do_dead_code_local(&shader
->ir
) || progress
;
1471 progress
= do_dead_code_unlinked(state
, &shader
->ir
) || progress
;
1472 progress
= do_constant_variable_unlinked(&shader
->ir
) || progress
;
1473 progress
= do_constant_folding(&shader
->ir
) || progress
;
1474 progress
= do_vec_index_to_swizzle(&shader
->ir
) || progress
;
1475 progress
= do_swizzle_swizzle(&shader
->ir
) || progress
;
1479 shader
->symbols
= state
->symbols
;
1481 shader
->CompileStatus
= !state
->error
;
1482 shader
->InfoLog
= state
->info_log
;
1492 _mesa_glsl_compile_shader(GLcontext
*ctx
, struct gl_shader
*sh
)
1494 struct glsl_shader
*shader
;
1495 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
1497 shader
= _mesa_get_glsl_shader(ctx
, mem_ctx
, sh
);
1499 sh
->CompileStatus
= shader
->CompileStatus
;
1500 sh
->InfoLog
= strdup(shader
->InfoLog
);
1501 talloc_free(mem_ctx
);
1505 _mesa_glsl_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
1507 struct glsl_program
*whole_program
;
1510 _mesa_clear_shader_program_data(ctx
, prog
);
1512 whole_program
= talloc_zero(NULL
, struct glsl_program
);
1513 whole_program
->LinkStatus
= GL_TRUE
;
1514 whole_program
->NumShaders
= prog
->NumShaders
;
1515 whole_program
->Shaders
= talloc_array(whole_program
, struct glsl_shader
*,
1518 for (i
= 0; i
< prog
->NumShaders
; i
++) {
1519 whole_program
->Shaders
[i
] = _mesa_get_glsl_shader(ctx
, whole_program
,
1521 if (!whole_program
->Shaders
[i
]->CompileStatus
) {
1522 whole_program
->InfoLog
=
1523 talloc_asprintf_append(whole_program
->InfoLog
,
1524 "linking with uncompiled shader");
1525 whole_program
->LinkStatus
= GL_FALSE
;
1529 prog
->Uniforms
= _mesa_new_uniform_list();
1530 prog
->Varying
= _mesa_new_parameter_list();
1531 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
1532 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
1534 if (whole_program
->LinkStatus
)
1535 link_shaders(whole_program
);
1537 prog
->LinkStatus
= whole_program
->LinkStatus
;
1539 /* FINISHME: This should use the linker-generated code */
1540 if (prog
->LinkStatus
) {
1541 for (i
= 0; i
< prog
->NumShaders
; i
++) {
1542 struct gl_program
*linked_prog
;
1544 linked_prog
= get_mesa_program(ctx
, whole_program
,
1545 whole_program
->Shaders
[i
]);
1546 count_resources(linked_prog
);
1548 link_uniforms_to_shared_uniform_list(prog
->Uniforms
, linked_prog
);
1550 switch (whole_program
->Shaders
[i
]->Type
) {
1551 case GL_VERTEX_SHADER
:
1552 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
1553 (struct gl_vertex_program
*)linked_prog
);
1555 case GL_FRAGMENT_SHADER
:
1556 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
1557 (struct gl_fragment_program
*)linked_prog
);
1563 talloc_free(whole_program
);