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
;
189 struct gl_shader_compiler_options
*options
;
193 variable_storage
*find_variable_storage(ir_variable
*var
);
195 function_entry
*get_function_signature(ir_function_signature
*sig
);
197 ir_to_mesa_src_reg
get_temp(const glsl_type
*type
);
198 void reladdr_to_temp(ir_instruction
*ir
,
199 ir_to_mesa_src_reg
*reg
, int *num_reladdr
);
201 struct ir_to_mesa_src_reg
src_reg_for_float(float val
);
204 * \name Visit methods
206 * As typical for the visitor pattern, there must be one \c visit method for
207 * each concrete subclass of \c ir_instruction. Virtual base classes within
208 * the hierarchy should not have \c visit methods.
211 virtual void visit(ir_variable
*);
212 virtual void visit(ir_loop
*);
213 virtual void visit(ir_loop_jump
*);
214 virtual void visit(ir_function_signature
*);
215 virtual void visit(ir_function
*);
216 virtual void visit(ir_expression
*);
217 virtual void visit(ir_swizzle
*);
218 virtual void visit(ir_dereference_variable
*);
219 virtual void visit(ir_dereference_array
*);
220 virtual void visit(ir_dereference_record
*);
221 virtual void visit(ir_assignment
*);
222 virtual void visit(ir_constant
*);
223 virtual void visit(ir_call
*);
224 virtual void visit(ir_return
*);
225 virtual void visit(ir_discard
*);
226 virtual void visit(ir_texture
*);
227 virtual void visit(ir_if
*);
230 struct ir_to_mesa_src_reg result
;
232 /** List of variable_storage */
235 /** List of function_entry */
236 exec_list function_signatures
;
237 int next_signature_id
;
239 /** List of ir_to_mesa_instruction */
240 exec_list instructions
;
242 ir_to_mesa_instruction
*ir_to_mesa_emit_op0(ir_instruction
*ir
,
243 enum prog_opcode op
);
245 ir_to_mesa_instruction
*ir_to_mesa_emit_op1(ir_instruction
*ir
,
247 ir_to_mesa_dst_reg dst
,
248 ir_to_mesa_src_reg src0
);
250 ir_to_mesa_instruction
*ir_to_mesa_emit_op2(ir_instruction
*ir
,
252 ir_to_mesa_dst_reg dst
,
253 ir_to_mesa_src_reg src0
,
254 ir_to_mesa_src_reg src1
);
256 ir_to_mesa_instruction
*ir_to_mesa_emit_op3(ir_instruction
*ir
,
258 ir_to_mesa_dst_reg dst
,
259 ir_to_mesa_src_reg src0
,
260 ir_to_mesa_src_reg src1
,
261 ir_to_mesa_src_reg src2
);
263 void ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
265 ir_to_mesa_dst_reg dst
,
266 ir_to_mesa_src_reg src0
);
268 void ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
270 ir_to_mesa_dst_reg dst
,
271 ir_to_mesa_src_reg src0
,
272 ir_to_mesa_src_reg src1
);
274 GLboolean
try_emit_mad(ir_expression
*ir
,
277 int get_sampler_uniform_value(ir_dereference
*deref
);
282 ir_to_mesa_src_reg ir_to_mesa_undef
= ir_to_mesa_src_reg(PROGRAM_UNDEFINED
, 0, NULL
);
284 ir_to_mesa_dst_reg ir_to_mesa_undef_dst
= {
285 PROGRAM_UNDEFINED
, 0, SWIZZLE_NOOP
, COND_TR
, NULL
,
288 ir_to_mesa_dst_reg ir_to_mesa_address_reg
= {
289 PROGRAM_ADDRESS
, 0, WRITEMASK_X
, COND_TR
, NULL
292 static void fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...) PRINTFLIKE(2, 3);
294 static void fail_link(struct gl_shader_program
*prog
, const char *fmt
, ...)
298 prog
->InfoLog
= talloc_vasprintf_append(prog
->InfoLog
, fmt
, args
);
301 prog
->LinkStatus
= GL_FALSE
;
304 static int swizzle_for_size(int size
)
306 int size_swizzles
[4] = {
307 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
308 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
309 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
310 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
313 return size_swizzles
[size
- 1];
316 ir_to_mesa_instruction
*
317 ir_to_mesa_visitor::ir_to_mesa_emit_op3(ir_instruction
*ir
,
319 ir_to_mesa_dst_reg dst
,
320 ir_to_mesa_src_reg src0
,
321 ir_to_mesa_src_reg src1
,
322 ir_to_mesa_src_reg src2
)
324 ir_to_mesa_instruction
*inst
= new(mem_ctx
) ir_to_mesa_instruction();
327 /* If we have to do relative addressing, we want to load the ARL
328 * reg directly for one of the regs, and preload the other reladdr
329 * sources into temps.
331 num_reladdr
+= dst
.reladdr
!= NULL
;
332 num_reladdr
+= src0
.reladdr
!= NULL
;
333 num_reladdr
+= src1
.reladdr
!= NULL
;
334 num_reladdr
+= src2
.reladdr
!= NULL
;
336 reladdr_to_temp(ir
, &src2
, &num_reladdr
);
337 reladdr_to_temp(ir
, &src1
, &num_reladdr
);
338 reladdr_to_temp(ir
, &src0
, &num_reladdr
);
341 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
,
346 assert(num_reladdr
== 0);
350 inst
->src_reg
[0] = src0
;
351 inst
->src_reg
[1] = src1
;
352 inst
->src_reg
[2] = src2
;
355 inst
->function
= NULL
;
357 this->instructions
.push_tail(inst
);
363 ir_to_mesa_instruction
*
364 ir_to_mesa_visitor::ir_to_mesa_emit_op2(ir_instruction
*ir
,
366 ir_to_mesa_dst_reg dst
,
367 ir_to_mesa_src_reg src0
,
368 ir_to_mesa_src_reg src1
)
370 return ir_to_mesa_emit_op3(ir
, op
, dst
, src0
, src1
, ir_to_mesa_undef
);
373 ir_to_mesa_instruction
*
374 ir_to_mesa_visitor::ir_to_mesa_emit_op1(ir_instruction
*ir
,
376 ir_to_mesa_dst_reg dst
,
377 ir_to_mesa_src_reg src0
)
379 assert(dst
.writemask
!= 0);
380 return ir_to_mesa_emit_op3(ir
, op
, dst
,
381 src0
, ir_to_mesa_undef
, ir_to_mesa_undef
);
384 ir_to_mesa_instruction
*
385 ir_to_mesa_visitor::ir_to_mesa_emit_op0(ir_instruction
*ir
,
388 return ir_to_mesa_emit_op3(ir
, op
, ir_to_mesa_undef_dst
,
394 inline ir_to_mesa_dst_reg
395 ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg reg
)
397 ir_to_mesa_dst_reg dst_reg
;
399 dst_reg
.file
= reg
.file
;
400 dst_reg
.index
= reg
.index
;
401 dst_reg
.writemask
= WRITEMASK_XYZW
;
402 dst_reg
.cond_mask
= COND_TR
;
403 dst_reg
.reladdr
= reg
.reladdr
;
408 inline ir_to_mesa_src_reg
409 ir_to_mesa_src_reg_from_dst(ir_to_mesa_dst_reg reg
)
411 return ir_to_mesa_src_reg(reg
.file
, reg
.index
, NULL
);
415 * Emits Mesa scalar opcodes to produce unique answers across channels.
417 * Some Mesa opcodes are scalar-only, like ARB_fp/vp. The src X
418 * channel determines the result across all channels. So to do a vec4
419 * of this operation, we want to emit a scalar per source channel used
420 * to produce dest channels.
423 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op2(ir_instruction
*ir
,
425 ir_to_mesa_dst_reg dst
,
426 ir_to_mesa_src_reg orig_src0
,
427 ir_to_mesa_src_reg orig_src1
)
430 int done_mask
= ~dst
.writemask
;
432 /* Mesa RCP is a scalar operation splatting results to all channels,
433 * like ARB_fp/vp. So emit as many RCPs as necessary to cover our
436 for (i
= 0; i
< 4; i
++) {
437 GLuint this_mask
= (1 << i
);
438 ir_to_mesa_instruction
*inst
;
439 ir_to_mesa_src_reg src0
= orig_src0
;
440 ir_to_mesa_src_reg src1
= orig_src1
;
442 if (done_mask
& this_mask
)
445 GLuint src0_swiz
= GET_SWZ(src0
.swizzle
, i
);
446 GLuint src1_swiz
= GET_SWZ(src1
.swizzle
, i
);
447 for (j
= i
+ 1; j
< 4; j
++) {
448 if (!(done_mask
& (1 << j
)) &&
449 GET_SWZ(src0
.swizzle
, j
) == src0_swiz
&&
450 GET_SWZ(src1
.swizzle
, j
) == src1_swiz
) {
451 this_mask
|= (1 << j
);
454 src0
.swizzle
= MAKE_SWIZZLE4(src0_swiz
, src0_swiz
,
455 src0_swiz
, src0_swiz
);
456 src1
.swizzle
= MAKE_SWIZZLE4(src1_swiz
, src1_swiz
,
457 src1_swiz
, src1_swiz
);
459 inst
= ir_to_mesa_emit_op2(ir
, op
,
463 inst
->dst_reg
.writemask
= this_mask
;
464 done_mask
|= this_mask
;
469 ir_to_mesa_visitor::ir_to_mesa_emit_scalar_op1(ir_instruction
*ir
,
471 ir_to_mesa_dst_reg dst
,
472 ir_to_mesa_src_reg src0
)
474 ir_to_mesa_src_reg undef
= ir_to_mesa_undef
;
476 undef
.swizzle
= SWIZZLE_XXXX
;
478 ir_to_mesa_emit_scalar_op2(ir
, op
, dst
, src0
, undef
);
481 struct ir_to_mesa_src_reg
482 ir_to_mesa_visitor::src_reg_for_float(float val
)
484 ir_to_mesa_src_reg
src_reg(PROGRAM_CONSTANT
, -1, NULL
);
486 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
487 &val
, 1, &src_reg
.swizzle
);
493 type_size(const struct glsl_type
*type
)
498 switch (type
->base_type
) {
501 case GLSL_TYPE_FLOAT
:
503 if (type
->is_matrix()) {
504 return type
->matrix_columns
;
506 /* Regardless of size of vector, it gets a vec4. This is bad
507 * packing for things like floats, but otherwise arrays become a
508 * mess. Hopefully a later pass over the code can pack scalars
509 * down if appropriate.
513 case GLSL_TYPE_ARRAY
:
514 return type_size(type
->fields
.array
) * type
->length
;
515 case GLSL_TYPE_STRUCT
:
517 for (i
= 0; i
< type
->length
; i
++) {
518 size
+= type_size(type
->fields
.structure
[i
].type
);
521 case GLSL_TYPE_SAMPLER
:
522 /* Samplers take up one slot in UNIFORMS[], but they're baked in
533 * In the initial pass of codegen, we assign temporary numbers to
534 * intermediate results. (not SSA -- variable assignments will reuse
535 * storage). Actual register allocation for the Mesa VM occurs in a
536 * pass over the Mesa IR later.
539 ir_to_mesa_visitor::get_temp(const glsl_type
*type
)
541 ir_to_mesa_src_reg src_reg
;
545 src_reg
.file
= PROGRAM_TEMPORARY
;
546 src_reg
.index
= next_temp
;
547 src_reg
.reladdr
= NULL
;
548 next_temp
+= type_size(type
);
550 if (type
->is_array() || type
->is_record()) {
551 src_reg
.swizzle
= SWIZZLE_NOOP
;
553 for (i
= 0; i
< type
->vector_elements
; i
++)
556 swizzle
[i
] = type
->vector_elements
- 1;
557 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
558 swizzle
[2], swizzle
[3]);
566 ir_to_mesa_visitor::find_variable_storage(ir_variable
*var
)
569 variable_storage
*entry
;
571 foreach_iter(exec_list_iterator
, iter
, this->variables
) {
572 entry
= (variable_storage
*)iter
.get();
574 if (entry
->var
== var
)
581 struct statevar_element
{
583 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 static struct statevar_element gl_NormalScale_elements
[] = {
707 {NULL
, {STATE_NORMAL_SCALE
}, SWIZZLE_XXXX
},
710 #define MATRIX(name, statevar, modifier) \
711 static struct statevar_element name ## _elements[] = { \
712 { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW }, \
713 { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW }, \
714 { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW }, \
715 { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW }, \
718 MATRIX(gl_ModelViewMatrix
,
719 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_TRANSPOSE
);
720 MATRIX(gl_ModelViewMatrixInverse
,
721 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVTRANS
);
722 MATRIX(gl_ModelViewMatrixTranspose
,
723 STATE_MODELVIEW_MATRIX
, 0);
724 MATRIX(gl_ModelViewMatrixInverseTranspose
,
725 STATE_MODELVIEW_MATRIX
, STATE_MATRIX_INVERSE
);
727 MATRIX(gl_ProjectionMatrix
,
728 STATE_PROJECTION_MATRIX
, STATE_MATRIX_TRANSPOSE
);
729 MATRIX(gl_ProjectionMatrixInverse
,
730 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVTRANS
);
731 MATRIX(gl_ProjectionMatrixTranspose
,
732 STATE_PROJECTION_MATRIX
, 0);
733 MATRIX(gl_ProjectionMatrixInverseTranspose
,
734 STATE_PROJECTION_MATRIX
, STATE_MATRIX_INVERSE
);
736 MATRIX(gl_ModelViewProjectionMatrix
,
737 STATE_MVP_MATRIX
, STATE_MATRIX_TRANSPOSE
);
738 MATRIX(gl_ModelViewProjectionMatrixInverse
,
739 STATE_MVP_MATRIX
, STATE_MATRIX_INVTRANS
);
740 MATRIX(gl_ModelViewProjectionMatrixTranspose
,
741 STATE_MVP_MATRIX
, 0);
742 MATRIX(gl_ModelViewProjectionMatrixInverseTranspose
,
743 STATE_MVP_MATRIX
, STATE_MATRIX_INVERSE
);
745 MATRIX(gl_TextureMatrix
,
746 STATE_TEXTURE_MATRIX
, STATE_MATRIX_TRANSPOSE
);
747 MATRIX(gl_TextureMatrixInverse
,
748 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVTRANS
);
749 MATRIX(gl_TextureMatrixTranspose
,
750 STATE_TEXTURE_MATRIX
, 0);
751 MATRIX(gl_TextureMatrixInverseTranspose
,
752 STATE_TEXTURE_MATRIX
, STATE_MATRIX_INVERSE
);
754 static struct statevar_element gl_NormalMatrix_elements
[] = {
755 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 0, 0, STATE_MATRIX_INVERSE
},
757 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 1, 1, STATE_MATRIX_INVERSE
},
759 { NULL
, { STATE_MODELVIEW_MATRIX
, 0, 2, 2, STATE_MATRIX_INVERSE
},
765 #define STATEVAR(name) {#name, name ## _elements, Elements(name ## _elements)}
767 static const struct statevar
{
769 struct statevar_element
*elements
;
770 unsigned int num_elements
;
772 STATEVAR(gl_DepthRange
),
773 STATEVAR(gl_ClipPlane
),
775 STATEVAR(gl_FrontMaterial
),
776 STATEVAR(gl_BackMaterial
),
777 STATEVAR(gl_LightSource
),
778 STATEVAR(gl_LightModel
),
779 STATEVAR(gl_FrontLightModelProduct
),
780 STATEVAR(gl_BackLightModelProduct
),
781 STATEVAR(gl_FrontLightProduct
),
782 STATEVAR(gl_BackLightProduct
),
783 STATEVAR(gl_TextureEnvColor
),
784 STATEVAR(gl_EyePlaneS
),
785 STATEVAR(gl_EyePlaneT
),
786 STATEVAR(gl_EyePlaneR
),
787 STATEVAR(gl_EyePlaneQ
),
788 STATEVAR(gl_ObjectPlaneS
),
789 STATEVAR(gl_ObjectPlaneT
),
790 STATEVAR(gl_ObjectPlaneR
),
791 STATEVAR(gl_ObjectPlaneQ
),
794 STATEVAR(gl_ModelViewMatrix
),
795 STATEVAR(gl_ModelViewMatrixInverse
),
796 STATEVAR(gl_ModelViewMatrixTranspose
),
797 STATEVAR(gl_ModelViewMatrixInverseTranspose
),
799 STATEVAR(gl_ProjectionMatrix
),
800 STATEVAR(gl_ProjectionMatrixInverse
),
801 STATEVAR(gl_ProjectionMatrixTranspose
),
802 STATEVAR(gl_ProjectionMatrixInverseTranspose
),
804 STATEVAR(gl_ModelViewProjectionMatrix
),
805 STATEVAR(gl_ModelViewProjectionMatrixInverse
),
806 STATEVAR(gl_ModelViewProjectionMatrixTranspose
),
807 STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose
),
809 STATEVAR(gl_TextureMatrix
),
810 STATEVAR(gl_TextureMatrixInverse
),
811 STATEVAR(gl_TextureMatrixTranspose
),
812 STATEVAR(gl_TextureMatrixInverseTranspose
),
814 STATEVAR(gl_NormalMatrix
),
815 STATEVAR(gl_NormalScale
),
819 ir_to_mesa_visitor::visit(ir_variable
*ir
)
821 if (strcmp(ir
->name
, "gl_FragCoord") == 0) {
822 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
824 fp
->OriginUpperLeft
= ir
->origin_upper_left
;
825 fp
->PixelCenterInteger
= ir
->pixel_center_integer
;
828 if (ir
->mode
== ir_var_uniform
&& strncmp(ir
->name
, "gl_", 3) == 0) {
831 for (i
= 0; i
< Elements(statevars
); i
++) {
832 if (strcmp(ir
->name
, statevars
[i
].name
) == 0)
836 if (i
== Elements(statevars
)) {
837 fail_link(this->shader_program
,
838 "Failed to find builtin uniform `%s'\n", ir
->name
);
842 const struct statevar
*statevar
= &statevars
[i
];
845 if (ir
->type
->is_array()) {
846 array_count
= ir
->type
->length
;
851 /* Check if this statevar's setup in the STATE file exactly
852 * matches how we'll want to reference it as a
853 * struct/array/whatever. If not, then we need to move it into
854 * temporary storage and hope that it'll get copy-propagated
857 for (i
= 0; i
< statevar
->num_elements
; i
++) {
858 if (statevar
->elements
[i
].swizzle
!= SWIZZLE_XYZW
) {
863 struct variable_storage
*storage
;
864 ir_to_mesa_dst_reg dst
;
865 if (i
== statevar
->num_elements
) {
866 /* We'll set the index later. */
867 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_STATE_VAR
, -1);
868 this->variables
.push_tail(storage
);
870 dst
= ir_to_mesa_undef_dst
;
872 storage
= new(mem_ctx
) variable_storage(ir
, PROGRAM_TEMPORARY
,
874 this->variables
.push_tail(storage
);
875 this->next_temp
+= type_size(ir
->type
);
877 dst
= ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg(PROGRAM_TEMPORARY
,
883 for (int a
= 0; a
< array_count
; a
++) {
884 for (unsigned int i
= 0; i
< statevar
->num_elements
; i
++) {
885 struct statevar_element
*element
= &statevar
->elements
[i
];
886 int tokens
[STATE_LENGTH
];
888 memcpy(tokens
, element
->tokens
, sizeof(element
->tokens
));
889 if (ir
->type
->is_array()) {
893 int index
= _mesa_add_state_reference(this->prog
->Parameters
,
894 (gl_state_index
*)tokens
);
896 if (storage
->file
== PROGRAM_STATE_VAR
) {
897 if (storage
->index
== -1) {
898 storage
->index
= index
;
901 (int)(storage
->index
+ a
* statevar
->num_elements
+ i
));
904 ir_to_mesa_src_reg
src(PROGRAM_STATE_VAR
, index
, NULL
);
905 src
.swizzle
= element
->swizzle
;
906 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, dst
, src
);
907 /* even a float takes up a whole vec4 reg in a struct/array. */
912 if (storage
->file
== PROGRAM_TEMPORARY
&&
913 dst
.index
!= storage
->index
+ type_size(ir
->type
)) {
914 fail_link(this->shader_program
,
915 "failed to load builtin uniform `%s' (%d/%d regs loaded)\n",
916 ir
->name
, dst
.index
- storage
->index
,
917 type_size(ir
->type
));
923 ir_to_mesa_visitor::visit(ir_loop
*ir
)
925 ir_dereference_variable
*counter
= NULL
;
927 if (ir
->counter
!= NULL
)
928 counter
= new(ir
) ir_dereference_variable(ir
->counter
);
930 if (ir
->from
!= NULL
) {
931 assert(ir
->counter
!= NULL
);
933 ir_assignment
*a
= new(ir
) ir_assignment(counter
, ir
->from
, NULL
);
939 ir_to_mesa_emit_op0(NULL
, OPCODE_BGNLOOP
);
943 new(ir
) ir_expression(ir
->cmp
, glsl_type::bool_type
,
945 ir_if
*if_stmt
= new(ir
) ir_if(e
);
947 ir_loop_jump
*brk
= new(ir
) ir_loop_jump(ir_loop_jump::jump_break
);
949 if_stmt
->then_instructions
.push_tail(brk
);
951 if_stmt
->accept(this);
958 visit_exec_list(&ir
->body_instructions
, this);
962 new(ir
) ir_expression(ir_binop_add
, counter
->type
,
963 counter
, ir
->increment
);
965 ir_assignment
*a
= new(ir
) ir_assignment(counter
, e
, NULL
);
972 ir_to_mesa_emit_op0(NULL
, OPCODE_ENDLOOP
);
976 ir_to_mesa_visitor::visit(ir_loop_jump
*ir
)
979 case ir_loop_jump::jump_break
:
980 ir_to_mesa_emit_op0(NULL
, OPCODE_BRK
);
982 case ir_loop_jump::jump_continue
:
983 ir_to_mesa_emit_op0(NULL
, OPCODE_CONT
);
990 ir_to_mesa_visitor::visit(ir_function_signature
*ir
)
997 ir_to_mesa_visitor::visit(ir_function
*ir
)
999 /* Ignore function bodies other than main() -- we shouldn't see calls to
1000 * them since they should all be inlined before we get to ir_to_mesa.
1002 if (strcmp(ir
->name
, "main") == 0) {
1003 const ir_function_signature
*sig
;
1006 sig
= ir
->matching_signature(&empty
);
1010 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1011 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1019 ir_to_mesa_visitor::try_emit_mad(ir_expression
*ir
, int mul_operand
)
1021 int nonmul_operand
= 1 - mul_operand
;
1022 ir_to_mesa_src_reg a
, b
, c
;
1024 ir_expression
*expr
= ir
->operands
[mul_operand
]->as_expression();
1025 if (!expr
|| expr
->operation
!= ir_binop_mul
)
1028 expr
->operands
[0]->accept(this);
1030 expr
->operands
[1]->accept(this);
1032 ir
->operands
[nonmul_operand
]->accept(this);
1035 this->result
= get_temp(ir
->type
);
1036 ir_to_mesa_emit_op3(ir
, OPCODE_MAD
,
1037 ir_to_mesa_dst_reg_from_src(this->result
), a
, b
, c
);
1043 ir_to_mesa_visitor::reladdr_to_temp(ir_instruction
*ir
,
1044 ir_to_mesa_src_reg
*reg
, int *num_reladdr
)
1049 ir_to_mesa_emit_op1(ir
, OPCODE_ARL
, ir_to_mesa_address_reg
, *reg
->reladdr
);
1051 if (*num_reladdr
!= 1) {
1052 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
1054 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
,
1055 ir_to_mesa_dst_reg_from_src(temp
), *reg
);
1063 ir_to_mesa_visitor::visit(ir_expression
*ir
)
1065 unsigned int operand
;
1066 struct ir_to_mesa_src_reg op
[2];
1067 struct ir_to_mesa_src_reg result_src
;
1068 struct ir_to_mesa_dst_reg result_dst
;
1069 const glsl_type
*vec4_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 4, 1);
1070 const glsl_type
*vec3_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 3, 1);
1071 const glsl_type
*vec2_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, 2, 1);
1073 /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
1075 if (ir
->operation
== ir_binop_add
) {
1076 if (try_emit_mad(ir
, 1))
1078 if (try_emit_mad(ir
, 0))
1082 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
1083 this->result
.file
= PROGRAM_UNDEFINED
;
1084 ir
->operands
[operand
]->accept(this);
1085 if (this->result
.file
== PROGRAM_UNDEFINED
) {
1087 printf("Failed to get tree for expression operand:\n");
1088 ir
->operands
[operand
]->accept(&v
);
1091 op
[operand
] = this->result
;
1093 /* Matrix expression operands should have been broken down to vector
1094 * operations already.
1096 assert(!ir
->operands
[operand
]->type
->is_matrix());
1099 this->result
.file
= PROGRAM_UNDEFINED
;
1101 /* Storage for our result. Ideally for an assignment we'd be using
1102 * the actual storage for the result here, instead.
1104 result_src
= get_temp(ir
->type
);
1105 /* convenience for the emit functions below. */
1106 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
1107 /* Limit writes to the channels that will be used by result_src later.
1108 * This does limit this temp's use as a temporary for multi-instruction
1111 result_dst
.writemask
= (1 << ir
->type
->vector_elements
) - 1;
1113 switch (ir
->operation
) {
1114 case ir_unop_logic_not
:
1115 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
,
1116 op
[0], src_reg_for_float(0.0));
1119 op
[0].negate
= ~op
[0].negate
;
1123 ir_to_mesa_emit_op1(ir
, OPCODE_ABS
, result_dst
, op
[0]);
1126 ir_to_mesa_emit_op1(ir
, OPCODE_SSG
, result_dst
, op
[0]);
1129 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RCP
, result_dst
, op
[0]);
1133 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_EX2
, result_dst
, op
[0]);
1137 assert(!"not reached: should be handled by ir_explog_to_explog2");
1140 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_LG2
, result_dst
, op
[0]);
1143 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_SIN
, result_dst
, op
[0]);
1146 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_COS
, result_dst
, op
[0]);
1150 ir_to_mesa_emit_op1(ir
, OPCODE_DDX
, result_dst
, op
[0]);
1153 ir_to_mesa_emit_op1(ir
, OPCODE_DDY
, result_dst
, op
[0]);
1156 case ir_unop_noise
: {
1157 const enum prog_opcode opcode
=
1158 prog_opcode(OPCODE_NOISE1
1159 + (ir
->operands
[0]->type
->vector_elements
) - 1);
1160 assert((opcode
>= OPCODE_NOISE1
) && (opcode
<= OPCODE_NOISE4
));
1162 ir_to_mesa_emit_op1(ir
, opcode
, result_dst
, op
[0]);
1167 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
, result_dst
, op
[0], op
[1]);
1170 ir_to_mesa_emit_op2(ir
, OPCODE_SUB
, result_dst
, op
[0], op
[1]);
1174 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, op
[0], op
[1]);
1177 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1179 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1183 ir_to_mesa_emit_op2(ir
, OPCODE_SLT
, result_dst
, op
[0], op
[1]);
1185 case ir_binop_greater
:
1186 ir_to_mesa_emit_op2(ir
, OPCODE_SGT
, result_dst
, op
[0], op
[1]);
1188 case ir_binop_lequal
:
1189 ir_to_mesa_emit_op2(ir
, OPCODE_SLE
, result_dst
, op
[0], op
[1]);
1191 case ir_binop_gequal
:
1192 ir_to_mesa_emit_op2(ir
, OPCODE_SGE
, result_dst
, op
[0], op
[1]);
1194 case ir_binop_equal
:
1195 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1197 case ir_binop_nequal
:
1198 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1200 case ir_binop_all_equal
:
1201 /* "==" operator producing a scalar boolean. */
1202 if (ir
->operands
[0]->type
->is_vector() ||
1203 ir
->operands
[1]->type
->is_vector()) {
1204 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
1205 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1206 ir_to_mesa_dst_reg_from_src(temp
), op
[0], op
[1]);
1207 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, temp
, temp
);
1208 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
,
1209 result_dst
, result_src
, src_reg_for_float(0.0));
1211 ir_to_mesa_emit_op2(ir
, OPCODE_SEQ
, result_dst
, op
[0], op
[1]);
1214 case ir_binop_any_nequal
:
1215 /* "!=" operator producing a scalar boolean. */
1216 if (ir
->operands
[0]->type
->is_vector() ||
1217 ir
->operands
[1]->type
->is_vector()) {
1218 ir_to_mesa_src_reg temp
= get_temp(glsl_type::vec4_type
);
1219 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1220 ir_to_mesa_dst_reg_from_src(temp
), op
[0], op
[1]);
1221 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, temp
, temp
);
1222 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1223 result_dst
, result_src
, src_reg_for_float(0.0));
1225 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1230 switch (ir
->operands
[0]->type
->vector_elements
) {
1232 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
, result_dst
, op
[0], op
[0]);
1235 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
, result_dst
, op
[0], op
[0]);
1238 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
, result_dst
, op
[0], op
[0]);
1241 assert(!"unreached: ir_unop_any of non-bvec");
1244 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1245 result_dst
, result_src
, src_reg_for_float(0.0));
1248 case ir_binop_logic_xor
:
1249 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
, op
[0], op
[1]);
1252 case ir_binop_logic_or
:
1253 /* This could be a saturated add and skip the SNE. */
1254 ir_to_mesa_emit_op2(ir
, OPCODE_ADD
,
1258 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
,
1260 result_src
, src_reg_for_float(0.0));
1263 case ir_binop_logic_and
:
1264 /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
1265 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1271 if (ir
->operands
[0]->type
== vec4_type
) {
1272 assert(ir
->operands
[1]->type
== vec4_type
);
1273 ir_to_mesa_emit_op2(ir
, OPCODE_DP4
,
1276 } else if (ir
->operands
[0]->type
== vec3_type
) {
1277 assert(ir
->operands
[1]->type
== vec3_type
);
1278 ir_to_mesa_emit_op2(ir
, OPCODE_DP3
,
1281 } else if (ir
->operands
[0]->type
== vec2_type
) {
1282 assert(ir
->operands
[1]->type
== vec2_type
);
1283 ir_to_mesa_emit_op2(ir
, OPCODE_DP2
,
1289 case ir_binop_cross
:
1290 ir_to_mesa_emit_op2(ir
, OPCODE_XPD
, result_dst
, op
[0], op
[1]);
1294 /* sqrt(x) = x * rsq(x). */
1295 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
1296 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, result_dst
, result_src
, op
[0]);
1297 /* For incoming channels <= 0, set the result to 0. */
1298 op
[0].negate
= ~op
[0].negate
;
1299 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, result_dst
,
1300 op
[0], result_src
, src_reg_for_float(0.0));
1303 ir_to_mesa_emit_scalar_op1(ir
, OPCODE_RSQ
, result_dst
, op
[0]);
1308 /* Mesa IR lacks types, ints are stored as truncated floats. */
1312 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1316 ir_to_mesa_emit_op2(ir
, OPCODE_SNE
, result_dst
,
1317 op
[0], src_reg_for_float(0.0));
1320 ir_to_mesa_emit_op1(ir
, OPCODE_TRUNC
, result_dst
, op
[0]);
1323 op
[0].negate
= ~op
[0].negate
;
1324 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1325 result_src
.negate
= ~result_src
.negate
;
1328 ir_to_mesa_emit_op1(ir
, OPCODE_FLR
, result_dst
, op
[0]);
1331 ir_to_mesa_emit_op1(ir
, OPCODE_FRC
, result_dst
, op
[0]);
1335 ir_to_mesa_emit_op2(ir
, OPCODE_MIN
, result_dst
, op
[0], op
[1]);
1338 ir_to_mesa_emit_op2(ir
, OPCODE_MAX
, result_dst
, op
[0], op
[1]);
1341 ir_to_mesa_emit_scalar_op2(ir
, OPCODE_POW
, result_dst
, op
[0], op
[1]);
1344 case ir_unop_bit_not
:
1346 case ir_binop_lshift
:
1347 case ir_binop_rshift
:
1348 case ir_binop_bit_and
:
1349 case ir_binop_bit_xor
:
1350 case ir_binop_bit_or
:
1351 assert(!"GLSL 1.30 features unsupported");
1355 this->result
= result_src
;
1360 ir_to_mesa_visitor::visit(ir_swizzle
*ir
)
1362 ir_to_mesa_src_reg src_reg
;
1366 /* Note that this is only swizzles in expressions, not those on the left
1367 * hand side of an assignment, which do write masking. See ir_assignment
1371 ir
->val
->accept(this);
1372 src_reg
= this->result
;
1373 assert(src_reg
.file
!= PROGRAM_UNDEFINED
);
1375 for (i
= 0; i
< 4; i
++) {
1376 if (i
< ir
->type
->vector_elements
) {
1379 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.x
);
1382 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.y
);
1385 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.z
);
1388 swizzle
[i
] = GET_SWZ(src_reg
.swizzle
, ir
->mask
.w
);
1392 /* If the type is smaller than a vec4, replicate the last
1395 swizzle
[i
] = swizzle
[ir
->type
->vector_elements
- 1];
1399 src_reg
.swizzle
= MAKE_SWIZZLE4(swizzle
[0],
1404 this->result
= src_reg
;
1408 ir_to_mesa_visitor::visit(ir_dereference_variable
*ir
)
1410 variable_storage
*entry
= find_variable_storage(ir
->var
);
1413 switch (ir
->var
->mode
) {
1414 case ir_var_uniform
:
1415 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_UNIFORM
,
1417 this->variables
.push_tail(entry
);
1422 /* The linker assigns locations for varyings and attributes,
1423 * including deprecated builtins (like gl_Color), user-assign
1424 * generic attributes (glBindVertexLocation), and
1425 * user-defined varyings.
1427 * FINISHME: We would hit this path for function arguments. Fix!
1429 assert(ir
->var
->location
!= -1);
1430 if (ir
->var
->mode
== ir_var_in
||
1431 ir
->var
->mode
== ir_var_inout
) {
1432 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1436 if (this->prog
->Target
== GL_VERTEX_PROGRAM_ARB
&&
1437 ir
->var
->location
>= VERT_ATTRIB_GENERIC0
) {
1438 _mesa_add_attribute(prog
->Attributes
,
1440 _mesa_sizeof_glsl_type(ir
->var
->type
->gl_type
),
1441 ir
->var
->type
->gl_type
,
1442 ir
->var
->location
- VERT_ATTRIB_GENERIC0
);
1445 entry
= new(mem_ctx
) variable_storage(ir
->var
,
1452 case ir_var_temporary
:
1453 entry
= new(mem_ctx
) variable_storage(ir
->var
, PROGRAM_TEMPORARY
,
1455 this->variables
.push_tail(entry
);
1457 next_temp
+= type_size(ir
->var
->type
);
1462 printf("Failed to make storage for %s\n", ir
->var
->name
);
1467 this->result
= ir_to_mesa_src_reg(entry
->file
, entry
->index
, ir
->var
->type
);
1471 ir_to_mesa_visitor::visit(ir_dereference_array
*ir
)
1474 ir_to_mesa_src_reg src_reg
;
1475 int element_size
= type_size(ir
->type
);
1477 index
= ir
->array_index
->constant_expression_value();
1479 ir
->array
->accept(this);
1480 src_reg
= this->result
;
1483 src_reg
.index
+= index
->value
.i
[0] * element_size
;
1485 ir_to_mesa_src_reg array_base
= this->result
;
1486 /* Variable index array dereference. It eats the "vec4" of the
1487 * base of the array and an index that offsets the Mesa register
1490 ir
->array_index
->accept(this);
1492 ir_to_mesa_src_reg index_reg
;
1494 if (element_size
== 1) {
1495 index_reg
= this->result
;
1497 index_reg
= get_temp(glsl_type::float_type
);
1499 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
,
1500 ir_to_mesa_dst_reg_from_src(index_reg
),
1501 this->result
, src_reg_for_float(element_size
));
1504 src_reg
.reladdr
= talloc(mem_ctx
, ir_to_mesa_src_reg
);
1505 memcpy(src_reg
.reladdr
, &index_reg
, sizeof(index_reg
));
1508 /* If the type is smaller than a vec4, replicate the last channel out. */
1509 if (ir
->type
->is_scalar() || ir
->type
->is_vector())
1510 src_reg
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1512 src_reg
.swizzle
= SWIZZLE_NOOP
;
1514 this->result
= src_reg
;
1518 ir_to_mesa_visitor::visit(ir_dereference_record
*ir
)
1521 const glsl_type
*struct_type
= ir
->record
->type
;
1524 ir
->record
->accept(this);
1526 for (i
= 0; i
< struct_type
->length
; i
++) {
1527 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
1529 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
1531 this->result
.swizzle
= swizzle_for_size(ir
->type
->vector_elements
);
1532 this->result
.index
+= offset
;
1536 * We want to be careful in assignment setup to hit the actual storage
1537 * instead of potentially using a temporary like we might with the
1538 * ir_dereference handler.
1540 static struct ir_to_mesa_dst_reg
1541 get_assignment_lhs(ir_dereference
*ir
, ir_to_mesa_visitor
*v
)
1543 /* The LHS must be a dereference. If the LHS is a variable indexed array
1544 * access of a vector, it must be separated into a series conditional moves
1545 * before reaching this point (see ir_vec_index_to_cond_assign).
1547 assert(ir
->as_dereference());
1548 ir_dereference_array
*deref_array
= ir
->as_dereference_array();
1550 assert(!deref_array
->array
->type
->is_vector());
1553 /* Use the rvalue deref handler for the most part. We'll ignore
1554 * swizzles in it and write swizzles using writemask, though.
1557 return ir_to_mesa_dst_reg_from_src(v
->result
);
1561 ir_to_mesa_visitor::visit(ir_assignment
*ir
)
1563 struct ir_to_mesa_dst_reg l
;
1564 struct ir_to_mesa_src_reg r
;
1567 ir
->rhs
->accept(this);
1570 l
= get_assignment_lhs(ir
->lhs
, this);
1572 /* FINISHME: This should really set to the correct maximal writemask for each
1573 * FINISHME: component written (in the loops below). This case can only
1574 * FINISHME: occur for matrices, arrays, and structures.
1576 if (ir
->write_mask
== 0) {
1577 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
1578 l
.writemask
= WRITEMASK_XYZW
;
1579 } else if (ir
->lhs
->type
->is_scalar()) {
1580 /* FINISHME: This hack makes writing to gl_FragData, which lives in the
1581 * FINISHME: W component of fragment shader output zero, work correctly.
1583 l
.writemask
= WRITEMASK_XYZW
;
1585 assert(ir
->lhs
->type
->is_vector());
1586 l
.writemask
= ir
->write_mask
;
1589 assert(l
.file
!= PROGRAM_UNDEFINED
);
1590 assert(r
.file
!= PROGRAM_UNDEFINED
);
1592 if (ir
->condition
) {
1593 ir_to_mesa_src_reg condition
;
1595 ir
->condition
->accept(this);
1596 condition
= this->result
;
1598 /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves,
1599 * and the condition we produced is 0.0 or 1.0. By flipping the
1600 * sign, we can choose which value OPCODE_CMP produces without
1601 * an extra computing the condition.
1603 condition
.negate
= ~condition
.negate
;
1604 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1605 ir_to_mesa_emit_op3(ir
, OPCODE_CMP
, l
,
1606 condition
, r
, ir_to_mesa_src_reg_from_dst(l
));
1611 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
1612 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1621 ir_to_mesa_visitor::visit(ir_constant
*ir
)
1623 ir_to_mesa_src_reg src_reg
;
1624 GLfloat stack_vals
[4] = { 0 };
1625 GLfloat
*values
= stack_vals
;
1628 /* Unfortunately, 4 floats is all we can get into
1629 * _mesa_add_unnamed_constant. So, make a temp to store an
1630 * aggregate constant and move each constant value into it. If we
1631 * get lucky, copy propagation will eliminate the extra moves.
1634 if (ir
->type
->base_type
== GLSL_TYPE_STRUCT
) {
1635 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1636 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1638 foreach_iter(exec_list_iterator
, iter
, ir
->components
) {
1639 ir_constant
*field_value
= (ir_constant
*)iter
.get();
1640 int size
= type_size(field_value
->type
);
1644 field_value
->accept(this);
1645 src_reg
= this->result
;
1647 for (i
= 0; i
< (unsigned int)size
; i
++) {
1648 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1654 this->result
= temp_base
;
1658 if (ir
->type
->is_array()) {
1659 ir_to_mesa_src_reg temp_base
= get_temp(ir
->type
);
1660 ir_to_mesa_dst_reg temp
= ir_to_mesa_dst_reg_from_src(temp_base
);
1661 int size
= type_size(ir
->type
->fields
.array
);
1665 for (i
= 0; i
< ir
->type
->length
; i
++) {
1666 ir
->array_elements
[i
]->accept(this);
1667 src_reg
= this->result
;
1668 for (int j
= 0; j
< size
; j
++) {
1669 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, temp
, src_reg
);
1675 this->result
= temp_base
;
1679 if (ir
->type
->is_matrix()) {
1680 ir_to_mesa_src_reg mat
= get_temp(ir
->type
);
1681 ir_to_mesa_dst_reg mat_column
= ir_to_mesa_dst_reg_from_src(mat
);
1683 for (i
= 0; i
< ir
->type
->matrix_columns
; i
++) {
1684 assert(ir
->type
->base_type
== GLSL_TYPE_FLOAT
);
1685 values
= &ir
->value
.f
[i
* ir
->type
->vector_elements
];
1687 src_reg
= ir_to_mesa_src_reg(PROGRAM_CONSTANT
, -1, NULL
);
1688 src_reg
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1690 ir
->type
->vector_elements
,
1692 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, mat_column
, src_reg
);
1701 src_reg
.file
= PROGRAM_CONSTANT
;
1702 switch (ir
->type
->base_type
) {
1703 case GLSL_TYPE_FLOAT
:
1704 values
= &ir
->value
.f
[0];
1706 case GLSL_TYPE_UINT
:
1707 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1708 values
[i
] = ir
->value
.u
[i
];
1712 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1713 values
[i
] = ir
->value
.i
[i
];
1716 case GLSL_TYPE_BOOL
:
1717 for (i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1718 values
[i
] = ir
->value
.b
[i
];
1722 assert(!"Non-float/uint/int/bool constant");
1725 this->result
= ir_to_mesa_src_reg(PROGRAM_CONSTANT
, -1, ir
->type
);
1726 this->result
.index
= _mesa_add_unnamed_constant(this->prog
->Parameters
,
1728 ir
->type
->vector_elements
,
1729 &this->result
.swizzle
);
1733 ir_to_mesa_visitor::get_function_signature(ir_function_signature
*sig
)
1735 function_entry
*entry
;
1737 foreach_iter(exec_list_iterator
, iter
, this->function_signatures
) {
1738 entry
= (function_entry
*)iter
.get();
1740 if (entry
->sig
== sig
)
1744 entry
= talloc(mem_ctx
, function_entry
);
1746 entry
->sig_id
= this->next_signature_id
++;
1747 entry
->bgn_inst
= NULL
;
1749 /* Allocate storage for all the parameters. */
1750 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1751 ir_variable
*param
= (ir_variable
*)iter
.get();
1752 variable_storage
*storage
;
1754 storage
= find_variable_storage(param
);
1757 storage
= new(mem_ctx
) variable_storage(param
, PROGRAM_TEMPORARY
,
1759 this->variables
.push_tail(storage
);
1761 this->next_temp
+= type_size(param
->type
);
1764 if (!sig
->return_type
->is_void()) {
1765 entry
->return_reg
= get_temp(sig
->return_type
);
1767 entry
->return_reg
= ir_to_mesa_undef
;
1770 this->function_signatures
.push_tail(entry
);
1775 ir_to_mesa_visitor::visit(ir_call
*ir
)
1777 ir_to_mesa_instruction
*call_inst
;
1778 ir_function_signature
*sig
= ir
->get_callee();
1779 function_entry
*entry
= get_function_signature(sig
);
1782 /* Process in parameters. */
1783 exec_list_iterator sig_iter
= sig
->parameters
.iterator();
1784 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1785 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1786 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1788 if (param
->mode
== ir_var_in
||
1789 param
->mode
== ir_var_inout
) {
1790 variable_storage
*storage
= find_variable_storage(param
);
1793 param_rval
->accept(this);
1794 ir_to_mesa_src_reg r
= this->result
;
1796 ir_to_mesa_dst_reg l
;
1797 l
.file
= storage
->file
;
1798 l
.index
= storage
->index
;
1800 l
.writemask
= WRITEMASK_XYZW
;
1801 l
.cond_mask
= COND_TR
;
1803 for (i
= 0; i
< type_size(param
->type
); i
++) {
1804 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1812 assert(!sig_iter
.has_next());
1814 /* Emit call instruction */
1815 call_inst
= ir_to_mesa_emit_op1(ir
, OPCODE_CAL
,
1816 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
1817 call_inst
->function
= entry
;
1819 /* Process out parameters. */
1820 sig_iter
= sig
->parameters
.iterator();
1821 foreach_iter(exec_list_iterator
, iter
, *ir
) {
1822 ir_rvalue
*param_rval
= (ir_rvalue
*)iter
.get();
1823 ir_variable
*param
= (ir_variable
*)sig_iter
.get();
1825 if (param
->mode
== ir_var_out
||
1826 param
->mode
== ir_var_inout
) {
1827 variable_storage
*storage
= find_variable_storage(param
);
1830 ir_to_mesa_src_reg r
;
1831 r
.file
= storage
->file
;
1832 r
.index
= storage
->index
;
1834 r
.swizzle
= SWIZZLE_NOOP
;
1837 param_rval
->accept(this);
1838 ir_to_mesa_dst_reg l
= ir_to_mesa_dst_reg_from_src(this->result
);
1840 for (i
= 0; i
< type_size(param
->type
); i
++) {
1841 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
1849 assert(!sig_iter
.has_next());
1851 /* Process return value. */
1852 this->result
= entry
->return_reg
;
1855 class get_sampler_name
: public ir_hierarchical_visitor
1858 get_sampler_name(ir_to_mesa_visitor
*mesa
, ir_dereference
*last
)
1860 this->mem_ctx
= mesa
->mem_ctx
;
1867 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
)
1869 this->name
= ir
->var
->name
;
1870 return visit_continue
;
1873 virtual ir_visitor_status
visit_leave(ir_dereference_record
*ir
)
1875 this->name
= talloc_asprintf(mem_ctx
, "%s.%s", name
, ir
->field
);
1876 return visit_continue
;
1879 virtual ir_visitor_status
visit_leave(ir_dereference_array
*ir
)
1881 ir_constant
*index
= ir
->array_index
->as_constant();
1885 i
= index
->value
.i
[0];
1887 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
1888 * while GLSL 1.30 requires that the array indices be
1889 * constant integer expressions. We don't expect any driver
1890 * to actually work with a really variable array index, so
1891 * all that would work would be an unrolled loop counter that ends
1892 * up being constant above.
1894 mesa
->shader_program
->InfoLog
=
1895 talloc_asprintf_append(mesa
->shader_program
->InfoLog
,
1896 "warning: Variable sampler array index "
1897 "unsupported.\nThis feature of the language "
1898 "was removed in GLSL 1.20 and is unlikely "
1899 "to be supported for 1.10 in Mesa.\n");
1903 this->name
= talloc_asprintf(mem_ctx
, "%s[%d]", name
, i
);
1907 return visit_continue
;
1910 ir_to_mesa_visitor
*mesa
;
1914 ir_dereference
*last
;
1918 ir_to_mesa_visitor::get_sampler_uniform_value(ir_dereference
*sampler
)
1920 get_sampler_name
getname(this, sampler
);
1922 sampler
->accept(&getname
);
1924 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
1928 fail_link(this->shader_program
,
1929 "failed to find sampler named %s.\n", getname
.name
);
1933 index
+= getname
.offset
;
1935 return this->prog
->Parameters
->ParameterValues
[index
][0];
1939 ir_to_mesa_visitor::visit(ir_texture
*ir
)
1941 ir_to_mesa_src_reg result_src
, coord
, lod_info
, projector
;
1942 ir_to_mesa_dst_reg result_dst
, coord_dst
;
1943 ir_to_mesa_instruction
*inst
= NULL
;
1944 prog_opcode opcode
= OPCODE_NOP
;
1946 ir
->coordinate
->accept(this);
1948 /* Put our coords in a temp. We'll need to modify them for shadow,
1949 * projection, or LOD, so the only case we'd use it as is is if
1950 * we're doing plain old texturing. Mesa IR optimization should
1951 * handle cleaning up our mess in that case.
1953 coord
= get_temp(glsl_type::vec4_type
);
1954 coord_dst
= ir_to_mesa_dst_reg_from_src(coord
);
1955 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
,
1958 if (ir
->projector
) {
1959 ir
->projector
->accept(this);
1960 projector
= this->result
;
1963 /* Storage for our result. Ideally for an assignment we'd be using
1964 * the actual storage for the result here, instead.
1966 result_src
= get_temp(glsl_type::vec4_type
);
1967 result_dst
= ir_to_mesa_dst_reg_from_src(result_src
);
1971 opcode
= OPCODE_TEX
;
1974 opcode
= OPCODE_TXB
;
1975 ir
->lod_info
.bias
->accept(this);
1976 lod_info
= this->result
;
1979 opcode
= OPCODE_TXL
;
1980 ir
->lod_info
.lod
->accept(this);
1981 lod_info
= this->result
;
1985 assert(!"GLSL 1.30 features unsupported");
1989 if (ir
->projector
) {
1990 if (opcode
== OPCODE_TEX
) {
1991 /* Slot the projector in as the last component of the coord. */
1992 coord_dst
.writemask
= WRITEMASK_W
;
1993 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, projector
);
1994 coord_dst
.writemask
= WRITEMASK_XYZW
;
1995 opcode
= OPCODE_TXP
;
1997 ir_to_mesa_src_reg coord_w
= coord
;
1998 coord_w
.swizzle
= SWIZZLE_WWWW
;
2000 /* For the other TEX opcodes there's no projective version
2001 * since the last slot is taken up by lod info. Do the
2002 * projective divide now.
2004 coord_dst
.writemask
= WRITEMASK_W
;
2005 ir_to_mesa_emit_op1(ir
, OPCODE_RCP
, coord_dst
, projector
);
2007 coord_dst
.writemask
= WRITEMASK_XYZ
;
2008 ir_to_mesa_emit_op2(ir
, OPCODE_MUL
, coord_dst
, coord
, coord_w
);
2010 coord_dst
.writemask
= WRITEMASK_XYZW
;
2011 coord
.swizzle
= SWIZZLE_XYZW
;
2015 if (ir
->shadow_comparitor
) {
2016 /* Slot the shadow value in as the second to last component of the
2019 ir
->shadow_comparitor
->accept(this);
2020 coord_dst
.writemask
= WRITEMASK_Z
;
2021 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, this->result
);
2022 coord_dst
.writemask
= WRITEMASK_XYZW
;
2025 if (opcode
== OPCODE_TXL
|| opcode
== OPCODE_TXB
) {
2026 /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2027 coord_dst
.writemask
= WRITEMASK_W
;
2028 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, coord_dst
, lod_info
);
2029 coord_dst
.writemask
= WRITEMASK_XYZW
;
2032 inst
= ir_to_mesa_emit_op1(ir
, opcode
, result_dst
, coord
);
2034 if (ir
->shadow_comparitor
)
2035 inst
->tex_shadow
= GL_TRUE
;
2037 inst
->sampler
= get_sampler_uniform_value(ir
->sampler
);
2039 const glsl_type
*sampler_type
= ir
->sampler
->type
;
2041 switch (sampler_type
->sampler_dimensionality
) {
2042 case GLSL_SAMPLER_DIM_1D
:
2043 inst
->tex_target
= (sampler_type
->sampler_array
)
2044 ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
2046 case GLSL_SAMPLER_DIM_2D
:
2047 inst
->tex_target
= (sampler_type
->sampler_array
)
2048 ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
2050 case GLSL_SAMPLER_DIM_3D
:
2051 inst
->tex_target
= TEXTURE_3D_INDEX
;
2053 case GLSL_SAMPLER_DIM_CUBE
:
2054 inst
->tex_target
= TEXTURE_CUBE_INDEX
;
2056 case GLSL_SAMPLER_DIM_RECT
:
2057 inst
->tex_target
= TEXTURE_RECT_INDEX
;
2059 case GLSL_SAMPLER_DIM_BUF
:
2060 assert(!"FINISHME: Implement ARB_texture_buffer_object");
2063 assert(!"Should not get here.");
2066 this->result
= result_src
;
2070 ir_to_mesa_visitor::visit(ir_return
*ir
)
2072 if (ir
->get_value()) {
2073 ir_to_mesa_dst_reg l
;
2076 assert(current_function
);
2078 ir
->get_value()->accept(this);
2079 ir_to_mesa_src_reg r
= this->result
;
2081 l
= ir_to_mesa_dst_reg_from_src(current_function
->return_reg
);
2083 for (i
= 0; i
< type_size(current_function
->sig
->return_type
); i
++) {
2084 ir_to_mesa_emit_op1(ir
, OPCODE_MOV
, l
, r
);
2090 ir_to_mesa_emit_op0(ir
, OPCODE_RET
);
2094 ir_to_mesa_visitor::visit(ir_discard
*ir
)
2096 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*)this->prog
;
2098 assert(ir
->condition
== NULL
); /* FINISHME */
2100 ir_to_mesa_emit_op0(ir
, OPCODE_KIL_NV
);
2101 fp
->UsesKill
= GL_TRUE
;
2105 ir_to_mesa_visitor::visit(ir_if
*ir
)
2107 ir_to_mesa_instruction
*cond_inst
, *if_inst
, *else_inst
= NULL
;
2108 ir_to_mesa_instruction
*prev_inst
;
2110 prev_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2112 ir
->condition
->accept(this);
2113 assert(this->result
.file
!= PROGRAM_UNDEFINED
);
2115 if (this->options
->EmitCondCodes
) {
2116 cond_inst
= (ir_to_mesa_instruction
*)this->instructions
.get_tail();
2118 /* See if we actually generated any instruction for generating
2119 * the condition. If not, then cook up a move to a temp so we
2120 * have something to set cond_update on.
2122 if (cond_inst
== prev_inst
) {
2123 ir_to_mesa_src_reg temp
= get_temp(glsl_type::bool_type
);
2124 cond_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_MOV
,
2125 ir_to_mesa_dst_reg_from_src(temp
),
2128 cond_inst
->cond_update
= GL_TRUE
;
2130 if_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_IF
);
2131 if_inst
->dst_reg
.cond_mask
= COND_NE
;
2133 if_inst
= ir_to_mesa_emit_op1(ir
->condition
,
2134 OPCODE_IF
, ir_to_mesa_undef_dst
,
2138 this->instructions
.push_tail(if_inst
);
2140 visit_exec_list(&ir
->then_instructions
, this);
2142 if (!ir
->else_instructions
.is_empty()) {
2143 else_inst
= ir_to_mesa_emit_op0(ir
->condition
, OPCODE_ELSE
);
2144 visit_exec_list(&ir
->else_instructions
, this);
2147 if_inst
= ir_to_mesa_emit_op1(ir
->condition
, OPCODE_ENDIF
,
2148 ir_to_mesa_undef_dst
, ir_to_mesa_undef
);
2151 ir_to_mesa_visitor::ir_to_mesa_visitor()
2153 result
.file
= PROGRAM_UNDEFINED
;
2155 next_signature_id
= 1;
2156 current_function
= NULL
;
2157 mem_ctx
= talloc_new(NULL
);
2160 ir_to_mesa_visitor::~ir_to_mesa_visitor()
2162 talloc_free(mem_ctx
);
2165 static struct prog_src_register
2166 mesa_src_reg_from_ir_src_reg(ir_to_mesa_src_reg reg
)
2168 struct prog_src_register mesa_reg
;
2170 mesa_reg
.File
= reg
.file
;
2171 assert(reg
.index
< (1 << INST_INDEX_BITS
) - 1);
2172 mesa_reg
.Index
= reg
.index
;
2173 mesa_reg
.Swizzle
= reg
.swizzle
;
2174 mesa_reg
.RelAddr
= reg
.reladdr
!= NULL
;
2175 mesa_reg
.Negate
= reg
.negate
;
2177 mesa_reg
.HasIndex2
= GL_FALSE
;
2178 mesa_reg
.RelAddr2
= 0;
2179 mesa_reg
.Index2
= 0;
2185 set_branchtargets(ir_to_mesa_visitor
*v
,
2186 struct prog_instruction
*mesa_instructions
,
2187 int num_instructions
)
2189 int if_count
= 0, loop_count
= 0;
2190 int *if_stack
, *loop_stack
;
2191 int if_stack_pos
= 0, loop_stack_pos
= 0;
2194 for (i
= 0; i
< num_instructions
; i
++) {
2195 switch (mesa_instructions
[i
].Opcode
) {
2199 case OPCODE_BGNLOOP
:
2204 mesa_instructions
[i
].BranchTarget
= -1;
2211 if_stack
= talloc_zero_array(v
->mem_ctx
, int, if_count
);
2212 loop_stack
= talloc_zero_array(v
->mem_ctx
, int, loop_count
);
2214 for (i
= 0; i
< num_instructions
; i
++) {
2215 switch (mesa_instructions
[i
].Opcode
) {
2217 if_stack
[if_stack_pos
] = i
;
2221 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2222 if_stack
[if_stack_pos
- 1] = i
;
2225 mesa_instructions
[if_stack
[if_stack_pos
- 1]].BranchTarget
= i
;
2228 case OPCODE_BGNLOOP
:
2229 loop_stack
[loop_stack_pos
] = i
;
2232 case OPCODE_ENDLOOP
:
2234 /* Rewrite any breaks/conts at this nesting level (haven't
2235 * already had a BranchTarget assigned) to point to the end
2238 for (j
= loop_stack
[loop_stack_pos
]; j
< i
; j
++) {
2239 if (mesa_instructions
[j
].Opcode
== OPCODE_BRK
||
2240 mesa_instructions
[j
].Opcode
== OPCODE_CONT
) {
2241 if (mesa_instructions
[j
].BranchTarget
== -1) {
2242 mesa_instructions
[j
].BranchTarget
= i
;
2246 /* The loop ends point at each other. */
2247 mesa_instructions
[i
].BranchTarget
= loop_stack
[loop_stack_pos
];
2248 mesa_instructions
[loop_stack
[loop_stack_pos
]].BranchTarget
= i
;
2251 foreach_iter(exec_list_iterator
, iter
, v
->function_signatures
) {
2252 function_entry
*entry
= (function_entry
*)iter
.get();
2254 if (entry
->sig_id
== mesa_instructions
[i
].BranchTarget
) {
2255 mesa_instructions
[i
].BranchTarget
= entry
->inst
;
2267 print_program(struct prog_instruction
*mesa_instructions
,
2268 ir_instruction
**mesa_instruction_annotation
,
2269 int num_instructions
)
2271 ir_instruction
*last_ir
= NULL
;
2275 for (i
= 0; i
< num_instructions
; i
++) {
2276 struct prog_instruction
*mesa_inst
= mesa_instructions
+ i
;
2277 ir_instruction
*ir
= mesa_instruction_annotation
[i
];
2279 fprintf(stdout
, "%3d: ", i
);
2281 if (last_ir
!= ir
&& ir
) {
2284 for (j
= 0; j
< indent
; j
++) {
2285 fprintf(stdout
, " ");
2291 fprintf(stdout
, " "); /* line number spacing. */
2294 indent
= _mesa_fprint_instruction_opt(stdout
, mesa_inst
, indent
,
2295 PROG_PRINT_DEBUG
, NULL
);
2300 count_resources(struct gl_program
*prog
)
2304 prog
->SamplersUsed
= 0;
2306 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
2307 struct prog_instruction
*inst
= &prog
->Instructions
[i
];
2309 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
2310 prog
->SamplerTargets
[inst
->TexSrcUnit
] =
2311 (gl_texture_index
)inst
->TexSrcTarget
;
2312 prog
->SamplersUsed
|= 1 << inst
->TexSrcUnit
;
2313 if (inst
->TexShadow
) {
2314 prog
->ShadowSamplers
|= 1 << inst
->TexSrcUnit
;
2319 _mesa_update_shader_textures_used(prog
);
2322 struct uniform_sort
{
2323 struct gl_uniform
*u
;
2327 /* The shader_program->Uniforms list is almost sorted in increasing
2328 * uniform->{Frag,Vert}Pos locations, but not quite when there are
2329 * uniforms shared between targets. We need to add parameters in
2330 * increasing order for the targets.
2333 sort_uniforms(const void *a
, const void *b
)
2335 struct uniform_sort
*u1
= (struct uniform_sort
*)a
;
2336 struct uniform_sort
*u2
= (struct uniform_sort
*)b
;
2338 return u1
->pos
- u2
->pos
;
2341 /* Add the uniforms to the parameters. The linker chose locations
2342 * in our parameters lists (which weren't created yet), which the
2343 * uniforms code will use to poke values into our parameters list
2344 * when uniforms are updated.
2347 add_uniforms_to_parameters_list(struct gl_shader_program
*shader_program
,
2348 struct gl_shader
*shader
,
2349 struct gl_program
*prog
)
2352 unsigned int next_sampler
= 0, num_uniforms
= 0;
2353 struct uniform_sort
*sorted_uniforms
;
2355 sorted_uniforms
= talloc_array(NULL
, struct uniform_sort
,
2356 shader_program
->Uniforms
->NumUniforms
);
2358 for (i
= 0; i
< shader_program
->Uniforms
->NumUniforms
; i
++) {
2359 struct gl_uniform
*uniform
= shader_program
->Uniforms
->Uniforms
+ i
;
2360 int parameter_index
= -1;
2362 switch (shader
->Type
) {
2363 case GL_VERTEX_SHADER
:
2364 parameter_index
= uniform
->VertPos
;
2366 case GL_FRAGMENT_SHADER
:
2367 parameter_index
= uniform
->FragPos
;
2369 case GL_GEOMETRY_SHADER
:
2370 parameter_index
= uniform
->GeomPos
;
2374 /* Only add uniforms used in our target. */
2375 if (parameter_index
!= -1) {
2376 sorted_uniforms
[num_uniforms
].pos
= parameter_index
;
2377 sorted_uniforms
[num_uniforms
].u
= uniform
;
2382 qsort(sorted_uniforms
, num_uniforms
, sizeof(struct uniform_sort
),
2385 for (i
= 0; i
< num_uniforms
; i
++) {
2386 struct gl_uniform
*uniform
= sorted_uniforms
[i
].u
;
2387 int parameter_index
= sorted_uniforms
[i
].pos
;
2388 const glsl_type
*type
= uniform
->Type
;
2391 if (type
->is_vector() ||
2392 type
->is_scalar()) {
2393 size
= type
->vector_elements
;
2395 size
= type_size(type
) * 4;
2398 gl_register_file file
;
2399 if (type
->is_sampler() ||
2400 (type
->is_array() && type
->fields
.array
->is_sampler())) {
2401 file
= PROGRAM_SAMPLER
;
2403 file
= PROGRAM_UNIFORM
;
2406 GLint index
= _mesa_lookup_parameter_index(prog
->Parameters
, -1,
2410 index
= _mesa_add_parameter(prog
->Parameters
, file
,
2411 uniform
->Name
, size
, type
->gl_type
,
2414 /* Sampler uniform values are stored in prog->SamplerUnits,
2415 * and the entry in that array is selected by this index we
2416 * store in ParameterValues[].
2418 if (file
== PROGRAM_SAMPLER
) {
2419 for (unsigned int j
= 0; j
< size
/ 4; j
++)
2420 prog
->Parameters
->ParameterValues
[index
+ j
][0] = next_sampler
++;
2423 /* The location chosen in the Parameters list here (returned
2424 * from _mesa_add_uniform) has to match what the linker chose.
2426 if (index
!= parameter_index
) {
2427 fail_link(shader_program
, "Allocation of uniform `%s' to target "
2428 "failed (%d vs %d)\n",
2429 uniform
->Name
, index
, parameter_index
);
2434 talloc_free(sorted_uniforms
);
2438 set_uniform_initializer(GLcontext
*ctx
, void *mem_ctx
,
2439 struct gl_shader_program
*shader_program
,
2440 const char *name
, const glsl_type
*type
,
2443 if (type
->is_record()) {
2444 ir_constant
*field_constant
;
2446 field_constant
= (ir_constant
*)val
->components
.get_head();
2448 for (unsigned int i
= 0; i
< type
->length
; i
++) {
2449 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
2450 const char *field_name
= talloc_asprintf(mem_ctx
, "%s.%s", name
,
2451 type
->fields
.structure
[i
].name
);
2452 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, field_name
,
2453 field_type
, field_constant
);
2454 field_constant
= (ir_constant
*)field_constant
->next
;
2459 int loc
= _mesa_get_uniform_location(ctx
, shader_program
, name
);
2462 fail_link(shader_program
,
2463 "Couldn't find uniform for initializer %s\n", name
);
2467 for (unsigned int i
= 0; i
< (type
->is_array() ? type
->length
: 1); i
++) {
2468 ir_constant
*element
;
2469 const glsl_type
*element_type
;
2470 if (type
->is_array()) {
2471 element
= val
->array_elements
[i
];
2472 element_type
= type
->fields
.array
;
2475 element_type
= type
;
2480 if (element_type
->base_type
== GLSL_TYPE_BOOL
) {
2481 int *conv
= talloc_array(mem_ctx
, int, element_type
->components());
2482 for (unsigned int j
= 0; j
< element_type
->components(); j
++) {
2483 conv
[j
] = element
->value
.b
[j
];
2485 values
= (void *)conv
;
2486 element_type
= glsl_type::get_instance(GLSL_TYPE_INT
,
2487 element_type
->vector_elements
,
2490 values
= &element
->value
;
2493 if (element_type
->is_matrix()) {
2494 _mesa_uniform_matrix(ctx
, shader_program
,
2495 element_type
->matrix_columns
,
2496 element_type
->vector_elements
,
2497 loc
, 1, GL_FALSE
, (GLfloat
*)values
);
2498 loc
+= element_type
->matrix_columns
;
2500 _mesa_uniform(ctx
, shader_program
, loc
, element_type
->matrix_columns
,
2501 values
, element_type
->gl_type
);
2502 loc
+= type_size(element_type
);
2508 set_uniform_initializers(GLcontext
*ctx
,
2509 struct gl_shader_program
*shader_program
)
2511 void *mem_ctx
= NULL
;
2513 for (unsigned int i
= 0; i
< shader_program
->_NumLinkedShaders
; i
++) {
2514 struct gl_shader
*shader
= shader_program
->_LinkedShaders
[i
];
2515 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2516 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2517 ir_variable
*var
= ir
->as_variable();
2519 if (!var
|| var
->mode
!= ir_var_uniform
|| !var
->constant_value
)
2523 mem_ctx
= talloc_new(NULL
);
2525 set_uniform_initializer(ctx
, mem_ctx
, shader_program
, var
->name
,
2526 var
->type
, var
->constant_value
);
2530 talloc_free(mem_ctx
);
2534 get_mesa_program(GLcontext
*ctx
, struct gl_shader_program
*shader_program
,
2535 struct gl_shader
*shader
)
2537 ir_to_mesa_visitor v
;
2538 struct prog_instruction
*mesa_instructions
, *mesa_inst
;
2539 ir_instruction
**mesa_instruction_annotation
;
2541 struct gl_program
*prog
;
2543 const char *target_string
;
2545 struct gl_shader_compiler_options
*options
=
2546 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(shader
->Type
)];
2548 switch (shader
->Type
) {
2549 case GL_VERTEX_SHADER
:
2550 target
= GL_VERTEX_PROGRAM_ARB
;
2551 target_string
= "vertex";
2553 case GL_FRAGMENT_SHADER
:
2554 target
= GL_FRAGMENT_PROGRAM_ARB
;
2555 target_string
= "fragment";
2558 assert(!"should not be reached");
2562 validate_ir_tree(shader
->ir
);
2564 prog
= ctx
->Driver
.NewProgram(ctx
, target
, shader_program
->Name
);
2567 prog
->Parameters
= _mesa_new_parameter_list();
2568 prog
->Varying
= _mesa_new_parameter_list();
2569 prog
->Attributes
= _mesa_new_parameter_list();
2572 v
.shader_program
= shader_program
;
2573 v
.options
= options
;
2575 add_uniforms_to_parameters_list(shader_program
, shader
, prog
);
2577 /* Emit Mesa IR for main(). */
2578 visit_exec_list(shader
->ir
, &v
);
2579 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_END
);
2581 /* Now emit bodies for any functions that were used. */
2583 progress
= GL_FALSE
;
2585 foreach_iter(exec_list_iterator
, iter
, v
.function_signatures
) {
2586 function_entry
*entry
= (function_entry
*)iter
.get();
2588 if (!entry
->bgn_inst
) {
2589 v
.current_function
= entry
;
2591 entry
->bgn_inst
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_BGNSUB
);
2592 entry
->bgn_inst
->function
= entry
;
2594 visit_exec_list(&entry
->sig
->body
, &v
);
2596 ir_to_mesa_instruction
*last
;
2597 last
= (ir_to_mesa_instruction
*)v
.instructions
.get_tail();
2598 if (last
->op
!= OPCODE_RET
)
2599 v
.ir_to_mesa_emit_op0(NULL
, OPCODE_RET
);
2601 ir_to_mesa_instruction
*end
;
2602 end
= v
.ir_to_mesa_emit_op0(NULL
, OPCODE_ENDSUB
);
2603 end
->function
= entry
;
2610 prog
->NumTemporaries
= v
.next_temp
;
2612 int num_instructions
= 0;
2613 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2618 (struct prog_instruction
*)calloc(num_instructions
,
2619 sizeof(*mesa_instructions
));
2620 mesa_instruction_annotation
= talloc_array(v
.mem_ctx
, ir_instruction
*,
2623 mesa_inst
= mesa_instructions
;
2625 foreach_iter(exec_list_iterator
, iter
, v
.instructions
) {
2626 ir_to_mesa_instruction
*inst
= (ir_to_mesa_instruction
*)iter
.get();
2628 mesa_inst
->Opcode
= inst
->op
;
2629 mesa_inst
->CondUpdate
= inst
->cond_update
;
2630 mesa_inst
->DstReg
.File
= inst
->dst_reg
.file
;
2631 mesa_inst
->DstReg
.Index
= inst
->dst_reg
.index
;
2632 mesa_inst
->DstReg
.CondMask
= inst
->dst_reg
.cond_mask
;
2633 mesa_inst
->DstReg
.WriteMask
= inst
->dst_reg
.writemask
;
2634 mesa_inst
->DstReg
.RelAddr
= inst
->dst_reg
.reladdr
!= NULL
;
2635 mesa_inst
->SrcReg
[0] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[0]);
2636 mesa_inst
->SrcReg
[1] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[1]);
2637 mesa_inst
->SrcReg
[2] = mesa_src_reg_from_ir_src_reg(inst
->src_reg
[2]);
2638 mesa_inst
->TexSrcUnit
= inst
->sampler
;
2639 mesa_inst
->TexSrcTarget
= inst
->tex_target
;
2640 mesa_inst
->TexShadow
= inst
->tex_shadow
;
2641 mesa_instruction_annotation
[i
] = inst
->ir
;
2643 /* Set IndirectRegisterFiles. */
2644 if (mesa_inst
->DstReg
.RelAddr
)
2645 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->DstReg
.File
;
2647 for (unsigned src
= 0; src
< 3; src
++)
2648 if (mesa_inst
->SrcReg
[src
].RelAddr
)
2649 prog
->IndirectRegisterFiles
|= 1 << mesa_inst
->SrcReg
[src
].File
;
2651 if (options
->EmitNoIfs
&& mesa_inst
->Opcode
== OPCODE_IF
) {
2652 fail_link(shader_program
, "Couldn't flatten if statement\n");
2655 switch (mesa_inst
->Opcode
) {
2657 inst
->function
->inst
= i
;
2658 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2661 mesa_inst
->Comment
= strdup(inst
->function
->sig
->function_name());
2664 mesa_inst
->BranchTarget
= inst
->function
->sig_id
; /* rewritten later */
2667 prog
->NumAddressRegs
= 1;
2677 set_branchtargets(&v
, mesa_instructions
, num_instructions
);
2679 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2681 printf("GLSL IR for linked %s program %d:\n", target_string
,
2682 shader_program
->Name
);
2683 _mesa_print_ir(shader
->ir
, NULL
);
2686 printf("Mesa IR for linked %s program %d:\n", target_string
,
2687 shader_program
->Name
);
2688 print_program(mesa_instructions
, mesa_instruction_annotation
,
2692 prog
->Instructions
= mesa_instructions
;
2693 prog
->NumInstructions
= num_instructions
;
2695 do_set_program_inouts(shader
->ir
, prog
);
2696 count_resources(prog
);
2698 _mesa_reference_program(ctx
, &shader
->Program
, prog
);
2700 if ((ctx
->Shader
.Flags
& GLSL_NO_OPT
) == 0) {
2701 _mesa_optimize_program(ctx
, prog
);
2709 _mesa_ir_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
2711 assert(shader
->CompileStatus
);
2718 _mesa_ir_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
2720 assert(prog
->LinkStatus
);
2722 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2724 exec_list
*ir
= prog
->_LinkedShaders
[i
]->ir
;
2725 struct gl_shader_compiler_options
*options
=
2726 &ctx
->ShaderCompilerOptions
[_mesa_shader_type_to_index(prog
->_LinkedShaders
[i
]->Type
)];
2732 do_mat_op_to_vec(ir
);
2733 do_mod_to_fract(ir
);
2734 do_div_to_mul_rcp(ir
);
2735 do_explog_to_explog2(ir
);
2737 progress
= do_lower_jumps(ir
, true, true, options
->EmitNoMainReturn
, options
->EmitNoCont
, options
->EmitNoLoops
) || progress
;
2739 progress
= do_common_optimization(ir
, true, options
->MaxUnrollIterations
) || progress
;
2741 if (options
->EmitNoIfs
)
2742 progress
= do_if_to_cond_assign(ir
) || progress
;
2744 if (options
->EmitNoNoise
)
2745 progress
= lower_noise(ir
) || progress
;
2747 /* If there are forms of indirect addressing that the driver
2748 * cannot handle, perform the lowering pass.
2750 if (options
->EmitNoIndirectInput
|| options
->EmitNoIndirectOutput
2751 || options
->EmitNoIndirectTemp
|| options
->EmitNoIndirectUniform
)
2753 lower_variable_index_to_cond_assign(ir
,
2754 options
->EmitNoIndirectInput
,
2755 options
->EmitNoIndirectOutput
,
2756 options
->EmitNoIndirectTemp
,
2757 options
->EmitNoIndirectUniform
)
2760 progress
= do_vec_index_to_cond_assign(ir
) || progress
;
2763 validate_ir_tree(ir
);
2766 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2767 struct gl_program
*linked_prog
;
2770 linked_prog
= get_mesa_program(ctx
, prog
, prog
->_LinkedShaders
[i
]);
2772 switch (prog
->_LinkedShaders
[i
]->Type
) {
2773 case GL_VERTEX_SHADER
:
2774 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
,
2775 (struct gl_vertex_program
*)linked_prog
);
2776 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
2779 case GL_FRAGMENT_SHADER
:
2780 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
,
2781 (struct gl_fragment_program
*)linked_prog
);
2782 ok
= ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
2789 _mesa_reference_program(ctx
, &linked_prog
, NULL
);
2796 _mesa_glsl_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
2798 struct _mesa_glsl_parse_state
*state
=
2799 new(shader
) _mesa_glsl_parse_state(ctx
, shader
->Type
, shader
);
2801 const char *source
= shader
->Source
;
2802 /* Check if the user called glCompileShader without first calling
2803 * glShaderSource. This should fail to compile, but not raise a GL_ERROR.
2805 if (source
== NULL
) {
2806 shader
->CompileStatus
= GL_FALSE
;
2810 state
->error
= preprocess(state
, &source
, &state
->info_log
,
2811 &ctx
->Extensions
, ctx
->API
);
2813 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2814 printf("GLSL source for shader %d:\n", shader
->Name
);
2815 printf("%s\n", shader
->Source
);
2818 if (!state
->error
) {
2819 _mesa_glsl_lexer_ctor(state
, source
);
2820 _mesa_glsl_parse(state
);
2821 _mesa_glsl_lexer_dtor(state
);
2824 talloc_free(shader
->ir
);
2825 shader
->ir
= new(shader
) exec_list
;
2826 if (!state
->error
&& !state
->translation_unit
.is_empty())
2827 _mesa_ast_to_hir(shader
->ir
, state
);
2829 if (!state
->error
&& !shader
->ir
->is_empty()) {
2830 validate_ir_tree(shader
->ir
);
2832 /* Do some optimization at compile time to reduce shader IR size
2833 * and reduce later work if the same shader is linked multiple times
2835 while (do_common_optimization(shader
->ir
, false, 32))
2838 validate_ir_tree(shader
->ir
);
2841 shader
->symbols
= state
->symbols
;
2843 shader
->CompileStatus
= !state
->error
;
2844 shader
->InfoLog
= state
->info_log
;
2845 shader
->Version
= state
->language_version
;
2846 memcpy(shader
->builtins_to_link
, state
->builtins_to_link
,
2847 sizeof(shader
->builtins_to_link
[0]) * state
->num_builtins_to_link
);
2848 shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
2850 if (ctx
->Shader
.Flags
& GLSL_LOG
) {
2851 _mesa_write_shader_to_file(shader
);
2854 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2855 if (shader
->CompileStatus
) {
2856 printf("GLSL IR for shader %d:\n", shader
->Name
);
2857 _mesa_print_ir(shader
->ir
, NULL
);
2860 printf("GLSL shader %d failed to compile.\n", shader
->Name
);
2862 if (shader
->InfoLog
&& shader
->InfoLog
[0] != 0) {
2863 printf("GLSL shader %d info log:\n", shader
->Name
);
2864 printf("%s\n", shader
->InfoLog
);
2868 /* Retain any live IR, but trash the rest. */
2869 reparent_ir(shader
->ir
, shader
->ir
);
2873 if (shader
->CompileStatus
) {
2874 if (!ctx
->Driver
.CompileShader(ctx
, shader
))
2875 shader
->CompileStatus
= GL_FALSE
;
2880 _mesa_glsl_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
2884 _mesa_clear_shader_program_data(ctx
, prog
);
2886 prog
->LinkStatus
= GL_TRUE
;
2888 for (i
= 0; i
< prog
->NumShaders
; i
++) {
2889 if (!prog
->Shaders
[i
]->CompileStatus
) {
2890 fail_link(prog
, "linking with uncompiled shader");
2891 prog
->LinkStatus
= GL_FALSE
;
2895 prog
->Varying
= _mesa_new_parameter_list();
2896 _mesa_reference_vertprog(ctx
, &prog
->VertexProgram
, NULL
);
2897 _mesa_reference_fragprog(ctx
, &prog
->FragmentProgram
, NULL
);
2899 if (prog
->LinkStatus
) {
2900 link_shaders(ctx
, prog
);
2903 if (prog
->LinkStatus
) {
2904 if (!ctx
->Driver
.LinkShader(ctx
, prog
)) {
2905 prog
->LinkStatus
= GL_FALSE
;
2909 set_uniform_initializers(ctx
, prog
);
2911 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
2912 if (!prog
->LinkStatus
) {
2913 printf("GLSL shader program %d failed to link\n", prog
->Name
);
2916 if (prog
->InfoLog
&& prog
->InfoLog
[0] != 0) {
2917 printf("GLSL shader program %d info log:\n", prog
->Name
);
2918 printf("%s\n", prog
->InfoLog
);