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