mesa: refuse to change textures when a handle is allocated
[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 * These can be supported by GLES 3.1 with GL_NV_image_formats &
434 * GL_EXT_texture_norm16 extensions but we don't have support for the
435 * latter in Mesa yet.
436 */
437 case GL_RGBA16:
438 case GL_RGBA16_SNORM:
439 case GL_RG16:
440 case GL_RG16_SNORM:
441 case GL_R16:
442 case GL_R16_SNORM:
443 return _mesa_is_desktop_gl(ctx);
444
445 default:
446 return false;
447 }
448 }
449
450 struct gl_image_unit
451 _mesa_default_image_unit(struct gl_context *ctx)
452 {
453 const GLenum format = _mesa_is_desktop_gl(ctx) ? GL_R8 : GL_R32UI;
454 const struct gl_image_unit u = {
455 .Access = GL_READ_ONLY,
456 .Format = format,
457 ._ActualFormat = _mesa_get_shader_image_format(format)
458 };
459 return u;
460 }
461
462 void
463 _mesa_init_image_units(struct gl_context *ctx)
464 {
465 unsigned i;
466
467 for (i = 0; i < ARRAY_SIZE(ctx->ImageUnits); ++i)
468 ctx->ImageUnits[i] = _mesa_default_image_unit(ctx);
469 }
470
471 GLboolean
472 _mesa_is_image_unit_valid(struct gl_context *ctx, struct gl_image_unit *u)
473 {
474 struct gl_texture_object *t = u->TexObj;
475 mesa_format tex_format;
476
477 if (!t)
478 return GL_FALSE;
479
480 if (!t->_BaseComplete && !t->_MipmapComplete)
481 _mesa_test_texobj_completeness(ctx, t);
482
483 if (u->Level < t->BaseLevel ||
484 u->Level > t->_MaxLevel ||
485 (u->Level == t->BaseLevel && !t->_BaseComplete) ||
486 (u->Level != t->BaseLevel && !t->_MipmapComplete))
487 return GL_FALSE;
488
489 if (_mesa_tex_target_is_layered(t->Target) &&
490 u->_Layer >= _mesa_get_texture_layers(t, u->Level))
491 return GL_FALSE;
492
493 if (t->Target == GL_TEXTURE_BUFFER) {
494 tex_format = _mesa_get_shader_image_format(t->BufferObjectFormat);
495
496 } else {
497 struct gl_texture_image *img = (t->Target == GL_TEXTURE_CUBE_MAP ?
498 t->Image[u->_Layer][u->Level] :
499 t->Image[0][u->Level]);
500
501 if (!img || img->Border || img->NumSamples > ctx->Const.MaxImageSamples)
502 return GL_FALSE;
503
504 tex_format = _mesa_get_shader_image_format(img->InternalFormat);
505 }
506
507 if (!tex_format)
508 return GL_FALSE;
509
510 switch (t->ImageFormatCompatibilityType) {
511 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE:
512 if (_mesa_get_format_bytes(tex_format) !=
513 _mesa_get_format_bytes(u->_ActualFormat))
514 return GL_FALSE;
515 break;
516
517 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_CLASS:
518 if (get_image_format_class(tex_format) !=
519 get_image_format_class(u->_ActualFormat))
520 return GL_FALSE;
521 break;
522
523 default:
524 assert(!"Unexpected image format compatibility type");
525 }
526
527 return GL_TRUE;
528 }
529
530 static GLboolean
531 validate_bind_image_texture(struct gl_context *ctx, GLuint unit,
532 GLuint texture, GLint level, GLint layer,
533 GLenum access, GLenum format)
534 {
535 assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS);
536
537 if (unit >= ctx->Const.MaxImageUnits) {
538 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(unit)");
539 return GL_FALSE;
540 }
541
542 if (level < 0) {
543 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(level)");
544 return GL_FALSE;
545 }
546
547 if (layer < 0) {
548 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(layer)");
549 return GL_FALSE;
550 }
551
552 if (access != GL_READ_ONLY &&
553 access != GL_WRITE_ONLY &&
554 access != GL_READ_WRITE) {
555 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(access)");
556 return GL_FALSE;
557 }
558
559 if (!_mesa_is_shader_image_format_supported(ctx, format)) {
560 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(format)");
561 return GL_FALSE;
562 }
563
564 return GL_TRUE;
565 }
566
567 static void
568 set_image_binding(struct gl_image_unit *u, struct gl_texture_object *texObj,
569 GLint level, GLboolean layered, GLint layer, GLenum access,
570 GLenum format)
571 {
572 u->Level = level;
573 u->Access = access;
574 u->Format = format;
575 u->_ActualFormat = _mesa_get_shader_image_format(format);
576
577 if (texObj && _mesa_tex_target_is_layered(texObj->Target)) {
578 u->Layered = layered;
579 u->Layer = layer;
580 u->_Layer = (u->Layered ? 0 : u->Layer);
581 } else {
582 u->Layered = GL_FALSE;
583 u->Layer = 0;
584 }
585
586 _mesa_reference_texobj(&u->TexObj, texObj);
587 }
588
589 static void
590 bind_image_texture(struct gl_context *ctx, struct gl_texture_object *texObj,
591 GLuint unit, GLint level, GLboolean layered, GLint layer,
592 GLenum access, GLenum format)
593 {
594 struct gl_image_unit *u;
595
596 u = &ctx->ImageUnits[unit];
597
598 FLUSH_VERTICES(ctx, 0);
599 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
600
601 set_image_binding(u, texObj, level, layered, layer, access, format);
602 }
603
604 void GLAPIENTRY
605 _mesa_BindImageTexture_no_error(GLuint unit, GLuint texture, GLint level,
606 GLboolean layered, GLint layer, GLenum access,
607 GLenum format)
608 {
609 struct gl_texture_object *texObj = NULL;
610
611 GET_CURRENT_CONTEXT(ctx);
612
613 if (texture)
614 texObj = _mesa_lookup_texture(ctx, texture);
615
616 bind_image_texture(ctx, texObj, unit, level, layered, layer, access, format);
617 }
618
619 void GLAPIENTRY
620 _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
621 GLboolean layered, GLint layer, GLenum access,
622 GLenum format)
623 {
624 struct gl_texture_object *texObj = NULL;
625
626 GET_CURRENT_CONTEXT(ctx);
627
628 if (!validate_bind_image_texture(ctx, unit, texture, level, layer, access,
629 format))
630 return;
631
632 if (texture) {
633 texObj = _mesa_lookup_texture(ctx, texture);
634
635 if (!texObj) {
636 _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(texture)");
637 return;
638 }
639
640 /* From section 8.22 "Texture Image Loads and Stores" of the OpenGL ES
641 * 3.1 spec:
642 *
643 * "An INVALID_OPERATION error is generated if texture is not the name
644 * of an immutable texture object."
645 *
646 * However note that issue 7 of the GL_OES_texture_buffer spec
647 * recognizes that there is no way to create immutable buffer textures,
648 * so those are excluded from this requirement.
649 */
650 if (_mesa_is_gles(ctx) && !texObj->Immutable &&
651 texObj->Target != GL_TEXTURE_BUFFER) {
652 _mesa_error(ctx, GL_INVALID_OPERATION,
653 "glBindImageTexture(!immutable)");
654 return;
655 }
656 }
657
658 bind_image_texture(ctx, texObj, unit, level, layered, layer, access, format);
659 }
660
661 static ALWAYS_INLINE void
662 bind_image_textures(struct gl_context *ctx, GLuint first, GLuint count,
663 const GLuint *textures, bool no_error)
664 {
665 int i;
666
667 /* Assume that at least one binding will be changed */
668 FLUSH_VERTICES(ctx, 0);
669 ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
670
671 /* Note that the error semantics for multi-bind commands differ from
672 * those of other GL commands.
673 *
674 * The Issues section in the ARB_multi_bind spec says:
675 *
676 * "(11) Typically, OpenGL specifies that if an error is generated by
677 * a command, that command has no effect. This is somewhat
678 * unfortunate for multi-bind commands, because it would require
679 * a first pass to scan the entire list of bound objects for
680 * errors and then a second pass to actually perform the
681 * bindings. Should we have different error semantics?
682 *
683 * RESOLVED: Yes. In this specification, when the parameters for
684 * one of the <count> binding points are invalid, that binding
685 * point is not updated and an error will be generated. However,
686 * other binding points in the same command will be updated if
687 * their parameters are valid and no other error occurs."
688 */
689
690 _mesa_HashLockMutex(ctx->Shared->TexObjects);
691
692 for (i = 0; i < count; i++) {
693 struct gl_image_unit *u = &ctx->ImageUnits[first + i];
694 const GLuint texture = textures ? textures[i] : 0;
695
696 if (texture) {
697 struct gl_texture_object *texObj = u->TexObj;
698 GLenum tex_format;
699
700 if (!texObj || texObj->Name != texture) {
701 texObj = _mesa_lookup_texture_locked(ctx, texture);
702 if (!no_error && !texObj) {
703 /* The ARB_multi_bind spec says:
704 *
705 * "An INVALID_OPERATION error is generated if any value
706 * in <textures> is not zero or the name of an existing
707 * texture object (per binding)."
708 */
709 _mesa_error(ctx, GL_INVALID_OPERATION,
710 "glBindImageTextures(textures[%d]=%u "
711 "is not zero or the name of an existing texture "
712 "object)", i, texture);
713 continue;
714 }
715 }
716
717 if (texObj->Target == GL_TEXTURE_BUFFER) {
718 tex_format = texObj->BufferObjectFormat;
719 } else {
720 struct gl_texture_image *image = texObj->Image[0][0];
721
722 if (!no_error && (!image || image->Width == 0 ||
723 image->Height == 0 || image->Depth == 0)) {
724 /* The ARB_multi_bind spec says:
725 *
726 * "An INVALID_OPERATION error is generated if the width,
727 * height, or depth of the level zero texture image of
728 * any texture in <textures> is zero (per binding)."
729 */
730 _mesa_error(ctx, GL_INVALID_OPERATION,
731 "glBindImageTextures(the width, height or depth "
732 "of the level zero texture image of "
733 "textures[%d]=%u is zero)", i, texture);
734 continue;
735 }
736
737 tex_format = image->InternalFormat;
738 }
739
740 if (!no_error &&
741 !_mesa_is_shader_image_format_supported(ctx, tex_format)) {
742 /* The ARB_multi_bind spec says:
743 *
744 * "An INVALID_OPERATION error is generated if the internal
745 * format of the level zero texture image of any texture
746 * in <textures> is not found in table 8.33 (per binding)."
747 */
748 _mesa_error(ctx, GL_INVALID_OPERATION,
749 "glBindImageTextures(the internal format %s of "
750 "the level zero texture image of textures[%d]=%u "
751 "is not supported)",
752 _mesa_enum_to_string(tex_format),
753 i, texture);
754 continue;
755 }
756
757 /* Update the texture binding */
758 set_image_binding(u, texObj, 0,
759 _mesa_tex_target_is_layered(texObj->Target),
760 0, GL_READ_WRITE, tex_format);
761 } else {
762 /* Unbind the texture from the unit */
763 set_image_binding(u, NULL, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8);
764 }
765 }
766
767 _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
768 }
769
770 void GLAPIENTRY
771 _mesa_BindImageTextures_no_error(GLuint first, GLsizei count,
772 const GLuint *textures)
773 {
774 GET_CURRENT_CONTEXT(ctx);
775
776 bind_image_textures(ctx, first, count, textures, true);
777 }
778
779 void GLAPIENTRY
780 _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
781 {
782 GET_CURRENT_CONTEXT(ctx);
783
784 if (!ctx->Extensions.ARB_shader_image_load_store) {
785 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindImageTextures()");
786 return;
787 }
788
789 if (first + count > ctx->Const.MaxImageUnits) {
790 /* The ARB_multi_bind spec says:
791 *
792 * "An INVALID_OPERATION error is generated if <first> + <count>
793 * is greater than the number of image units supported by
794 * the implementation."
795 */
796 _mesa_error(ctx, GL_INVALID_OPERATION,
797 "glBindImageTextures(first=%u + count=%d > the value of "
798 "GL_MAX_IMAGE_UNITS=%u)",
799 first, count, ctx->Const.MaxImageUnits);
800 return;
801 }
802
803 bind_image_textures(ctx, first, count, textures, false);
804 }