METAL: use Uchar4Normalized format to transfert color as 4 bytes, instead of 4 floats

main
Sylvain Becker 2021-04-04 21:32:40 +02:00
parent fd236af8eb
commit c6ceaaeb4b
1 changed files with 36 additions and 99 deletions

View File

@ -208,8 +208,6 @@ IsMetalAvailable(const SDL_SysWMinfo *syswm)
static const MTLBlendOperation invalidBlendOperation = (MTLBlendOperation)0xFFFFFFFF; static const MTLBlendOperation invalidBlendOperation = (MTLBlendOperation)0xFFFFFFFF;
static const MTLBlendFactor invalidBlendFactor = (MTLBlendFactor)0xFFFFFFFF; static const MTLBlendFactor invalidBlendFactor = (MTLBlendFactor)0xFFFFFFFF;
static const float inv255f = 1.0f / 255.0f;
static MTLBlendOperation static MTLBlendOperation
GetBlendOperation(SDL_BlendOperation operation) GetBlendOperation(SDL_BlendOperation operation)
{ {
@ -281,34 +279,34 @@ MakePipelineState(METAL_RenderData *data, METAL_PipelineCache *cache,
switch (cache->vertexFunction) { switch (cache->vertexFunction) {
case SDL_METAL_VERTEX_SOLID: case SDL_METAL_VERTEX_SOLID:
/* position (float2), color (float4) */ /* position (float2), color (uchar4normalized) */
vertdesc.layouts[0].stride = sizeof(float) * (2 + 4); vertdesc.layouts[0].stride = sizeof(float) * 2 + sizeof (int);
vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex; vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
vertdesc.attributes[0].format = MTLVertexFormatFloat2; vertdesc.attributes[0].format = MTLVertexFormatFloat2;
vertdesc.attributes[0].offset = 0; vertdesc.attributes[0].offset = 0;
vertdesc.attributes[0].bufferIndex = 0; vertdesc.attributes[0].bufferIndex = 0;
vertdesc.attributes[1].format = MTLVertexFormatFloat4; vertdesc.attributes[1].format = MTLVertexFormatUChar4Normalized;
vertdesc.attributes[1].offset = sizeof (float) * 2; vertdesc.attributes[1].offset = sizeof (float) * 2;
vertdesc.attributes[1].bufferIndex = 0; vertdesc.attributes[1].bufferIndex = 0;
break; break;
case SDL_METAL_VERTEX_COPY: case SDL_METAL_VERTEX_COPY:
/* position (float2), color (float4), texcoord (float2) */ /* position (float2), color (uchar4normalized), texcoord (float2) */
vertdesc.layouts[0].stride = sizeof(float) * (2 + 4 + 2); vertdesc.layouts[0].stride = sizeof(float) * 2 + sizeof (int) + sizeof (float) * 2;
vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex; vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
vertdesc.attributes[0].format = MTLVertexFormatFloat2; vertdesc.attributes[0].format = MTLVertexFormatFloat2;
vertdesc.attributes[0].offset = 0; vertdesc.attributes[0].offset = 0;
vertdesc.attributes[0].bufferIndex = 0; vertdesc.attributes[0].bufferIndex = 0;
vertdesc.attributes[1].format = MTLVertexFormatFloat4; vertdesc.attributes[1].format = MTLVertexFormatUChar4Normalized;
vertdesc.attributes[1].offset = sizeof (float) * 2; vertdesc.attributes[1].offset = sizeof (float) * 2;
vertdesc.attributes[1].bufferIndex = 0; vertdesc.attributes[1].bufferIndex = 0;
vertdesc.attributes[2].format = MTLVertexFormatFloat2; vertdesc.attributes[2].format = MTLVertexFormatFloat2;
vertdesc.attributes[2].offset = sizeof(float) * (2 + 4); vertdesc.attributes[2].offset = sizeof(float) * 2 + sizeof (int);
vertdesc.attributes[2].bufferIndex = 0; vertdesc.attributes[2].bufferIndex = 0;
break; break;
} }
@ -1085,11 +1083,8 @@ METAL_QueueSetDrawColor(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
static int static int
METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count) METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
{ {
const float r = cmd->data.draw.r * inv255f; const int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
const float g = cmd->data.draw.g * inv255f; const size_t vertlen = (2 * sizeof (float) + sizeof (int)) * count;
const float b = cmd->data.draw.b * inv255f;
const float a = cmd->data.draw.a * inv255f;
const size_t vertlen = sizeof (float) * (2 + 4) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first); float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) { if (!verts) {
return -1; return -1;
@ -1099,10 +1094,7 @@ METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL
for (int i = 0; i < count; i++, points++) { for (int i = 0; i < count; i++, points++) {
*(verts++) = points->x; *(verts++) = points->x;
*(verts++) = points->y; *(verts++) = points->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
} }
return 0; return 0;
} }
@ -1110,14 +1102,12 @@ METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL
static int static int
METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count) METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
{ {
const float r = cmd->data.draw.r * inv255f;
const float g = cmd->data.draw.g * inv255f; const int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
const float b = cmd->data.draw.b * inv255f;
const float a = cmd->data.draw.a * inv255f;
SDL_assert(count >= 2); /* should have been checked at the higher level. */ SDL_assert(count >= 2); /* should have been checked at the higher level. */
const size_t vertlen = sizeof (float) * (2 + 4) * count; const size_t vertlen = (2 * sizeof (float) + sizeof (int)) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first); float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) { if (!verts) {
return -1; return -1;
@ -1127,10 +1117,7 @@ METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
for (int i = 0; i < count; i++, points++) { for (int i = 0; i < count; i++, points++) {
*(verts++) = points->x; *(verts++) = points->x;
*(verts++) = points->y; *(verts++) = points->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
} }
/* If the line segment is completely horizontal or vertical, /* If the line segment is completely horizontal or vertical,
@ -1142,7 +1129,7 @@ METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
angles. Maybe !!! FIXME for later, though. */ angles. Maybe !!! FIXME for later, though. */
points -= 2; /* update the last line. */ points -= 2; /* update the last line. */
verts -= 2 + 4; verts -= 2 + 1;
const float xstart = points[0].x; const float xstart = points[0].x;
const float ystart = points[0].y; const float ystart = points[0].y;
@ -1161,11 +1148,8 @@ METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
static int static int
METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count) METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
{ {
const float r = cmd->data.draw.r * inv255f; const int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
const float g = cmd->data.draw.g * inv255f; const size_t vertlen = 4 * (2 * sizeof (float) + sizeof (int)) * count;
const float b = cmd->data.draw.b * inv255f;
const float a = cmd->data.draw.a * inv255f;
const size_t vertlen = sizeof (float) * 4 * (2 + 4) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first); float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) { if (!verts) {
return -1; return -1;
@ -1184,31 +1168,19 @@ METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
} else { } else {
*(verts++) = rects->x; *(verts++) = rects->x;
*(verts++) = rects->y + rects->h; *(verts++) = rects->y + rects->h;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = rects->x; *(verts++) = rects->x;
*(verts++) = rects->y; *(verts++) = rects->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = rects->x + rects->w; *(verts++) = rects->x + rects->w;
*(verts++) = rects->y + rects->h; *(verts++) = rects->y + rects->h;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = rects->x + rects->w; *(verts++) = rects->x + rects->w;
*(verts++) = rects->y; *(verts++) = rects->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
} }
} }
@ -1225,13 +1197,9 @@ METAL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * t
{ {
const float texw = (float) texture->w; const float texw = (float) texture->w;
const float texh = (float) texture->h; const float texh = (float) texture->h;
const float r = cmd->data.draw.r * inv255f; const int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
const float g = cmd->data.draw.g * inv255f;
const float b = cmd->data.draw.b * inv255f;
const float a = cmd->data.draw.a * inv255f;
// !!! FIXME: use an index buffer // !!! FIXME: use an index buffer
const size_t vertlen = (sizeof (float) * 4 * (2 + 4 + 2)); const size_t vertlen = 4 * (2 * sizeof (float) + sizeof (int) + 2 * sizeof (float));
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first); float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) { if (!verts) {
return -1; return -1;
@ -1242,37 +1210,25 @@ METAL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * t
/* Interleaved positions and texture coordinates */ /* Interleaved positions and texture coordinates */
*(verts++) = dstrect->x; *(verts++) = dstrect->x;
*(verts++) = dstrect->y + dstrect->h; *(verts++) = dstrect->y + dstrect->h;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = normtex(srcrect->x, texw); *(verts++) = normtex(srcrect->x, texw);
*(verts++) = normtex(srcrect->y + srcrect->h, texh); *(verts++) = normtex(srcrect->y + srcrect->h, texh);
*(verts++) = dstrect->x; *(verts++) = dstrect->x;
*(verts++) = dstrect->y; *(verts++) = dstrect->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = normtex(srcrect->x, texw); *(verts++) = normtex(srcrect->x, texw);
*(verts++) = normtex(srcrect->y, texh); *(verts++) = normtex(srcrect->y, texh);
*(verts++) = dstrect->x + dstrect->w; *(verts++) = dstrect->x + dstrect->w;
*(verts++) = dstrect->y + dstrect->h; *(verts++) = dstrect->y + dstrect->h;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = normtex(srcrect->x + srcrect->w, texw); *(verts++) = normtex(srcrect->x + srcrect->w, texw);
*(verts++) = normtex(srcrect->y + srcrect->h, texh); *(verts++) = normtex(srcrect->y + srcrect->h, texh);
*(verts++) = dstrect->x + dstrect->w; *(verts++) = dstrect->x + dstrect->w;
*(verts++) = dstrect->y; *(verts++) = dstrect->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = normtex(srcrect->x + srcrect->w, texw); *(verts++) = normtex(srcrect->x + srcrect->w, texw);
*(verts++) = normtex(srcrect->y, texh); *(verts++) = normtex(srcrect->y, texh);
@ -1284,16 +1240,13 @@ METAL_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture *
const SDL_Rect * srcquad, const SDL_FRect * dstrect, const SDL_Rect * srcquad, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip) const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
{ {
const float r = cmd->data.draw.r * inv255f; const int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
const float g = cmd->data.draw.g * inv255f;
const float b = cmd->data.draw.b * inv255f;
const float a = cmd->data.draw.a * inv255f;
const float texw = (float) texture->w; const float texw = (float) texture->w;
const float texh = (float) texture->h; const float texh = (float) texture->h;
const float rads = (float)(M_PI * (float) angle / 180.0f); const float rads = (float)(M_PI * (float) angle / 180.0f);
const float c = cosf(rads), s = sinf(rads); const float c = cosf(rads), s = sinf(rads);
float minu, maxu, minv, maxv; float minu, maxu, minv, maxv;
const size_t vertlen = sizeof (float) * (16 + 4 * (2 + 4 + 2)); const size_t vertlen = 16 * sizeof (float) + 4 * (2 * sizeof (float) + sizeof (int) + 2 * sizeof (float));
float *verts; float *verts;
// cheat and store this offset in (count) because it needs to be aligned in ways other fields don't and we aren't using count otherwise. // cheat and store this offset in (count) because it needs to be aligned in ways other fields don't and we aren't using count otherwise.
@ -1340,37 +1293,25 @@ METAL_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture *
/* Interleaved positions and texture coordinates */ /* Interleaved positions and texture coordinates */
*(verts++) = -center->x; *(verts++) = -center->x;
*(verts++) = dstrect->h - center->y; *(verts++) = dstrect->h - center->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = minu; *(verts++) = minu;
*(verts++) = maxv; *(verts++) = maxv;
*(verts++) = -center->x; *(verts++) = -center->x;
*(verts++) = -center->y; *(verts++) = -center->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = minu; *(verts++) = minu;
*(verts++) = minv; *(verts++) = minv;
*(verts++) = dstrect->w - center->x; *(verts++) = dstrect->w - center->x;
*(verts++) = dstrect->h - center->y; *(verts++) = dstrect->h - center->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = maxu; *(verts++) = maxu;
*(verts++) = maxv; *(verts++) = maxv;
*(verts++) = dstrect->w - center->x; *(verts++) = dstrect->w - center->x;
*(verts++) = -center->y; *(verts++) = -center->y;
*(verts++) = r; *((int *)verts++) = color;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*(verts++) = maxu; *(verts++) = maxu;
*(verts++) = minv; *(verts++) = minv;
@ -1384,8 +1325,7 @@ METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture
float scale_x, float scale_y) float scale_x, float scale_y)
{ {
int count = indices ? num_indices : num_vertices; int count = indices ? num_indices : num_vertices;
int sz = 2 + 4 + (texture ? 2 : 0); const size_t vertlen = (2 * sizeof (float) + sizeof (int) + (texture ? 2 : 0) * sizeof (float)) * count;
const size_t vertlen = sizeof (float) * sz * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first); float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) { if (!verts) {
return -1; return -1;
@ -1406,15 +1346,12 @@ METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture
} }
float *xy_ = (float *)((char*)xy + j * xy_stride); float *xy_ = (float *)((char*)xy + j * xy_stride);
SDL_Color col_ = *(SDL_Color *)((char*)color + j * color_stride); int col_ = *(int *)((char*)color + j * color_stride);
*(verts++) = xy_[0] * scale_x; *(verts++) = xy_[0] * scale_x;
*(verts++) = xy_[1] * scale_y; *(verts++) = xy_[1] * scale_y;
*(verts++) = col_.r * inv255f; *((int *)verts++) = col_;
*(verts++) = col_.g * inv255f;
*(verts++) = col_.b * inv255f;
*(verts++) = col_.a * inv255f;
if (texture) { if (texture) {
float *uv_ = (float *)((char*)uv + j * uv_stride); float *uv_ = (float *)((char*)uv + j * uv_stride);