mesa: fix malformed assertion in _image_format_class_to_glenum()
[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 GLenum
335 _image_format_class_to_glenum(enum image_format_class class)
336 {
337 switch (class) {
338 case IMAGE_FORMAT_CLASS_NONE:
339 return GL_NONE;
340 case IMAGE_FORMAT_CLASS_1X8:
341 return GL_IMAGE_CLASS_1_X_8;
342 case IMAGE_FORMAT_CLASS_1X16:
343 return GL_IMAGE_CLASS_1_X_16;
344 case IMAGE_FORMAT_CLASS_1X32:
345 return GL_IMAGE_CLASS_1_X_32;
346 case IMAGE_FORMAT_CLASS_2X8:
347 return GL_IMAGE_CLASS_2_X_8;
348 case IMAGE_FORMAT_CLASS_2X16:
349 return GL_IMAGE_CLASS_2_X_16;
350 case IMAGE_FORMAT_CLASS_2X32:
351 return GL_IMAGE_CLASS_2_X_32;
352 case IMAGE_FORMAT_CLASS_10_11_11:
353 return GL_IMAGE_CLASS_11_11_10;
354 case IMAGE_FORMAT_CLASS_4X8:
355 return GL_IMAGE_CLASS_4_X_8;
356 case IMAGE_FORMAT_CLASS_4X16:
357 return GL_IMAGE_CLASS_4_X_16;
358 case IMAGE_FORMAT_CLASS_4X32:
359 return GL_IMAGE_CLASS_4_X_32;
360 case IMAGE_FORMAT_CLASS_2_10_10_10:
361 return GL_IMAGE_CLASS_10_10_10_2;
362 default:
363 assert(!"Invalid image_format_class");
364 return GL_NONE;
365 }
366 }
367
368 GLenum
369 _mesa_get_image_format_class(GLenum format)
370 {
371 mesa_format tex_format = _mesa_get_shader_image_format(format);
372 if (tex_format == MESA_FORMAT_NONE)
373 return GL_NONE;
374
375 enum image_format_class class = get_image_format_class(tex_format);
376 return _image_format_class_to_glenum(class);
377 }
378
379 bool
380 _mesa_is_shader_image_format_supported(const struct gl_context *ctx,
381 GLenum format)
382 {
383 switch (format) {
384 /* Formats supported on both desktop and ES GL, c.f. table 8.27 of the
385 * OpenGL ES 3.1 specification.
386 */
387 case GL_RGBA32F:
388 case GL_RGBA16F:
389 case GL_R32F:
390 case GL_RGBA32UI:
391 case GL_RGBA16UI:
392 case GL_RGBA8UI:
393 case GL_R32UI:
394 case GL_RGBA32I:
395 case GL_RGBA16I:
396 case GL_RGBA8I:
397 case GL_R32I:
398 case GL_RGBA8:
399 case GL_RGBA8_SNORM:
400 return true;
401
402 /* Formats supported on unextended desktop GL and the original
403 * ARB_shader_image_load_store extension, c.f. table 3.21 of the OpenGL 4.2
404 * specification.
405 */
406 case GL_RG32F:
407 case GL_RG16F:
408 case GL_R11F_G11F_B10F:
409 case GL_R16F:
410 case GL_RGB10_A2UI:
411 case GL_RG32UI:
412 case GL_RG16UI:
413 case GL_RG8UI:
414 case GL_R16UI:
415 case GL_R8UI:
416 case GL_RG32I:
417 case GL_RG16I:
418 case GL_RG8I:
419 case GL_R16I:
420 case GL_R8I:
421 case GL_RGBA16:
422 case GL_RGB10_A2:
423 case GL_RG16:
424 case GL_RG8:
425 case GL_R16:
426 case GL_R8:
427 case GL_RGBA16_SNORM:
428 case GL_RG16_SNORM:
429 case GL_RG8_SNORM:
430 case GL_R16_SNORM:
431 case GL_R8_SNORM:
432 return _mesa_is_desktop_gl(ctx);
433
434 default:
435 return false;
436 }
437 }
438
439 struct gl_image_unit
440 _mesa_default_image_unit(struct gl_context *ctx)
441 {
442 const GLenum format = _mesa_is_desktop_gl(ctx) ? GL_R8 : GL_R32UI;
443 const struct gl_image_unit u = {
444 .Access = GL_READ_ONLY,
445 .Format = format,
446 ._ActualFormat = _mesa_get_shader_image_format(format)
447 };
448 return u;
449 }
450
451 void
452 _mesa_init_image_units(struct gl_context *ctx)
453 {
454 unsigned i;
455
456 for (i = 0; i < ARRAY_SIZE(ctx->ImageUnits); ++i)
457 ctx->ImageUnits[i] = _mesa_default_image_unit(ctx);
458 }
459
460 GLboolean
461 _mesa_is_image_unit_valid(struct gl_context *ctx, struct gl_image_unit *u)
462 {
463 struct gl_texture_object *t = u->TexObj;
464 mesa_format tex_format;
465
466 if (!t)
467 return GL_FALSE;
468
469 if (!t->_BaseComplete && !t->_MipmapComplete)
470 _mesa_test_texobj_completeness(ctx, t);
471
472 if (u->Level < t->BaseLevel ||
473 u->Level > t->_MaxLevel ||
474 (u->Level == t->BaseLevel && !t->_BaseComplete) ||
475 (u->Level != t->BaseLevel && !t->_MipmapComplete))
476 return GL_FALSE;
477
478 if (_mesa_tex_target_is_layered(t->Target) &&
479 u->_Layer >= _mesa_get_texture_layers(t, u->Level))
480 return GL_FALSE;
481
482 if (t->Target == GL_TEXTURE_BUFFER) {
483 tex_format = _mesa_get_shader_image_format(t->BufferObjectFormat);
484
485 } else {
486 struct gl_texture_image *img = (t->Target == GL_TEXTURE_CUBE_MAP ?
487 t->Image[u->_Layer][u->Level] :
488 t->Image[0][u->Level]);
489
490 if (!img || img->Border || img->NumSamples > ctx->Const.MaxImageSamples)
491 return GL_FALSE;
492
493 tex_format = _mesa_get_shader_image_format(img->InternalFormat);
494 }
495
496 if (!tex_format)
497 return GL_FALSE;
498
499 switch (t->ImageFormatCompatibilityType) {
500 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE:
501 if (_mesa_get_format_bytes(tex_format) !=
502 _mesa_get_format_bytes(u->_ActualFormat))
503 return GL_FALSE;
504 break;
505
506 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_CLASS:
507 if (get_image_format_class(tex_format) !=
508 get_image_format_class(u->_ActualFormat))
509 return GL_FALSE;
510 break;
511
512 default:
513 assert(!"Unexpected image format compatibility type");
514 }
515
516 return GL_TRUE;
517 }
518
519 static GLboolean
520 validate_bind_image_texture(struct gl_context *ctx, GLuint unit,
521 GLuint texture, GLint level, GLboolean layered,
522 GLint layer, GLenum access, GLenum format)
523 {
524 assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS);
525
526 if (unit >= ctx->Const.MaxImageUnits) {
527 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(unit)");
528 return GL_FALSE;
529 }
530
531 if (level < 0) {
532 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(level)");
533 return GL_FALSE;
534 }
535
536 if (layer < 0) {
537 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(layer)");
538 return GL_FALSE;
539 }
540
541 if (access != GL_READ_ONLY &&
542 access != GL_WRITE_ONLY &&
543 access != GL_READ_WRITE) {
544 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(access)");
545 return GL_FALSE;
546 }
547
548 if (!_mesa_is_shader_image_format_supported(ctx, format)) {
549 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(format)");
550 return GL_FALSE;
551 }
552
553 return GL_TRUE;
554 }
555
556 void GLAPIENTRY
557 _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
558 GLboolean layered, GLint layer, GLenum access,
559 GLenum format)
560 {
561 GET_CURRENT_CONTEXT(ctx);
562 struct gl_image_unit *u;
563
564 if (!validate_bind_image_texture(ctx, unit, texture, level,
565 layered, layer, access, format))
566 return;
567
568 u = &ctx->ImageUnits[unit];
569
570 FLUSH_VERTICES(ctx, 0);
571 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
572
573 if (texture) {
574 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texture);
575
576 if (!t) {
577 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(texture)");
578 return;
579 }
580
581 /* From section 8.22 "Texture Image Loads and Stores" of the OpenGL ES
582 * 3.1 spec:
583 *
584 * "An INVALID_OPERATION error is generated if texture is not the name
585 * of an immutable texture object."
586 */
587 if (_mesa_is_gles(ctx) && !t->Immutable) {
588 _mesa_error(ctx, GL_INVALID_OPERATION,
589 "glBindImageTexture(!immutable)");
590 return;
591 }
592
593 _mesa_reference_texobj(&u->TexObj, t);
594 } else {
595 _mesa_reference_texobj(&u->TexObj, NULL);
596 }
597
598 u->Level = level;
599 u->Access = access;
600 u->Format = format;
601 u->_ActualFormat = _mesa_get_shader_image_format(format);
602
603 if (u->TexObj && _mesa_tex_target_is_layered(u->TexObj->Target)) {
604 u->Layered = layered;
605 u->Layer = layer;
606 u->_Layer = (u->Layered ? 0 : u->Layer);
607 } else {
608 u->Layered = GL_FALSE;
609 u->Layer = 0;
610 }
611 }
612
613 void GLAPIENTRY
614 _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
615 {
616 GET_CURRENT_CONTEXT(ctx);
617 int i;
618
619 if (!ctx->Extensions.ARB_shader_image_load_store) {
620 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindImageTextures()");
621 return;
622 }
623
624 if (first + count > ctx->Const.MaxImageUnits) {
625 /* The ARB_multi_bind spec says:
626 *
627 * "An INVALID_OPERATION error is generated if <first> + <count>
628 * is greater than the number of image units supported by
629 * the implementation."
630 */
631 _mesa_error(ctx, GL_INVALID_OPERATION,
632 "glBindImageTextures(first=%u + count=%d > the value of "
633 "GL_MAX_IMAGE_UNITS=%u)",
634 first, count, ctx->Const.MaxImageUnits);
635 return;
636 }
637
638 /* Assume that at least one binding will be changed */
639 FLUSH_VERTICES(ctx, 0);
640 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
641
642 /* Note that the error semantics for multi-bind commands differ from
643 * those of other GL commands.
644 *
645 * The Issues section in the ARB_multi_bind spec says:
646 *
647 * "(11) Typically, OpenGL specifies that if an error is generated by
648 * a command, that command has no effect. This is somewhat
649 * unfortunate for multi-bind commands, because it would require
650 * a first pass to scan the entire list of bound objects for
651 * errors and then a second pass to actually perform the
652 * bindings. Should we have different error semantics?
653 *
654 * RESOLVED: Yes. In this specification, when the parameters for
655 * one of the <count> binding points are invalid, that binding
656 * point is not updated and an error will be generated. However,
657 * other binding points in the same command will be updated if
658 * their parameters are valid and no other error occurs."
659 */
660
661 _mesa_begin_texture_lookups(ctx);
662
663 for (i = 0; i < count; i++) {
664 struct gl_image_unit *u = &ctx->ImageUnits[first + i];
665 const GLuint texture = textures ? textures[i] : 0;
666
667 if (texture != 0) {
668 struct gl_texture_object *texObj;
669 GLenum tex_format;
670
671 if (!u->TexObj || u->TexObj->Name != texture) {
672 texObj = _mesa_lookup_texture_locked(ctx, texture);
673 if (!texObj) {
674 /* The ARB_multi_bind spec says:
675 *
676 * "An INVALID_OPERATION error is generated if any value
677 * in <textures> is not zero or the name of an existing
678 * texture object (per binding)."
679 */
680 _mesa_error(ctx, GL_INVALID_OPERATION,
681 "glBindImageTextures(textures[%d]=%u "
682 "is not zero or the name of an existing texture "
683 "object)", i, texture);
684 continue;
685 }
686 } else {
687 texObj = u->TexObj;
688 }
689
690 if (texObj->Target == GL_TEXTURE_BUFFER) {
691 tex_format = texObj->BufferObjectFormat;
692 } else {
693 struct gl_texture_image *image = texObj->Image[0][0];
694
695 if (!image || image->Width == 0 || image->Height == 0 ||
696 image->Depth == 0) {
697 /* The ARB_multi_bind spec says:
698 *
699 * "An INVALID_OPERATION error is generated if the width,
700 * height, or depth of the level zero texture image of
701 * any texture in <textures> is zero (per binding)."
702 */
703 _mesa_error(ctx, GL_INVALID_OPERATION,
704 "glBindImageTextures(the width, height or depth "
705 "of the level zero texture image of "
706 "textures[%d]=%u is zero)", i, texture);
707 continue;
708 }
709
710 tex_format = image->InternalFormat;
711 }
712
713 if (!_mesa_is_shader_image_format_supported(ctx, tex_format)) {
714 /* The ARB_multi_bind spec says:
715 *
716 * "An INVALID_OPERATION error is generated if the internal
717 * format of the level zero texture image of any texture
718 * in <textures> is not found in table 8.33 (per binding)."
719 */
720 _mesa_error(ctx, GL_INVALID_OPERATION,
721 "glBindImageTextures(the internal format %s of "
722 "the level zero texture image of textures[%d]=%u "
723 "is not supported)",
724 _mesa_enum_to_string(tex_format),
725 i, texture);
726 continue;
727 }
728
729 /* Update the texture binding */
730 _mesa_reference_texobj(&u->TexObj, texObj);
731 u->Level = 0;
732 u->Layered = _mesa_tex_target_is_layered(texObj->Target);
733 u->_Layer = u->Layer = 0;
734 u->Access = GL_READ_WRITE;
735 u->Format = tex_format;
736 u->_ActualFormat = _mesa_get_shader_image_format(tex_format);
737 } else {
738 /* Unbind the texture from the unit */
739 _mesa_reference_texobj(&u->TexObj, NULL);
740 u->Level = 0;
741 u->Layered = GL_FALSE;
742 u->_Layer = u->Layer = 0;
743 u->Access = GL_READ_ONLY;
744 u->Format = GL_R8;
745 u->_ActualFormat = MESA_FORMAT_R_UNORM8;
746 }
747 }
748
749 _mesa_end_texture_lookups(ctx);
750 }
751
752 void GLAPIENTRY
753 _mesa_MemoryBarrier(GLbitfield barriers)
754 {
755 GET_CURRENT_CONTEXT(ctx);
756
757 if (ctx->Driver.MemoryBarrier)
758 ctx->Driver.MemoryBarrier(ctx, barriers);
759 }
760
761 void GLAPIENTRY
762 _mesa_MemoryBarrierByRegion(GLbitfield barriers)
763 {
764 GET_CURRENT_CONTEXT(ctx);
765
766 GLbitfield all_allowed_bits = GL_ATOMIC_COUNTER_BARRIER_BIT |
767 GL_FRAMEBUFFER_BARRIER_BIT |
768 GL_SHADER_IMAGE_ACCESS_BARRIER_BIT |
769 GL_SHADER_STORAGE_BARRIER_BIT |
770 GL_TEXTURE_FETCH_BARRIER_BIT |
771 GL_UNIFORM_BARRIER_BIT;
772
773 if (ctx->Driver.MemoryBarrier) {
774 /* From section 7.11.2 of the OpenGL ES 3.1 specification:
775 *
776 * "When barriers is ALL_BARRIER_BITS, shader memory accesses will be
777 * synchronized relative to all these barrier bits, but not to other
778 * barrier bits specific to MemoryBarrier."
779 *
780 * That is, if barriers is the special value GL_ALL_BARRIER_BITS, then all
781 * barriers allowed by glMemoryBarrierByRegion should be activated."
782 */
783 if (barriers == GL_ALL_BARRIER_BITS) {
784 ctx->Driver.MemoryBarrier(ctx, all_allowed_bits);
785 return;
786 }
787
788 /* From section 7.11.2 of the OpenGL ES 3.1 specification:
789 *
790 * "An INVALID_VALUE error is generated if barriers is not the special
791 * value ALL_BARRIER_BITS, and has any bits set other than those
792 * described above."
793 */
794 if ((barriers & ~all_allowed_bits) != 0) {
795 _mesa_error(ctx, GL_INVALID_VALUE,
796 "glMemoryBarrierByRegion(unsupported barrier bit");
797 }
798
799 ctx->Driver.MemoryBarrier(ctx, barriers);
800 }
801 }