2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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 shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 #define DEBUG_PARSING 0
28 * \file arbprogparse.c
29 * ARB_*_program parser core
34 Notes on program parameters, etc.
36 The instructions we emit will use six kinds of source registers:
38 PROGRAM_INPUT - input registers
39 PROGRAM_TEMPORARY - temp registers
40 PROGRAM_ADDRESS - address/indirect register
41 PROGRAM_SAMPLER - texture sampler
42 PROGRAM_CONSTANT - indexes into program->Parameters, a known constant/literal
43 PROGRAM_STATE_VAR - indexes into program->Parameters, and may actually be:
44 + a state variable, like "state.fog.color", or
45 + a pointer to a "program.local[k]" parameter, or
46 + a pointer to a "program.env[k]" parameter
48 Basically, all the program.local[] and program.env[] values will get mapped
49 into the unified gl_program->Parameters array. This solves the problem of
50 having three separate program parameter arrays.
54 #include "main/glheader.h"
55 #include "main/imports.h"
56 #include "main/context.h"
57 #include "main/mtypes.h"
58 #include "arbprogparse.h"
59 #include "programopt.h"
60 #include "prog_parameter.h"
61 #include "prog_statevars.h"
62 #include "prog_instruction.h"
63 #include "prog_optimize.h"
64 #include "program_parser.h"
68 _mesa_parse_arb_fragment_program(struct gl_context
* ctx
, GLenum target
,
69 const GLvoid
*str
, GLsizei len
,
70 struct gl_fragment_program
*program
)
72 struct gl_program prog
;
73 struct asm_parser_state state
;
76 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
78 memset(&prog
, 0, sizeof(prog
));
79 memset(&state
, 0, sizeof(state
));
82 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
,
84 /* Error in the program. Just return. */
88 if ((ctx
->_Shader
->Flags
& GLSL_NO_OPT
) == 0)
89 _mesa_optimize_program(ctx
, &prog
);
91 free(program
->Base
.String
);
93 /* Copy the relevant contents of the arb_program struct into the
94 * fragment_program struct.
96 program
->Base
.String
= prog
.String
;
97 program
->Base
.NumInstructions
= prog
.NumInstructions
;
98 program
->Base
.NumTemporaries
= prog
.NumTemporaries
;
99 program
->Base
.NumParameters
= prog
.NumParameters
;
100 program
->Base
.NumAttributes
= prog
.NumAttributes
;
101 program
->Base
.NumAddressRegs
= prog
.NumAddressRegs
;
102 program
->Base
.NumNativeInstructions
= prog
.NumNativeInstructions
;
103 program
->Base
.NumNativeTemporaries
= prog
.NumNativeTemporaries
;
104 program
->Base
.NumNativeParameters
= prog
.NumNativeParameters
;
105 program
->Base
.NumNativeAttributes
= prog
.NumNativeAttributes
;
106 program
->Base
.NumNativeAddressRegs
= prog
.NumNativeAddressRegs
;
107 program
->Base
.NumAluInstructions
= prog
.NumAluInstructions
;
108 program
->Base
.NumTexInstructions
= prog
.NumTexInstructions
;
109 program
->Base
.NumTexIndirections
= prog
.NumTexIndirections
;
110 program
->Base
.NumNativeAluInstructions
= prog
.NumAluInstructions
;
111 program
->Base
.NumNativeTexInstructions
= prog
.NumTexInstructions
;
112 program
->Base
.NumNativeTexIndirections
= prog
.NumTexIndirections
;
113 program
->Base
.InputsRead
= prog
.InputsRead
;
114 program
->Base
.OutputsWritten
= prog
.OutputsWritten
;
115 program
->Base
.IndirectRegisterFiles
= prog
.IndirectRegisterFiles
;
116 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++) {
117 program
->Base
.TexturesUsed
[i
] = prog
.TexturesUsed
[i
];
118 if (prog
.TexturesUsed
[i
])
119 program
->Base
.SamplersUsed
|= (1 << i
);
121 program
->Base
.ShadowSamplers
= prog
.ShadowSamplers
;
122 program
->OriginUpperLeft
= state
.option
.OriginUpperLeft
;
123 program
->PixelCenterInteger
= state
.option
.PixelCenterInteger
;
125 program
->UsesKill
= state
.fragment
.UsesKill
;
126 program
->UsesDFdy
= state
.fragment
.UsesDFdy
;
128 free(program
->Base
.Instructions
);
129 program
->Base
.Instructions
= prog
.Instructions
;
131 if (program
->Base
.Parameters
)
132 _mesa_free_parameter_list(program
->Base
.Parameters
);
133 program
->Base
.Parameters
= prog
.Parameters
;
135 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
136 * or similar. We used to leave this up to drivers, but it appears
137 * there's no hardware that wants to do fog in a discrete stage separate
138 * from the fragment shader.
140 if (state
.option
.Fog
!= OPTION_NONE
) {
141 static const GLenum fog_modes
[4] = {
142 GL_NONE
, GL_EXP
, GL_EXP2
, GL_LINEAR
145 /* XXX: we should somehow recompile this to remove clamping if disabled
146 * On the ATI driver, this is unclampled if fragment clamping is disabled
148 _mesa_append_fog_code(ctx
, program
, fog_modes
[state
.option
.Fog
], GL_TRUE
);
152 printf("____________Fragment program %u ________\n", program
->Base
.Id
);
153 _mesa_print_program(&program
->Base
);
160 * Parse the vertex program string. If success, update the given
161 * vertex_program object with the new program. Else, leave the vertex_program
165 _mesa_parse_arb_vertex_program(struct gl_context
*ctx
, GLenum target
,
166 const GLvoid
*str
, GLsizei len
,
167 struct gl_vertex_program
*program
)
169 struct gl_program prog
;
170 struct asm_parser_state state
;
172 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
174 memset(&prog
, 0, sizeof(prog
));
175 memset(&state
, 0, sizeof(state
));
178 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
,
180 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glProgramString(bad program)");
184 if ((ctx
->_Shader
->Flags
& GLSL_NO_OPT
) == 0)
185 _mesa_optimize_program(ctx
, &prog
);
187 free(program
->Base
.String
);
189 /* Copy the relevant contents of the arb_program struct into the
190 * vertex_program struct.
192 program
->Base
.String
= prog
.String
;
193 program
->Base
.NumInstructions
= prog
.NumInstructions
;
194 program
->Base
.NumTemporaries
= prog
.NumTemporaries
;
195 program
->Base
.NumParameters
= prog
.NumParameters
;
196 program
->Base
.NumAttributes
= prog
.NumAttributes
;
197 program
->Base
.NumAddressRegs
= prog
.NumAddressRegs
;
198 program
->Base
.NumNativeInstructions
= prog
.NumNativeInstructions
;
199 program
->Base
.NumNativeTemporaries
= prog
.NumNativeTemporaries
;
200 program
->Base
.NumNativeParameters
= prog
.NumNativeParameters
;
201 program
->Base
.NumNativeAttributes
= prog
.NumNativeAttributes
;
202 program
->Base
.NumNativeAddressRegs
= prog
.NumNativeAddressRegs
;
203 program
->Base
.InputsRead
= prog
.InputsRead
;
204 program
->Base
.OutputsWritten
= prog
.OutputsWritten
;
205 program
->Base
.IndirectRegisterFiles
= prog
.IndirectRegisterFiles
;
206 program
->IsPositionInvariant
= (state
.option
.PositionInvariant
)
207 ? GL_TRUE
: GL_FALSE
;
209 free(program
->Base
.Instructions
);
210 program
->Base
.Instructions
= prog
.Instructions
;
212 if (program
->Base
.Parameters
)
213 _mesa_free_parameter_list(program
->Base
.Parameters
);
214 program
->Base
.Parameters
= prog
.Parameters
;
217 printf("____________Vertex program %u __________\n", program
->Base
.Id
);
218 _mesa_print_program(&program
->Base
);