Skip to content

Commit 1164ac6

Browse files
committed
Added SDL_PROP_GPU_TEXTURE_CREATE_PIXELBUFFER_POINTER
1 parent 37417c2 commit 1164ac6

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

include/SDL3/SDL_gpu.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,11 @@ extern SDL_DECLSPEC SDL_GPUShader * SDLCALL SDL_CreateGPUShader(
27132713
* There are optional properties that can be provided through
27142714
* SDL_GPUTextureCreateInfo's `props`. These are the supported properties:
27152715
*
2716+
* - `SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING`: a name that can be displayed
2717+
* in debugging tools.
2718+
*
2719+
* With the direct3d12 driver:
2720+
*
27162721
* - `SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_R_FLOAT`: (Direct3D 12 only) if
27172722
* the texture usage is SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, clear the texture
27182723
* to a color with this red intensity. Defaults to zero.
@@ -2731,10 +2736,14 @@ extern SDL_DECLSPEC SDL_GPUShader * SDLCALL SDL_CreateGPUShader(
27312736
* - `SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_STENCIL_NUMBER`: (Direct3D 12
27322737
* only) if the texture usage is SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET,
27332738
* clear the texture to a stencil of this Uint8 value. Defaults to zero.
2734-
* - `SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING`: a name that can be displayed
2735-
* in debugging tools.
27362739
* - `SDL_PROP_GPU_TEXTURE_CREATE_DXGI_SHARED_HANDLE_POINTER`: a DXGI shared handle associated with the texture, if you want to wrap an existing texture. The texture must have been created with D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX.
27372740
*
2741+
* With the metal driver:
2742+
*
2743+
* - `SDL_PROP_GPU_TEXTURE_CREATE_PIXELBUFFER_POINTER`: the CVPixelBufferRef
2744+
* associated with the texture, if you want to create a texture from an
2745+
* existing pixel buffer.
2746+
*
27382747
* \param device a GPU Context.
27392748
* \param createinfo a struct describing the state of the texture to create.
27402749
* \returns a texture object on success, or NULL on failure; call
@@ -2759,14 +2768,15 @@ extern SDL_DECLSPEC SDL_GPUTexture * SDLCALL SDL_CreateGPUTexture(
27592768
SDL_GPUDevice *device,
27602769
const SDL_GPUTextureCreateInfo *createinfo);
27612770

2771+
#define SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING "SDL.gpu.texture.create.name"
27622772
#define SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_R_FLOAT "SDL.gpu.texture.create.d3d12.clear.r"
27632773
#define SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_G_FLOAT "SDL.gpu.texture.create.d3d12.clear.g"
27642774
#define SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_B_FLOAT "SDL.gpu.texture.create.d3d12.clear.b"
27652775
#define SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_A_FLOAT "SDL.gpu.texture.create.d3d12.clear.a"
27662776
#define SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_DEPTH_FLOAT "SDL.gpu.texture.create.d3d12.clear.depth"
27672777
#define SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_STENCIL_NUMBER "SDL.gpu.texture.create.d3d12.clear.stencil"
2768-
#define SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING "SDL.gpu.texture.create.name"
27692778
#define SDL_PROP_GPU_TEXTURE_CREATE_DXGI_SHARED_HANDLE_POINTER "SDL.gpu.texture.create.dxgi_handle"
2779+
#define SDL_PROP_GPU_TEXTURE_CREATE_PIXELBUFFER_POINTER "SDL.gpu.texture.create.pixelbuffer"
27702780

27712781
/**
27722782
* Creates a buffer object to be used in graphics or compute workflows.

src/gpu/metal/SDL_gpu_metal.m

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <Metal/Metal.h>
2727
#include <QuartzCore/CoreAnimation.h>
28+
#include <CoreVideo/CoreVideo.h>
2829

2930
#include "../SDL_sysgpu.h"
3031

@@ -1428,6 +1429,49 @@ static void METAL_PopDebugGroup(
14281429
MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor new];
14291430
id<MTLTexture> texture;
14301431
MetalTexture *metalTexture;
1432+
CVPixelBufferRef pixelbuffer = nil;
1433+
IOSurfaceRef surface = nil;
1434+
NSUInteger plane = 0;
1435+
1436+
pixelbuffer = SDL_GetPointerProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_PIXELBUFFER_POINTER, nil);
1437+
if (pixelbuffer) {
1438+
surface = CVPixelBufferGetIOSurface(pixelbuffer);
1439+
if (!surface) {
1440+
SET_STRING_ERROR_AND_RETURN("CVPixelBufferGetIOSurface() failed", NULL);
1441+
}
1442+
1443+
OSType format = CVPixelBufferGetPixelFormatType(pixelbuffer);
1444+
switch (format) {
1445+
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
1446+
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
1447+
switch (createinfo->format) {
1448+
case SDL_GPU_TEXTUREFORMAT_R8_UNORM:
1449+
plane = 0;
1450+
break;
1451+
case SDL_GPU_TEXTUREFORMAT_R8G8_UNORM:
1452+
plane = 1;
1453+
break;
1454+
default:
1455+
break;
1456+
}
1457+
break;
1458+
case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
1459+
case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
1460+
switch (createinfo->format) {
1461+
case SDL_GPU_TEXTUREFORMAT_R16_UNORM:
1462+
plane = 0;
1463+
break;
1464+
case SDL_GPU_TEXTUREFORMAT_R16G16_UNORM:
1465+
plane = 1;
1466+
break;
1467+
default:
1468+
break;
1469+
}
1470+
break;
1471+
default:
1472+
break;
1473+
}
1474+
}
14311475

14321476
textureDescriptor.textureType = SDLToMetal_TextureType(createinfo->type, createinfo->sample_count > SDL_GPU_SAMPLECOUNT_1);
14331477
textureDescriptor.pixelFormat = SDLToMetal_TextureFormat(createinfo->format);
@@ -1469,7 +1513,11 @@ static void METAL_PopDebugGroup(
14691513
textureDescriptor.usage |= MTLTextureUsageShaderWrite;
14701514
}
14711515

1472-
texture = [renderer->device newTextureWithDescriptor:textureDescriptor];
1516+
if (surface) {
1517+
texture = [renderer->device newTextureWithDescriptor:textureDescriptor iosurface:surface plane:plane];
1518+
} else {
1519+
texture = [renderer->device newTextureWithDescriptor:textureDescriptor];
1520+
}
14731521
if (texture == NULL) {
14741522
SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to create MTLTexture!");
14751523
return NULL;

test/testffmpeg.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,11 @@ static bool GetTextureForVideoToolboxFrame(AVFrame *frame, SDL_Texture **texture
11051105
}
11061106

11071107
props = CreateVideoTextureProperties(frame, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_STATIC);
1108-
SDL_SetPointerProperty(props, SDL_PROP_TEXTURE_CREATE_METAL_PIXELBUFFER_POINTER, pPixelBuffer);
1108+
if (have_gpu_renderer) {
1109+
SDL_SetPointerProperty(props, SDL_PROP_GPU_TEXTURE_CREATE_PIXELBUFFER_POINTER, pPixelBuffer);
1110+
} else {
1111+
SDL_SetPointerProperty(props, SDL_PROP_TEXTURE_CREATE_METAL_PIXELBUFFER_POINTER, pPixelBuffer);
1112+
}
11091113
*texture = SDL_CreateTextureWithProperties(renderer, props);
11101114
SDL_DestroyProperties(props);
11111115
if (!*texture) {

0 commit comments

Comments
 (0)