1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Mesa 3-D graphics library
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#define DEBUG_PARSING 0
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci/**
28bf215546Sopenharmony_ci * \file arbprogparse.c
29bf215546Sopenharmony_ci * ARB_*_program parser core
30bf215546Sopenharmony_ci * \author Karl Rasche
31bf215546Sopenharmony_ci */
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci/**
34bf215546Sopenharmony_ciNotes on program parameters, etc.
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ciThe instructions we emit will use six kinds of source registers:
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci  PROGRAM_INPUT      - input registers
39bf215546Sopenharmony_ci  PROGRAM_TEMPORARY  - temp registers
40bf215546Sopenharmony_ci  PROGRAM_ADDRESS    - address/indirect register
41bf215546Sopenharmony_ci  PROGRAM_CONSTANT   - indexes into program->Parameters, a known constant/literal
42bf215546Sopenharmony_ci  PROGRAM_STATE_VAR  - indexes into program->Parameters, and may actually be:
43bf215546Sopenharmony_ci                       + a state variable, like "state.fog.color", or
44bf215546Sopenharmony_ci                       + a pointer to a "program.local[k]" parameter, or
45bf215546Sopenharmony_ci                       + a pointer to a "program.env[k]" parameter
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ciBasically, all the program.local[] and program.env[] values will get mapped
48bf215546Sopenharmony_ciinto the unified gl_program->Parameters array.  This solves the problem of
49bf215546Sopenharmony_cihaving three separate program parameter arrays.
50bf215546Sopenharmony_ci*/
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci#include "main/glheader.h"
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci#include "main/context.h"
56bf215546Sopenharmony_ci#include "arbprogparse.h"
57bf215546Sopenharmony_ci#include "programopt.h"
58bf215546Sopenharmony_ci#include "prog_parameter.h"
59bf215546Sopenharmony_ci#include "prog_statevars.h"
60bf215546Sopenharmony_ci#include "prog_instruction.h"
61bf215546Sopenharmony_ci#include "program_parser.h"
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_civoid
65bf215546Sopenharmony_ci_mesa_parse_arb_fragment_program(struct gl_context* ctx, GLenum target,
66bf215546Sopenharmony_ci                                 const GLvoid *str, GLsizei len,
67bf215546Sopenharmony_ci                                 struct gl_program *program)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci   struct gl_program prog;
70bf215546Sopenharmony_ci   struct asm_parser_state state;
71bf215546Sopenharmony_ci   GLuint i;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   assert(target == GL_FRAGMENT_PROGRAM_ARB);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   memset(&prog, 0, sizeof(prog));
76bf215546Sopenharmony_ci   memset(&state, 0, sizeof(state));
77bf215546Sopenharmony_ci   state.prog = &prog;
78bf215546Sopenharmony_ci   state.mem_ctx = program;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
81bf215546Sopenharmony_ci				&state)) {
82bf215546Sopenharmony_ci      /* Error in the program. Just return. */
83bf215546Sopenharmony_ci      return;
84bf215546Sopenharmony_ci   }
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   ralloc_free(program->String);
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   /* Copy the relevant contents of the arb_program struct into the
89bf215546Sopenharmony_ci    * fragment_program struct.
90bf215546Sopenharmony_ci    */
91bf215546Sopenharmony_ci   program->String          = prog.String;
92bf215546Sopenharmony_ci   program->arb.NumInstructions = prog.arb.NumInstructions;
93bf215546Sopenharmony_ci   program->arb.NumTemporaries  = prog.arb.NumTemporaries;
94bf215546Sopenharmony_ci   program->arb.NumParameters   = prog.arb.NumParameters;
95bf215546Sopenharmony_ci   program->arb.NumAttributes   = prog.arb.NumAttributes;
96bf215546Sopenharmony_ci   program->arb.NumAddressRegs  = prog.arb.NumAddressRegs;
97bf215546Sopenharmony_ci   program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
98bf215546Sopenharmony_ci   program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
99bf215546Sopenharmony_ci   program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
100bf215546Sopenharmony_ci   program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
101bf215546Sopenharmony_ci   program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
102bf215546Sopenharmony_ci   program->arb.NumAluInstructions   = prog.arb.NumAluInstructions;
103bf215546Sopenharmony_ci   program->arb.NumTexInstructions   = prog.arb.NumTexInstructions;
104bf215546Sopenharmony_ci   program->arb.NumTexIndirections   = prog.arb.NumTexIndirections;
105bf215546Sopenharmony_ci   program->arb.NumNativeAluInstructions = prog.arb.NumAluInstructions;
106bf215546Sopenharmony_ci   program->arb.NumNativeTexInstructions = prog.arb.NumTexInstructions;
107bf215546Sopenharmony_ci   program->arb.NumNativeTexIndirections = prog.arb.NumTexIndirections;
108bf215546Sopenharmony_ci   program->info.inputs_read      = prog.info.inputs_read;
109bf215546Sopenharmony_ci   program->info.outputs_written = prog.info.outputs_written;
110bf215546Sopenharmony_ci   program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
111bf215546Sopenharmony_ci   for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
112bf215546Sopenharmony_ci      program->TexturesUsed[i] = prog.TexturesUsed[i];
113bf215546Sopenharmony_ci      if (prog.TexturesUsed[i])
114bf215546Sopenharmony_ci         program->SamplersUsed |= (1 << i);
115bf215546Sopenharmony_ci   }
116bf215546Sopenharmony_ci   program->ShadowSamplers = prog.ShadowSamplers;
117bf215546Sopenharmony_ci   program->info.fs.origin_upper_left = state.option.OriginUpperLeft;
118bf215546Sopenharmony_ci   program->info.fs.pixel_center_integer = state.option.PixelCenterInteger;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   program->info.fs.uses_discard = state.fragment.UsesKill;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   ralloc_free(program->arb.Instructions);
123bf215546Sopenharmony_ci   program->arb.Instructions = prog.arb.Instructions;
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   if (program->Parameters)
126bf215546Sopenharmony_ci      _mesa_free_parameter_list(program->Parameters);
127bf215546Sopenharmony_ci   program->Parameters    = prog.Parameters;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
130bf215546Sopenharmony_ci    * or similar.  We used to leave this up to drivers, but it appears
131bf215546Sopenharmony_ci    * there's no hardware that wants to do fog in a discrete stage separate
132bf215546Sopenharmony_ci    * from the fragment shader.
133bf215546Sopenharmony_ci    */
134bf215546Sopenharmony_ci   if (state.option.Fog != OPTION_NONE) {
135bf215546Sopenharmony_ci      static const GLenum fog_modes[4] = {
136bf215546Sopenharmony_ci	 GL_NONE, GL_EXP, GL_EXP2, GL_LINEAR
137bf215546Sopenharmony_ci      };
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci      /* XXX: we should somehow recompile this to remove clamping if disabled
140bf215546Sopenharmony_ci       * On the ATI driver, this is unclampled if fragment clamping is disabled
141bf215546Sopenharmony_ci       */
142bf215546Sopenharmony_ci      _mesa_append_fog_code(ctx, program, fog_modes[state.option.Fog], GL_TRUE);
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci#if DEBUG_FP
146bf215546Sopenharmony_ci   printf("____________Fragment program %u ________\n", program->Id);
147bf215546Sopenharmony_ci   _mesa_print_program(&program->Base);
148bf215546Sopenharmony_ci#endif
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci/**
154bf215546Sopenharmony_ci * Parse the vertex program string.  If success, update the given
155bf215546Sopenharmony_ci * vertex_program object with the new program.  Else, leave the vertex_program
156bf215546Sopenharmony_ci * object unchanged.
157bf215546Sopenharmony_ci */
158bf215546Sopenharmony_civoid
159bf215546Sopenharmony_ci_mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
160bf215546Sopenharmony_ci			       const GLvoid *str, GLsizei len,
161bf215546Sopenharmony_ci			       struct gl_program *program)
162bf215546Sopenharmony_ci{
163bf215546Sopenharmony_ci   struct gl_program prog;
164bf215546Sopenharmony_ci   struct asm_parser_state state;
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci   assert(target == GL_VERTEX_PROGRAM_ARB);
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci   memset(&prog, 0, sizeof(prog));
169bf215546Sopenharmony_ci   memset(&state, 0, sizeof(state));
170bf215546Sopenharmony_ci   state.prog = &prog;
171bf215546Sopenharmony_ci   state.mem_ctx = program;
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
174bf215546Sopenharmony_ci				&state)) {
175bf215546Sopenharmony_ci      _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
176bf215546Sopenharmony_ci      return;
177bf215546Sopenharmony_ci   }
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   ralloc_free(program->String);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   /* Copy the relevant contents of the arb_program struct into the
182bf215546Sopenharmony_ci    * vertex_program struct.
183bf215546Sopenharmony_ci    */
184bf215546Sopenharmony_ci   program->String          = prog.String;
185bf215546Sopenharmony_ci   program->arb.NumInstructions = prog.arb.NumInstructions;
186bf215546Sopenharmony_ci   program->arb.NumTemporaries  = prog.arb.NumTemporaries;
187bf215546Sopenharmony_ci   program->arb.NumParameters   = prog.arb.NumParameters;
188bf215546Sopenharmony_ci   program->arb.NumAttributes   = prog.arb.NumAttributes;
189bf215546Sopenharmony_ci   program->arb.NumAddressRegs  = prog.arb.NumAddressRegs;
190bf215546Sopenharmony_ci   program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
191bf215546Sopenharmony_ci   program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
192bf215546Sopenharmony_ci   program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
193bf215546Sopenharmony_ci   program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
194bf215546Sopenharmony_ci   program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
195bf215546Sopenharmony_ci   program->info.inputs_read     = prog.info.inputs_read;
196bf215546Sopenharmony_ci   program->info.outputs_written = prog.info.outputs_written;
197bf215546Sopenharmony_ci   program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
198bf215546Sopenharmony_ci   program->arb.IsPositionInvariant = (state.option.PositionInvariant)
199bf215546Sopenharmony_ci      ? GL_TRUE : GL_FALSE;
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci   ralloc_free(program->arb.Instructions);
202bf215546Sopenharmony_ci   program->arb.Instructions = prog.arb.Instructions;
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   if (program->Parameters)
205bf215546Sopenharmony_ci      _mesa_free_parameter_list(program->Parameters);
206bf215546Sopenharmony_ci   program->Parameters = prog.Parameters;
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci#if DEBUG_VP
209bf215546Sopenharmony_ci   printf("____________Vertex program %u __________\n", program->Id);
210bf215546Sopenharmony_ci   _mesa_print_program(program);
211bf215546Sopenharmony_ci#endif
212bf215546Sopenharmony_ci}
213