1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg#define DEBUG_PARSING 0
26848b8605Smrg
27848b8605Smrg/**
28848b8605Smrg * \file arbprogparse.c
29848b8605Smrg * ARB_*_program parser core
30848b8605Smrg * \author Karl Rasche
31848b8605Smrg */
32848b8605Smrg
33848b8605Smrg/**
34848b8605SmrgNotes on program parameters, etc.
35848b8605Smrg
36848b8605SmrgThe instructions we emit will use six kinds of source registers:
37848b8605Smrg
38848b8605Smrg  PROGRAM_INPUT      - input registers
39848b8605Smrg  PROGRAM_TEMPORARY  - temp registers
40848b8605Smrg  PROGRAM_ADDRESS    - address/indirect register
41848b8605Smrg  PROGRAM_SAMPLER    - texture sampler
42848b8605Smrg  PROGRAM_CONSTANT   - indexes into program->Parameters, a known constant/literal
43848b8605Smrg  PROGRAM_STATE_VAR  - indexes into program->Parameters, and may actually be:
44848b8605Smrg                       + a state variable, like "state.fog.color", or
45848b8605Smrg                       + a pointer to a "program.local[k]" parameter, or
46848b8605Smrg                       + a pointer to a "program.env[k]" parameter
47848b8605Smrg
48848b8605SmrgBasically, all the program.local[] and program.env[] values will get mapped
49848b8605Smrginto the unified gl_program->Parameters array.  This solves the problem of
50848b8605Smrghaving three separate program parameter arrays.
51848b8605Smrg*/
52848b8605Smrg
53848b8605Smrg
54848b8605Smrg#include "main/glheader.h"
55848b8605Smrg#include "main/imports.h"
56848b8605Smrg#include "main/context.h"
57848b8605Smrg#include "main/mtypes.h"
58848b8605Smrg#include "arbprogparse.h"
59848b8605Smrg#include "programopt.h"
60848b8605Smrg#include "prog_parameter.h"
61848b8605Smrg#include "prog_statevars.h"
62848b8605Smrg#include "prog_instruction.h"
63848b8605Smrg#include "prog_optimize.h"
64848b8605Smrg#include "program_parser.h"
65848b8605Smrg
66848b8605Smrg
67848b8605Smrgvoid
68848b8605Smrg_mesa_parse_arb_fragment_program(struct gl_context* ctx, GLenum target,
69848b8605Smrg                                 const GLvoid *str, GLsizei len,
70b8e80941Smrg                                 struct gl_program *program)
71848b8605Smrg{
72848b8605Smrg   struct gl_program prog;
73848b8605Smrg   struct asm_parser_state state;
74848b8605Smrg   GLuint i;
75848b8605Smrg
76b8e80941Smrg   assert(target == GL_FRAGMENT_PROGRAM_ARB);
77848b8605Smrg
78848b8605Smrg   memset(&prog, 0, sizeof(prog));
79848b8605Smrg   memset(&state, 0, sizeof(state));
80848b8605Smrg   state.prog = &prog;
81b8e80941Smrg   state.mem_ctx = program;
82848b8605Smrg
83848b8605Smrg   if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
84848b8605Smrg				&state)) {
85848b8605Smrg      /* Error in the program. Just return. */
86848b8605Smrg      return;
87848b8605Smrg   }
88848b8605Smrg
89b8e80941Smrg   ralloc_free(program->String);
90848b8605Smrg
91848b8605Smrg   /* Copy the relevant contents of the arb_program struct into the
92848b8605Smrg    * fragment_program struct.
93848b8605Smrg    */
94b8e80941Smrg   program->String          = prog.String;
95b8e80941Smrg   program->arb.NumInstructions = prog.arb.NumInstructions;
96b8e80941Smrg   program->arb.NumTemporaries  = prog.arb.NumTemporaries;
97b8e80941Smrg   program->arb.NumParameters   = prog.arb.NumParameters;
98b8e80941Smrg   program->arb.NumAttributes   = prog.arb.NumAttributes;
99b8e80941Smrg   program->arb.NumAddressRegs  = prog.arb.NumAddressRegs;
100b8e80941Smrg   program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
101b8e80941Smrg   program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
102b8e80941Smrg   program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
103b8e80941Smrg   program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
104b8e80941Smrg   program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
105b8e80941Smrg   program->arb.NumAluInstructions   = prog.arb.NumAluInstructions;
106b8e80941Smrg   program->arb.NumTexInstructions   = prog.arb.NumTexInstructions;
107b8e80941Smrg   program->arb.NumTexIndirections   = prog.arb.NumTexIndirections;
108b8e80941Smrg   program->arb.NumNativeAluInstructions = prog.arb.NumAluInstructions;
109b8e80941Smrg   program->arb.NumNativeTexInstructions = prog.arb.NumTexInstructions;
110b8e80941Smrg   program->arb.NumNativeTexIndirections = prog.arb.NumTexIndirections;
111b8e80941Smrg   program->info.inputs_read      = prog.info.inputs_read;
112b8e80941Smrg   program->info.outputs_written = prog.info.outputs_written;
113b8e80941Smrg   program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
114848b8605Smrg   for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
115b8e80941Smrg      program->TexturesUsed[i] = prog.TexturesUsed[i];
116848b8605Smrg      if (prog.TexturesUsed[i])
117b8e80941Smrg         program->SamplersUsed |= (1 << i);
118848b8605Smrg   }
119b8e80941Smrg   program->ShadowSamplers = prog.ShadowSamplers;
120b8e80941Smrg   program->info.fs.origin_upper_left = state.option.OriginUpperLeft;
121b8e80941Smrg   program->info.fs.pixel_center_integer = state.option.PixelCenterInteger;
122848b8605Smrg
123b8e80941Smrg   program->info.fs.uses_discard = state.fragment.UsesKill;
124848b8605Smrg
125b8e80941Smrg   ralloc_free(program->arb.Instructions);
126b8e80941Smrg   program->arb.Instructions = prog.arb.Instructions;
127848b8605Smrg
128b8e80941Smrg   if (program->Parameters)
129b8e80941Smrg      _mesa_free_parameter_list(program->Parameters);
130b8e80941Smrg   program->Parameters    = prog.Parameters;
131848b8605Smrg
132848b8605Smrg   /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
133848b8605Smrg    * or similar.  We used to leave this up to drivers, but it appears
134848b8605Smrg    * there's no hardware that wants to do fog in a discrete stage separate
135848b8605Smrg    * from the fragment shader.
136848b8605Smrg    */
137848b8605Smrg   if (state.option.Fog != OPTION_NONE) {
138848b8605Smrg      static const GLenum fog_modes[4] = {
139848b8605Smrg	 GL_NONE, GL_EXP, GL_EXP2, GL_LINEAR
140848b8605Smrg      };
141848b8605Smrg
142848b8605Smrg      /* XXX: we should somehow recompile this to remove clamping if disabled
143848b8605Smrg       * On the ATI driver, this is unclampled if fragment clamping is disabled
144848b8605Smrg       */
145848b8605Smrg      _mesa_append_fog_code(ctx, program, fog_modes[state.option.Fog], GL_TRUE);
146848b8605Smrg   }
147848b8605Smrg
148848b8605Smrg#if DEBUG_FP
149b8e80941Smrg   printf("____________Fragment program %u ________\n", program->Id);
150848b8605Smrg   _mesa_print_program(&program->Base);
151848b8605Smrg#endif
152848b8605Smrg}
153848b8605Smrg
154848b8605Smrg
155848b8605Smrg
156848b8605Smrg/**
157848b8605Smrg * Parse the vertex program string.  If success, update the given
158848b8605Smrg * vertex_program object with the new program.  Else, leave the vertex_program
159848b8605Smrg * object unchanged.
160848b8605Smrg */
161848b8605Smrgvoid
162848b8605Smrg_mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
163848b8605Smrg			       const GLvoid *str, GLsizei len,
164b8e80941Smrg			       struct gl_program *program)
165848b8605Smrg{
166848b8605Smrg   struct gl_program prog;
167848b8605Smrg   struct asm_parser_state state;
168848b8605Smrg
169b8e80941Smrg   assert(target == GL_VERTEX_PROGRAM_ARB);
170848b8605Smrg
171848b8605Smrg   memset(&prog, 0, sizeof(prog));
172848b8605Smrg   memset(&state, 0, sizeof(state));
173848b8605Smrg   state.prog = &prog;
174b8e80941Smrg   state.mem_ctx = program;
175848b8605Smrg
176848b8605Smrg   if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
177848b8605Smrg				&state)) {
178b8e80941Smrg      ralloc_free(prog.arb.Instructions);
179b8e80941Smrg      ralloc_free(prog.String);
180848b8605Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
181848b8605Smrg      return;
182848b8605Smrg   }
183848b8605Smrg
184b8e80941Smrg   _mesa_optimize_program(&prog, program);
185848b8605Smrg
186b8e80941Smrg   ralloc_free(program->String);
187848b8605Smrg
188848b8605Smrg   /* Copy the relevant contents of the arb_program struct into the
189848b8605Smrg    * vertex_program struct.
190848b8605Smrg    */
191b8e80941Smrg   program->String          = prog.String;
192b8e80941Smrg   program->arb.NumInstructions = prog.arb.NumInstructions;
193b8e80941Smrg   program->arb.NumTemporaries  = prog.arb.NumTemporaries;
194b8e80941Smrg   program->arb.NumParameters   = prog.arb.NumParameters;
195b8e80941Smrg   program->arb.NumAttributes   = prog.arb.NumAttributes;
196b8e80941Smrg   program->arb.NumAddressRegs  = prog.arb.NumAddressRegs;
197b8e80941Smrg   program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
198b8e80941Smrg   program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
199b8e80941Smrg   program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
200b8e80941Smrg   program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
201b8e80941Smrg   program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
202b8e80941Smrg   program->info.inputs_read     = prog.info.inputs_read;
203b8e80941Smrg   program->info.outputs_written = prog.info.outputs_written;
204b8e80941Smrg   program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
205b8e80941Smrg   program->arb.IsPositionInvariant = (state.option.PositionInvariant)
206848b8605Smrg      ? GL_TRUE : GL_FALSE;
207848b8605Smrg
208b8e80941Smrg   ralloc_free(program->arb.Instructions);
209b8e80941Smrg   program->arb.Instructions = prog.arb.Instructions;
210848b8605Smrg
211b8e80941Smrg   if (program->Parameters)
212b8e80941Smrg      _mesa_free_parameter_list(program->Parameters);
213b8e80941Smrg   program->Parameters = prog.Parameters;
214848b8605Smrg
215848b8605Smrg#if DEBUG_VP
216b8e80941Smrg   printf("____________Vertex program %u __________\n", program->Id);
217b8e80941Smrg   _mesa_print_program(program);
218848b8605Smrg#endif
219848b8605Smrg}
220