summaryrefslogtreecommitdiffstats
path: root/src/mesa/program/prog_parameter.h
blob: 570500420127fbfc98f5a4985c7cde4d1016c798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * \file prog_parameter.c
 * Program parameter lists and functions.
 * \author Brian Paul
 */

#ifndef PROG_PARAMETER_H
#define PROG_PARAMETER_H

#include "prog_statevars.h"

#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Names of the various vertex/fragment program register files, etc.
 *
 * NOTE: first four tokens must fit into 2 bits (see t_vb_arbprogram.c)
 * All values should fit in a 4-bit field.
 *
 * NOTE: PROGRAM_STATE_VAR, PROGRAM_CONSTANT, and PROGRAM_UNIFORM can all be
 * considered to be "uniform" variables since they can only be set outside
 * glBegin/End.  They're also all stored in the same Parameters array.
 */
typedef enum
{
   PROGRAM_TEMPORARY,   /**< machine->Temporary[] */
   PROGRAM_ARRAY,       /**< Arrays & Matrixes */
   PROGRAM_INPUT,       /**< machine->Inputs[] */
   PROGRAM_OUTPUT,      /**< machine->Outputs[] */
   PROGRAM_STATE_VAR,   /**< gl_program->Parameters[] */
   PROGRAM_CONSTANT,    /**< gl_program->Parameters[] */
   PROGRAM_UNIFORM,     /**< gl_program->Parameters[] */
   PROGRAM_WRITE_ONLY,  /**< A dummy, write-only register */
   PROGRAM_ADDRESS,     /**< machine->AddressReg */
   PROGRAM_SAMPLER,     /**< for shader samplers, compile-time only */
   PROGRAM_SYSTEM_VALUE,/**< InstanceId, PrimitiveID, etc. */
   PROGRAM_UNDEFINED,   /**< Invalid/TBD value */
   PROGRAM_IMMEDIATE,   /**< Immediate value, used by TGSI */
   PROGRAM_BUFFER,      /**< for shader buffers, compile-time only */
   PROGRAM_MEMORY,      /**< for shared, global and local memory */
   PROGRAM_IMAGE,       /**< for shader images, compile-time only */
   PROGRAM_HW_ATOMIC,   /**< for hw atomic counters, compile-time only */
   PROGRAM_FILE_MAX
} gl_register_file;


/**
 * Actual data for constant values of parameters.
 */
typedef union gl_constant_value
{
   GLfloat f;
   GLint b;
   GLint i;
   GLuint u;
} gl_constant_value;


/**
 * Program parameter.
 * Used by shaders/programs for uniforms, constants, varying vars, etc.
 */
struct gl_program_parameter
{
   const char *Name;        /**< Null-terminated string */
   gl_register_file Type:16;  /**< PROGRAM_CONSTANT or STATE_VAR */
   GLenum16 DataType;         /**< GL_FLOAT, GL_FLOAT_VEC2, etc */
   /**
    * Number of components (1..4), or more.
    * If the number of components is greater than 4,
    * this parameter is part of a larger uniform like a GLSL matrix or array.
    * The next program parameter's Size will be Size-4 of this parameter.
    */
   GLushort Size;
   /**
    * A sequence of STATE_* tokens and integers to identify GL state.
    */
   gl_state_index16 StateIndexes[STATE_LENGTH];

   /**
    * We need to keep track of whether the param is padded for use in the
    * shader cache.
    */
   bool Padded;
};


/**
 * List of gl_program_parameter instances.
 */
struct gl_program_parameter_list
{
   GLuint Size;           /**< allocated size of Parameters, ParameterValues */
   GLuint NumParameters;  /**< number of used parameters in array */
   unsigned NumParameterValues;  /**< number of used parameter values array */
   struct gl_program_parameter *Parameters; /**< Array [Size] */
   unsigned *ParameterValueOffset;
   gl_constant_value *ParameterValues; /**< Array [Size] of gl_constant_value */
   GLbitfield StateFlags; /**< _NEW_* flags indicating which state changes
                               might invalidate ParameterValues[] */
};


extern struct gl_program_parameter_list *
_mesa_new_parameter_list(void);

extern struct gl_program_parameter_list *
_mesa_new_parameter_list_sized(unsigned size);

extern void
_mesa_free_parameter_list(struct gl_program_parameter_list *paramList);

extern void
_mesa_reserve_parameter_storage(struct gl_program_parameter_list *paramList,
                                unsigned reserve_slots);

extern GLint
_mesa_add_parameter(struct gl_program_parameter_list *paramList,
                    gl_register_file type, const char *name,
                    GLuint size, GLenum datatype,
                    const gl_constant_value *values,
                    const gl_state_index16 state[STATE_LENGTH],
                    bool pad_and_align);

extern GLint
_mesa_add_typed_unnamed_constant(struct gl_program_parameter_list *paramList,
                           const gl_constant_value values[4], GLuint size,
                           GLenum datatype, GLuint *swizzleOut);

static inline GLint
_mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
                           const gl_constant_value values[4], GLuint size,
                           GLuint *swizzleOut)
{
   return _mesa_add_typed_unnamed_constant(paramList, values, size, GL_NONE,
                                           swizzleOut);
}

extern GLint
_mesa_add_sized_state_reference(struct gl_program_parameter_list *paramList,
                                const gl_state_index16 stateTokens[STATE_LENGTH],
                                const unsigned size, bool pad_and_align);

extern GLint
_mesa_add_state_reference(struct gl_program_parameter_list *paramList,
                          const gl_state_index16 stateTokens[]);


static inline GLint
_mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
                             const char *name)
{
   if (!paramList)
      return -1;

   /* name must be null-terminated */
   for (GLint i = 0; i < (GLint) paramList->NumParameters; i++) {
      if (paramList->Parameters[i].Name &&
         strcmp(paramList->Parameters[i].Name, name) == 0)
         return i;
   }

   return -1;
}

static inline bool
_mesa_gl_datatype_is_64bit(GLenum datatype)
{
   switch (datatype) {
   case GL_DOUBLE:
   case GL_DOUBLE_VEC2:
   case GL_DOUBLE_VEC3:
   case GL_DOUBLE_VEC4:
   case GL_DOUBLE_MAT2:
   case GL_DOUBLE_MAT2x3:
   case GL_DOUBLE_MAT2x4:
   case GL_DOUBLE_MAT3:
   case GL_DOUBLE_MAT3x2:
   case GL_DOUBLE_MAT3x4:
   case GL_DOUBLE_MAT4:
   case GL_DOUBLE_MAT4x2:
   case GL_DOUBLE_MAT4x3:
   case GL_INT64_ARB:
   case GL_INT64_VEC2_ARB:
   case GL_INT64_VEC3_ARB:
   case GL_INT64_VEC4_ARB:
   case GL_UNSIGNED_INT64_ARB:
   case GL_UNSIGNED_INT64_VEC2_ARB:
   case GL_UNSIGNED_INT64_VEC3_ARB:
   case GL_UNSIGNED_INT64_VEC4_ARB:
      return true;
   default:
      return false;
   }
}

#ifdef __cplusplus
}
#endif

#endif /* PROG_PARAMETER_H */