2 * Copyright 2013 Intel Corporation
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:
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
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.
24 * Francisco Jerez <currojerez@riseup.net>
29 #include "shaderimage.h"
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
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
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
62 _mesa_get_shader_image_format(GLenum format
)
66 return MESA_FORMAT_RGBA_FLOAT32
;
69 return MESA_FORMAT_RGBA_FLOAT16
;
72 return MESA_FORMAT_RG_FLOAT32
;
75 return MESA_FORMAT_RG_FLOAT16
;
77 case GL_R11F_G11F_B10F
:
78 return MESA_FORMAT_R11G11B10_FLOAT
;
81 return MESA_FORMAT_R_FLOAT32
;
84 return MESA_FORMAT_R_FLOAT16
;
87 return MESA_FORMAT_RGBA_UINT32
;
90 return MESA_FORMAT_RGBA_UINT16
;
93 return MESA_FORMAT_R10G10B10A2_UINT
;
96 return MESA_FORMAT_RGBA_UINT8
;
99 return MESA_FORMAT_RG_UINT32
;
102 return MESA_FORMAT_RG_UINT16
;
105 return MESA_FORMAT_RG_UINT8
;
108 return MESA_FORMAT_R_UINT32
;
111 return MESA_FORMAT_R_UINT16
;
114 return MESA_FORMAT_R_UINT8
;
117 return MESA_FORMAT_RGBA_SINT32
;
120 return MESA_FORMAT_RGBA_SINT16
;
123 return MESA_FORMAT_RGBA_SINT8
;
126 return MESA_FORMAT_RG_SINT32
;
129 return MESA_FORMAT_RG_SINT16
;
132 return MESA_FORMAT_RG_SINT8
;
135 return MESA_FORMAT_R_SINT32
;
138 return MESA_FORMAT_R_SINT16
;
141 return MESA_FORMAT_R_SINT8
;
144 return MESA_FORMAT_RGBA_UNORM16
;
147 return MESA_FORMAT_R10G10B10A2_UNORM
;
150 return MESA_FORMAT_RGBA_8
;
153 return MESA_FORMAT_RG_16
;
156 return MESA_FORMAT_RG_8
;
159 return MESA_FORMAT_R_UNORM16
;
162 return MESA_FORMAT_R_UNORM8
;
164 case GL_RGBA16_SNORM
:
165 return MESA_FORMAT_RGBA_SNORM16
;
168 return MESA_FORMAT_SIGNED_RGBA_8
;
171 return MESA_FORMAT_SIGNED_RG_16
;
174 return MESA_FORMAT_SIGNED_RG_8
;
177 return MESA_FORMAT_R_SNORM16
;
180 return MESA_FORMAT_R_SNORM8
;
183 return MESA_FORMAT_NONE
;
187 enum image_format_class
189 /** Not a valid image format. */
190 IMAGE_FORMAT_CLASS_NONE
= 0,
192 /** Classes of image formats you can cast into each other. */
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
208 static enum image_format_class
209 get_image_format_class(mesa_format format
)
212 case MESA_FORMAT_RGBA_FLOAT32
:
213 return IMAGE_FORMAT_CLASS_4X32
;
215 case MESA_FORMAT_RGBA_FLOAT16
:
216 return IMAGE_FORMAT_CLASS_4X16
;
218 case MESA_FORMAT_RG_FLOAT32
:
219 return IMAGE_FORMAT_CLASS_2X32
;
221 case MESA_FORMAT_RG_FLOAT16
:
222 return IMAGE_FORMAT_CLASS_2X16
;
224 case MESA_FORMAT_R11G11B10_FLOAT
:
225 return IMAGE_FORMAT_CLASS_10_11_11
;
227 case MESA_FORMAT_R_FLOAT32
:
228 return IMAGE_FORMAT_CLASS_1X32
;
230 case MESA_FORMAT_R_FLOAT16
:
231 return IMAGE_FORMAT_CLASS_1X16
;
233 case MESA_FORMAT_RGBA_UINT32
:
234 return IMAGE_FORMAT_CLASS_4X32
;
236 case MESA_FORMAT_RGBA_UINT16
:
237 return IMAGE_FORMAT_CLASS_4X16
;
239 case MESA_FORMAT_R10G10B10A2_UINT
:
240 return IMAGE_FORMAT_CLASS_2_10_10_10
;
242 case MESA_FORMAT_RGBA_UINT8
:
243 return IMAGE_FORMAT_CLASS_4X8
;
245 case MESA_FORMAT_RG_UINT32
:
246 return IMAGE_FORMAT_CLASS_2X32
;
248 case MESA_FORMAT_RG_UINT16
:
249 return IMAGE_FORMAT_CLASS_2X16
;
251 case MESA_FORMAT_RG_UINT8
:
252 return IMAGE_FORMAT_CLASS_2X8
;
254 case MESA_FORMAT_R_UINT32
:
255 return IMAGE_FORMAT_CLASS_1X32
;
257 case MESA_FORMAT_R_UINT16
:
258 return IMAGE_FORMAT_CLASS_1X16
;
260 case MESA_FORMAT_R_UINT8
:
261 return IMAGE_FORMAT_CLASS_1X8
;
263 case MESA_FORMAT_RGBA_SINT32
:
264 return IMAGE_FORMAT_CLASS_4X32
;
266 case MESA_FORMAT_RGBA_SINT16
:
267 return IMAGE_FORMAT_CLASS_4X16
;
269 case MESA_FORMAT_RGBA_SINT8
:
270 return IMAGE_FORMAT_CLASS_4X8
;
272 case MESA_FORMAT_RG_SINT32
:
273 return IMAGE_FORMAT_CLASS_2X32
;
275 case MESA_FORMAT_RG_SINT16
:
276 return IMAGE_FORMAT_CLASS_2X16
;
278 case MESA_FORMAT_RG_SINT8
:
279 return IMAGE_FORMAT_CLASS_2X8
;
281 case MESA_FORMAT_R_SINT32
:
282 return IMAGE_FORMAT_CLASS_1X32
;
284 case MESA_FORMAT_R_SINT16
:
285 return IMAGE_FORMAT_CLASS_1X16
;
287 case MESA_FORMAT_R_SINT8
:
288 return IMAGE_FORMAT_CLASS_1X8
;
290 case MESA_FORMAT_RGBA_UNORM16
:
291 return IMAGE_FORMAT_CLASS_4X16
;
293 case MESA_FORMAT_R10G10B10A2_UNORM
:
294 return IMAGE_FORMAT_CLASS_2_10_10_10
;
296 case MESA_FORMAT_RGBA_8
:
297 return IMAGE_FORMAT_CLASS_4X8
;
299 case MESA_FORMAT_RG_16
:
300 return IMAGE_FORMAT_CLASS_2X16
;
302 case MESA_FORMAT_RG_8
:
303 return IMAGE_FORMAT_CLASS_2X8
;
305 case MESA_FORMAT_R_UNORM16
:
306 return IMAGE_FORMAT_CLASS_1X16
;
308 case MESA_FORMAT_R_UNORM8
:
309 return IMAGE_FORMAT_CLASS_1X8
;
311 case MESA_FORMAT_RGBA_SNORM16
:
312 return IMAGE_FORMAT_CLASS_4X16
;
314 case MESA_FORMAT_SIGNED_RGBA_8
:
315 return IMAGE_FORMAT_CLASS_4X8
;
317 case MESA_FORMAT_SIGNED_RG_16
:
318 return IMAGE_FORMAT_CLASS_2X16
;
320 case MESA_FORMAT_SIGNED_RG_8
:
321 return IMAGE_FORMAT_CLASS_2X8
;
323 case MESA_FORMAT_R_SNORM16
:
324 return IMAGE_FORMAT_CLASS_1X16
;
326 case MESA_FORMAT_R_SNORM8
:
327 return IMAGE_FORMAT_CLASS_1X8
;
330 return IMAGE_FORMAT_CLASS_NONE
;
335 * Return whether an image format should be supported based on the current API
336 * version of the context.
339 is_image_format_supported(const struct gl_context
*ctx
, GLenum format
)
342 /* Formats supported on both desktop and ES GL, c.f. table 8.27 of the
343 * OpenGL ES 3.1 specification.
360 /* Formats supported on unextended desktop GL and the original
361 * ARB_shader_image_load_store extension, c.f. table 3.21 of the OpenGL 4.2
366 case GL_R11F_G11F_B10F
:
385 case GL_RGBA16_SNORM
:
390 return _mesa_is_desktop_gl(ctx
);
398 _mesa_default_image_unit(struct gl_context
*ctx
)
400 const GLenum format
= _mesa_is_desktop_gl(ctx
) ? GL_R8
: GL_R32UI
;
401 const struct gl_image_unit u
= {
402 .Access
= GL_READ_ONLY
,
404 ._ActualFormat
= _mesa_get_shader_image_format(format
)
410 _mesa_init_image_units(struct gl_context
*ctx
)
414 for (i
= 0; i
< ARRAY_SIZE(ctx
->ImageUnits
); ++i
)
415 ctx
->ImageUnits
[i
] = _mesa_default_image_unit(ctx
);
419 validate_image_unit(struct gl_context
*ctx
, struct gl_image_unit
*u
)
421 struct gl_texture_object
*t
= u
->TexObj
;
422 mesa_format tex_format
;
427 _mesa_test_texobj_completeness(ctx
, t
);
429 if (u
->Level
< t
->BaseLevel
||
430 u
->Level
> t
->_MaxLevel
||
431 (u
->Level
== t
->BaseLevel
&& !t
->_BaseComplete
) ||
432 (u
->Level
!= t
->BaseLevel
&& !t
->_MipmapComplete
))
435 if (_mesa_tex_target_is_layered(t
->Target
) &&
436 u
->_Layer
>= _mesa_get_texture_layers(t
, u
->Level
))
439 if (t
->Target
== GL_TEXTURE_BUFFER
) {
440 tex_format
= _mesa_get_shader_image_format(t
->BufferObjectFormat
);
443 struct gl_texture_image
*img
= (t
->Target
== GL_TEXTURE_CUBE_MAP
?
444 t
->Image
[u
->_Layer
][u
->Level
] :
445 t
->Image
[0][u
->Level
]);
447 if (!img
|| img
->Border
|| img
->NumSamples
> ctx
->Const
.MaxImageSamples
)
450 tex_format
= _mesa_get_shader_image_format(img
->InternalFormat
);
456 switch (t
->ImageFormatCompatibilityType
) {
457 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE
:
458 if (_mesa_get_format_bytes(tex_format
) !=
459 _mesa_get_format_bytes(u
->_ActualFormat
))
463 case GL_IMAGE_FORMAT_COMPATIBILITY_BY_CLASS
:
464 if (get_image_format_class(tex_format
) !=
465 get_image_format_class(u
->_ActualFormat
))
470 assert(!"Unexpected image format compatibility type");
477 _mesa_validate_image_units(struct gl_context
*ctx
)
481 for (i
= 0; i
< ctx
->Const
.MaxImageUnits
; ++i
) {
482 struct gl_image_unit
*u
= &ctx
->ImageUnits
[i
];
483 u
->_Valid
= validate_image_unit(ctx
, u
);
488 validate_bind_image_texture(struct gl_context
*ctx
, GLuint unit
,
489 GLuint texture
, GLint level
, GLboolean layered
,
490 GLint layer
, GLenum access
, GLenum format
)
492 assert(ctx
->Const
.MaxImageUnits
<= MAX_IMAGE_UNITS
);
494 if (unit
>= ctx
->Const
.MaxImageUnits
) {
495 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindImageTexture(unit)");
500 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindImageTexture(level)");
505 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindImageTexture(layer)");
509 if (access
!= GL_READ_ONLY
&&
510 access
!= GL_WRITE_ONLY
&&
511 access
!= GL_READ_WRITE
) {
512 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindImageTexture(access)");
516 if (!is_image_format_supported(ctx
, format
)) {
517 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindImageTexture(format)");
525 _mesa_BindImageTexture(GLuint unit
, GLuint texture
, GLint level
,
526 GLboolean layered
, GLint layer
, GLenum access
,
529 GET_CURRENT_CONTEXT(ctx
);
530 struct gl_image_unit
*u
;
532 if (!validate_bind_image_texture(ctx
, unit
, texture
, level
,
533 layered
, layer
, access
, format
))
536 u
= &ctx
->ImageUnits
[unit
];
538 FLUSH_VERTICES(ctx
, 0);
539 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
542 struct gl_texture_object
*t
= _mesa_lookup_texture(ctx
, texture
);
545 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindImageTexture(texture)");
549 /* From section 8.22 "Texture Image Loads and Stores" of the OpenGL ES
552 * "An INVALID_OPERATION error is generated if texture is not the name
553 * of an immutable texture object."
555 if (_mesa_is_gles(ctx
) && !t
->Immutable
) {
556 _mesa_error(ctx
, GL_INVALID_OPERATION
,
557 "glBindImageTexture(!immutable)");
561 _mesa_reference_texobj(&u
->TexObj
, t
);
563 _mesa_reference_texobj(&u
->TexObj
, NULL
);
569 u
->_ActualFormat
= _mesa_get_shader_image_format(format
);
570 u
->_Valid
= validate_image_unit(ctx
, u
);
572 if (u
->TexObj
&& _mesa_tex_target_is_layered(u
->TexObj
->Target
)) {
573 u
->Layered
= layered
;
575 u
->_Layer
= (u
->Layered
? 0 : u
->Layer
);
577 u
->Layered
= GL_FALSE
;
583 _mesa_BindImageTextures(GLuint first
, GLsizei count
, const GLuint
*textures
)
585 GET_CURRENT_CONTEXT(ctx
);
588 if (!ctx
->Extensions
.ARB_shader_image_load_store
) {
589 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindImageTextures()");
593 if (first
+ count
> ctx
->Const
.MaxImageUnits
) {
594 /* The ARB_multi_bind spec says:
596 * "An INVALID_OPERATION error is generated if <first> + <count>
597 * is greater than the number of image units supported by
598 * the implementation."
600 _mesa_error(ctx
, GL_INVALID_OPERATION
,
601 "glBindImageTextures(first=%u + count=%d > the value of "
602 "GL_MAX_IMAGE_UNITS=%u)",
603 first
, count
, ctx
->Const
.MaxImageUnits
);
607 /* Assume that at least one binding will be changed */
608 FLUSH_VERTICES(ctx
, 0);
609 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewImageUnits
;
611 /* Note that the error semantics for multi-bind commands differ from
612 * those of other GL commands.
614 * The Issues section in the ARB_multi_bind spec says:
616 * "(11) Typically, OpenGL specifies that if an error is generated by
617 * a command, that command has no effect. This is somewhat
618 * unfortunate for multi-bind commands, because it would require
619 * a first pass to scan the entire list of bound objects for
620 * errors and then a second pass to actually perform the
621 * bindings. Should we have different error semantics?
623 * RESOLVED: Yes. In this specification, when the parameters for
624 * one of the <count> binding points are invalid, that binding
625 * point is not updated and an error will be generated. However,
626 * other binding points in the same command will be updated if
627 * their parameters are valid and no other error occurs."
630 _mesa_begin_texture_lookups(ctx
);
632 for (i
= 0; i
< count
; i
++) {
633 struct gl_image_unit
*u
= &ctx
->ImageUnits
[first
+ i
];
634 const GLuint texture
= textures
? textures
[i
] : 0;
637 struct gl_texture_object
*texObj
;
640 if (!u
->TexObj
|| u
->TexObj
->Name
!= texture
) {
641 texObj
= _mesa_lookup_texture_locked(ctx
, texture
);
643 /* The ARB_multi_bind spec says:
645 * "An INVALID_OPERATION error is generated if any value
646 * in <textures> is not zero or the name of an existing
647 * texture object (per binding)."
649 _mesa_error(ctx
, GL_INVALID_OPERATION
,
650 "glBindImageTextures(textures[%d]=%u "
651 "is not zero or the name of an existing texture "
652 "object)", i
, texture
);
659 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
660 tex_format
= texObj
->BufferObjectFormat
;
662 struct gl_texture_image
*image
= texObj
->Image
[0][0];
664 if (!image
|| image
->Width
== 0 || image
->Height
== 0 ||
666 /* The ARB_multi_bind spec says:
668 * "An INVALID_OPERATION error is generated if the width,
669 * height, or depth of the level zero texture image of
670 * any texture in <textures> is zero (per binding)."
672 _mesa_error(ctx
, GL_INVALID_OPERATION
,
673 "glBindImageTextures(the width, height or depth "
674 "of the level zero texture image of "
675 "textures[%d]=%u is zero)", i
, texture
);
679 tex_format
= image
->InternalFormat
;
682 if (!is_image_format_supported(ctx
, tex_format
)) {
683 /* The ARB_multi_bind spec says:
685 * "An INVALID_OPERATION error is generated if the internal
686 * format of the level zero texture image of any texture
687 * in <textures> is not found in table 8.33 (per binding)."
689 _mesa_error(ctx
, GL_INVALID_OPERATION
,
690 "glBindImageTextures(the internal format %s of "
691 "the level zero texture image of textures[%d]=%u "
693 _mesa_enum_to_string(tex_format
),
698 /* Update the texture binding */
699 _mesa_reference_texobj(&u
->TexObj
, texObj
);
701 u
->Layered
= _mesa_tex_target_is_layered(texObj
->Target
);
702 u
->_Layer
= u
->Layer
= 0;
703 u
->Access
= GL_READ_WRITE
;
704 u
->Format
= tex_format
;
705 u
->_ActualFormat
= _mesa_get_shader_image_format(tex_format
);
706 u
->_Valid
= validate_image_unit(ctx
, u
);
708 /* Unbind the texture from the unit */
709 _mesa_reference_texobj(&u
->TexObj
, NULL
);
711 u
->Layered
= GL_FALSE
;
712 u
->_Layer
= u
->Layer
= 0;
713 u
->Access
= GL_READ_ONLY
;
715 u
->_ActualFormat
= MESA_FORMAT_R_UNORM8
;
716 u
->_Valid
= GL_FALSE
;
720 _mesa_end_texture_lookups(ctx
);
724 _mesa_MemoryBarrier(GLbitfield barriers
)
726 GET_CURRENT_CONTEXT(ctx
);
728 if (ctx
->Driver
.MemoryBarrier
)
729 ctx
->Driver
.MemoryBarrier(ctx
, barriers
);
733 _mesa_MemoryBarrierByRegion(GLbitfield barriers
)
735 GET_CURRENT_CONTEXT(ctx
);
737 GLbitfield all_allowed_bits
= GL_ATOMIC_COUNTER_BARRIER_BIT
|
738 GL_FRAMEBUFFER_BARRIER_BIT
|
739 GL_SHADER_IMAGE_ACCESS_BARRIER_BIT
|
740 GL_SHADER_STORAGE_BARRIER_BIT
|
741 GL_TEXTURE_FETCH_BARRIER_BIT
|
742 GL_UNIFORM_BARRIER_BIT
;
744 if (ctx
->Driver
.MemoryBarrier
) {
745 /* From section 7.11.2 of the OpenGL ES 3.1 specification:
747 * "When barriers is ALL_BARRIER_BITS, shader memory accesses will be
748 * synchronized relative to all these barrier bits, but not to other
749 * barrier bits specific to MemoryBarrier."
751 * That is, if barriers is the special value GL_ALL_BARRIER_BITS, then all
752 * barriers allowed by glMemoryBarrierByRegion should be activated."
754 if (barriers
== GL_ALL_BARRIER_BITS
)
755 return ctx
->Driver
.MemoryBarrier(ctx
, all_allowed_bits
);
757 /* From section 7.11.2 of the OpenGL ES 3.1 specification:
759 * "An INVALID_VALUE error is generated if barriers is not the special
760 * value ALL_BARRIER_BITS, and has any bits set other than those
763 if ((barriers
& ~all_allowed_bits
) != 0) {
764 _mesa_error(ctx
, GL_INVALID_VALUE
,
765 "glMemoryBarrierByRegion(unsupported barrier bit");
768 ctx
->Driver
.MemoryBarrier(ctx
, barriers
);