005698c526f4fa99d58e240315117ef0a450d0dd
[mesa.git] / src / mesa / main / shaderimage.c
1 /*
2 * Copyright 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Francisco Jerez <currojerez@riseup.net>
25 */
26
27 #include <assert.h>
28
29 #include "shaderimage.h"
30 #include "mtypes.h"
31 #include "formats.h"
32 #include "errors.h"
33 #include "context.h"
34 #include "texobj.h"
35 #include "teximage.h"
36 #include "enums.h"
37
38 /*
39 * Define endian-invariant aliases for some mesa formats that are
40 * defined in terms of their channel layout from LSB to MSB in a
41 * 32-bit word. The actual byte offsets matter here because the user
42 * is allowed to bit-cast one format into another and get predictable
43 * results.
44 */
45 #ifdef MESA_BIG_ENDIAN
46 # define MESA_FORMAT_RGBA_8 MESA_FORMAT_A8B8G8R8_UNORM
47 # define MESA_FORMAT_RG_16 MESA_FORMAT_G16R16_UNORM
48 # define MESA_FORMAT_RG_8 MESA_FORMAT_G8R8_UNORM
49 # define MESA_FORMAT_SIGNED_RGBA_8 MESA_FORMAT_A8B8G8R8_SNORM
50 # define MESA_FORMAT_SIGNED_RG_16 MESA_FORMAT_G16R16_SNORM
51 # define MESA_FORMAT_SIGNED_RG_8 MESA_FORMAT_G8R8_SNORM
52 #else
53 # define MESA_FORMAT_RGBA_8 MESA_FORMAT_R8G8B8A8_UNORM
54 # define MESA_FORMAT_RG_16 MESA_FORMAT_R16G16_UNORM
55 # define MESA_FORMAT_RG_8 MESA_FORMAT_R8G8_UNORM
56 # define MESA_FORMAT_SIGNED_RGBA_8 MESA_FORMAT_R8G8B8A8_SNORM
57 # define MESA_FORMAT_SIGNED_RG_16 MESA_FORMAT_R16G16_SNORM
58 # define MESA_FORMAT_SIGNED_RG_8 MESA_FORMAT_R8G8_SNORM
59 #endif
60
61 mesa_format
62 _mesa_get_shader_image_format(GLenum format)
63 {
64 switch (format) {
65 case GL_RGBA32F:
66 return MESA_FORMAT_RGBA_FLOAT32;
67
68 case GL_RGBA16F:
69 return MESA_FORMAT_RGBA_FLOAT16;
70
71 case GL_RG32F:
72 return MESA_FORMAT_RG_FLOAT32;
73
74 case GL_RG16F:
75 return MESA_FORMAT_RG_FLOAT16;
76
77 case GL_R11F_G11F_B10F:
78 return MESA_FORMAT_R11G11B10_FLOAT;
79
80 case GL_R32F:
81 return MESA_FORMAT_R_FLOAT32;
82
83 case GL_R16F:
84 return MESA_FORMAT_R_FLOAT16;
85
86 case GL_RGBA32UI:
87 return MESA_FORMAT_RGBA_UINT32;
88
89 case GL_RGBA16UI:
90 return MESA_FORMAT_RGBA_UINT16;
91
92 case GL_RGB10_A2UI:
93 return MESA_FORMAT_R10G10B10A2_UINT;
94
95 case GL_RGBA8UI:
96 return MESA_FORMAT_RGBA_UINT8;
97
98 case GL_RG32UI:
99 return MESA_FORMAT_RG_UINT32;
100
101 case GL_RG16UI:
102 return MESA_FORMAT_RG_UINT16;
103
104 case GL_RG8UI:
105 return MESA_FORMAT_RG_UINT8;
106
107 case GL_R32UI:
108 return MESA_FORMAT_R_UINT32;
109
110 case GL_R16UI:
111 return MESA_FORMAT_R_UINT16;
112
113 case GL_R8UI:
114 return MESA_FORMAT_R_UINT8;
115
116 case GL_RGBA32I:
117 return MESA_FORMAT_RGBA_SINT32;
118
119 case GL_RGBA16I:
120 return MESA_FORMAT_RGBA_SINT16;
121
122 case GL_RGBA8I:
123 return MESA_FORMAT_RGBA_SINT8;
124
125 case GL_RG32I:
126 return MESA_FORMAT_RG_SINT32;
127
128 case GL_RG16I:
129 return MESA_FORMAT_RG_SINT16;
130
131 case GL_RG8I:
132 return MESA_FORMAT_RG_SINT8;
133
134 case GL_R32I:
135 return MESA_FORMAT_R_SINT32;
136
137 case GL_R16I:
138 return MESA_FORMAT_R_SINT16;
139
140 case GL_R8I:
141 return MESA_FORMAT_R_SINT8;
142
143 case GL_RGBA16:
144 return MESA_FORMAT_RGBA_UNORM16;
145
146 case GL_RGB10_A2:
147 return MESA_FORMAT_R10G10B10A2_UNORM;
148
149 case GL_RGBA8:
150 return MESA_FORMAT_RGBA_8;
151
152 case GL_RG16:
153 return MESA_FORMAT_RG_16;
154
155 case GL_RG8:
156 return MESA_FORMAT_RG_8;
157
158 case GL_R16:
159 return MESA_FORMAT_R_UNORM16;
160
161 case GL_R8:
162 return MESA_FORMAT_R_UNORM8;
163
164 case GL_RGBA16_SNORM:
165 return MESA_FORMAT_RGBA_SNORM16;
166
167 case GL_RGBA8_SNORM:
168 return MESA_FORMAT_SIGNED_RGBA_8;
169
170 case GL_RG16_SNORM:
171 return MESA_FORMAT_SIGNED_RG_16;
172
173 case GL_RG8_SNORM:
174 return MESA_FORMAT_SIGNED_RG_8;
175
176 case GL_R16_SNORM:
177 return MESA_FORMAT_R_SNORM16;
178
179 case GL_R8_SNORM:
180 return MESA_FORMAT_R_SNORM8;
181
182 default:
183 return MESA_FORMAT_NONE;
184 }
185 }
186
187 enum image_format_class
188 {
189 /** Not a valid image format. */
190 IMAGE_FORMAT_CLASS_NONE = 0,
191
192 /** Classes of image formats you can cast into each other. */
193 /** \{ */
194 IMAGE_FORMAT_CLASS_1X8,
195 IMAGE_FORMAT_CLASS_1X16,
196 IMAGE_FORMAT_CLASS_1X32,
197 IMAGE_FORMAT_CLASS_2X8,
198 IMAGE_FORMAT_CLASS_2X16,
199 IMAGE_FORMAT_CLASS_2X32,
200 IMAGE_FORMAT_CLASS_10_11_11,
201 IMAGE_FORMAT_CLASS_4X8,
202 IMAGE_FORMAT_CLASS_4X16,
203 IMAGE_FORMAT_CLASS_4X32,
204 IMAGE_FORMAT_CLASS_2_10_10_10
205 /** \} */
206 };
207
208 static enum image_format_class
209 get_image_format_class(mesa_format format)
210 {
211 switch (format) {
212 case MESA_FORMAT_RGBA_FLOAT32:
213 return IMAGE_FORMAT_CLASS_4X32;
214
215 case MESA_FORMAT_RGBA_FLOAT16:
216 return IMAGE_FORMAT_CLASS_4X16;
217
218 case MESA_FORMAT_RG_FLOAT32:
219 return IMAGE_FORMAT_CLASS_2X32;
220
221 case MESA_FORMAT_RG_FLOAT16:
222 return IMAGE_FORMAT_CLASS_2X16;
223
224 case MESA_FORMAT_R11G11B10_FLOAT:
225 return IMAGE_FORMAT_CLASS_10_11_11;
226
227 case MESA_FORMAT_R_FLOAT32:
228 return IMAGE_FORMAT_CLASS_1X32;
229
230 case MESA_FORMAT_R_FLOAT16:
231 return IMAGE_FORMAT_CLASS_1X16;
232
233 case MESA_FORMAT_RGBA_UINT32:
234 return IMAGE_FORMAT_CLASS_4X32;
235
236 case MESA_FORMAT_RGBA_UINT16:
237 return IMAGE_FORMAT_CLASS_4X16;
238
239 case MESA_FORMAT_R10G10B10A2_UINT:
240 return IMAGE_FORMAT_CLASS_2_10_10_10;
241
242 case MESA_FORMAT_RGBA_UINT8:
243 return IMAGE_FORMAT_CLASS_4X8;
244
245 case MESA_FORMAT_RG_UINT32:
246 return IMAGE_FORMAT_CLASS_2X32;
247
248 case MESA_FORMAT_RG_UINT16:
249 return IMAGE_FORMAT_CLASS_2X16;
250
251 case MESA_FORMAT_RG_UINT8:
252 return IMAGE_FORMAT_CLASS_2X8;
253
254 case MESA_FORMAT_R_UINT32:
255 return IMAGE_FORMAT_CLASS_1X32;
256
257 case MESA_FORMAT_R_UINT16:
258 return IMAGE_FORMAT_CLASS_1X16;
259
260 case MESA_FORMAT_R_UINT8:
261 return IMAGE_FORMAT_CLASS_1X8;
262
263 case MESA_FORMAT_RGBA_SINT32:
264 return IMAGE_FORMAT_CLASS_4X32;
265
266 case MESA_FORMAT_RGBA_SINT16:
267 return IMAGE_FORMAT_CLASS_4X16;
268
269 case MESA_FORMAT_RGBA_SINT8:
270 return IMAGE_FORMAT_CLASS_4X8;
271
272 case MESA_FORMAT_RG_SINT32:
273 return IMAGE_FORMAT_CLASS_2X32;
274
275 case MESA_FORMAT_RG_SINT16:
276 return IMAGE_FORMAT_CLASS_2X16;
277
278 case MESA_FORMAT_RG_SINT8:
279 return IMAGE_FORMAT_CLASS_2X8;
280
281 case MESA_FORMAT_R_SINT32:
282 return IMAGE_FORMAT_CLASS_1X32;
283
284 case MESA_FORMAT_R_SINT16:
285 return IMAGE_FORMAT_CLASS_1X16;
286
287 case MESA_FORMAT_R_SINT8:
288 return IMAGE_FORMAT_CLASS_1X8;
289
290 case MESA_FORMAT_RGBA_UNORM16:
291 return IMAGE_FORMAT_CLASS_4X16;
292
293 case MESA_FORMAT_R10G10B10A2_UNORM:
294 return IMAGE_FORMAT_CLASS_2_10_10_10;
295
296 case MESA_FORMAT_RGBA_8:
297 return IMAGE_FORMAT_CLASS_4X8;
298
299 case MESA_FORMAT_RG_16:
300 return IMAGE_FORMAT_CLASS_2X16;
301
302 case MESA_FORMAT_RG_8:
303 return IMAGE_FORMAT_CLASS_2X8;
304
305 case MESA_FORMAT_R_UNORM16:
306 return IMAGE_FORMAT_CLASS_1X16;
307
308 case MESA_FORMAT_R_UNORM8:
309 return IMAGE_FORMAT_CLASS_1X8;
310
311 case MESA_FORMAT_RGBA_SNORM16:
312 return IMAGE_FORMAT_CLASS_4X16;
313
314 case MESA_FORMAT_SIGNED_RGBA_8:
315 return IMAGE_FORMAT_CLASS_4X8;
316
317 case MESA_FORMAT_SIGNED_RG_16:
318 return IMAGE_FORMAT_CLASS_2X16;
319
320 case MESA_FORMAT_SIGNED_RG_8:
321 return IMAGE_FORMAT_CLASS_2X8;
322
323 case MESA_FORMAT_R_SNORM16:
324 return IMAGE_FORMAT_CLASS_1X16;
325
326 case MESA_FORMAT_R_SNORM8:
327 return IMAGE_FORMAT_CLASS_1X8;
328
329 default:
330 return IMAGE_FORMAT_CLASS_NONE;
331 }
332 }
333
334 static GLboolean
335 validate_image_unit(struct gl_context *ctx, struct gl_image_unit *u)
336 {
337 struct gl_texture_object *t = u->TexObj;
338 mesa_format tex_format;
339
340 if (!t || u->Level < t->BaseLevel ||
341 u->Level > t->_MaxLevel)
342 return GL_FALSE;
343
344 _mesa_test_texobj_completeness(ctx, t);
345
346 if ((u->Level == t->BaseLevel && !t->_BaseComplete) ||
347 (u->Level != t->BaseLevel && !t->_MipmapComplete))
348 return GL_FALSE;
349
350 if (_mesa_tex_target_is_layered(t->Target) &&
351 u->Layer >= _mesa_get_texture_layers(t, u->Level))
352 return GL_FALSE;
353
354 if (t->Target == GL_TEXTURE_BUFFER) {
355 tex_format = t->_BufferObjectFormat;
356
357 } else {
358 struct gl_texture_image *img = (t->Target == GL_TEXTURE_CUBE_MAP ?
359 t->Image[u->Layer][u->Level] :
360 t->Image[0][u->Level]);
361
362 if (!img || img->Border || img->NumSamples > ctx->Const.MaxImageSamples)
363 return GL_FALSE;
364
365 tex_format = img->TexFormat;
366 }
367
368 if (get_image_format_class(tex_format) == IMAGE_FORMAT_CLASS_NONE)
369 return GL_FALSE;
370
371 switch (t->ImageFormatCompatibilityType) {
372 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE:
373 if (_mesa_get_format_bytes(tex_format) !=
374 _mesa_get_format_bytes(u->_ActualFormat))
375 return GL_FALSE;
376 break;
377
378 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_CLASS:
379 if (get_image_format_class(tex_format) !=
380 get_image_format_class(u->_ActualFormat))
381 return GL_FALSE;
382 break;
383
384 default:
385 assert(!"Unexpected image format compatibility type");
386 }
387
388 return GL_TRUE;
389 }
390
391 void
392 _mesa_validate_image_units(struct gl_context *ctx)
393 {
394 unsigned i;
395
396 for (i = 0; i < ctx->Const.MaxImageUnits; ++i) {
397 struct gl_image_unit *u = &ctx->ImageUnits[i];
398 u->_Valid = validate_image_unit(ctx, u);
399 }
400 }
401
402 static GLboolean
403 validate_bind_image_texture(struct gl_context *ctx, GLuint unit,
404 GLuint texture, GLint level, GLboolean layered,
405 GLint layer, GLenum access, GLenum format)
406 {
407 assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS);
408
409 if (unit >= ctx->Const.MaxImageUnits) {
410 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(unit)");
411 return GL_FALSE;
412 }
413
414 if (level < 0) {
415 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(level)");
416 return GL_FALSE;
417 }
418
419 if (layer < 0) {
420 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(layer)");
421 return GL_FALSE;
422 }
423
424 if (access != GL_READ_ONLY &&
425 access != GL_WRITE_ONLY &&
426 access != GL_READ_WRITE) {
427 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(access)");
428 return GL_FALSE;
429 }
430
431 if (!_mesa_get_shader_image_format(format)) {
432 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(format)");
433 return GL_FALSE;
434 }
435
436 return GL_TRUE;
437 }
438
439 void GLAPIENTRY
440 _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
441 GLboolean layered, GLint layer, GLenum access,
442 GLenum format)
443 {
444 GET_CURRENT_CONTEXT(ctx);
445 struct gl_texture_object *t = NULL;
446 struct gl_image_unit *u;
447
448 if (!validate_bind_image_texture(ctx, unit, texture, level,
449 layered, layer, access, format))
450 return;
451
452 u = &ctx->ImageUnits[unit];
453
454 FLUSH_VERTICES(ctx, 0);
455 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
456
457 if (texture) {
458 t = _mesa_lookup_texture(ctx, texture);
459 if (!t) {
460 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(texture)");
461 return;
462 }
463
464 _mesa_reference_texobj(&u->TexObj, t);
465 u->Level = level;
466 u->Access = access;
467 u->Format = format;
468 u->_ActualFormat = _mesa_get_shader_image_format(format);
469
470 if (_mesa_tex_target_is_layered(t->Target)) {
471 u->Layered = layered;
472 u->Layer = (layered ? 0 : layer);
473 } else {
474 u->Layered = GL_FALSE;
475 u->Layer = 0;
476 }
477
478 } else {
479 _mesa_reference_texobj(&u->TexObj, NULL);
480 }
481
482 u->_Valid = validate_image_unit(ctx, u);
483
484 if (ctx->Driver.BindImageTexture)
485 ctx->Driver.BindImageTexture(ctx, u, t, level, layered,
486 layer, access, format);
487 }
488
489 void GLAPIENTRY
490 _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
491 {
492 GET_CURRENT_CONTEXT(ctx);
493 int i;
494
495 if (!ctx->Extensions.ARB_shader_image_load_store) {
496 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindImageTextures()");
497 return;
498 }
499
500 if (first + count > ctx->Const.MaxImageUnits) {
501 /* The ARB_multi_bind spec says:
502 *
503 * "An INVALID_OPERATION error is generated if <first> + <count>
504 * is greater than the number of image units supported by
505 * the implementation."
506 */
507 _mesa_error(ctx, GL_INVALID_OPERATION,
508 "glBindImageTextures(first=%u + count=%d > the value of "
509 "GL_MAX_IMAGE_UNITS=%u)",
510 first, count, ctx->Const.MaxImageUnits);
511 return;
512 }
513
514 /* Assume that at least one binding will be changed */
515 FLUSH_VERTICES(ctx, 0);
516 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
517
518 /* Note that the error semantics for multi-bind commands differ from
519 * those of other GL commands.
520 *
521 * The Issues section in the ARB_multi_bind spec says:
522 *
523 * "(11) Typically, OpenGL specifies that if an error is generated by
524 * a command, that command has no effect. This is somewhat
525 * unfortunate for multi-bind commands, because it would require
526 * a first pass to scan the entire list of bound objects for
527 * errors and then a second pass to actually perform the
528 * bindings. Should we have different error semantics?
529 *
530 * RESOLVED: Yes. In this specification, when the parameters for
531 * one of the <count> binding points are invalid, that binding
532 * point is not updated and an error will be generated. However,
533 * other binding points in the same command will be updated if
534 * their parameters are valid and no other error occurs."
535 */
536
537 _mesa_begin_texture_lookups(ctx);
538
539 for (i = 0; i < count; i++) {
540 struct gl_image_unit *u = &ctx->ImageUnits[first + i];
541 const GLuint texture = textures ? textures[i] : 0;
542
543 if (texture != 0) {
544 struct gl_texture_object *texObj;
545 GLenum tex_format;
546
547 if (!u->TexObj || u->TexObj->Name != texture) {
548 texObj = _mesa_lookup_texture_locked(ctx, texture);
549 if (!texObj) {
550 /* The ARB_multi_bind spec says:
551 *
552 * "An INVALID_OPERATION error is generated if any value
553 * in <textures> is not zero or the name of an existing
554 * texture object (per binding)."
555 */
556 _mesa_error(ctx, GL_INVALID_OPERATION,
557 "glBindImageTextures(textures[%d]=%u "
558 "is not zero or the name of an existing texture "
559 "object)", i, texture);
560 continue;
561 }
562 } else {
563 texObj = u->TexObj;
564 }
565
566 if (texObj->Target == GL_TEXTURE_BUFFER) {
567 tex_format = texObj->BufferObjectFormat;
568 } else {
569 struct gl_texture_image *image = texObj->Image[0][0];
570
571 if (!image || image->Width == 0 || image->Height == 0 ||
572 image->Depth == 0) {
573 /* The ARB_multi_bind spec says:
574 *
575 * "An INVALID_OPERATION error is generated if the width,
576 * height, or depth of the level zero texture image of
577 * any texture in <textures> is zero (per binding)."
578 */
579 _mesa_error(ctx, GL_INVALID_OPERATION,
580 "glBindImageTextures(the width, height or depth "
581 "of the level zero texture image of "
582 "textures[%d]=%u is zero)", i, texture);
583 continue;
584 }
585
586 tex_format = image->InternalFormat;
587 }
588
589 if (_mesa_get_shader_image_format(tex_format) == MESA_FORMAT_NONE) {
590 /* The ARB_multi_bind spec says:
591 *
592 * "An INVALID_OPERATION error is generated if the internal
593 * format of the level zero texture image of any texture
594 * in <textures> is not found in table 8.33 (per binding)."
595 */
596 _mesa_error(ctx, GL_INVALID_OPERATION,
597 "glBindImageTextures(the internal format %s of "
598 "the level zero texture image of textures[%d]=%u "
599 "is not supported)",
600 _mesa_lookup_enum_by_nr(tex_format),
601 i, texture);
602 continue;
603 }
604
605 /* Update the texture binding */
606 _mesa_reference_texobj(&u->TexObj, texObj);
607 u->Level = 0;
608 u->Layered = _mesa_tex_target_is_layered(texObj->Target);
609 u->Layer = 0;
610 u->Access = GL_READ_WRITE;
611 u->Format = tex_format;
612 u->_ActualFormat = _mesa_get_shader_image_format(tex_format);
613 u->_Valid = validate_image_unit(ctx, u);
614 } else {
615 /* Unbind the texture from the unit */
616 _mesa_reference_texobj(&u->TexObj, NULL);
617 u->Level = 0;
618 u->Layered = GL_FALSE;
619 u->Layer = 0;
620 u->Access = GL_READ_ONLY;
621 u->Format = GL_R8;
622 u->_ActualFormat = MESA_FORMAT_R_UNORM8;
623 u->_Valid = GL_FALSE;
624 }
625
626 /* Pass the BindImageTexture call down to the device driver */
627 if (ctx->Driver.BindImageTexture)
628 ctx->Driver.BindImageTexture(ctx, u, u->TexObj, u->Level, u->Layered,
629 u->Layer, u->Access, u->Format);
630 }
631
632 _mesa_end_texture_lookups(ctx);
633 }
634
635 void GLAPIENTRY
636 _mesa_MemoryBarrier(GLbitfield barriers)
637 {
638 GET_CURRENT_CONTEXT(ctx);
639
640 if (ctx->Driver.MemoryBarrier)
641 ctx->Driver.MemoryBarrier(ctx, barriers);
642 }