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

This commit is contained in:
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 MTLBlendFactor invalidBlendFactor = (MTLBlendFactor)0xFFFFFFFF;
static const float inv255f = 1.0f / 255.0f;
static MTLBlendOperation
GetBlendOperation(SDL_BlendOperation operation)
{
@ -281,34 +279,34 @@ MakePipelineState(METAL_RenderData *data, METAL_PipelineCache *cache,
switch (cache->vertexFunction) {
case SDL_METAL_VERTEX_SOLID:
/* position (float2), color (float4) */
vertdesc.layouts[0].stride = sizeof(float) * (2 + 4);
/* position (float2), color (uchar4normalized) */
vertdesc.layouts[0].stride = sizeof(float) * 2 + sizeof (int);
vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
vertdesc.attributes[0].format = MTLVertexFormatFloat2;
vertdesc.attributes[0].offset = 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].bufferIndex = 0;
break;
case SDL_METAL_VERTEX_COPY:
/* position (float2), color (float4), texcoord (float2) */
vertdesc.layouts[0].stride = sizeof(float) * (2 + 4 + 2);
/* position (float2), color (uchar4normalized), texcoord (float2) */
vertdesc.layouts[0].stride = sizeof(float) * 2 + sizeof (int) + sizeof (float) * 2;
vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
vertdesc.attributes[0].format = MTLVertexFormatFloat2;
vertdesc.attributes[0].offset = 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].bufferIndex = 0;
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;
break;
}
@ -1085,11 +1083,8 @@ METAL_QueueSetDrawColor(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
static int
METAL_QueueDrawPoints(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 float b = cmd->data.draw.b * inv255f;
const float a = cmd->data.draw.a * inv255f;
const size_t vertlen = sizeof (float) * (2 + 4) * count;
const int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
const size_t vertlen = (2 * sizeof (float) + sizeof (int)) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) {
return -1;
@ -1099,10 +1094,7 @@ METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL
for (int i = 0; i < count; i++, points++) {
*(verts++) = points->x;
*(verts++) = points->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
}
return 0;
}
@ -1110,14 +1102,12 @@ METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL
static int
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 float b = cmd->data.draw.b * inv255f;
const float a = cmd->data.draw.a * inv255f;
const int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
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);
if (!verts) {
return -1;
@ -1127,10 +1117,7 @@ METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
for (int i = 0; i < count; i++, points++) {
*(verts++) = points->x;
*(verts++) = points->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
}
/* 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. */
points -= 2; /* update the last line. */
verts -= 2 + 4;
verts -= 2 + 1;
const float xstart = points[0].x;
const float ystart = points[0].y;
@ -1161,11 +1148,8 @@ METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
static int
METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
{
const float r = cmd->data.draw.r * inv255f;
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 size_t vertlen = sizeof (float) * 4 * (2 + 4) * count;
const int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
const size_t vertlen = 4 * (2 * sizeof (float) + sizeof (int)) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) {
return -1;
@ -1184,31 +1168,19 @@ METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_
} else {
*(verts++) = rects->x;
*(verts++) = rects->y + rects->h;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = rects->x;
*(verts++) = rects->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = rects->x + rects->w;
*(verts++) = rects->y + rects->h;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = rects->x + rects->w;
*(verts++) = rects->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
}
}
@ -1225,13 +1197,9 @@ METAL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * t
{
const float texw = (float) texture->w;
const float texh = (float) texture->h;
const float r = cmd->data.draw.r * inv255f;
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 int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
// !!! 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);
if (!verts) {
return -1;
@ -1242,37 +1210,25 @@ METAL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * t
/* Interleaved positions and texture coordinates */
*(verts++) = dstrect->x;
*(verts++) = dstrect->y + dstrect->h;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = normtex(srcrect->x, texw);
*(verts++) = normtex(srcrect->y + srcrect->h, texh);
*(verts++) = dstrect->x;
*(verts++) = dstrect->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = normtex(srcrect->x, texw);
*(verts++) = normtex(srcrect->y, texh);
*(verts++) = dstrect->x + dstrect->w;
*(verts++) = dstrect->y + dstrect->h;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = normtex(srcrect->x + srcrect->w, texw);
*(verts++) = normtex(srcrect->y + srcrect->h, texh);
*(verts++) = dstrect->x + dstrect->w;
*(verts++) = dstrect->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = normtex(srcrect->x + srcrect->w, texw);
*(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 double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
{
const float r = cmd->data.draw.r * inv255f;
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 int color = (cmd->data.draw.r << 0) | (cmd->data.draw.g << 8) | (cmd->data.draw.b << 16) | (cmd->data.draw.a << 24);
const float texw = (float) texture->w;
const float texh = (float) texture->h;
const float rads = (float)(M_PI * (float) angle / 180.0f);
const float c = cosf(rads), s = sinf(rads);
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;
// 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 */
*(verts++) = -center->x;
*(verts++) = dstrect->h - center->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = minu;
*(verts++) = maxv;
*(verts++) = -center->x;
*(verts++) = -center->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = minu;
*(verts++) = minv;
*(verts++) = dstrect->w - center->x;
*(verts++) = dstrect->h - center->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = maxu;
*(verts++) = maxv;
*(verts++) = dstrect->w - center->x;
*(verts++) = -center->y;
*(verts++) = r;
*(verts++) = g;
*(verts++) = b;
*(verts++) = a;
*((int *)verts++) = color;
*(verts++) = maxu;
*(verts++) = minv;
@ -1384,8 +1325,7 @@ METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture
float scale_x, float scale_y)
{
int count = indices ? num_indices : num_vertices;
int sz = 2 + 4 + (texture ? 2 : 0);
const size_t vertlen = sizeof (float) * sz * count;
const size_t vertlen = (2 * sizeof (float) + sizeof (int) + (texture ? 2 : 0) * sizeof (float)) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) {
return -1;
@ -1406,15 +1346,12 @@ METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture
}
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_[1] * scale_y;
*(verts++) = col_.r * inv255f;
*(verts++) = col_.g * inv255f;
*(verts++) = col_.b * inv255f;
*(verts++) = col_.a * inv255f;
*((int *)verts++) = col_;
if (texture) {
float *uv_ = (float *)((char*)uv + j * uv_stride);