mesa: add pixel_storei() helper
[mesa.git] / src / mesa / main / pixelstore.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 /**
26 * \file pixelstore.c
27 * glPixelStore functions.
28 */
29
30
31 #include "glheader.h"
32 #include "bufferobj.h"
33 #include "context.h"
34 #include "pixelstore.h"
35 #include "mtypes.h"
36
37
38 static ALWAYS_INLINE void
39 pixel_storei(GLenum pname, GLint param, bool no_error)
40 {
41 /* NOTE: this call can't be compiled into the display list */
42 GET_CURRENT_CONTEXT(ctx);
43
44 switch (pname) {
45 case GL_PACK_SWAP_BYTES:
46 if (!no_error && !_mesa_is_desktop_gl(ctx))
47 goto invalid_enum_error;
48 ctx->Pack.SwapBytes = param ? GL_TRUE : GL_FALSE;
49 break;
50 case GL_PACK_LSB_FIRST:
51 if (!no_error && !_mesa_is_desktop_gl(ctx))
52 goto invalid_enum_error;
53 ctx->Pack.LsbFirst = param ? GL_TRUE : GL_FALSE;
54 break;
55 case GL_PACK_ROW_LENGTH:
56 if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
57 goto invalid_enum_error;
58 if (!no_error && param<0)
59 goto invalid_value_error;
60 ctx->Pack.RowLength = param;
61 break;
62 case GL_PACK_IMAGE_HEIGHT:
63 if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
64 goto invalid_enum_error;
65 if (!no_error && param<0)
66 goto invalid_value_error;
67 ctx->Pack.ImageHeight = param;
68 break;
69 case GL_PACK_SKIP_PIXELS:
70 if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
71 goto invalid_enum_error;
72 if (!no_error && param<0)
73 goto invalid_value_error;
74 ctx->Pack.SkipPixels = param;
75 break;
76 case GL_PACK_SKIP_ROWS:
77 if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
78 goto invalid_enum_error;
79 if (!no_error && param<0)
80 goto invalid_value_error;
81 ctx->Pack.SkipRows = param;
82 break;
83 case GL_PACK_SKIP_IMAGES:
84 if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
85 goto invalid_enum_error;
86 if (!no_error && param<0)
87 goto invalid_value_error;
88 ctx->Pack.SkipImages = param;
89 break;
90 case GL_PACK_ALIGNMENT:
91 if (!no_error && param!=1 && param!=2 && param!=4 && param!=8)
92 goto invalid_value_error;
93 ctx->Pack.Alignment = param;
94 break;
95 case GL_PACK_INVERT_MESA:
96 if (!no_error &&
97 (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.MESA_pack_invert))
98 goto invalid_enum_error;
99 ctx->Pack.Invert = param;
100 break;
101 case GL_PACK_COMPRESSED_BLOCK_WIDTH:
102 if (!no_error && !_mesa_is_desktop_gl(ctx))
103 goto invalid_enum_error;
104 if (!no_error && param<0)
105 goto invalid_value_error;
106 ctx->Pack.CompressedBlockWidth = param;
107 break;
108 case GL_PACK_COMPRESSED_BLOCK_HEIGHT:
109 if (!no_error && !_mesa_is_desktop_gl(ctx))
110 goto invalid_enum_error;
111 if (!no_error && param<0)
112 goto invalid_value_error;
113 ctx->Pack.CompressedBlockHeight = param;
114 break;
115 case GL_PACK_COMPRESSED_BLOCK_DEPTH:
116 if (!no_error && !_mesa_is_desktop_gl(ctx))
117 goto invalid_enum_error;
118 if (!no_error && param<0)
119 goto invalid_value_error;
120 ctx->Pack.CompressedBlockDepth = param;
121 break;
122 case GL_PACK_COMPRESSED_BLOCK_SIZE:
123 if (!no_error && !_mesa_is_desktop_gl(ctx))
124 goto invalid_enum_error;
125 if (!no_error && param<0)
126 goto invalid_value_error;
127 ctx->Pack.CompressedBlockSize = param;
128 break;
129
130 case GL_UNPACK_SWAP_BYTES:
131 if (!no_error && !_mesa_is_desktop_gl(ctx))
132 goto invalid_enum_error;
133 ctx->Unpack.SwapBytes = param ? GL_TRUE : GL_FALSE;
134 break;
135 case GL_UNPACK_LSB_FIRST:
136 if (!no_error && !_mesa_is_desktop_gl(ctx))
137 goto invalid_enum_error;
138 ctx->Unpack.LsbFirst = param ? GL_TRUE : GL_FALSE;
139 break;
140 case GL_UNPACK_ROW_LENGTH:
141 if (!no_error && ctx->API == API_OPENGLES)
142 goto invalid_enum_error;
143 if (!no_error && param<0)
144 goto invalid_value_error;
145 ctx->Unpack.RowLength = param;
146 break;
147 case GL_UNPACK_IMAGE_HEIGHT:
148 if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
149 goto invalid_enum_error;
150 if (!no_error && param<0)
151 goto invalid_value_error;
152 ctx->Unpack.ImageHeight = param;
153 break;
154 case GL_UNPACK_SKIP_PIXELS:
155 if (!no_error && ctx->API == API_OPENGLES)
156 goto invalid_enum_error;
157 if (!no_error && param<0)
158 goto invalid_value_error;
159 ctx->Unpack.SkipPixels = param;
160 break;
161 case GL_UNPACK_SKIP_ROWS:
162 if (!no_error && ctx->API == API_OPENGLES)
163 goto invalid_enum_error;
164 if (!no_error && param<0)
165 goto invalid_value_error;
166 ctx->Unpack.SkipRows = param;
167 break;
168 case GL_UNPACK_SKIP_IMAGES:
169 if (!no_error && !_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
170 goto invalid_enum_error;
171 if (!no_error && param < 0)
172 goto invalid_value_error;
173 ctx->Unpack.SkipImages = param;
174 break;
175 case GL_UNPACK_ALIGNMENT:
176 if (!no_error && param!=1 && param!=2 && param!=4 && param!=8)
177 goto invalid_value_error;
178 ctx->Unpack.Alignment = param;
179 break;
180 case GL_UNPACK_COMPRESSED_BLOCK_WIDTH:
181 if (!no_error && !_mesa_is_desktop_gl(ctx))
182 goto invalid_enum_error;
183 if (!no_error && param<0)
184 goto invalid_value_error;
185 ctx->Unpack.CompressedBlockWidth = param;
186 break;
187 case GL_UNPACK_COMPRESSED_BLOCK_HEIGHT:
188 if (!no_error && !_mesa_is_desktop_gl(ctx))
189 goto invalid_enum_error;
190 if (!no_error && param<0)
191 goto invalid_value_error;
192 ctx->Unpack.CompressedBlockHeight = param;
193 break;
194 case GL_UNPACK_COMPRESSED_BLOCK_DEPTH:
195 if (!no_error && !_mesa_is_desktop_gl(ctx))
196 goto invalid_enum_error;
197 if (!no_error && param<0)
198 goto invalid_value_error;
199 ctx->Unpack.CompressedBlockDepth = param;
200 break;
201 case GL_UNPACK_COMPRESSED_BLOCK_SIZE:
202 if (!no_error && !_mesa_is_desktop_gl(ctx))
203 goto invalid_enum_error;
204 if (!no_error && param<0)
205 goto invalid_value_error;
206 ctx->Unpack.CompressedBlockSize = param;
207 break;
208 default:
209 if (!no_error)
210 goto invalid_enum_error;
211 else
212 unreachable("invalid pixel store enum");
213 }
214
215 return;
216
217 invalid_enum_error:
218 _mesa_error( ctx, GL_INVALID_ENUM, "glPixelStore" );
219 return;
220
221 invalid_value_error:
222 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
223 return;
224 }
225
226
227 void GLAPIENTRY
228 _mesa_PixelStorei( GLenum pname, GLint param )
229 {
230 pixel_storei(pname, param, false);
231 }
232
233
234 void GLAPIENTRY
235 _mesa_PixelStoref( GLenum pname, GLfloat param )
236 {
237 _mesa_PixelStorei( pname, IROUND(param) );
238 }
239
240
241
242 /**
243 * Initialize the context's pixel store state.
244 */
245 void
246 _mesa_init_pixelstore( struct gl_context *ctx )
247 {
248 /* Pixel transfer */
249 ctx->Pack.Alignment = 4;
250 ctx->Pack.RowLength = 0;
251 ctx->Pack.ImageHeight = 0;
252 ctx->Pack.SkipPixels = 0;
253 ctx->Pack.SkipRows = 0;
254 ctx->Pack.SkipImages = 0;
255 ctx->Pack.SwapBytes = GL_FALSE;
256 ctx->Pack.LsbFirst = GL_FALSE;
257 ctx->Pack.Invert = GL_FALSE;
258 ctx->Pack.CompressedBlockWidth = 0;
259 ctx->Pack.CompressedBlockHeight = 0;
260 ctx->Pack.CompressedBlockDepth = 0;
261 ctx->Pack.CompressedBlockSize = 0;
262 _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj,
263 ctx->Shared->NullBufferObj);
264 ctx->Unpack.Alignment = 4;
265 ctx->Unpack.RowLength = 0;
266 ctx->Unpack.ImageHeight = 0;
267 ctx->Unpack.SkipPixels = 0;
268 ctx->Unpack.SkipRows = 0;
269 ctx->Unpack.SkipImages = 0;
270 ctx->Unpack.SwapBytes = GL_FALSE;
271 ctx->Unpack.LsbFirst = GL_FALSE;
272 ctx->Unpack.Invert = GL_FALSE;
273 ctx->Unpack.CompressedBlockWidth = 0;
274 ctx->Unpack.CompressedBlockHeight = 0;
275 ctx->Unpack.CompressedBlockDepth = 0;
276 ctx->Unpack.CompressedBlockSize = 0;
277 _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj,
278 ctx->Shared->NullBufferObj);
279
280 /*
281 * _mesa_unpack_image() returns image data in this format. When we
282 * execute image commands (glDrawPixels(), glTexImage(), etc) from
283 * within display lists we have to be sure to set the current
284 * unpacking parameters to these values!
285 */
286 ctx->DefaultPacking.Alignment = 1;
287 ctx->DefaultPacking.RowLength = 0;
288 ctx->DefaultPacking.SkipPixels = 0;
289 ctx->DefaultPacking.SkipRows = 0;
290 ctx->DefaultPacking.ImageHeight = 0;
291 ctx->DefaultPacking.SkipImages = 0;
292 ctx->DefaultPacking.SwapBytes = GL_FALSE;
293 ctx->DefaultPacking.LsbFirst = GL_FALSE;
294 ctx->DefaultPacking.Invert = GL_FALSE;
295 _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj,
296 ctx->Shared->NullBufferObj);
297 }
298
299
300 /**
301 * Check if the given compressed pixel storage parameters are legal.
302 * Record a GL error if illegal.
303 * \return true if legal, false if illegal
304 */
305 bool
306 _mesa_compressed_pixel_storage_error_check(
307 struct gl_context *ctx,
308 GLint dimensions,
309 const struct gl_pixelstore_attrib *packing,
310 const char *caller)
311 {
312 if (!_mesa_is_desktop_gl(ctx) || !packing->CompressedBlockSize)
313 return true;
314
315 if (packing->CompressedBlockWidth &&
316 packing->SkipPixels % packing->CompressedBlockWidth) {
317 _mesa_error(ctx, GL_INVALID_OPERATION,
318 "%s(skip-pixels %% block-width)", caller);
319 return false;
320 }
321
322 if (dimensions > 1 &&
323 packing->CompressedBlockHeight &&
324 packing->SkipRows % packing->CompressedBlockHeight) {
325 _mesa_error(ctx, GL_INVALID_OPERATION,
326 "%s(skip-rows %% block-height)", caller);
327 return false;
328 }
329
330 if (dimensions > 2 &&
331 packing->CompressedBlockDepth &&
332 packing->SkipImages % packing->CompressedBlockDepth) {
333 _mesa_error(ctx, GL_INVALID_OPERATION,
334 "%s(skip-images %% block-depth)", caller);
335 return false;
336 }
337
338 return true;
339 }