2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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/macros.h"
58 #include "main/mtypes.h"
59 #include "arbprogparse.h"
61 #include "programopt.h"
62 #include "prog_parameter.h"
63 #include "prog_statevars.h"
64 #include "prog_instruction.h"
65 #include "program_parser.h"
69 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
70 const GLvoid
*str
, GLsizei len
,
71 struct gl_fragment_program
*program
)
73 struct gl_program prog
;
74 struct asm_parser_state state
;
77 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
79 memset(&prog
, 0, sizeof(prog
));
80 memset(&state
, 0, sizeof(state
));
83 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
,
85 /* Error in the program. Just return. */
89 if (program
->Base
.String
!= NULL
)
90 _mesa_free(program
->Base
.String
);
92 /* Copy the relevant contents of the arb_program struct into the
93 * fragment_program struct.
95 program
->Base
.String
= prog
.String
;
96 program
->Base
.NumInstructions
= prog
.NumInstructions
;
97 program
->Base
.NumTemporaries
= prog
.NumTemporaries
;
98 program
->Base
.NumParameters
= prog
.NumParameters
;
99 program
->Base
.NumAttributes
= prog
.NumAttributes
;
100 program
->Base
.NumAddressRegs
= prog
.NumAddressRegs
;
101 program
->Base
.NumNativeInstructions
= prog
.NumNativeInstructions
;
102 program
->Base
.NumNativeTemporaries
= prog
.NumNativeTemporaries
;
103 program
->Base
.NumNativeParameters
= prog
.NumNativeParameters
;
104 program
->Base
.NumNativeAttributes
= prog
.NumNativeAttributes
;
105 program
->Base
.NumNativeAddressRegs
= prog
.NumNativeAddressRegs
;
106 program
->Base
.NumAluInstructions
= prog
.NumAluInstructions
;
107 program
->Base
.NumTexInstructions
= prog
.NumTexInstructions
;
108 program
->Base
.NumTexIndirections
= prog
.NumTexIndirections
;
109 program
->Base
.NumNativeAluInstructions
= prog
.NumAluInstructions
;
110 program
->Base
.NumNativeTexInstructions
= prog
.NumTexInstructions
;
111 program
->Base
.NumNativeTexIndirections
= prog
.NumTexIndirections
;
112 program
->Base
.InputsRead
= prog
.InputsRead
;
113 program
->Base
.OutputsWritten
= prog
.OutputsWritten
;
114 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++) {
115 program
->Base
.TexturesUsed
[i
] = prog
.TexturesUsed
[i
];
116 if (prog
.TexturesUsed
[i
])
117 program
->Base
.SamplersUsed
|= (1 << i
);
119 program
->Base
.ShadowSamplers
= prog
.ShadowSamplers
;
120 switch (state
.option
.Fog
) {
121 case OPTION_FOG_EXP
: program
->FogOption
= GL_EXP
; break;
122 case OPTION_FOG_EXP2
: program
->FogOption
= GL_EXP2
; break;
123 case OPTION_FOG_LINEAR
: program
->FogOption
= GL_LINEAR
; break;
124 default: program
->FogOption
= GL_NONE
; break;
127 program
->UsesKill
= state
.fragment
.UsesKill
;
129 if (program
->FogOption
)
130 program
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
132 if (program
->Base
.Instructions
)
133 _mesa_free(program
->Base
.Instructions
);
134 program
->Base
.Instructions
= prog
.Instructions
;
136 if (program
->Base
.Parameters
)
137 _mesa_free_parameter_list(program
->Base
.Parameters
);
138 program
->Base
.Parameters
= prog
.Parameters
;
140 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
141 * or similar. We used to leave this up to drivers, but it appears
142 * there's no hardware that wants to do fog in a discrete stage separate
143 * from the fragment shader.
145 if (program
->FogOption
!= GL_NONE
) {
146 _mesa_append_fog_code(ctx
, program
);
147 program
->FogOption
= GL_NONE
;
151 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.Id
);
152 _mesa_print_program(&program
->Base
);
159 * Parse the vertex program string. If success, update the given
160 * vertex_program object with the new program. Else, leave the vertex_program
164 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
165 const GLvoid
*str
, GLsizei len
,
166 struct gl_vertex_program
*program
)
168 struct gl_program prog
;
169 struct asm_parser_state state
;
171 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
173 memset(&prog
, 0, sizeof(prog
));
174 memset(&state
, 0, sizeof(state
));
177 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
,
179 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glProgramString(bad program)");
183 if (program
->Base
.String
!= NULL
)
184 _mesa_free(program
->Base
.String
);
186 /* Copy the relevant contents of the arb_program struct into the
187 * vertex_program struct.
189 program
->Base
.String
= prog
.String
;
190 program
->Base
.NumInstructions
= prog
.NumInstructions
;
191 program
->Base
.NumTemporaries
= prog
.NumTemporaries
;
192 program
->Base
.NumParameters
= prog
.NumParameters
;
193 program
->Base
.NumAttributes
= prog
.NumAttributes
;
194 program
->Base
.NumAddressRegs
= prog
.NumAddressRegs
;
195 program
->Base
.NumNativeInstructions
= prog
.NumNativeInstructions
;
196 program
->Base
.NumNativeTemporaries
= prog
.NumNativeTemporaries
;
197 program
->Base
.NumNativeParameters
= prog
.NumNativeParameters
;
198 program
->Base
.NumNativeAttributes
= prog
.NumNativeAttributes
;
199 program
->Base
.NumNativeAddressRegs
= prog
.NumNativeAddressRegs
;
200 program
->Base
.InputsRead
= prog
.InputsRead
;
201 program
->Base
.OutputsWritten
= prog
.OutputsWritten
;
202 program
->IsPositionInvariant
= (state
.option
.PositionInvariant
)
203 ? GL_TRUE
: GL_FALSE
;
205 if (program
->Base
.Instructions
)
206 _mesa_free(program
->Base
.Instructions
);
207 program
->Base
.Instructions
= prog
.Instructions
;
209 if (program
->Base
.Parameters
)
210 _mesa_free_parameter_list(program
->Base
.Parameters
);
211 program
->Base
.Parameters
= prog
.Parameters
;
214 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
215 _mesa_print_program(&program
->Base
);