Skip to content

Commit

Permalink
8305164: Replace MTLTexture.replaceRegion call with drawing a Rect
Browse files Browse the repository at this point in the history
  • Loading branch information
arapte committed Mar 29, 2023
1 parent ecfc882 commit 89cd35a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
Expand Up @@ -76,7 +76,6 @@ typedef enum VertexInputIndex {

MTLScissorRect scissorRect;
bool isScissorRectSet;
id<MTLBuffer> clearBuffer;
MetalRTTexture* rtt;
bool rttCleared;
int rttClearColor;
Expand Down
49 changes: 18 additions & 31 deletions modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.m
Expand Up @@ -98,18 +98,6 @@ - (void) setRTT:(MetalRTTexture*)rttPtr
rtt = rttPtr;
CTX_LOG(@"-> Native: MetalContext.setRTT() %lu , %lu",
[rtt getTexture].width, [rtt getTexture].height);

// TODO: MTL: This is temporary to support replaceRegion call in clearRTT()
// It will be removed when we remove replaceRegion call.
if (clearBuffer == nil) {
int length = [rtt getTexture].width * [rtt getTexture].height;
clearBuffer = [device newBufferWithLength:(length * 4)
options:MTLResourceStorageModeShared];
int* pixels = [clearBuffer contents];
for (int i = 0; i < length; i++) {
*pixels++ = rttClearColor;
}
}
rttPassDesc.colorAttachments[0].texture = [rtt getTexture];
[self resetClip];
}
Expand Down Expand Up @@ -327,28 +315,27 @@ - (void) clearRTT:(int)color red:(float)red green:(float)green blue:(float)blue
} else if (isScissorRectSet) {
CTX_LOG(@" MetalContext.clearRTT() scissorRect.x = %lu, scissorRect.y = %lu, scissorRect.width = %lu, scissorRect.height = %lu, color = %u",
scissorRect.x, scissorRect.y, scissorRect.width, scissorRect.height, color);

MTLRegion region = MTLRegionMake2D(scissorRect.x, scissorRect.y,
scissorRect.width, scissorRect.height);
CTX_LOG(@" MetalContext.clearRTT() %lu , %lu", [rtt getTexture].width, [rtt getTexture].height);

if (rttClearColor != color) {
rttClearColor = color;
int* pixels = [clearBuffer contents];
int length = [rtt getTexture].width * [rtt getTexture].height;
for (int i = 0; i < length; i++) {
*pixels++ = rttClearColor;
}
}
id<MTLRenderPipelineState> pipeState = currentPipeState;
currentPipeState = nil;

struct PrismSourceVertex scissorRectVertices[4] = {
{scissorRect.x, scissorRect.y, 0, 0, 0, 0},
{scissorRect.x + scissorRect.width, scissorRect.y, 0, 0, 0, 0},
{scissorRect.x, scissorRect.y + scissorRect.height, 0, 0, 0, 0},
{scissorRect.x + scissorRect.width, scissorRect.y + scissorRect.height, 0, 0, 0, 0}
};

char r = red * 0xFF;
char g = green * 0xFF;
char b = blue * 0xFF;
char a = alpha * 0xFF;
char colors[] = {r, g, b, a, r, g, b, a, r, g, b, a, r, g, b, a, r, g, b, a, r, g, b, a};

[self drawIndexedQuads:scissorRectVertices ofColors:colors vertexCount:4];

// TODO: MTL: replaceRegion is a temporary fix for clearing the clipRect in rtt.
// It should be changed to drawing a rect of size scissorRect, with all vertices
// having rttClearColor as color. and should clear the depth buffer too.
// When removing this code, clearBuffer should be removed.
[[rtt getTexture] replaceRegion:region
mipmapLevel:0
withBytes:[clearBuffer contents]
bytesPerRow:(scissorRect.width * 4)];
currentPipeState = pipeState;
}
CTX_LOG(@"<<<< MetalContext.clearRTT()");
}
Expand Down

0 comments on commit 89cd35a

Please sign in to comment.