mesa: Fix setup of ctx->Point.PointSprite for GLES2.
[mesa.git] / src / mesa / main / points.c
1 /**
2 * \file points.c
3 * Point operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 7.1
9 *
10 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "glheader.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "points.h"
35 #include "mtypes.h"
36
37
38 /**
39 * Set current point size.
40 * \param size point diameter in pixels
41 * \sa glPointSize().
42 */
43 void GLAPIENTRY
44 _mesa_PointSize( GLfloat size )
45 {
46 GET_CURRENT_CONTEXT(ctx);
47
48 if (size <= 0.0) {
49 _mesa_error( ctx, GL_INVALID_VALUE, "glPointSize" );
50 return;
51 }
52
53 if (ctx->Point.Size == size)
54 return;
55
56 FLUSH_VERTICES(ctx, _NEW_POINT);
57 ctx->Point.Size = size;
58
59 if (ctx->Driver.PointSize)
60 ctx->Driver.PointSize(ctx, size);
61 }
62
63
64 void GLAPIENTRY
65 _mesa_PointParameteri( GLenum pname, GLint param )
66 {
67 GLfloat p[3];
68 p[0] = (GLfloat) param;
69 p[1] = p[2] = 0.0F;
70 _mesa_PointParameterfv(pname, p);
71 }
72
73
74 void GLAPIENTRY
75 _mesa_PointParameteriv( GLenum pname, const GLint *params )
76 {
77 GLfloat p[3];
78 p[0] = (GLfloat) params[0];
79 if (pname == GL_DISTANCE_ATTENUATION_EXT) {
80 p[1] = (GLfloat) params[1];
81 p[2] = (GLfloat) params[2];
82 }
83 _mesa_PointParameterfv(pname, p);
84 }
85
86
87 void GLAPIENTRY
88 _mesa_PointParameterf( GLenum pname, GLfloat param)
89 {
90 GLfloat p[3];
91 p[0] = param;
92 p[1] = p[2] = 0.0F;
93 _mesa_PointParameterfv(pname, p);
94 }
95
96
97 void GLAPIENTRY
98 _mesa_PointParameterfv( GLenum pname, const GLfloat *params)
99 {
100 GET_CURRENT_CONTEXT(ctx);
101
102 /* Drivers that support point sprites must also support point parameters.
103 * If point parameters aren't supported, then this function shouldn't even
104 * exist.
105 */
106 ASSERT(!(ctx->Extensions.ARB_point_sprite
107 || ctx->Extensions.NV_point_sprite)
108 || ctx->Extensions.EXT_point_parameters);
109
110 if (!ctx->Extensions.EXT_point_parameters) {
111 _mesa_error(ctx, GL_INVALID_OPERATION,
112 "unsupported function called (unsupported extension)");
113 return;
114 }
115
116 switch (pname) {
117 case GL_DISTANCE_ATTENUATION_EXT:
118 if (TEST_EQ_3V(ctx->Point.Params, params))
119 return;
120 FLUSH_VERTICES(ctx, _NEW_POINT);
121 COPY_3V(ctx->Point.Params, params);
122 ctx->Point._Attenuated = (ctx->Point.Params[0] != 1.0 ||
123 ctx->Point.Params[1] != 0.0 ||
124 ctx->Point.Params[2] != 0.0);
125
126 if (ctx->Point._Attenuated)
127 ctx->_TriangleCaps |= DD_POINT_ATTEN;
128 else
129 ctx->_TriangleCaps &= ~DD_POINT_ATTEN;
130 break;
131 case GL_POINT_SIZE_MIN_EXT:
132 if (params[0] < 0.0F) {
133 _mesa_error( ctx, GL_INVALID_VALUE,
134 "glPointParameterf[v]{EXT,ARB}(param)" );
135 return;
136 }
137 if (ctx->Point.MinSize == params[0])
138 return;
139 FLUSH_VERTICES(ctx, _NEW_POINT);
140 ctx->Point.MinSize = params[0];
141 break;
142 case GL_POINT_SIZE_MAX_EXT:
143 if (params[0] < 0.0F) {
144 _mesa_error( ctx, GL_INVALID_VALUE,
145 "glPointParameterf[v]{EXT,ARB}(param)" );
146 return;
147 }
148 if (ctx->Point.MaxSize == params[0])
149 return;
150 FLUSH_VERTICES(ctx, _NEW_POINT);
151 ctx->Point.MaxSize = params[0];
152 break;
153 case GL_POINT_FADE_THRESHOLD_SIZE_EXT:
154 if (params[0] < 0.0F) {
155 _mesa_error( ctx, GL_INVALID_VALUE,
156 "glPointParameterf[v]{EXT,ARB}(param)" );
157 return;
158 }
159 if (ctx->Point.Threshold == params[0])
160 return;
161 FLUSH_VERTICES(ctx, _NEW_POINT);
162 ctx->Point.Threshold = params[0];
163 break;
164 case GL_POINT_SPRITE_R_MODE_NV:
165 /* This is one area where ARB_point_sprite and NV_point_sprite
166 * differ. In ARB_point_sprite the POINT_SPRITE_R_MODE is
167 * always ZERO. NV_point_sprite adds the S and R modes.
168 */
169 if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_point_sprite) {
170 GLenum value = (GLenum) params[0];
171 if (value != GL_ZERO && value != GL_S && value != GL_R) {
172 _mesa_error(ctx, GL_INVALID_VALUE,
173 "glPointParameterf[v]{EXT,ARB}(param)");
174 return;
175 }
176 if (ctx->Point.SpriteRMode == value)
177 return;
178 FLUSH_VERTICES(ctx, _NEW_POINT);
179 ctx->Point.SpriteRMode = value;
180 }
181 else {
182 _mesa_error(ctx, GL_INVALID_ENUM,
183 "glPointParameterf[v]{EXT,ARB}(pname)");
184 return;
185 }
186 break;
187 case GL_POINT_SPRITE_COORD_ORIGIN:
188 /* GL_POINT_SPRITE_COORD_ORIGIN was added to point sprites when the
189 * extension was merged into OpenGL 2.0.
190 */
191 if ((ctx->API == API_OPENGL_COMPAT && ctx->Version >= 20)
192 || ctx->API == API_OPENGL_CORE) {
193 GLenum value = (GLenum) params[0];
194 if (value != GL_LOWER_LEFT && value != GL_UPPER_LEFT) {
195 _mesa_error(ctx, GL_INVALID_VALUE,
196 "glPointParameterf[v]{EXT,ARB}(param)");
197 return;
198 }
199 if (ctx->Point.SpriteOrigin == value)
200 return;
201 FLUSH_VERTICES(ctx, _NEW_POINT);
202 ctx->Point.SpriteOrigin = value;
203 }
204 else {
205 _mesa_error(ctx, GL_INVALID_ENUM,
206 "glPointParameterf[v]{EXT,ARB}(pname)");
207 return;
208 }
209 break;
210 default:
211 _mesa_error( ctx, GL_INVALID_ENUM,
212 "glPointParameterf[v]{EXT,ARB}(pname)" );
213 return;
214 }
215
216 if (ctx->Driver.PointParameterfv)
217 (*ctx->Driver.PointParameterfv)(ctx, pname, params);
218 }
219
220
221
222 /**
223 * Initialize the context point state.
224 *
225 * \param ctx GL context.
226 *
227 * Initializes __struct gl_contextRec::Point and point related constants in
228 * __struct gl_contextRec::Const.
229 */
230 void
231 _mesa_init_point(struct gl_context *ctx)
232 {
233 GLuint i;
234
235 ctx->Point.SmoothFlag = GL_FALSE;
236 ctx->Point.Size = 1.0;
237 ctx->Point.Params[0] = 1.0;
238 ctx->Point.Params[1] = 0.0;
239 ctx->Point.Params[2] = 0.0;
240 ctx->Point._Attenuated = GL_FALSE;
241 ctx->Point.MinSize = 0.0;
242 ctx->Point.MaxSize
243 = MAX2(ctx->Const.MaxPointSize, ctx->Const.MaxPointSizeAA);
244 ctx->Point.Threshold = 1.0;
245
246 /* Page 403 (page 423 of the PDF) of the OpenGL 3.0 spec says:
247 *
248 * "Non-sprite points (section 3.4) - Enable/Disable targets
249 * POINT_SMOOTH and POINT_SPRITE, and all associated state. Point
250 * rasterization is always performed as though POINT_SPRITE were
251 * enabled."
252 *
253 * In a core context, the state will default to true, and the setters and
254 * getters are disabled.
255 */
256 ctx->Point.PointSprite = (ctx->API == API_OPENGL_CORE ||
257 ctx->API == API_OPENGLES2);
258
259 ctx->Point.SpriteRMode = GL_ZERO; /* GL_NV_point_sprite (only!) */
260 ctx->Point.SpriteOrigin = GL_UPPER_LEFT; /* GL_ARB_point_sprite */
261 for (i = 0; i < Elements(ctx->Point.CoordReplace); i++) {
262 ctx->Point.CoordReplace[i] = GL_FALSE; /* GL_ARB/NV_point_sprite */
263 }
264 }