2 * Copyright © 2016 Red Hat.
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.
26 #include "externalobjects.h"
29 #include "glformats.h"
30 #include "texstorage.h"
33 * Allocate and initialize a new memory object. But don't put it into the
34 * memory object hash table.
36 * Called via ctx->Driver.NewMemoryObject, unless overridden by a device
39 * \return pointer to new memory object.
41 static struct gl_memory_object
*
42 _mesa_new_memory_object(struct gl_context
*ctx
, GLuint name
)
44 struct gl_memory_object
*obj
= MALLOC_STRUCT(gl_memory_object
);
48 _mesa_initialize_memory_object(ctx
, obj
, name
);
53 * Delete a memory object. Called via ctx->Driver.DeleteMemory().
54 * Not removed from hash table here.
57 _mesa_delete_memory_object(struct gl_context
*ctx
,
58 struct gl_memory_object
*memObj
)
64 _mesa_init_memory_object_functions(struct dd_function_table
*driver
)
66 driver
->NewMemoryObject
= _mesa_new_memory_object
;
67 driver
->DeleteMemoryObject
= _mesa_delete_memory_object
;
71 * Initialize a buffer object to default values.
74 _mesa_initialize_memory_object(struct gl_context
*ctx
,
75 struct gl_memory_object
*obj
,
78 memset(obj
, 0, sizeof(struct gl_memory_object
));
80 obj
->Dedicated
= GL_FALSE
;
84 _mesa_DeleteMemoryObjectsEXT(GLsizei n
, const GLuint
*memoryObjects
)
86 GET_CURRENT_CONTEXT(ctx
);
88 if (MESA_VERBOSE
& (VERBOSE_API
)) {
89 _mesa_debug(ctx
, "glDeleteMemoryObjectsEXT(%d, %p)\n", n
,
93 if (!ctx
->Extensions
.EXT_memory_object
) {
94 _mesa_error(ctx
, GL_INVALID_OPERATION
,
95 "glDeleteMemoryObjectsEXT(unsupported)");
100 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteMemoryObjectsEXT(n < 0)");
107 _mesa_HashLockMutex(ctx
->Shared
->MemoryObjects
);
108 for (GLint i
= 0; i
< n
; i
++) {
109 if (memoryObjects
[i
] > 0) {
110 struct gl_memory_object
*delObj
111 = _mesa_lookup_memory_object_locked(ctx
, memoryObjects
[i
]);
114 _mesa_HashRemoveLocked(ctx
->Shared
->MemoryObjects
,
116 ctx
->Driver
.DeleteMemoryObject(ctx
, delObj
);
120 _mesa_HashUnlockMutex(ctx
->Shared
->MemoryObjects
);
124 _mesa_IsMemoryObjectEXT(GLuint memoryObject
)
126 GET_CURRENT_CONTEXT(ctx
);
128 if (!ctx
->Extensions
.EXT_memory_object
) {
129 _mesa_error(ctx
, GL_INVALID_OPERATION
,
130 "glIsMemoryObjectEXT(unsupported)");
134 struct gl_memory_object
*obj
=
135 _mesa_lookup_memory_object(ctx
, memoryObject
);
137 return obj
? GL_TRUE
: GL_FALSE
;
141 _mesa_CreateMemoryObjectsEXT(GLsizei n
, GLuint
*memoryObjects
)
143 GET_CURRENT_CONTEXT(ctx
);
145 const char *func
= "glCreateMemoryObjectsEXT";
147 if (MESA_VERBOSE
& (VERBOSE_API
))
148 _mesa_debug(ctx
, "%s(%d, %p)", func
, n
, memoryObjects
);
150 if (!ctx
->Extensions
.EXT_memory_object
) {
151 _mesa_error(ctx
, GL_INVALID_OPERATION
,
152 "glCreateMemoryObjectsEXT(unsupported)");
157 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
164 _mesa_HashLockMutex(ctx
->Shared
->MemoryObjects
);
165 GLuint first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->MemoryObjects
, n
);
167 for (GLsizei i
= 0; i
< n
; i
++) {
168 struct gl_memory_object
*memObj
;
170 memoryObjects
[i
] = first
+ i
;
172 /* allocate memory object */
173 memObj
= ctx
->Driver
.NewMemoryObject(ctx
, memoryObjects
[i
]);
175 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
176 _mesa_HashUnlockMutex(ctx
->Shared
->MemoryObjects
);
180 /* insert into hash table */
181 _mesa_HashInsertLocked(ctx
->Shared
->MemoryObjects
,
187 _mesa_HashUnlockMutex(ctx
->Shared
->MemoryObjects
);
191 _mesa_MemoryObjectParameterivEXT(GLuint memoryObject
,
195 GET_CURRENT_CONTEXT(ctx
);
196 struct gl_memory_object
*memObj
;
198 if (!ctx
->Extensions
.EXT_memory_object
) {
199 _mesa_error(ctx
, GL_INVALID_OPERATION
,
200 "glMemoryObjectParameterivEXT(unsupported)");
204 memObj
= _mesa_lookup_memory_object(ctx
, memoryObject
);
208 if (memObj
->Immutable
) {
209 _mesa_error(ctx
, GL_INVALID_OPERATION
,
210 "glMemoryObjectParameterivEXT(memoryObject is immutable");
215 case GL_DEDICATED_MEMORY_OBJECT_EXT
:
216 memObj
->Dedicated
= (GLboolean
) params
[0];
218 case GL_PROTECTED_MEMORY_OBJECT_EXT
:
219 /* EXT_protected_textures not supported */
227 _mesa_error(ctx
, GL_INVALID_ENUM
,
228 "glMemoryObjectParameterivEXT(pname=0x%x)", pname
);
232 _mesa_GetMemoryObjectParameterivEXT(GLuint memoryObject
,
236 GET_CURRENT_CONTEXT(ctx
);
237 struct gl_memory_object
*memObj
;
239 if (!ctx
->Extensions
.EXT_memory_object
) {
240 _mesa_error(ctx
, GL_INVALID_OPERATION
,
241 "glGetMemoryObjectParameterivEXT(unsupported)");
245 memObj
= _mesa_lookup_memory_object(ctx
, memoryObject
);
250 case GL_DEDICATED_MEMORY_OBJECT_EXT
:
251 *params
= (GLint
) memObj
->Dedicated
;
253 case GL_PROTECTED_MEMORY_OBJECT_EXT
:
254 /* EXT_protected_textures not supported */
262 _mesa_error(ctx
, GL_INVALID_ENUM
,
263 "glGetMemoryObjectParameterivEXT(pname=0x%x)", pname
);
266 static struct gl_memory_object
*
267 lookup_memory_object_err(struct gl_context
*ctx
, unsigned memory
,
271 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory=0)", func
);
275 struct gl_memory_object
*memObj
= _mesa_lookup_memory_object(ctx
, memory
);
279 if (!memObj
->Immutable
) {
280 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
289 * Helper used by _mesa_TexStorageMem1/2/3DEXT().
292 texstorage_memory(GLuint dims
, GLenum target
, GLsizei levels
,
293 GLenum internalFormat
, GLsizei width
, GLsizei height
,
294 GLsizei depth
, GLuint memory
, GLuint64 offset
,
297 struct gl_texture_object
*texObj
;
298 struct gl_memory_object
*memObj
;
300 GET_CURRENT_CONTEXT(ctx
);
302 if (!ctx
->Extensions
.EXT_memory_object
) {
303 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
307 texObj
= _mesa_get_current_tex_object(ctx
, target
);
311 memObj
= lookup_memory_object_err(ctx
, memory
, func
);
315 _mesa_texture_storage_memory(ctx
, dims
, texObj
, memObj
, target
,
316 levels
, internalFormat
,
317 width
, height
, depth
, offset
, false);
321 texstorage_memory_ms(GLuint dims
, GLenum target
, GLsizei samples
,
322 GLenum internalFormat
, GLsizei width
, GLsizei height
,
323 GLsizei depth
, GLboolean fixedSampleLocations
,
324 GLuint memory
, GLuint64 offset
, const char* func
)
326 struct gl_texture_object
*texObj
;
327 struct gl_memory_object
*memObj
;
329 GET_CURRENT_CONTEXT(ctx
);
331 if (!ctx
->Extensions
.EXT_memory_object
) {
332 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
336 texObj
= _mesa_get_current_tex_object(ctx
, target
);
340 memObj
= lookup_memory_object_err(ctx
, memory
, func
);
344 _mesa_texture_storage_ms_memory(ctx
, dims
, texObj
, memObj
, target
, samples
,
345 internalFormat
, width
, height
, depth
,
346 fixedSampleLocations
, offset
, func
);
350 * Helper used by _mesa_TextureStorageMem1/2/3DEXT().
353 texturestorage_memory(GLuint dims
, GLuint texture
, GLsizei levels
,
354 GLenum internalFormat
, GLsizei width
, GLsizei height
,
355 GLsizei depth
, GLuint memory
, GLuint64 offset
,
358 struct gl_texture_object
*texObj
;
359 struct gl_memory_object
*memObj
;
361 GET_CURRENT_CONTEXT(ctx
);
363 if (!ctx
->Extensions
.EXT_memory_object
) {
364 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
368 texObj
= _mesa_lookup_texture(ctx
, texture
);
372 memObj
= lookup_memory_object_err(ctx
, memory
, func
);
376 _mesa_texture_storage_memory(ctx
, dims
, texObj
, memObj
, texObj
->Target
,
377 levels
, internalFormat
,
378 width
, height
, depth
, offset
, true);
382 texturestorage_memory_ms(GLuint dims
, GLuint texture
, GLsizei samples
,
383 GLenum internalFormat
, GLsizei width
, GLsizei height
,
384 GLsizei depth
, GLboolean fixedSampleLocations
,
385 GLuint memory
, GLuint64 offset
, const char* func
)
387 struct gl_texture_object
*texObj
;
388 struct gl_memory_object
*memObj
;
390 GET_CURRENT_CONTEXT(ctx
);
392 if (!ctx
->Extensions
.EXT_memory_object
) {
393 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
397 texObj
= _mesa_lookup_texture(ctx
, texture
);
401 memObj
= lookup_memory_object_err(ctx
, memory
, func
);
405 _mesa_texture_storage_ms_memory(ctx
, dims
, texObj
, memObj
, texObj
->Target
,
406 samples
, internalFormat
, width
, height
,
407 depth
, fixedSampleLocations
, offset
, func
);
411 _mesa_TexStorageMem2DEXT(GLenum target
,
413 GLenum internalFormat
,
419 texstorage_memory(2, target
, levels
, internalFormat
, width
, height
, 1,
420 memory
, offset
, "glTexStorageMem2DEXT");
424 _mesa_TexStorageMem2DMultisampleEXT(GLenum target
,
426 GLenum internalFormat
,
429 GLboolean fixedSampleLocations
,
433 texstorage_memory_ms(2, target
, samples
, internalFormat
, width
, height
, 1,
434 fixedSampleLocations
, memory
, offset
,
435 "glTexStorageMem2DMultisampleEXT");
439 _mesa_TexStorageMem3DEXT(GLenum target
,
441 GLenum internalFormat
,
448 texstorage_memory(3, target
, levels
, internalFormat
, width
, height
, depth
,
449 memory
, offset
, "glTexStorageMem3DEXT");
453 _mesa_TexStorageMem3DMultisampleEXT(GLenum target
,
455 GLenum internalFormat
,
459 GLboolean fixedSampleLocations
,
463 texstorage_memory_ms(3, target
, samples
, internalFormat
, width
, height
,
464 depth
, fixedSampleLocations
, memory
, offset
,
465 "glTexStorageMem3DMultisampleEXT");
469 _mesa_TextureStorageMem2DEXT(GLuint texture
,
471 GLenum internalFormat
,
477 texturestorage_memory(2, texture
, levels
, internalFormat
, width
, height
, 1,
478 memory
, offset
, "glTexureStorageMem2DEXT");
482 _mesa_TextureStorageMem2DMultisampleEXT(GLuint texture
,
484 GLenum internalFormat
,
487 GLboolean fixedSampleLocations
,
491 texturestorage_memory_ms(2, texture
, samples
, internalFormat
, width
, height
,
492 1, fixedSampleLocations
, memory
, offset
,
493 "glTextureStorageMem2DMultisampleEXT");
497 _mesa_TextureStorageMem3DEXT(GLuint texture
,
499 GLenum internalFormat
,
506 texturestorage_memory(3, texture
, levels
, internalFormat
, width
, height
,
507 depth
, memory
, offset
, "glTextureStorageMem3DEXT");
511 _mesa_TextureStorageMem3DMultisampleEXT(GLuint texture
,
513 GLenum internalFormat
,
517 GLboolean fixedSampleLocations
,
521 texturestorage_memory_ms(3, texture
, samples
, internalFormat
, width
, height
,
522 depth
, fixedSampleLocations
, memory
, offset
,
523 "glTextureStorageMem3DMultisampleEXT");
527 _mesa_TexStorageMem1DEXT(GLenum target
,
529 GLenum internalFormat
,
534 texstorage_memory(1, target
, levels
, internalFormat
, width
, 1, 1, memory
,
535 offset
, "glTexStorageMem1DEXT");
539 _mesa_TextureStorageMem1DEXT(GLuint texture
,
541 GLenum internalFormat
,
546 texturestorage_memory(1, texture
, levels
, internalFormat
, width
, 1, 1,
547 memory
, offset
, "glTextureStorageMem1DEXT");
551 _mesa_GenSemaphoresEXT(GLsizei n
, GLuint
*semaphores
)
557 _mesa_DeleteSemaphoresEXT(GLsizei n
, const GLuint
*semaphores
)
563 _mesa_IsSemaphoreEXT(GLuint semaphore
)
569 _mesa_SemaphoreParameterui64vEXT(GLuint semaphore
,
571 const GLuint64
*params
)
577 _mesa_GetSemaphoreParameterui64vEXT(GLuint semaphore
,
585 _mesa_WaitSemaphoreEXT(GLuint semaphore
,
586 GLuint numBufferBarriers
,
587 const GLuint
*buffers
,
588 GLuint numTextureBarriers
,
589 const GLuint
*textures
,
590 const GLenum
*srcLayouts
)
596 _mesa_SignalSemaphoreEXT(GLuint semaphore
,
597 GLuint numBufferBarriers
,
598 const GLuint
*buffers
,
599 GLuint numTextureBarriers
,
600 const GLuint
*textures
,
601 const GLenum
*dstLayouts
)
607 _mesa_ImportMemoryFdEXT(GLuint memory
,
612 GET_CURRENT_CONTEXT(ctx
);
614 if (!ctx
->Extensions
.EXT_memory_object_fd
) {
615 _mesa_error(ctx
, GL_INVALID_OPERATION
,
616 "glImportMemoryFdEXT(unsupported)");
620 if (handleType
!= GL_HANDLE_TYPE_OPAQUE_FD_EXT
) {
621 _mesa_error(ctx
, GL_INVALID_VALUE
, "glImportMemoryFdEXT(handleType=%u)",
626 struct gl_memory_object
*memObj
= _mesa_lookup_memory_object(ctx
, memory
);
630 ctx
->Driver
.ImportMemoryObjectFd(ctx
, memObj
, size
, fd
);
631 memObj
->Immutable
= GL_TRUE
;
635 _mesa_ImportSemaphoreFdEXT(GLuint semaphore
,