Skip to content

Commit

Permalink
8293703: Add 24-bit color texture support for metal pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
aghaisas committed Sep 14, 2022
1 parent 790998b commit 604a431
Showing 1 changed file with 36 additions and 2 deletions.
Expand Up @@ -91,7 +91,10 @@ native private static void nUpdate(long contextHandle, long pResource,
@Override
public void update(Buffer buffer, PixelFormat format, int dstx, int dsty, int srcx, int srcy, int srcw, int srch, int srcscan, boolean skipFlush) {

// TODO: MTL: Copy according to PixelFormat - this works for RGBA format as of now
// ------------------------------------------------------------------------------
// TODO: MTL: Copy according to PixelFormat - this works for BGRA & RGB formats as of now
// ------------------------------------------------------------------------------

ByteBuffer buf = (ByteBuffer)buffer;
byte[] arr = buf.hasArray()? buf.array(): null;

Expand All @@ -100,7 +103,38 @@ public void update(Buffer buffer, PixelFormat format, int dstx, int dsty, int sr
buf.get(arr);
}

nUpdate(this.context.getContextHandle(), /*MetalTexture*/this.getNativeHandle(), arr, dstx, dsty, srcx, srcy, srcw, srch, srcscan);
// TODO: MTL: Implement cases for other PixelFormats
switch(format) {
case BYTE_RGB: {
// Metal does not support 24-bit format
// hence `arr` data needs to be converted to BGRA format that
// the native metal texture expects
byte[] arr32Bit = new byte[srcw * srch * 4];
int dstIndex = 0;
int index = 0;

final int rowStride = srcw * 3;
final int totalBytes = srch * rowStride;

for (int rowIndex = 0; rowIndex < totalBytes; rowIndex += rowStride) {
for (int colIndex = 0; colIndex < rowStride; colIndex += 3) {
index = rowIndex + colIndex;
arr32Bit[dstIndex++] = arr[index+2];
arr32Bit[dstIndex++] = arr[index+1];
arr32Bit[dstIndex++] = arr[index];
arr32Bit[dstIndex++] = (byte)255;
}
}

nUpdate(this.context.getContextHandle(), /*MetalTexture*/this.getNativeHandle(), arr32Bit, dstx, dsty, srcx, srcy, srcw, srch, srcw*4);
}
break;

default:
// assume that `arr` data is in BGRA format
nUpdate(this.context.getContextHandle(), /*MetalTexture*/this.getNativeHandle(), arr, dstx, dsty, srcx, srcy, srcw, srch, srcscan);
break;
}
}

@Override
Expand Down

0 comments on commit 604a431

Please sign in to comment.