METAL: use Uchar4Normalized format to transfert color as 4 bytes, instead of 4 floats
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
diff --git a/src/render/metal/SDL_render_metal.m b/src/render/metal/SDL_render_metal.m
index 2b90288..ace1f5e 100644
--- a/src/render/metal/SDL_render_metal.m
+++ b/src/render/metal/SDL_render_metal.m
@@ -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);