Skip to content
This repository has been archived by the owner. It is now read-only.

8262496: Lanai: Do not use resources of off-line MTLDevice #205

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -361,6 +361,12 @@ jboolean clipDestCoords(
RETURN_IF_NULL(mtlc);
RETURN_IF_NULL(srcOps);
RETURN_IF_NULL(dstOps);
// Verify if we use a valid MTLContext
MTLSDOps *dstMTLOps = (MTLSDOps *)dstOps->privOps;
RETURN_IF_TRUE(dstMTLOps->configInfo != NULL && mtlc != dstMTLOps->configInfo->context);

MTLSDOps *srcMTLOps = (MTLSDOps *)srcOps->privOps;
RETURN_IF_TRUE(srcMTLOps->configInfo != NULL && mtlc != srcMTLOps->configInfo->context);

id<MTLTexture> srcTex = srcOps->pTexture;
id<MTLTexture> dstTex = dstOps->pTexture;
@@ -484,6 +490,9 @@ jboolean clipDestCoords(
RETURN_IF_NULL(mtlc);
RETURN_IF_NULL(srcOps);
RETURN_IF_NULL(dstOps);
// Verify if we use a valid MTLContext
MTLSDOps *dstMTLOps = (MTLSDOps *)dstOps->privOps;
RETURN_IF_TRUE(dstMTLOps->configInfo != NULL && mtlc != dstMTLOps->configInfo->context);

id<MTLTexture> dest = dstOps->pTexture;
if (dest == NULL) {
@@ -26,10 +26,10 @@
#include "MTLClip.h"

#include "MTLContext.h"
#include "MTLStencilManager.h"
#include "common.h"

static MTLRenderPipelineDescriptor * templateStencilPipelineDesc = nil;
static id<MTLDepthStencilState> stencilState = nil;

static void initTemplatePipelineDescriptors() {
if (templateStencilPipelineDesc != nil)
@@ -50,27 +50,6 @@ static void initTemplatePipelineDescriptors() {
templateStencilPipelineDesc.label = @"template_stencil";
}

static id<MTLDepthStencilState> getStencilState(id<MTLDevice> device) {
if (stencilState == nil) {
MTLDepthStencilDescriptor* stencilDescriptor;
stencilDescriptor = [[MTLDepthStencilDescriptor new] autorelease];
stencilDescriptor.frontFaceStencil.stencilCompareFunction = MTLCompareFunctionEqual;
stencilDescriptor.frontFaceStencil.stencilFailureOperation = MTLStencilOperationKeep;

// TODO : backFaceStencil can be set to nil if all primitives are drawn as front-facing primitives
// currently, fill parallelogram uses back-facing primitive drawing - that needs to be changed.
// Once that part is changed, set backFaceStencil to nil
//stencilDescriptor.backFaceStencil = nil;

stencilDescriptor.backFaceStencil.stencilCompareFunction = MTLCompareFunctionEqual;
stencilDescriptor.backFaceStencil.stencilFailureOperation = MTLStencilOperationKeep;

stencilState = [device newDepthStencilStateWithDescriptor:stencilDescriptor];
}

return stencilState;
}

@implementation MTLClip {
jint _clipType;
MTLScissorRect _clipRect;
@@ -128,6 +107,7 @@ - (void)copyFrom:(MTLClip *)other {
_clipType = other->_clipType;
_stencilMaskGenerationInProgress = other->_stencilMaskGenerationInProgress;
_dstOps = other->_dstOps;
_mtlc = other->_mtlc;
if (other->_clipType == RECT_CLIP) {
_clipRect = other->_clipRect;
}
@@ -138,12 +118,6 @@ - (void)reset {
_stencilMaskGenerationInProgress = JNI_FALSE;
}

- (void)resetStencilState {
if (stencilState != nil) {
[stencilState release];
stencilState = nil;
}
}

- (void)setClipRectX1:(jint)x1 Y1:(jint)y1 X2:(jint)x2 Y2:(jint)y2 {
if (_clipType == SHAPE_CLIP) {
@@ -308,7 +282,7 @@ - (void)setScissorOrStencil:(id<MTLRenderCommandEncoder>)encoder

if (_clipType == SHAPE_CLIP) {
// Enable stencil test
[encoder setDepthStencilState:getStencilState(device)];
[encoder setDepthStencilState:_mtlc.stencilManager.stencilState];
[encoder setStencilReferenceValue:0xFF];
}
}
@@ -38,6 +38,9 @@
#include "MTLPaints.h"
#include "MTLClip.h"
#include "EncoderManager.h"
#include "MTLSamplerManager.h"

@class MTLStencilManager;

// Constant from
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
@@ -74,6 +77,8 @@
@property (strong) id<MTLBuffer> vertexBuffer;

@property (readonly) EncoderManager * encoderManager;
@property (readonly) MTLSamplerManager * samplerManager;
@property (readonly) MTLStencilManager * stencilManager;

@property (strong)MTLPipelineStatesStorage* pipelineStateStorage;
@property (strong)MTLTexturePool* texturePool;
@@ -30,6 +30,8 @@
#include "jlong.h"
#import "MTLContext.h"
#include "MTLRenderQueue.h"
#import "MTLSamplerManager.h"
#import "MTLStencilManager.h"


extern jboolean MTLSD_InitMTLWindow(JNIEnv *env, MTLSDOps *mtlsdo);
@@ -108,12 +110,15 @@ @implementation MTLContext {
NSObject* _bufImgOp; // TODO: pass as parameter of IsoBlit

EncoderManager * _encoderManager;
MTLSamplerManager * _samplerManager;
MTLStencilManager * _stencilManager;
}

@synthesize textureFunction,
vertexCacheEnabled, aaEnabled, device, pipelineStateStorage,
commandQueue, blitCommandQueue, vertexBuffer,
texturePool, paint=_paint;
texturePool, paint=_paint, encoderManager=_encoderManager,
samplerManager=_samplerManager, stencilManager=_stencilManager;

extern void initSamplers(id<MTLDevice> device);

@@ -137,6 +142,8 @@ - (id)initWithDevice:(id<MTLDevice>)d shadersLib:(NSString*)shadersLib {

_encoderManager = [[EncoderManager alloc] init];
[_encoderManager setContext:self];
_samplerManager = [[MTLSamplerManager alloc] initWithDevice:device];
_stencilManager = [[MTLStencilManager alloc] initWithDevice:device];
_composite = [[MTLComposite alloc] init];
_paint = [[MTLPaint alloc] init];
_transform = [[MTLTransform alloc] init];
@@ -150,8 +157,6 @@ - (id)initWithDevice:(id<MTLDevice>)d shadersLib:(NSString*)shadersLib {
blitCommandQueue = [device newCommandQueue];

_tempTransform = [[MTLTransform alloc] init];

initSamplers(device);
}
return self;
}
@@ -165,6 +170,8 @@ - (void)dealloc {
self.blitCommandQueue = nil;
self.pipelineStateStorage = nil;
[_encoderManager release];
[_samplerManager release];
[_stencilManager release];
[_composite release];
[_paint release];
[_transform release];
@@ -177,7 +184,6 @@ - (void) reset {
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLContext : reset");

// Add code for context state reset here
[_clip resetStencilState];
}

- (MTLCommandBufferWrapper *) getCommandBufferWrapper {
@@ -54,10 +54,9 @@
static MTLRenderPipelineDescriptor * templateAATexturePipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateLCDPipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateAAPipelineDesc = nil;
static void setTxtUniforms(
id<MTLRenderCommandEncoder> encoder, int color, int mode, int interpolation, bool repeat, jfloat extraAlpha,
const SurfaceRasterFlags * srcFlags, const SurfaceRasterFlags * dstFlags
);
static void
setTxtUniforms(MTLContext *mtlc, int color, id <MTLRenderCommandEncoder> encoder, int interpolation, bool repeat,
jfloat extraAlpha, const SurfaceRasterFlags *srcFlags, const SurfaceRasterFlags *dstFlags, int mode);

static void initTemplatePipelineDescriptors() {
if (templateRenderPipelineDesc != nil && templateTexturePipelineDesc != nil)
@@ -205,9 +204,9 @@ - (void)setPipelineState:(id<MTLRenderCommandEncoder>)encoder
fragShader = @"lcd_color";
rpDesc = [[templateLCDPipelineDesc copy] autorelease];
}
setTxtUniforms(encoder, _color, 1,
setTxtUniforms(mtlc, _color, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha], &renderOptions->srcFlags,
&renderOptions->dstFlags);
&renderOptions->dstFlags, 1);
} else if (renderOptions->isAAShader) {
vertShader = @"vert_col_aa";
fragShader = @"frag_col_aa";
@@ -248,9 +247,9 @@ - (void)setXorModePipelineState:(id<MTLRenderCommandEncoder>)encoder
fragShader = @"frag_txt_xorMode";
rpDesc = [[templateTexturePipelineDesc copy] autorelease];

setTxtUniforms(encoder, col, 1,
setTxtUniforms(mtlc, col, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha],
&renderOptions->srcFlags, &renderOptions->dstFlags);
&renderOptions->srcFlags, &renderOptions->dstFlags, 1);
[encoder setFragmentBytes:&xorColor length:sizeof(xorColor) atIndex:0];

[encoder setFragmentTexture:dstOps->pTexture atIndex:1];
@@ -786,9 +785,9 @@ - (void)setPipelineState:(id)encoder
[encoder setFragmentTexture:_paintTexture atIndex:0];
}
const SurfaceRasterFlags srcFlags = {_isOpaque, renderOptions->srcFlags.isPremultiplied};
setTxtUniforms(encoder, 0, 0,
setTxtUniforms(mtlc, 0, encoder,
renderOptions->interpolation, YES, [mtlc.composite getExtraAlpha],
&srcFlags, &renderOptions->dstFlags);
&srcFlags, &renderOptions->dstFlags, 0);

id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
@@ -867,72 +866,12 @@ - (NSString *)description {
return @"unknown-paint";
}

static bool samplersInitialized = false;
static id<MTLSamplerState> samplerNearestClamp = nil;
static id<MTLSamplerState> samplerLinearClamp = nil;
static id<MTLSamplerState> samplerNearestRepeat = nil;
static id<MTLSamplerState> samplerLinearRepeat = nil;

void initSamplers(id<MTLDevice> device) {
// TODO: move this code into SamplerManager (need implement)

if (samplersInitialized) {
// Release old samplers if any
[samplerNearestClamp release];
[samplerLinearClamp release];
[samplerNearestRepeat release];
[samplerLinearRepeat release];

samplersInitialized = false;
}

MTLSamplerDescriptor *samplerDescriptor = [[MTLSamplerDescriptor new] autorelease];

samplerDescriptor.rAddressMode = MTLSamplerAddressModeClampToEdge;
samplerDescriptor.sAddressMode = MTLSamplerAddressModeClampToEdge;
samplerDescriptor.tAddressMode = MTLSamplerAddressModeClampToEdge;

samplerDescriptor.minFilter = MTLSamplerMinMagFilterNearest;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterNearest;
samplerNearestClamp = [device newSamplerStateWithDescriptor:samplerDescriptor];

samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
samplerLinearClamp = [device newSamplerStateWithDescriptor:samplerDescriptor];

samplerDescriptor.rAddressMode = MTLSamplerAddressModeRepeat;
samplerDescriptor.sAddressMode = MTLSamplerAddressModeRepeat;
samplerDescriptor.tAddressMode = MTLSamplerAddressModeRepeat;

samplerDescriptor.minFilter = MTLSamplerMinMagFilterNearest;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterNearest;
samplerNearestRepeat = [device newSamplerStateWithDescriptor:samplerDescriptor];

samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
samplerLinearRepeat = [device newSamplerStateWithDescriptor:samplerDescriptor];

samplersInitialized = true;
}

static void setSampler(id<MTLRenderCommandEncoder> encoder, int interpolation, bool repeat) {
id<MTLSamplerState> sampler;
if (repeat) {
sampler = interpolation == INTERPOLATION_BILINEAR ? samplerLinearRepeat : samplerNearestRepeat;
} else {
sampler = interpolation == INTERPOLATION_BILINEAR ? samplerLinearClamp : samplerNearestClamp;
}
[encoder setFragmentSamplerState:sampler atIndex:0];
}

static void setTxtUniforms(
id<MTLRenderCommandEncoder> encoder, int color, int mode, int interpolation, bool repeat, jfloat extraAlpha,
const SurfaceRasterFlags * srcFlags, const SurfaceRasterFlags * dstFlags
) {
static void
setTxtUniforms(MTLContext *mtlc, int color, id <MTLRenderCommandEncoder> encoder, int interpolation, bool repeat,
jfloat extraAlpha, const SurfaceRasterFlags *srcFlags, const SurfaceRasterFlags *dstFlags, int mode) {
struct TxtFrameUniforms uf = {RGBA_TO_V4(color), mode, srcFlags->isOpaque, dstFlags->isOpaque, extraAlpha};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];

setSampler(encoder, interpolation, repeat);
[mtlc.samplerManager setSamplerWithEncoder:encoder interpolation:interpolation repeat:repeat];
}

// For the current paint mode:
@@ -963,7 +902,7 @@ - (void)setPipelineState:(id <MTLRenderCommandEncoder>)encoder
FLOAT_ARR_TO_V4([rescaleOp getScaleFactors]), FLOAT_ARR_TO_V4([rescaleOp getOffsets])
};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
setSampler(encoder, renderOptions->interpolation, NO);
[mtlc.samplerManager setSamplerWithEncoder:encoder interpolation:renderOptions->interpolation repeat:NO];
} else if ([bufImgOp isKindOfClass:[MTLConvolveOp class]]) {
MTLConvolveOp *convolveOp = bufImgOp;
fragShader = @"frag_txt_op_convolve";
@@ -974,7 +913,7 @@ - (void)setPipelineState:(id <MTLRenderCommandEncoder>)encoder
convolveOp.kernelSize, convolveOp.isEdgeZeroFill,
};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
setSampler(encoder, renderOptions->interpolation, NO);
[mtlc.samplerManager setSamplerWithEncoder:encoder interpolation:renderOptions->interpolation repeat:NO];

[encoder setFragmentBuffer:[convolveOp getBuffer] offset:0 atIndex:2];
} else if ([bufImgOp isKindOfClass:[MTLLookupOp class]]) {
@@ -986,14 +925,14 @@ - (void)setPipelineState:(id <MTLRenderCommandEncoder>)encoder
FLOAT_ARR_TO_V4([lookupOp getOffset]), lookupOp.isUseSrcAlpha, lookupOp.isNonPremult,
};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
setSampler(encoder, renderOptions->interpolation, NO);
[mtlc.samplerManager setSamplerWithEncoder:encoder interpolation:renderOptions->interpolation repeat:NO];
[encoder setFragmentTexture:[lookupOp getLookupTexture] atIndex:1];
}
} else {
setTxtUniforms(encoder, 0, 0,
setTxtUniforms(mtlc, 0, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha],
&renderOptions->srcFlags,
&renderOptions->dstFlags);
&renderOptions->dstFlags, 0);

}
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
@@ -1022,18 +961,18 @@ - (void)setXorModePipelineState:(id <MTLRenderCommandEncoder>)encoder
MTLRenderPipelineDescriptor * rpDesc = [[templateTexturePipelineDesc copy] autorelease];

const int col = 0 ^ xorColor;
setTxtUniforms(encoder, col, 0,
setTxtUniforms(mtlc, col, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha],
&renderOptions->srcFlags, &renderOptions->dstFlags);
&renderOptions->srcFlags, &renderOptions->dstFlags, 0);
[encoder setFragmentBytes:&xorColor length:sizeof(xorColor) atIndex: 0];

BMTLSDOps *dstOps = MTLRenderQueue_GetCurrentDestination();
[encoder setFragmentTexture:dstOps->pTexture atIndex:1];

setTxtUniforms(encoder, 0, 0,
setTxtUniforms(mtlc, 0, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha],
&renderOptions->srcFlags,
&renderOptions->dstFlags);
&renderOptions->dstFlags, 0);

id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
@@ -88,6 +88,15 @@ enum {
#define RETURN_IF_NULL(value) ACT_IF_NULL(return, value)
#define CONTINUE_IF_NULL(value) ACT_IF_NULL(continue, value)

#define ACT_IF_TRUE(ACTION, value) \
if ((value)) { \
J2dTraceLn1(J2D_TRACE_ERROR, \
"%s is false", #value);\
ACTION; \
} else do { } while (0)

#define RETURN_IF_TRUE(value) ACT_IF_TRUE(return, value)

MTLContext *MTLRenderQueue_GetCurrentContext();
BMTLSDOps *MTLRenderQueue_GetCurrentDestination();
void commitEncodedCommands();