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
,
94 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteMemoryObjectsEXT(n < 0)");
101 _mesa_HashLockMutex(ctx
->Shared
->MemoryObjects
);
102 for (GLint i
= 0; i
< n
; i
++) {
103 if (memoryObjects
[i
] > 0) {
104 struct gl_memory_object
*delObj
105 = _mesa_lookup_memory_object_locked(ctx
, memoryObjects
[i
]);
108 _mesa_HashRemoveLocked(ctx
->Shared
->MemoryObjects
,
110 ctx
->Driver
.DeleteMemoryObject(ctx
, delObj
);
114 _mesa_HashUnlockMutex(ctx
->Shared
->MemoryObjects
);
118 _mesa_IsMemoryObjectEXT(GLuint memoryObject
)
120 GET_CURRENT_CONTEXT(ctx
);
121 struct gl_memory_object
*obj
=
122 _mesa_lookup_memory_object(ctx
, memoryObject
);
124 return obj
? GL_TRUE
: GL_FALSE
;
128 _mesa_CreateMemoryObjectsEXT(GLsizei n
, GLuint
*memoryObjects
)
130 GET_CURRENT_CONTEXT(ctx
);
132 const char *func
= "glCreateMemoryObjectsEXT";
134 if (MESA_VERBOSE
& (VERBOSE_API
))
135 _mesa_debug(ctx
, "%s(%d, %p)", func
, n
, memoryObjects
);
138 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n < 0)", func
);
145 _mesa_HashLockMutex(ctx
->Shared
->MemoryObjects
);
146 GLuint first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->MemoryObjects
, n
);
148 for (GLsizei i
= 0; i
< n
; i
++) {
149 struct gl_memory_object
*memObj
;
151 memoryObjects
[i
] = first
+ i
;
153 /* allocate memory object */
154 memObj
= ctx
->Driver
.NewMemoryObject(ctx
, memoryObjects
[i
]);
156 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s()", func
);
157 _mesa_HashUnlockMutex(ctx
->Shared
->MemoryObjects
);
161 /* insert into hash table */
162 _mesa_HashInsertLocked(ctx
->Shared
->MemoryObjects
,
168 _mesa_HashUnlockMutex(ctx
->Shared
->MemoryObjects
);
172 _mesa_MemoryObjectParameterivEXT(GLuint memoryObject
,
176 GET_CURRENT_CONTEXT(ctx
);
177 struct gl_memory_object
*memObj
;
179 memObj
= _mesa_lookup_memory_object(ctx
, memoryObject
);
183 if (memObj
->Immutable
) {
184 _mesa_error(ctx
, GL_INVALID_OPERATION
,
185 "glMemoryObjectParameterivEXT(memoryObject is immutable");
190 case GL_DEDICATED_MEMORY_OBJECT_EXT
:
191 memObj
->Dedicated
= (GLboolean
) params
[0];
193 case GL_PROTECTED_MEMORY_OBJECT_EXT
:
194 /* EXT_protected_textures not supported */
202 _mesa_error(ctx
, GL_INVALID_ENUM
,
203 "glMemoryObjectParameterivEXT(pname=0x%x)", pname
);
207 _mesa_GetMemoryObjectParameterivEXT(GLuint memoryObject
,
211 GET_CURRENT_CONTEXT(ctx
);
212 struct gl_memory_object
*memObj
;
214 memObj
= _mesa_lookup_memory_object(ctx
, memoryObject
);
219 case GL_DEDICATED_MEMORY_OBJECT_EXT
:
220 *params
= (GLint
) memObj
->Dedicated
;
222 case GL_PROTECTED_MEMORY_OBJECT_EXT
:
223 /* EXT_protected_textures not supported */
231 _mesa_error(ctx
, GL_INVALID_ENUM
,
232 "glGetMemoryObjectParameterivEXT(pname=0x%x)", pname
);
235 static struct gl_memory_object
*
236 lookup_memory_object_err(struct gl_context
*ctx
, unsigned memory
,
240 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory=0)", func
);
244 struct gl_memory_object
*memObj
= _mesa_lookup_memory_object(ctx
, memory
);
248 if (!memObj
->Immutable
) {
249 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
258 * Helper used by _mesa_TexStorageMem1/2/3DEXT().
261 texstorage_memory(GLuint dims
, GLenum target
, GLsizei levels
,
262 GLenum internalFormat
, GLsizei width
, GLsizei height
,
263 GLsizei depth
, GLuint memory
, GLuint64 offset
,
266 struct gl_texture_object
*texObj
;
267 struct gl_memory_object
*memObj
;
269 GET_CURRENT_CONTEXT(ctx
);
271 texObj
= _mesa_get_current_tex_object(ctx
, target
);
275 memObj
= lookup_memory_object_err(ctx
, memory
, func
);
279 _mesa_texture_storage_memory(ctx
, dims
, texObj
, memObj
, target
,
280 levels
, internalFormat
,
281 width
, height
, depth
, offset
, false);
285 texstorage_memory_ms(GLuint dims
, GLenum target
, GLsizei samples
,
286 GLenum internalFormat
, GLsizei width
, GLsizei height
,
287 GLsizei depth
, GLboolean fixedSampleLocations
,
288 GLuint memory
, GLuint64 offset
, const char* func
)
290 struct gl_texture_object
*texObj
;
291 struct gl_memory_object
*memObj
;
293 GET_CURRENT_CONTEXT(ctx
);
295 texObj
= _mesa_get_current_tex_object(ctx
, target
);
299 memObj
= lookup_memory_object_err(ctx
, memory
, func
);
303 _mesa_texture_storage_ms_memory(ctx
, dims
, texObj
, memObj
, target
, samples
,
304 internalFormat
, width
, height
, depth
,
305 fixedSampleLocations
, offset
, func
);
309 * Helper used by _mesa_TextureStorageMem1/2/3DEXT().
312 texturestorage_memory(GLuint dims
, GLuint texture
, GLsizei levels
,
313 GLenum internalFormat
, GLsizei width
, GLsizei height
,
314 GLsizei depth
, GLuint memory
, GLuint64 offset
,
317 struct gl_texture_object
*texObj
;
318 struct gl_memory_object
*memObj
;
320 GET_CURRENT_CONTEXT(ctx
);
322 texObj
= _mesa_lookup_texture(ctx
, texture
);
326 memObj
= lookup_memory_object_err(ctx
, memory
, func
);
330 _mesa_texture_storage_memory(ctx
, dims
, texObj
, memObj
, texObj
->Target
,
331 levels
, internalFormat
,
332 width
, height
, depth
, offset
, true);
336 texturestorage_memory_ms(GLuint dims
, GLuint texture
, GLsizei samples
,
337 GLenum internalFormat
, GLsizei width
, GLsizei height
,
338 GLsizei depth
, GLboolean fixedSampleLocations
,
339 GLuint memory
, GLuint64 offset
, const char* func
)
341 struct gl_texture_object
*texObj
;
342 struct gl_memory_object
*memObj
;
344 GET_CURRENT_CONTEXT(ctx
);
346 texObj
= _mesa_lookup_texture(ctx
, texture
);
350 memObj
= lookup_memory_object_err(ctx
, memory
, func
);
354 _mesa_texture_storage_ms_memory(ctx
, dims
, texObj
, memObj
, texObj
->Target
,
355 samples
, internalFormat
, width
, height
,
356 depth
, fixedSampleLocations
, offset
, func
);
360 _mesa_TexStorageMem2DEXT(GLenum target
,
362 GLenum internalFormat
,
368 texstorage_memory(2, target
, levels
, internalFormat
, width
, height
, 1,
369 memory
, offset
, "glTexStorageMem2DEXT");
373 _mesa_TexStorageMem2DMultisampleEXT(GLenum target
,
375 GLenum internalFormat
,
378 GLboolean fixedSampleLocations
,
382 texstorage_memory_ms(2, target
, samples
, internalFormat
, width
, height
, 1,
383 fixedSampleLocations
, memory
, offset
,
384 "glTexStorageMem2DMultisampleEXT");
388 _mesa_TexStorageMem3DEXT(GLenum target
,
390 GLenum internalFormat
,
397 texstorage_memory(3, target
, levels
, internalFormat
, width
, height
, depth
,
398 memory
, offset
, "glTexStorageMem3DEXT");
402 _mesa_TexStorageMem3DMultisampleEXT(GLenum target
,
404 GLenum internalFormat
,
408 GLboolean fixedSampleLocations
,
412 texstorage_memory_ms(3, target
, samples
, internalFormat
, width
, height
,
413 depth
, fixedSampleLocations
, memory
, offset
,
414 "glTexStorageMem3DMultisampleEXT");
418 _mesa_TextureStorageMem2DEXT(GLuint texture
,
420 GLenum internalFormat
,
426 texturestorage_memory(2, texture
, levels
, internalFormat
, width
, height
, 1,
427 memory
, offset
, "glTexureStorageMem2DEXT");
431 _mesa_TextureStorageMem2DMultisampleEXT(GLuint texture
,
433 GLenum internalFormat
,
436 GLboolean fixedSampleLocations
,
440 texturestorage_memory_ms(2, texture
, samples
, internalFormat
, width
, height
,
441 1, fixedSampleLocations
, memory
, offset
,
442 "glTextureStorageMem2DMultisampleEXT");
446 _mesa_TextureStorageMem3DEXT(GLuint texture
,
448 GLenum internalFormat
,
455 texturestorage_memory(3, texture
, levels
, internalFormat
, width
, height
,
456 depth
, memory
, offset
, "glTextureStorageMem3DEXT");
460 _mesa_TextureStorageMem3DMultisampleEXT(GLuint texture
,
462 GLenum internalFormat
,
466 GLboolean fixedSampleLocations
,
470 texturestorage_memory_ms(3, texture
, samples
, internalFormat
, width
, height
,
471 depth
, fixedSampleLocations
, memory
, offset
,
472 "glTextureStorageMem3DMultisampleEXT");
476 _mesa_TexStorageMem1DEXT(GLenum target
,
478 GLenum internalFormat
,
483 texstorage_memory(1, target
, levels
, internalFormat
, width
, 1, 1, memory
,
484 offset
, "glTexStorageMem1DEXT");
488 _mesa_TextureStorageMem1DEXT(GLuint texture
,
490 GLenum internalFormat
,
495 texturestorage_memory(1, texture
, levels
, internalFormat
, width
, 1, 1,
496 memory
, offset
, "glTextureStorageMem1DEXT");
500 _mesa_GenSemaphoresEXT(GLsizei n
, GLuint
*semaphores
)
506 _mesa_DeleteSemaphoresEXT(GLsizei n
, const GLuint
*semaphores
)
512 _mesa_IsSemaphoreEXT(GLuint semaphore
)
518 _mesa_SemaphoreParameterui64vEXT(GLuint semaphore
,
520 const GLuint64
*params
)
526 _mesa_GetSemaphoreParameterui64vEXT(GLuint semaphore
,
534 _mesa_WaitSemaphoreEXT(GLuint semaphore
,
535 GLuint numBufferBarriers
,
536 const GLuint
*buffers
,
537 GLuint numTextureBarriers
,
538 const GLuint
*textures
,
539 const GLenum
*srcLayouts
)
545 _mesa_SignalSemaphoreEXT(GLuint semaphore
,
546 GLuint numBufferBarriers
,
547 const GLuint
*buffers
,
548 GLuint numTextureBarriers
,
549 const GLuint
*textures
,
550 const GLenum
*dstLayouts
)
556 _mesa_ImportMemoryFdEXT(GLuint memory
,
561 GET_CURRENT_CONTEXT(ctx
);
563 if (handleType
!= GL_HANDLE_TYPE_OPAQUE_FD_EXT
) {
564 _mesa_error(ctx
, GL_INVALID_VALUE
, "glImportMemoryFdEXT(handleType=%u)",
569 struct gl_memory_object
*memObj
= _mesa_lookup_memory_object(ctx
, memory
);
573 ctx
->Driver
.ImportMemoryObjectFd(ctx
, memObj
, size
, fd
);
574 memObj
->Immutable
= GL_TRUE
;
578 _mesa_ImportSemaphoreFdEXT(GLuint semaphore
,