Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8261703: Many java/awt/ tests fail with Metal API Validation Stencil …
…PixelFormat error
  • Loading branch information
jayathirthrao committed Feb 17, 2021
1 parent 946a51c commit 22b1c087dd749047cb92e162d3279e1045a56858
@@ -68,6 +68,10 @@
isSrcOpaque:(bool)isSrcOpaque
isDstOpaque:(bool)isDstOpaque;

- (id<MTLRenderCommandEncoder> _Nonnull) getLCDEncoder:(id<MTLTexture> _Nonnull)dest
isSrcOpaque:(bool)isSrcOpaque
isDstOpaque:(bool)isDstOpaque;

- (id<MTLRenderCommandEncoder> _Nonnull)getTextureEncoder:(id<MTLTexture> _Nonnull)dest
isSrcOpaque:(bool)isSrcOpaque
isDstOpaque:(bool)isDstOpaque
@@ -44,14 +44,16 @@ - (void)reset:(id<MTLTexture>)destination
isDstOpaque:(jboolean)isDstOpaque
isDstPremultiplied:(jboolean)isDstPremultiplied
isAA:(jboolean)isAA
isText:(jboolean)isText;
isText:(jboolean)isText
isLCD:(jboolean)isLCD;

- (void)updateEncoder:(id<MTLRenderCommandEncoder>)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
forceUpdate:(jboolean)forceUpdate;
@property (assign) jboolean aa;
@property (assign) jboolean text;
@property (assign) jboolean lcd;
@property (retain) MTLPaint* paint;
@end

@@ -65,6 +67,7 @@ @implementation EncoderStates {

jboolean _isAA;
jboolean _isText;
jboolean _isLCD;

//
// Cached 'mutable' states of encoder
@@ -89,6 +92,7 @@ @implementation EncoderStates {
}
@synthesize aa = _isAA;
@synthesize text = _isText;
@synthesize lcd = _isLCD;
@synthesize paint = _paint;

- (id)init {
@@ -119,12 +123,14 @@ - (void)reset:(id<MTLTexture>)destination
isDstOpaque:(jboolean)isDstOpaque
isDstPremultiplied:(jboolean)isDstPremultiplied
isAA:(jboolean)isAA
isText:(jboolean)isText {
isText:(jboolean)isText
isLCD:(jboolean)isLCD {
_destination = destination;
_dstFlags.isOpaque = isDstOpaque;
_dstFlags.isPremultiplied = isDstPremultiplied;
_isAA = isAA;
_isText = isText;
_isLCD = isLCD;
// NOTE: probably it's better to invalidate/reset all cached states now
}

@@ -173,6 +179,7 @@ - (void)updatePipelineState:(id<MTLRenderCommandEncoder>)encoder
&& (_isTexture == renderOptions->isTexture && (!renderOptions->isTexture || _interpolationMode == renderOptions->interpolation)) // interpolation is used only in texture mode
&& _isAA == renderOptions->isAA
&& _isText == renderOptions->isText
&& _isLCD == renderOptions->isLCD
&& _srcFlags.isOpaque == renderOptions->srcFlags.isOpaque && _srcFlags.isPremultiplied == renderOptions->srcFlags.isPremultiplied)
return;

@@ -182,6 +189,7 @@ - (void)updatePipelineState:(id<MTLRenderCommandEncoder>)encoder
_interpolationMode = renderOptions->interpolation;
_isAA = renderOptions->isAA;
_isText = renderOptions->isText;
_isLCD = renderOptions->isLCD;
_srcFlags = renderOptions->srcFlags;

if ((jint)[mtlc.composite getCompositeState] == sun_java2d_SunGraphics2D_COMP_XOR) {
@@ -275,14 +283,14 @@ - (void)setContext:(MTLContex * _Nonnull)mtlc {

- (id<MTLRenderCommandEncoder> _Nonnull)getAARenderEncoder:(const BMTLSDOps * _Nonnull)dstOps {
id<MTLTexture> dstTxt = dstOps->pTexture;
RenderOptions roptions = {JNI_FALSE, JNI_TRUE, INTERPOLATION_NEAREST_NEIGHBOR, defaultRasterFlags, {dstOps->isOpaque, JNI_TRUE}, JNI_FALSE};
RenderOptions roptions = {JNI_FALSE, JNI_TRUE, INTERPOLATION_NEAREST_NEIGHBOR, defaultRasterFlags, {dstOps->isOpaque, JNI_TRUE}, JNI_FALSE, JNI_FALSE};
return [self getEncoder:dstTxt renderOptions:&roptions];
}

- (id<MTLRenderCommandEncoder> _Nonnull)getRenderEncoder:(id<MTLTexture> _Nonnull)dest
isDstOpaque:(bool)isOpaque
{
RenderOptions roptions = {JNI_FALSE, JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR, defaultRasterFlags, {isOpaque, JNI_TRUE}, JNI_FALSE};
RenderOptions roptions = {JNI_FALSE, JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR, defaultRasterFlags, {isOpaque, JNI_TRUE}, JNI_FALSE, JNI_FALSE};
return [self getEncoder:dest renderOptions:&roptions];
}

@@ -306,13 +314,21 @@ - (void)setContext:(MTLContex * _Nonnull)mtlc {
isAA:JNI_FALSE];
}

- (id<MTLRenderCommandEncoder> _Nonnull) getLCDEncoder:(id<MTLTexture> _Nonnull)dest
isSrcOpaque:(bool)isSrcOpaque
isDstOpaque:(bool)isDstOpaque
{
RenderOptions roptions = {JNI_TRUE, JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR, {isSrcOpaque, JNI_TRUE }, {isDstOpaque, JNI_TRUE}, JNI_FALSE, JNI_TRUE};
return [self getEncoder:dest renderOptions:&roptions];
}

- (id<MTLRenderCommandEncoder> _Nonnull) getTextureEncoder:(id<MTLTexture> _Nonnull)dest
isSrcOpaque:(bool)isSrcOpaque
isDstOpaque:(bool)isDstOpaque
interpolation:(int)interpolation
isAA:(jboolean)isAA
{
RenderOptions roptions = {JNI_TRUE, isAA, interpolation, { isSrcOpaque, JNI_TRUE }, {isDstOpaque, JNI_TRUE}, JNI_FALSE};
RenderOptions roptions = {JNI_TRUE, isAA, interpolation, { isSrcOpaque, JNI_TRUE }, {isDstOpaque, JNI_TRUE}, JNI_FALSE, JNI_FALSE};
return [self getEncoder:dest renderOptions:&roptions];
}

@@ -327,7 +343,7 @@ - (void)setContext:(MTLContex * _Nonnull)mtlc {
- (id<MTLRenderCommandEncoder> _Nonnull) getTextEncoder:(const BMTLSDOps * _Nonnull)dstOps
isSrcOpaque:(bool)isSrcOpaque
{
RenderOptions roptions = {JNI_TRUE, JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR, { isSrcOpaque, JNI_TRUE }, {dstOps->isOpaque, JNI_TRUE}, JNI_TRUE};
RenderOptions roptions = {JNI_TRUE, JNI_FALSE, INTERPOLATION_NEAREST_NEIGHBOR, { isSrcOpaque, JNI_TRUE }, {dstOps->isOpaque, JNI_TRUE}, JNI_TRUE, JNI_FALSE};
return [self getEncoder:dstOps->pTexture renderOptions:&roptions];
}

@@ -407,7 +423,8 @@ - (void)setContext:(MTLContex * _Nonnull)mtlc {
isDstOpaque:renderOptions->dstFlags.isOpaque
isDstPremultiplied:YES
isAA:renderOptions->isAA
isText:renderOptions->isText];
isText:renderOptions->isText
isLCD:renderOptions->isLCD];
}

//
@@ -52,6 +52,7 @@
static MTLRenderPipelineDescriptor * templateRenderPipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateTexturePipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateAATexturePipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateLCDPipelineDesc = nil;
static void setTxtUniforms(
id<MTLRenderCommandEncoder> encoder, int color, int mode, int interpolation, bool repeat, jfloat extraAlpha,
const SurfaceRasterFlags * srcFlags, const SurfaceRasterFlags * dstFlags
@@ -93,6 +94,17 @@ static void initTemplatePipelineDescriptors() {
templateAATexturePipelineDesc = [templateTexturePipelineDesc copy];
templateAATexturePipelineDesc.label = @"template_aa_texture";

templateLCDPipelineDesc = [MTLRenderPipelineDescriptor new];
templateLCDPipelineDesc.sampleCount = 1;
templateLCDPipelineDesc.vertexDescriptor = vertDesc;
templateLCDPipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].format = MTLVertexFormatFloat2;
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].offset = 2*sizeof(float);
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].bufferIndex = MeshVertexBuffer;
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stride = sizeof(struct TxtVertex);
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepRate = 1;
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;
templateLCDPipelineDesc.label = @"template_lcd";
}


@@ -160,6 +172,11 @@ - (void)setPipelineState:(id<MTLRenderCommandEncoder>)encoder
if (renderOptions->isText) {
fragShader = @"frag_text";
}
if (renderOptions->isLCD) {
vertShader = @"vert_txt_lcd";
fragShader = @"lcd_color";
rpDesc = [[templateLCDPipelineDesc copy] autorelease];
}
setTxtUniforms(encoder, _color, 1,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha], &renderOptions->srcFlags,
&renderOptions->dstFlags);
@@ -84,7 +84,7 @@ - (NSPointerArray * ) getSubStates:(NSString *)vertexShaderId fragmentShader:(NS
vertexShaderId:(NSString *)vertexShaderId
fragmentShaderId:(NSString *)fragmentShaderId
{
RenderOptions defaultOptions = {JNI_FALSE, JNI_FALSE, 0/*unused*/, {JNI_FALSE, JNI_TRUE}, {JNI_FALSE, JNI_TRUE}, JNI_FALSE};
RenderOptions defaultOptions = {JNI_FALSE, JNI_FALSE, 0/*unused*/, {JNI_FALSE, JNI_TRUE}, {JNI_FALSE, JNI_TRUE}, JNI_FALSE, JNI_FALSE};
return [self getPipelineState:pipelineDescriptor
vertexShaderId:vertexShaderId
fragmentShaderId:fragmentShaderId
@@ -98,7 +98,7 @@ - (NSPointerArray * ) getSubStates:(NSString *)vertexShaderId fragmentShader:(NS
fragmentShaderId:(NSString *)fragmentShaderId
stencilNeeded:(bool)stencilNeeded
{
RenderOptions defaultOptions = {JNI_FALSE, JNI_FALSE, 0/*unused*/, {JNI_FALSE, JNI_TRUE}, {JNI_FALSE, JNI_TRUE}, JNI_FALSE};
RenderOptions defaultOptions = {JNI_FALSE, JNI_FALSE, 0/*unused*/, {JNI_FALSE, JNI_TRUE}, {JNI_FALSE, JNI_TRUE}, JNI_FALSE, JNI_FALSE};
return [self getPipelineState:pipelineDescriptor
vertexShaderId:vertexShaderId
fragmentShaderId:fragmentShaderId
@@ -247,123 +247,10 @@
}
}

static MTLRenderPipelineDescriptor * templateLCDPipelineDesc = nil;

/**
* Enables the LCD text shader and updates any related state, such as the
* gamma lookup table textures.
*/
static jboolean
MTLTR_EnableLCDGlyphModeState(id<MTLRenderCommandEncoder> encoder,
MTLContext *mtlc,
MTLSDOps *dstOps,
jint contrast)
{
if (![mtlc.paint isKindOfClass:[MTLColorPaint class]]) {
return JNI_FALSE;
}
MTLColorPaint* cPaint = (MTLColorPaint *) mtlc.paint;
// create the LCD text shader, if necessary
if (templateLCDPipelineDesc == nil) {

MTLVertexDescriptor *vertDesc = [[MTLVertexDescriptor new] autorelease];
vertDesc.attributes[VertexAttributePosition].format = MTLVertexFormatFloat2;
vertDesc.attributes[VertexAttributePosition].offset = 0;
vertDesc.attributes[VertexAttributePosition].bufferIndex = MeshVertexBuffer;
vertDesc.layouts[MeshVertexBuffer].stride = sizeof(struct Vertex);
vertDesc.layouts[MeshVertexBuffer].stepRate = 1;
vertDesc.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;

templateLCDPipelineDesc = [MTLRenderPipelineDescriptor new];
templateLCDPipelineDesc.sampleCount = 1;
templateLCDPipelineDesc.vertexDescriptor = vertDesc;
templateLCDPipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].format = MTLVertexFormatFloat2;
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].offset = 2*sizeof(float);
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].bufferIndex = MeshVertexBuffer;
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stride = sizeof(struct TxtVertex);
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepRate = 1;
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;
templateLCDPipelineDesc.label = @"template_lcd";
}

id<MTLRenderPipelineState> pipelineState =
[mtlc.pipelineStateStorage
getPipelineState:templateLCDPipelineDesc
vertexShaderId:@"vert_txt_lcd"
fragmentShaderId:@"lcd_color"
];

[encoder setRenderPipelineState:pipelineState];

// update the current color settings
double gamma = ((double)contrast) / 100.0;
double invgamma = 1.0/gamma;
jfloat radj, gadj, badj;
jfloat clr[4];
jint col = cPaint.color;

J2dTraceLn2(J2D_TRACE_INFO, "primary color %x, contrast %d", col, contrast);
J2dTraceLn2(J2D_TRACE_INFO, "gamma %f, invgamma %f", gamma, invgamma);

clr[0] = ((col >> 16) & 0xFF)/255.0f;
clr[1] = ((col >> 8) & 0xFF)/255.0f;
clr[2] = ((col) & 0xFF)/255.0f;

// gamma adjust the primary color
radj = (float)pow(clr[0], gamma);
gadj = (float)pow(clr[1], gamma);
badj = (float)pow(clr[2], gamma);

struct LCDFrameUniforms uf = {
{radj, gadj, badj},
{gamma, gamma, gamma},
{invgamma, invgamma, invgamma}};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];

return JNI_TRUE;
}

static jboolean
MTLTR_SetLCDCachePipelineState(MTLContext *mtlc)
{
if (templateLCDPipelineDesc == nil) {

MTLVertexDescriptor *vertDesc = [[MTLVertexDescriptor new] autorelease];
vertDesc.attributes[VertexAttributePosition].format = MTLVertexFormatFloat2;
vertDesc.attributes[VertexAttributePosition].offset = 0;
vertDesc.attributes[VertexAttributePosition].bufferIndex = MeshVertexBuffer;
vertDesc.layouts[MeshVertexBuffer].stride = sizeof(struct Vertex);
vertDesc.layouts[MeshVertexBuffer].stepRate = 1;
vertDesc.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;

templateLCDPipelineDesc = [MTLRenderPipelineDescriptor new];
templateLCDPipelineDesc.sampleCount = 1;
templateLCDPipelineDesc.vertexDescriptor = vertDesc;
templateLCDPipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].format = MTLVertexFormatFloat2;
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].offset = 2*sizeof(float);
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].bufferIndex = MeshVertexBuffer;
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stride = sizeof(struct TxtVertex);
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepRate = 1;
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;
templateLCDPipelineDesc.label = @"template_lcd";
}

id<MTLRenderPipelineState> pipelineState =
[mtlc.pipelineStateStorage
getPipelineState:templateLCDPipelineDesc
vertexShaderId:@"vert_txt_lcd"
fragmentShaderId:@"lcd_color"
];

[lcdCacheEncoder setRenderPipelineState:pipelineState];
return JNI_TRUE;
}

static jboolean
MTLTR_SetLCDContrast(MTLContext *mtlc,
jint contrast)
jint contrast,
id<MTLRenderCommandEncoder> encoder)
{
if (![mtlc.paint isKindOfClass:[MTLColorPaint class]]) {
return JNI_FALSE;
@@ -392,7 +279,7 @@
{radj, gadj, badj},
{gamma, gamma, gamma},
{invgamma, invgamma, invgamma}};
[lcdCacheEncoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
return JNI_TRUE;
}

@@ -504,8 +391,7 @@ void MTLTR_FreeGlyphCaches() {
}
}
if (lcdCacheEncoder == nil) {
lcdCacheEncoder = [mtlc.encoderManager getTextureEncoder:dstOps->pTexture isSrcOpaque:YES isDstOpaque:YES];
MTLTR_SetLCDCachePipelineState(mtlc);
lcdCacheEncoder = [mtlc.encoderManager getLCDEncoder:dstOps->pTexture isSrcOpaque:YES isDstOpaque:YES];
}
if (rgbOrder != lastRGBOrder) {
// need to invalidate the cache in this case; see comments
@@ -530,7 +416,7 @@ void MTLTR_FreeGlyphCaches() {
cell = (CacheCellInfo *) (ginfo->cellInfo);
cell->timesRendered++;

MTLTR_SetLCDContrast(mtlc, contrast);
MTLTR_SetLCDContrast(mtlc, contrast, lcdCacheEncoder);
tx1 = cell->tx1;
ty1 = cell->ty1;
tx2 = cell->tx2;
@@ -641,11 +527,8 @@ void MTLTR_FreeGlyphCaches() {

glyphMode = MODE_NO_CACHE_LCD;
}
encoder = [mtlc.encoderManager getTextureEncoder:dstOps->pTexture isSrcOpaque:YES isDstOpaque:YES];
if (!MTLTR_EnableLCDGlyphModeState(encoder, mtlc, dstOps,contrast))
{
return JNI_FALSE;
}
encoder = [mtlc.encoderManager getLCDEncoder:dstOps->pTexture isSrcOpaque:YES isDstOpaque:YES];
MTLTR_SetLCDContrast(mtlc, contrast, encoder);

unsigned int imageBytes = w * h *4;
unsigned char imageData[imageBytes];
@@ -37,6 +37,7 @@ typedef struct {
SurfaceRasterFlags srcFlags;
SurfaceRasterFlags dstFlags;
jboolean isText;
jboolean isLCD;
} RenderOptions;


0 comments on commit 22b1c08

Please sign in to comment.