mesa: add EXT_shader_image_load_store glBindImageTextureEXT function
[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 "hash.h"
34 #include "context.h"
35 #include "texobj.h"
36 #include "teximage.h"
37 #include "enums.h"
38
39 /*
40 * Define endian-invariant aliases for some mesa formats that are
41 * defined in terms of their channel layout from LSB to MSB in a
42 * 32-bit word. The actual byte offsets matter here because the user
43 * is allowed to bit-cast one format into another and get predictable
44 * results.
45 */
46 #ifdef MESA_BIG_ENDIAN
47 # define MESA_FORMAT_RGBA_8 MESA_FORMAT_A8B8G8R8_UNORM
48 # define MESA_FORMAT_RG_16 MESA_FORMAT_G16R16_UNORM
49 # define MESA_FORMAT_RG_8 MESA_FORMAT_G8R8_UNORM
50 # define MESA_FORMAT_SIGNED_RGBA_8 MESA_FORMAT_A8B8G8R8_SNORM
51 # define MESA_FORMAT_SIGNED_RG_16 MESA_FORMAT_G16R16_SNORM
52 # define MESA_FORMAT_SIGNED_RG_8 MESA_FORMAT_G8R8_SNORM
53 #else
54 # define MESA_FORMAT_RGBA_8 MESA_FORMAT_R8G8B8A8_UNORM
55 # define MESA_FORMAT_RG_16 MESA_FORMAT_R16G16_UNORM
56 # define MESA_FORMAT_RG_8 MESA_FORMAT_R8G8_UNORM
57 # define MESA_FORMAT_SIGNED_RGBA_8 MESA_FORMAT_R8G8B8A8_SNORM
58 # define MESA_FORMAT_SIGNED_RG_16 MESA_FORMAT_R16G16_SNORM
59 # define MESA_FORMAT_SIGNED_RG_8 MESA_FORMAT_R8G8_SNORM
60 #endif
61
62 mesa_format
63 _mesa_get_shader_image_format(GLenum format)
64 {
65 switch (format) {
66 case GL_RGBA32F:
67 return MESA_FORMAT_RGBA_FLOAT32;
68
69 case GL_RGBA16F:
70 return MESA_FORMAT_RGBA_FLOAT16;
71
72 case GL_RG32F:
73 return MESA_FORMAT_RG_FLOAT32;
74
75 case GL_RG16F:
76 return MESA_FORMAT_RG_FLOAT16;
77
78 case GL_R11F_G11F_B10F:
79 return MESA_FORMAT_R11G11B10_FLOAT;
80
81 case GL_R32F:
82 return MESA_FORMAT_R_FLOAT32;
83
84 case GL_R16F:
85 return MESA_FORMAT_R_FLOAT16;
86
87 case GL_RGBA32UI:
88 return MESA_FORMAT_RGBA_UINT32;
89
90 case GL_RGBA16UI:
91 return MESA_FORMAT_RGBA_UINT16;
92
93 case GL_RGB10_A2UI:
94 return MESA_FORMAT_R10G10B10A2_UINT;
95
96 case GL_RGBA8UI:
97 return MESA_FORMAT_RGBA_UINT8;
98
99 case GL_RG32UI:
100 return MESA_FORMAT_RG_UINT32;
101
102 case GL_RG16UI:
103 return MESA_FORMAT_RG_UINT16;
104
105 case GL_RG8UI:
106 return MESA_FORMAT_RG_UINT8;
107
108 case GL_R32UI:
109 return MESA_FORMAT_R_UINT32;
110
111 case GL_R16UI:
112 return MESA_FORMAT_R_UINT16;
113
114 case GL_R8UI:
115 return MESA_FORMAT_R_UINT8;
116
117 case GL_RGBA32I:
118 return MESA_FORMAT_RGBA_SINT32;
119
120 case GL_RGBA16I:
121 return MESA_FORMAT_RGBA_SINT16;
122
123 case GL_RGBA8I:
124 return MESA_FORMAT_RGBA_SINT8;
125
126 case GL_RG32I:
127 return MESA_FORMAT_RG_SINT32;
128
129 case GL_RG16I:
130 return MESA_FORMAT_RG_SINT16;
131
132 case GL_RG8I:
133 return MESA_FORMAT_RG_SINT8;
134
135 case GL_R32I:
136 return MESA_FORMAT_R_SINT32;
137
138 case GL_R16I:
139 return MESA_FORMAT_R_SINT16;
140
141 case GL_R8I:
142 return MESA_FORMAT_R_SINT8;
143
144 case GL_RGBA16:
145 return MESA_FORMAT_RGBA_UNORM16;
146
147 case GL_RGB10_A2:
148 return MESA_FORMAT_R10G10B10A2_UNORM;
149
150 case GL_RGBA8:
151 return MESA_FORMAT_RGBA_8;
152
153 case GL_RG16:
154 return MESA_FORMAT_RG_16;
155
156 case GL_RG8:
157 return MESA_FORMAT_RG_8;
158
159 case GL_R16:
160 return MESA_FORMAT_R_UNORM16;
161
162 case GL_R8:
163 return MESA_FORMAT_R_UNORM8;
164
165 case GL_RGBA16_SNORM:
166 return MESA_FORMAT_RGBA_SNORM16;
167
168 case GL_RGBA8_SNORM:
169 return MESA_FORMAT_SIGNED_RGBA_8;
170
171 case GL_RG16_SNORM:
172 return MESA_FORMAT_SIGNED_RG_16;
173
174 case GL_RG8_SNORM:
175 return MESA_FORMAT_SIGNED_RG_8;
176
177 case GL_R16_SNORM:
178 return MESA_FORMAT_R_SNORM16;
179
180 case GL_R8_SNORM:
181 return MESA_FORMAT_R_SNORM8;
182
183 default:
184 return MESA_FORMAT_NONE;
185 }
186 }
187
188 enum image_format_class
189 {
190 /** Not a valid image format. */
191 IMAGE_FORMAT_CLASS_NONE = 0,
192
193 /** Classes of image formats you can cast into each other. */
194 /** \{ */
195 IMAGE_FORMAT_CLASS_1X8,
196 IMAGE_FORMAT_CLASS_1X16,
197 IMAGE_FORMAT_CLASS_1X32,
198 IMAGE_FORMAT_CLASS_2X8,
199 IMAGE_FORMAT_CLASS_2X16,
200 IMAGE_FORMAT_CLASS_2X32,
201 IMAGE_FORMAT_CLASS_10_11_11,
202 IMAGE_FORMAT_CLASS_4X8,
203 IMAGE_FORMAT_CLASS_4X16,
204 IMAGE_FORMAT_CLASS_4X32,
205 IMAGE_FORMAT_CLASS_2_10_10_10
206 /** \} */
207 };
208
209 static enum image_format_class
210 get_image_format_class(mesa_format format)
211 {
212 switch (format) {
213 case MESA_FORMAT_RGBA_FLOAT32:
214 return IMAGE_FORMAT_CLASS_4X32;
215
216 case MESA_FORMAT_RGBA_FLOAT16:
217 return IMAGE_FORMAT_CLASS_4X16;
218
219 case MESA_FORMAT_RG_FLOAT32:
220 return IMAGE_FORMAT_CLASS_2X32;
221
222 case MESA_FORMAT_RG_FLOAT16:
223 return IMAGE_FORMAT_CLASS_2X16;
224
225 case MESA_FORMAT_R11G11B10_FLOAT:
226 return IMAGE_FORMAT_CLASS_10_11_11;
227
228 case MESA_FORMAT_R_FLOAT32:
229 return IMAGE_FORMAT_CLASS_1X32;
230
231 case MESA_FORMAT_R_FLOAT16:
232 return IMAGE_FORMAT_CLASS_1X16;
233
234 case MESA_FORMAT_RGBA_UINT32:
235 return IMAGE_FORMAT_CLASS_4X32;
236
237 case MESA_FORMAT_RGBA_UINT16:
238 return IMAGE_FORMAT_CLASS_4X16;
239
240 case MESA_FORMAT_R10G10B10A2_UINT:
241 return IMAGE_FORMAT_CLASS_2_10_10_10;
242
243 case MESA_FORMAT_RGBA_UINT8:
244 return IMAGE_FORMAT_CLASS_4X8;
245
246 case MESA_FORMAT_RG_UINT32:
247 return IMAGE_FORMAT_CLASS_2X32;
248
249 case MESA_FORMAT_RG_UINT16:
250 return IMAGE_FORMAT_CLASS_2X16;
251
252 case MESA_FORMAT_RG_UINT8:
253 return IMAGE_FORMAT_CLASS_2X8;
254
255 case MESA_FORMAT_R_UINT32:
256 return IMAGE_FORMAT_CLASS_1X32;
257
258 case MESA_FORMAT_R_UINT16:
259 return IMAGE_FORMAT_CLASS_1X16;
260
261 case MESA_FORMAT_R_UINT8:
262 return IMAGE_FORMAT_CLASS_1X8;
263
264 case MESA_FORMAT_RGBA_SINT32:
265 return IMAGE_FORMAT_CLASS_4X32;
266
267 case MESA_FORMAT_RGBA_SINT16:
268 return IMAGE_FORMAT_CLASS_4X16;
269
270 case MESA_FORMAT_RGBA_SINT8:
271 return IMAGE_FORMAT_CLASS_4X8;
272
273 case MESA_FORMAT_RG_SINT32:
274 return IMAGE_FORMAT_CLASS_2X32;
275
276 case MESA_FORMAT_RG_SINT16:
277 return IMAGE_FORMAT_CLASS_2X16;
278
279 case MESA_FORMAT_RG_SINT8:
280 return IMAGE_FORMAT_CLASS_2X8;
281
282 case MESA_FORMAT_R_SINT32:
283 return IMAGE_FORMAT_CLASS_1X32;
284
285 case MESA_FORMAT_R_SINT16:
286 return IMAGE_FORMAT_CLASS_1X16;
287
288 case MESA_FORMAT_R_SINT8:
289 return IMAGE_FORMAT_CLASS_1X8;
290
291 case MESA_FORMAT_RGBA_UNORM16:
292 return IMAGE_FORMAT_CLASS_4X16;
293
294 case MESA_FORMAT_R10G10B10A2_UNORM:
295 return IMAGE_FORMAT_CLASS_2_10_10_10;
296
297 case MESA_FORMAT_RGBA_8:
298 return IMAGE_FORMAT_CLASS_4X8;
299
300 case MESA_FORMAT_RG_16:
301 return IMAGE_FORMAT_CLASS_2X16;
302
303 case MESA_FORMAT_RG_8:
304 return IMAGE_FORMAT_CLASS_2X8;
305
306 case MESA_FORMAT_R_UNORM16:
307 return IMAGE_FORMAT_CLASS_1X16;
308
309 case MESA_FORMAT_R_UNORM8:
310 return IMAGE_FORMAT_CLASS_1X8;
311
312 case MESA_FORMAT_RGBA_SNORM16:
313 return IMAGE_FORMAT_CLASS_4X16;
314
315 case MESA_FORMAT_SIGNED_RGBA_8:
316 return IMAGE_FORMAT_CLASS_4X8;
317
318 case MESA_FORMAT_SIGNED_RG_16:
319 return IMAGE_FORMAT_CLASS_2X16;
320
321 case MESA_FORMAT_SIGNED_RG_8:
322 return IMAGE_FORMAT_CLASS_2X8;
323
324 case MESA_FORMAT_R_SNORM16:
325 return IMAGE_FORMAT_CLASS_1X16;
326
327 case MESA_FORMAT_R_SNORM8:
328 return IMAGE_FORMAT_CLASS_1X8;
329
330 default:
331 return IMAGE_FORMAT_CLASS_NONE;
332 }
333 }
334
335 static GLenum
336 _image_format_class_to_glenum(enum image_format_class class)
337 {
338 switch (class) {
339 case IMAGE_FORMAT_CLASS_NONE:
340 return GL_NONE;
341 case IMAGE_FORMAT_CLASS_1X8:
342 return GL_IMAGE_CLASS_1_X_8;
343 case IMAGE_FORMAT_CLASS_1X16:
344 return GL_IMAGE_CLASS_1_X_16;
345 case IMAGE_FORMAT_CLASS_1X32:
346 return GL_IMAGE_CLASS_1_X_32;
347 case IMAGE_FORMAT_CLASS_2X8:
348 return GL_IMAGE_CLASS_2_X_8;
349 case IMAGE_FORMAT_CLASS_2X16:
350 return GL_IMAGE_CLASS_2_X_16;
351 case IMAGE_FORMAT_CLASS_2X32:
352 return GL_IMAGE_CLASS_2_X_32;
353 case IMAGE_FORMAT_CLASS_10_11_11:
354 return GL_IMAGE_CLASS_11_11_10;
355 case IMAGE_FORMAT_CLASS_4X8:
356 return GL_IMAGE_CLASS_4_X_8;
357 case IMAGE_FORMAT_CLASS_4X16:
358 return GL_IMAGE_CLASS_4_X_16;
359 case IMAGE_FORMAT_CLASS_4X32:
360 return GL_IMAGE_CLASS_4_X_32;
361 case IMAGE_FORMAT_CLASS_2_10_10_10:
362 return GL_IMAGE_CLASS_10_10_10_2;
363 default:
364 assert(!"Invalid image_format_class");
365 return GL_NONE;
366 }
367 }
368
369 GLenum
370 _mesa_get_image_format_class(GLenum format)
371 {
372 mesa_format tex_format = _mesa_get_shader_image_format(format);
373 if (tex_format == MESA_FORMAT_NONE)
374 return GL_NONE;
375
376 enum image_format_class class = get_image_format_class(tex_format);
377 return _image_format_class_to_glenum(class);
378 }
379
380 bool
381 _mesa_is_shader_image_format_supported(const struct gl_context *ctx,
382 GLenum format)
383 {
384 switch (format) {
385 /* Formats supported on both desktop and ES GL, c.f. table 8.27 of the
386 * OpenGL ES 3.1 specification.
387 */
388 case GL_RGBA32F:
389 case GL_RGBA16F:
390 case GL_R32F:
391 case GL_RGBA32UI:
392 case GL_RGBA16UI:
393 case GL_RGBA8UI:
394 case GL_R32UI:
395 case GL_RGBA32I:
396 case GL_RGBA16I:
397 case GL_RGBA8I:
398 case GL_R32I:
399 case GL_RGBA8:
400 case GL_RGBA8_SNORM:
401 return true;
402
403 /* Formats supported on unextended desktop GL and the original
404 * ARB_shader_image_load_store extension, c.f. table 3.21 of the OpenGL 4.2
405 * specification or by GLES 3.1 with GL_NV_image_formats extension.
406 */
407 case GL_RG32F:
408 case GL_RG16F:
409 case GL_R11F_G11F_B10F:
410 case GL_R16F:
411 case GL_RGB10_A2UI:
412 case GL_RG32UI:
413 case GL_RG16UI:
414 case GL_RG8UI:
415 case GL_R16UI:
416 case GL_R8UI:
417 case GL_RG32I:
418 case GL_RG16I:
419 case GL_RG8I:
420 case GL_R16I:
421 case GL_R8I:
422 case GL_RGB10_A2:
423 case GL_RG8:
424 case GL_R8:
425 case GL_RG8_SNORM:
426 case GL_R8_SNORM:
427 return true;
428
429 /* Formats supported on unextended desktop GL and the original
430 * ARB_shader_image_load_store extension, c.f. table 3.21 of the OpenGL 4.2
431 * specification.
432 *
433 * Following formats are supported by GLES 3.1 with GL_NV_image_formats &
434 * GL_EXT_texture_norm16 extensions.
435 */
436 case GL_RGBA16:
437 case GL_RGBA16_SNORM:
438 case GL_RG16:
439 case GL_RG16_SNORM:
440 case GL_R16:
441 case GL_R16_SNORM:
442 return _mesa_is_desktop_gl(ctx) || _mesa_has_EXT_texture_norm16(ctx);
443
444 default:
445 return false;
446 }
447 }
448
449 struct gl_image_unit
450 _mesa_default_image_unit(struct gl_context *ctx)
451 {
452 const GLenum format = _mesa_is_desktop_gl(ctx) ? GL_R8 : GL_R32UI;
453 const struct gl_image_unit u = {
454 .Access = GL_READ_ONLY,
455 .Format = format,
456 ._ActualFormat = _mesa_get_shader_image_format(format)
457 };
458 return u;
459 }
460
461 void
462 _mesa_init_image_units(struct gl_context *ctx)
463 {
464 unsigned i;
465
466 ASSERT_BITFIELD_SIZE(struct gl_image_unit, Format, MESA_FORMAT_COUNT);
467
468 for (i = 0; i < ARRAY_SIZE(ctx->ImageUnits); ++i)
469 ctx->ImageUnits[i] = _mesa_default_image_unit(ctx);
470 }
471
472
473 void
474 _mesa_free_image_textures(struct gl_context *ctx)
475 {
476 unsigned i;
477
478 for (i = 0; i < ARRAY_SIZE(ctx->ImageUnits); ++i)
479 _mesa_reference_texobj(&ctx->ImageUnits[i].TexObj, NULL);
480 }
481
482 GLboolean
483 _mesa_is_image_unit_valid(struct gl_context *ctx, struct gl_image_unit *u)
484 {
485 struct gl_texture_object *t = u->TexObj;
486 mesa_format tex_format;
487
488 if (!t)
489 return GL_FALSE;
490
491 if (!t->_BaseComplete && !t->_MipmapComplete)
492 _mesa_test_texobj_completeness(ctx, t);
493
494 if (u->Level < t->BaseLevel ||
495 u->Level > t->_MaxLevel ||
496 (u->Level == t->BaseLevel && !t->_BaseComplete) ||
497 (u->Level != t->BaseLevel && !t->_MipmapComplete))
498 return GL_FALSE;
499
500 if (_mesa_tex_target_is_layered(t->Target) &&
501 u->_Layer >= _mesa_get_texture_layers(t, u->Level))
502 return GL_FALSE;
503
504 if (t->Target == GL_TEXTURE_BUFFER) {
505 tex_format = _mesa_get_shader_image_format(t->BufferObjectFormat);
506
507 } else {
508 struct gl_texture_image *img = (t->Target == GL_TEXTURE_CUBE_MAP ?
509 t->Image[u->_Layer][u->Level] :
510 t->Image[0][u->Level]);
511
512 if (!img || img->Border || img->NumSamples > ctx->Const.MaxImageSamples)
513 return GL_FALSE;
514
515 tex_format = _mesa_get_shader_image_format(img->InternalFormat);
516 }
517
518 if (!tex_format)
519 return GL_FALSE;
520
521 switch (t->ImageFormatCompatibilityType) {
522 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE:
523 if (_mesa_get_format_bytes(tex_format) !=
524 _mesa_get_format_bytes(u->_ActualFormat))
525 return GL_FALSE;
526 break;
527
528 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_CLASS:
529 if (get_image_format_class(tex_format) !=
530 get_image_format_class(u->_ActualFormat))
531 return GL_FALSE;
532 break;
533
534 default:
535 assert(!"Unexpected image format compatibility type");
536 }
537
538 return GL_TRUE;
539 }
540
541 static GLboolean
542 validate_bind_image_texture(struct gl_context *ctx, GLuint unit,
543 GLuint texture, GLint level, GLint layer,
544 GLenum access, GLenum format, bool check_level_layer)
545 {
546 assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS);
547
548 if (unit >= ctx->Const.MaxImageUnits) {
549 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(unit)");
550 return GL_FALSE;
551 }
552
553 if (check_level_layer) {
554 /* EXT_shader_image_load_store doesn't throw an error if level or
555 * layer is negative.
556 */
557 if (level < 0) {
558 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(level)");
559 return GL_FALSE;
560 }
561
562 if (layer < 0) {
563 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(layer)");
564 return GL_FALSE;
565 }
566 }
567
568 if (access != GL_READ_ONLY &&
569 access != GL_WRITE_ONLY &&
570 access != GL_READ_WRITE) {
571 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(access)");
572 return GL_FALSE;
573 }
574
575 if (!_mesa_is_shader_image_format_supported(ctx, format)) {
576 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(format)");
577 return GL_FALSE;
578 }
579
580 return GL_TRUE;
581 }
582
583 static void
584 set_image_binding(struct gl_image_unit *u, struct gl_texture_object *texObj,
585 GLint level, GLboolean layered, GLint layer, GLenum access,
586 GLenum format)
587 {
588 u->Level = level;
589 u->Access = access;
590 u->Format = format;
591 u->_ActualFormat = _mesa_get_shader_image_format(format);
592
593 if (texObj && _mesa_tex_target_is_layered(texObj->Target)) {
594 u->Layered = layered;
595 u->Layer = layer;
596 u->_Layer = (u->Layered ? 0 : u->Layer);
597 } else {
598 u->Layered = GL_FALSE;
599 u->Layer = 0;
600 }
601
602 _mesa_reference_texobj(&u->TexObj, texObj);
603 }
604
605 static void
606 bind_image_texture(struct gl_context *ctx, struct gl_texture_object *texObj,
607 GLuint unit, GLint level, GLboolean layered, GLint layer,
608 GLenum access, GLenum format)
609 {
610 struct gl_image_unit *u;
611
612 u = &ctx->ImageUnits[unit];
613
614 FLUSH_VERTICES(ctx, 0);
615 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
616
617 set_image_binding(u, texObj, level, layered, layer, access, format);
618 }
619
620 void GLAPIENTRY
621 _mesa_BindImageTexture_no_error(GLuint unit, GLuint texture, GLint level,
622 GLboolean layered, GLint layer, GLenum access,
623 GLenum format)
624 {
625 struct gl_texture_object *texObj = NULL;
626
627 GET_CURRENT_CONTEXT(ctx);
628
629 if (texture)
630 texObj = _mesa_lookup_texture(ctx, texture);
631
632 bind_image_texture(ctx, texObj, unit, level, layered, layer, access, format);
633 }
634
635 void GLAPIENTRY
636 _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
637 GLboolean layered, GLint layer, GLenum access,
638 GLenum format)
639 {
640 struct gl_texture_object *texObj = NULL;
641
642 GET_CURRENT_CONTEXT(ctx);
643
644 if (!validate_bind_image_texture(ctx, unit, texture, level, layer, access,
645 format, true))
646 return;
647
648 if (texture) {
649 texObj = _mesa_lookup_texture(ctx, texture);
650
651 if (!texObj) {
652 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(texture)");
653 return;
654 }
655
656 /* From section 8.22 "Texture Image Loads and Stores" of the OpenGL ES
657 * 3.1 spec:
658 *
659 * "An INVALID_OPERATION error is generated if texture is not the name
660 * of an immutable texture object."
661 *
662 * However note that issue 7 of the GL_OES_texture_buffer spec
663 * recognizes that there is no way to create immutable buffer textures,
664 * so those are excluded from this requirement.
665 */
666 if (_mesa_is_gles(ctx) && !texObj->Immutable &&
667 texObj->Target != GL_TEXTURE_BUFFER) {
668 _mesa_error(ctx, GL_INVALID_OPERATION,
669 "glBindImageTexture(!immutable)");
670 return;
671 }
672 }
673
674 bind_image_texture(ctx, texObj, unit, level, layered, layer, access, format);
675 }
676
677 void GLAPIENTRY
678 _mesa_BindImageTextureEXT(GLuint index, GLuint texture, GLint level,
679 GLboolean layered, GLint layer, GLenum access,
680 GLint format)
681 {
682 struct gl_texture_object *texObj = NULL;
683
684 GET_CURRENT_CONTEXT(ctx);
685
686 if (!validate_bind_image_texture(ctx, index, texture, level, layer, access,
687 format, false))
688 return;
689
690 if (texture) {
691 texObj = _mesa_lookup_texture(ctx, texture);
692
693 if (!texObj) {
694 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTextureEXT(texture)");
695 return;
696 }
697 }
698
699 bind_image_texture(ctx, texObj, index, level, layered, layer, access, format);
700 }
701
702 static ALWAYS_INLINE void
703 bind_image_textures(struct gl_context *ctx, GLuint first, GLuint count,
704 const GLuint *textures, bool no_error)
705 {
706 int i;
707
708 /* Assume that at least one binding will be changed */
709 FLUSH_VERTICES(ctx, 0);
710 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
711
712 /* Note that the error semantics for multi-bind commands differ from
713 * those of other GL commands.
714 *
715 * The Issues section in the ARB_multi_bind spec says:
716 *
717 * "(11) Typically, OpenGL specifies that if an error is generated by
718 * a command, that command has no effect. This is somewhat
719 * unfortunate for multi-bind commands, because it would require
720 * a first pass to scan the entire list of bound objects for
721 * errors and then a second pass to actually perform the
722 * bindings. Should we have different error semantics?
723 *
724 * RESOLVED: Yes. In this specification, when the parameters for
725 * one of the <count> binding points are invalid, that binding
726 * point is not updated and an error will be generated. However,
727 * other binding points in the same command will be updated if
728 * their parameters are valid and no other error occurs."
729 */
730
731 _mesa_HashLockMutex(ctx->Shared->TexObjects);
732
733 for (i = 0; i < count; i++) {
734 struct gl_image_unit *u = &ctx->ImageUnits[first + i];
735 const GLuint texture = textures ? textures[i] : 0;
736
737 if (texture) {
738 struct gl_texture_object *texObj = u->TexObj;
739 GLenum tex_format;
740
741 if (!texObj || texObj->Name != texture) {
742 texObj = _mesa_lookup_texture_locked(ctx, texture);
743 if (!no_error && !texObj) {
744 /* The ARB_multi_bind spec says:
745 *
746 * "An INVALID_OPERATION error is generated if any value
747 * in <textures> is not zero or the name of an existing
748 * texture object (per binding)."
749 */
750 _mesa_error(ctx, GL_INVALID_OPERATION,
751 "glBindImageTextures(textures[%d]=%u "
752 "is not zero or the name of an existing texture "
753 "object)", i, texture);
754 continue;
755 }
756 }
757
758 if (texObj->Target == GL_TEXTURE_BUFFER) {
759 tex_format = texObj->BufferObjectFormat;
760 } else {
761 struct gl_texture_image *image = texObj->Image[0][0];
762
763 if (!no_error && (!image || image->Width == 0 ||
764 image->Height == 0 || image->Depth == 0)) {
765 /* The ARB_multi_bind spec says:
766 *
767 * "An INVALID_OPERATION error is generated if the width,
768 * height, or depth of the level zero texture image of
769 * any texture in <textures> is zero (per binding)."
770 */
771 _mesa_error(ctx, GL_INVALID_OPERATION,
772 "glBindImageTextures(the width, height or depth "
773 "of the level zero texture image of "
774 "textures[%d]=%u is zero)", i, texture);
775 continue;
776 }
777
778 tex_format = image->InternalFormat;
779 }
780
781 if (!no_error &&
782 !_mesa_is_shader_image_format_supported(ctx, tex_format)) {
783 /* The ARB_multi_bind spec says:
784 *
785 * "An INVALID_OPERATION error is generated if the internal
786 * format of the level zero texture image of any texture
787 * in <textures> is not found in table 8.33 (per binding)."
788 */
789 _mesa_error(ctx, GL_INVALID_OPERATION,
790 "glBindImageTextures(the internal format %s of "
791 "the level zero texture image of textures[%d]=%u "
792 "is not supported)",
793 _mesa_enum_to_string(tex_format),
794 i, texture);
795 continue;
796 }
797
798 /* Update the texture binding */
799 set_image_binding(u, texObj, 0,
800 _mesa_tex_target_is_layered(texObj->Target),
801 0, GL_READ_WRITE, tex_format);
802 } else {
803 /* Unbind the texture from the unit */
804 set_image_binding(u, NULL, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8);
805 }
806 }
807
808 _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
809 }
810
811 void GLAPIENTRY
812 _mesa_BindImageTextures_no_error(GLuint first, GLsizei count,
813 const GLuint *textures)
814 {
815 GET_CURRENT_CONTEXT(ctx);
816
817 bind_image_textures(ctx, first, count, textures, true);
818 }
819
820 void GLAPIENTRY
821 _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
822 {
823 GET_CURRENT_CONTEXT(ctx);
824
825 if (!ctx->Extensions.ARB_shader_image_load_store) {
826 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindImageTextures()");
827 return;
828 }
829
830 if (first + count > ctx->Const.MaxImageUnits) {
831 /* The ARB_multi_bind spec says:
832 *
833 * "An INVALID_OPERATION error is generated if <first> + <count>
834 * is greater than the number of image units supported by
835 * the implementation."
836 */
837 _mesa_error(ctx, GL_INVALID_OPERATION,
838 "glBindImageTextures(first=%u + count=%d > the value of "
839 "GL_MAX_IMAGE_UNITS=%u)",
840 first, count, ctx->Const.MaxImageUnits);
841 return;
842 }
843
844 bind_image_textures(ctx, first, count, textures, false);
845 }