OSDN Git Service

mesa: remove outdated version lines in comments
[android-x86/external-mesa.git] / src / mesa / program / arbprogparse.c
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #define DEBUG_PARSING 0
26
27 /**
28  * \file arbprogparse.c
29  * ARB_*_program parser core
30  * \author Karl Rasche
31  */
32
33 /**
34 Notes on program parameters, etc.
35
36 The instructions we emit will use six kinds of source registers:
37
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
47
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.
51 */
52
53
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 "program_parser.h"
64
65
66 void
67 _mesa_parse_arb_fragment_program(struct gl_context* ctx, GLenum target,
68                                  const GLvoid *str, GLsizei len,
69                                  struct gl_fragment_program *program)
70 {
71    struct gl_program prog;
72    struct asm_parser_state state;
73    GLuint i;
74
75    ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
76
77    memset(&prog, 0, sizeof(prog));
78    memset(&state, 0, sizeof(state));
79    state.prog = &prog;
80
81    if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
82                                 &state)) {
83       /* Error in the program. Just return. */
84       return;
85    }
86
87    free(program->Base.String);
88
89    /* Copy the relevant contents of the arb_program struct into the
90     * fragment_program struct.
91     */
92    program->Base.String          = prog.String;
93    program->Base.NumInstructions = prog.NumInstructions;
94    program->Base.NumTemporaries  = prog.NumTemporaries;
95    program->Base.NumParameters   = prog.NumParameters;
96    program->Base.NumAttributes   = prog.NumAttributes;
97    program->Base.NumAddressRegs  = prog.NumAddressRegs;
98    program->Base.NumNativeInstructions = prog.NumNativeInstructions;
99    program->Base.NumNativeTemporaries = prog.NumNativeTemporaries;
100    program->Base.NumNativeParameters = prog.NumNativeParameters;
101    program->Base.NumNativeAttributes = prog.NumNativeAttributes;
102    program->Base.NumNativeAddressRegs = prog.NumNativeAddressRegs;
103    program->Base.NumAluInstructions   = prog.NumAluInstructions;
104    program->Base.NumTexInstructions   = prog.NumTexInstructions;
105    program->Base.NumTexIndirections   = prog.NumTexIndirections;
106    program->Base.NumNativeAluInstructions = prog.NumAluInstructions;
107    program->Base.NumNativeTexInstructions = prog.NumTexInstructions;
108    program->Base.NumNativeTexIndirections = prog.NumTexIndirections;
109    program->Base.InputsRead      = prog.InputsRead;
110    program->Base.OutputsWritten  = prog.OutputsWritten;
111    program->Base.IndirectRegisterFiles = prog.IndirectRegisterFiles;
112    for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
113       program->Base.TexturesUsed[i] = prog.TexturesUsed[i];
114       if (prog.TexturesUsed[i])
115          program->Base.SamplersUsed |= (1 << i);
116    }
117    program->Base.ShadowSamplers = prog.ShadowSamplers;
118    program->OriginUpperLeft = state.option.OriginUpperLeft;
119    program->PixelCenterInteger = state.option.PixelCenterInteger;
120
121    program->UsesKill            = state.fragment.UsesKill;
122    program->UsesDFdy            = state.fragment.UsesDFdy;
123
124    free(program->Base.Instructions);
125    program->Base.Instructions = prog.Instructions;
126
127    if (program->Base.Parameters)
128       _mesa_free_parameter_list(program->Base.Parameters);
129    program->Base.Parameters    = prog.Parameters;
130
131    /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
132     * or similar.  We used to leave this up to drivers, but it appears
133     * there's no hardware that wants to do fog in a discrete stage separate
134     * from the fragment shader.
135     */
136    if (state.option.Fog != OPTION_NONE) {
137       static const GLenum fog_modes[4] = {
138          GL_NONE, GL_EXP, GL_EXP2, GL_LINEAR
139       };
140
141       /* XXX: we should somehow recompile this to remove clamping if disabled
142        * On the ATI driver, this is unclampled if fragment clamping is disabled
143        */
144       _mesa_append_fog_code(ctx, program, fog_modes[state.option.Fog], GL_TRUE);
145    }
146
147 #if DEBUG_FP
148    printf("____________Fragment program %u ________\n", program->Base.Id);
149    _mesa_print_program(&program->Base);
150 #endif
151 }
152
153
154
155 /**
156  * Parse the vertex program string.  If success, update the given
157  * vertex_program object with the new program.  Else, leave the vertex_program
158  * object unchanged.
159  */
160 void
161 _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
162                                const GLvoid *str, GLsizei len,
163                                struct gl_vertex_program *program)
164 {
165    struct gl_program prog;
166    struct asm_parser_state state;
167
168    ASSERT(target == GL_VERTEX_PROGRAM_ARB);
169
170    memset(&prog, 0, sizeof(prog));
171    memset(&state, 0, sizeof(state));
172    state.prog = &prog;
173
174    if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
175                                 &state)) {
176       _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
177       return;
178    }
179
180    free(program->Base.String);
181
182    /* Copy the relevant contents of the arb_program struct into the 
183     * vertex_program struct.
184     */
185    program->Base.String          = prog.String;
186    program->Base.NumInstructions = prog.NumInstructions;
187    program->Base.NumTemporaries  = prog.NumTemporaries;
188    program->Base.NumParameters   = prog.NumParameters;
189    program->Base.NumAttributes   = prog.NumAttributes;
190    program->Base.NumAddressRegs  = prog.NumAddressRegs;
191    program->Base.NumNativeInstructions = prog.NumNativeInstructions;
192    program->Base.NumNativeTemporaries = prog.NumNativeTemporaries;
193    program->Base.NumNativeParameters = prog.NumNativeParameters;
194    program->Base.NumNativeAttributes = prog.NumNativeAttributes;
195    program->Base.NumNativeAddressRegs = prog.NumNativeAddressRegs;
196    program->Base.InputsRead     = prog.InputsRead;
197    program->Base.OutputsWritten = prog.OutputsWritten;
198    program->Base.IndirectRegisterFiles = prog.IndirectRegisterFiles;
199    program->IsPositionInvariant = (state.option.PositionInvariant)
200       ? GL_TRUE : GL_FALSE;
201
202    free(program->Base.Instructions);
203    program->Base.Instructions = prog.Instructions;
204
205    if (program->Base.Parameters)
206       _mesa_free_parameter_list(program->Base.Parameters);
207    program->Base.Parameters = prog.Parameters; 
208
209 #if DEBUG_VP
210    printf("____________Vertex program %u __________\n", program->Base.Id);
211    _mesa_print_program(&program->Base);
212 #endif
213 }