Skip to content

Commit

Permalink
render: Update Metal and GL backends to use new high-level features, …
Browse files Browse the repository at this point in the history
…etc.

Now nothing is uploaded as dynamic data with Metal's setVertexBytes, etc; it's
all in the one big vertex buffer, now.
  • Loading branch information
icculus committed Sep 24, 2018
1 parent cc56de4 commit 2241b33
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 58 deletions.
144 changes: 92 additions & 52 deletions src/render/metal/SDL_render_metal.m
Expand Up @@ -64,6 +64,8 @@ static int METAL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void METAL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int METAL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
static int METAL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd);
static int METAL_QueueSetDrawColor(SDL_Renderer * renderer, SDL_RenderCommand *cmd);
static int METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points,
int count);
static int METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects,
Expand Down Expand Up @@ -621,6 +623,8 @@ - (void)dealloc
renderer->LockTexture = METAL_LockTexture;
renderer->UnlockTexture = METAL_UnlockTexture;
renderer->SetRenderTarget = METAL_SetRenderTarget;
renderer->QueueSetViewport = METAL_QueueSetViewport;
renderer->QueueSetDrawColor = METAL_QueueSetDrawColor;
renderer->QueueDrawPoints = METAL_QueueDrawPoints;
renderer->QueueDrawLines = METAL_QueueDrawPoints; // lines and points queue the same way.
renderer->QueueFillRects = METAL_QueueFillRects;
Expand All @@ -637,6 +641,8 @@ - (void)dealloc
renderer->info = METAL_RenderDriver.info;
renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);

renderer->always_batch = SDL_TRUE;

#if defined(__MACOSX__) && defined(MAC_OS_X_VERSION_10_13)
if (@available(macOS 10.13, *)) {
data.mtllayer.displaySyncEnabled = (flags & SDL_RENDERER_PRESENTVSYNC) != 0;
Expand Down Expand Up @@ -740,6 +746,11 @@ - (void)dealloc
}

data.activepipelines = ChooseShaderPipelines(data, mtltexture.pixelFormat);

// make sure this has a definite place in the queue. This way it will
// execute reliably whether the app tries to make its own command buffers
// or whatever. This means we can _always_ batch rendering commands!
[data.mtlcmdbuffer enqueue];
}
}

Expand Down Expand Up @@ -1026,11 +1037,51 @@ - (void)dealloc
return _val / len;
}

static int
METAL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
{
float projection[4][4]; /* Prepare an orthographic projection */
const int w = cmd->data.viewport.rect.w;
const int h = cmd->data.viewport.rect.h;
const size_t matrixlen = sizeof (projection);
float *matrix = (float *) SDL_AllocateRenderVertices(renderer, matrixlen, CONSTANT_ALIGN, &cmd->data.viewport.first);
if (!matrix) {
return -1;
}

SDL_memset(projection, '\0', matrixlen);
if (w && h) {
projection[0][0] = 2.0f / w;
projection[1][1] = -2.0f / h;
projection[3][0] = -1.0f;
projection[3][1] = 1.0f;
projection[3][3] = 1.0f;
}
SDL_memcpy(matrix, projection, matrixlen);

return 0;
}

static int
METAL_QueueSetDrawColor(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
const size_t vertlen = sizeof (float) * 4;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, CONSTANT_ALIGN, &cmd->data.color.first);
if (!verts) {
return -1;
}
*(verts++) = ((float)cmd->data.color.r) / 255.0f;
*(verts++) = ((float)cmd->data.color.g) / 255.0f;
*(verts++) = ((float)cmd->data.color.b) / 255.0f;
*(verts++) = ((float)cmd->data.color.a) / 255.0f;
return 0;
}

static int
METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
{
const size_t vertlen = (sizeof (float) * 2) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, &cmd->data.draw.first);
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
}
Expand All @@ -1044,7 +1095,7 @@ - (void)dealloc
{
// !!! FIXME: use an index buffer
const size_t vertlen = (sizeof (float) * 8) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, &cmd->data.draw.first);
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
}
Expand Down Expand Up @@ -1082,7 +1133,7 @@ - (void)dealloc
const float texh = (float) texturedata.mtltexture.height;
// !!! FIXME: use an index buffer
const size_t vertlen = (sizeof (float) * 16);
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, &cmd->data.draw.first);
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
}
Expand Down Expand Up @@ -1123,7 +1174,7 @@ - (void)dealloc
float minu, maxu, minv, maxv;
// !!! FIXME: use an index buffer
const size_t vertlen = (sizeof (float) * 32);
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, &cmd->data.draw.first);
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
}
Expand Down Expand Up @@ -1185,29 +1236,30 @@ - (void)dealloc

typedef struct
{
#if __has_feature(objc_arc)
__unsafe_unretained id<MTLRenderPipelineState> pipeline;
#else
id<MTLRenderPipelineState> pipeline;
#endif
size_t constants_offset;
SDL_Texture *texture;
Uint32 color;
SDL_bool color_dirty;
SDL_bool cliprect_dirty;
SDL_bool cliprect_enabled;
SDL_Rect cliprect;
SDL_bool viewport_dirty;
SDL_Rect viewport;
SDL_Rect cliprect;
size_t projection_offset;
SDL_bool color_dirty;
size_t color_offset;
} METAL_DrawStateCache;

static void
SetDrawState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const SDL_MetalFragmentFunction shader,
const size_t constants_offset, id<MTLBuffer> mtlbufvertex, METAL_DrawStateCache *statecache)
{
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
const Uint8 r = cmd->data.draw.r;
const Uint8 g = cmd->data.draw.g;
const Uint8 b = cmd->data.draw.b;
const Uint8 a = cmd->data.draw.a;
const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
const SDL_BlendMode blend = cmd->data.draw.blend;
size_t first = cmd->data.draw.first;
id<MTLRenderPipelineState> newpipeline;

METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL);
Expand All @@ -1221,20 +1273,7 @@ - (void)dealloc
viewport.znear = 0.0;
viewport.zfar = 1.0;
[data.mtlcmdencoder setViewport:viewport];

float projection[4][4]; /* Prepare an orthographic projection */
SDL_memset(projection, '\0', sizeof (projection));

if (statecache->viewport.w && statecache->viewport.h) {
projection[0][0] = 2.0f / statecache->viewport.w;
projection[1][1] = -2.0f / statecache->viewport.h;
projection[3][0] = -1.0f;
projection[3][1] = 1.0f;
projection[3][3] = 1.0f;
}

// !!! FIXME: This should be in a buffer...
[data.mtlcmdencoder setVertexBytes:projection length:sizeof(float)*16 atIndex:2];
[data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:statecache->projection_offset atIndex:2]; // projection
statecache->viewport_dirty = SDL_FALSE;
}

Expand All @@ -1258,11 +1297,9 @@ - (void)dealloc
statecache->cliprect_dirty = SDL_FALSE;
}

if (statecache->color_dirty || (color != statecache->color)) {
const float colorf[4] = { ((float)r) / 255.0f, ((float)g) / 255.0f, ((float)b) / 255.0f, ((float)a) / 255.0f };
[data.mtlcmdencoder setFragmentBytes:colorf length:sizeof(colorf) atIndex:0];
if (statecache->color_dirty) {
[data.mtlcmdencoder setFragmentBuffer:mtlbufvertex offset:statecache->color_offset atIndex:0];
statecache->color_dirty = SDL_FALSE;
statecache->color = color;
}

newpipeline = ChoosePipelineState(data, data.activepipelines, shader, blend);
Expand All @@ -1278,7 +1315,7 @@ - (void)dealloc
statecache->constants_offset = constants_offset;
}

[data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:cmd->data.draw.first atIndex:0]; // position
[data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:first atIndex:0]; // position
}

static void
Expand Down Expand Up @@ -1320,13 +1357,11 @@ - (void)dealloc
statecache.pipeline = nil;
statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
statecache.texture = NULL;
statecache.color = ((renderer->a << 24) | (renderer->r << 16) | (renderer->g << 8) | renderer->b);
statecache.color_dirty = SDL_TRUE;
statecache.cliprect_dirty = SDL_TRUE;
statecache.viewport_dirty = SDL_TRUE; // TRUE so we set ortho matrix
statecache.cliprect_enabled = renderer->clipping_enabled;
SDL_memcpy(&statecache.viewport, &renderer->viewport, sizeof (statecache.viewport));
SDL_memcpy(&statecache.cliprect, &renderer->clip_rect, sizeof (statecache.cliprect));
statecache.viewport_dirty = SDL_TRUE;
statecache.projection_offset = 0;
statecache.color_offset = 0;

// !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation?
id<MTLBuffer> mtlbufvertexstaging = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
Expand Down Expand Up @@ -1357,20 +1392,22 @@ - (void)dealloc
while (cmd) {
switch (cmd->command) {
case SDL_RENDERCMD_SETVIEWPORT: {
if (SDL_memcmp(&statecache.viewport, &cmd->data.viewport, sizeof (statecache.viewport)) != 0) {
SDL_memcpy(&statecache.viewport, &cmd->data.viewport, sizeof (statecache.viewport));
statecache.viewport_dirty = SDL_TRUE;
}
SDL_memcpy(&statecache.viewport, &cmd->data.viewport.rect, sizeof (statecache.viewport));
statecache.projection_offset = cmd->data.viewport.first;
statecache.viewport_dirty = SDL_TRUE;
break;
}

case SDL_RENDERCMD_SETCLIPRECT: {
if ((statecache.cliprect_enabled != cmd->data.cliprect.enabled) ||
(SDL_memcmp(&statecache.cliprect, &cmd->data.cliprect.rect, sizeof (statecache.cliprect)) != 0)) {
SDL_memcpy(&statecache.cliprect, &cmd->data.cliprect.rect, sizeof (statecache.cliprect));
statecache.cliprect_enabled = cmd->data.cliprect.enabled;
statecache.cliprect_dirty = SDL_TRUE;
}
SDL_memcpy(&statecache.cliprect, &cmd->data.cliprect.rect, sizeof (statecache.cliprect));
statecache.cliprect_enabled = cmd->data.cliprect.enabled;
statecache.cliprect_dirty = SDL_TRUE;
break;
}

case SDL_RENDERCMD_SETDRAWCOLOR: {
statecache.color_offset = cmd->data.color.first;
statecache.color_dirty = SDL_TRUE;
break;
}

Expand All @@ -1380,16 +1417,13 @@ - (void)dealloc
operation via MTLLoadActionClear. */
if (data.mtlcmdencoder != nil) {
[data.mtlcmdencoder endEncoding];

// !!! FIXME: have to commit, or an uncommitted but enqueued buffer will prevent the frame from finishing.
[data.mtlcmdbuffer commit];
data.mtlcmdencoder = nil;
data.mtlcmdbuffer = nil;
}

const Uint8 r = cmd->data.color.r;
const Uint8 g = cmd->data.color.g;
const Uint8 b = cmd->data.color.b;
const Uint8 a = cmd->data.color.a;
MTLClearColor color = MTLClearColorMake(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);

// force all this state to be reconfigured on next command buffer.
statecache.pipeline = nil;
statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
Expand All @@ -1398,6 +1432,12 @@ - (void)dealloc
statecache.cliprect_dirty = SDL_TRUE;
statecache.viewport_dirty = SDL_TRUE;

const Uint8 r = cmd->data.color.r;
const Uint8 g = cmd->data.color.g;
const Uint8 b = cmd->data.color.b;
const Uint8 a = cmd->data.color.a;
MTLClearColor color = MTLClearColorMake(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);

// get new command encoder, set up with an initial clear operation.
METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionClear, &color);
break;
Expand Down
26 changes: 20 additions & 6 deletions src/render/opengl/SDL_render_gl.c
Expand Up @@ -68,6 +68,7 @@ static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
static int GL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd);
static int GL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd,
const SDL_FPoint * points, int count);
static int GL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd,
Expand Down Expand Up @@ -391,6 +392,8 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
renderer->LockTexture = GL_LockTexture;
renderer->UnlockTexture = GL_UnlockTexture;
renderer->SetRenderTarget = GL_SetRenderTarget;
renderer->QueueSetViewport = GL_QueueSetViewport;
renderer->QueueSetDrawColor = GL_QueueSetViewport; /* SetViewport and SetDrawColor are (currently) no-ops. */
renderer->QueueDrawPoints = GL_QueueDrawPoints;
renderer->QueueDrawLines = GL_QueueDrawPoints; /* lines and points queue vertices the same way. */
renderer->QueueFillRects = GL_QueueFillRects;
Expand Down Expand Up @@ -1009,10 +1012,16 @@ GL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
/* !!! FIXME: all these Queue* calls set up the vertex buffer the way the immediate mode
!!! FIXME: renderer wants it, but this might want to operate differently if we move to
!!! FIXME: VBOs at some point. */
static int
GL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
{
return 0; /* nothing to do in this backend. */
}

static int
GL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
{
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 2 * sizeof (GLfloat), &cmd->data.draw.first);
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 2 * sizeof (GLfloat), 0, &cmd->data.draw.first);
size_t i;

if (!verts) {
Expand All @@ -1031,7 +1040,7 @@ GL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FP
static int
GL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
{
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 4 * sizeof (GLfloat), &cmd->data.draw.first);
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, count * 4 * sizeof (GLfloat), 0, &cmd->data.draw.first);
size_t i;

if (!verts) {
Expand All @@ -1057,7 +1066,7 @@ GL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * text
GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
GLfloat minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 8 * sizeof (GLfloat), &cmd->data.draw.first);
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 8 * sizeof (GLfloat), 0, &cmd->data.draw.first);

if (!verts) {
return -1;
Expand Down Expand Up @@ -1100,7 +1109,7 @@ GL_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * te
GLfloat minx, miny, maxx, maxy;
GLfloat centerx, centery;
GLfloat minu, maxu, minv, maxv;
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 11 * sizeof (GLfloat), &cmd->data.draw.first);
GLfloat *verts = (GLfloat *) SDL_AllocateRenderVertices(renderer, 11 * sizeof (GLfloat), 0, &cmd->data.draw.first);

if (!verts) {
return -1;
Expand Down Expand Up @@ -1338,9 +1347,14 @@ GL_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertic

while (cmd) {
switch (cmd->command) {
case SDL_RENDERCMD_SETDRAWCOLOR: {
// !!! FIXME: use this.
break;
}

case SDL_RENDERCMD_SETVIEWPORT: {
if (SDL_memcmp(&cmd->data.viewport, &viewport, sizeof (SDL_Rect)) != 0) {
SDL_memcpy(&viewport, &cmd->data.viewport, sizeof (SDL_Rect));
if (SDL_memcmp(&cmd->data.viewport.rect, &viewport, sizeof (viewport)) != 0) {
SDL_memcpy(&viewport, &cmd->data.viewport.rect, sizeof (viewport));
data->glMatrixMode(GL_PROJECTION);
data->glLoadIdentity();
data->glViewport(viewport.x,
Expand Down

0 comments on commit 2241b33

Please sign in to comment.