Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,10 @@ protected void setDeviceParametersFor2D() {

@Override
protected void setDeviceParametersFor3D() {
// There are no Metal rendering pipeline states changed as a
// result of this call, hence the method is no-op.
// But overriding the method here for any future reference.
// We are using this call in Metal to only initialise the
// default 3D texture sampler's filter and addressing states
if (checkDisposed()) return;
nSetDeviceParametersFor3D(pContext);
}

long createMTLMesh() {
Expand Down Expand Up @@ -357,8 +358,10 @@ void setSpecularColor(long nativePhongMaterial, boolean set, float r, float g, f
nSetSpecularColor(pContext, nativePhongMaterial, set, r, g, b, a);
}

void setMap(long nativePhongMaterial, int mapType, long nativeTexture) {
nSetMap(pContext, nativePhongMaterial, mapType, nativeTexture);
void setMap(long nativePhongMaterial, int mapType,
long nativeTexture, boolean useMipmap) {
nSetMap(pContext, nativePhongMaterial,
mapType, nativeTexture, useMipmap);
}

long createMTLMeshView(long nativeMesh) {
Expand Down Expand Up @@ -521,6 +524,7 @@ private static native void nSetWorldTransform(long pContext,
double m20, double m21, double m22, double m23,
double m30, double m31, double m32, double m33);
private static native void nSetCameraPosition(long pContext, double x, double y, double z);
private static native void nSetDeviceParametersFor3D(long pContext);
private static native long nCreateMTLMesh(long pContext);
private static native void nReleaseMTLMesh(long pContext, long nativeHandle);
private static native boolean nBuildNativeGeometryShort(long pContext, long nativeHandle,
Expand All @@ -534,7 +538,7 @@ private static native void nSetDiffuseColor(long pContext, long nativePhongMater
private static native void nSetSpecularColor(long pContext, long nativePhongMaterial,
boolean set, float r, float g, float b, float a);
private static native void nSetMap(long pContext, long nativePhongMaterial,
int mapType, long texID);
int mapType, long texID, boolean useMipmap);
private static native long nCreateMTLMeshView(long pContext, long nativeMesh);
private static native void nReleaseMTLMeshView(long pContext, long nativeHandle);
private static native void nSetCullingMode(long pContext, long nativeMeshView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private Texture setupTexture(TextureMap map, boolean useMipmap) {
if (texture instanceof MTLTexture mtlTex) {
hTexture = mtlTex.getNativeHandle();
}
context.setMap(nativeHandle, map.getType().ordinal(), hTexture);
context.setMap(nativeHandle, map.getType().ordinal(), hTexture, useMipmap);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use mipmapping only when (!PlatformUtil.isEmbedded()) && (i == PhongMaterial.DIFFUSE || i == PhongMaterial.SELF_ILLUM);. We need to pass mipmap information, so that we can pick appropriate sampler state in native code.

return texture;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ typedef enum VertexInputIndex {
int cullMode;

id<MTLBuffer> pixelBuffer;
id<MTLSamplerState> nonMipmappedSamplerState;
id<MTLSamplerState> mipmappedSamplerState;
}

- (void) setCompositeMode:(int)mode;
Expand Down Expand Up @@ -183,6 +185,7 @@ typedef enum VertexInputIndex {
- (void) dealloc;
- (id<MTLSamplerState>) getSampler:(bool)isLinear wrapMode:(int)wrapMode;
- (id<MTLSamplerState>) createSampler:(bool)isLinear wrapMode:(int)wrapMode;
- (id<MTLSamplerState>) get3DSamplerState:(bool)mipmapped;
- (id<MTLCommandQueue>) getCommandQueue;

- (void) validatePixelBuffer:(NSUInteger)length;
Expand Down
57 changes: 55 additions & 2 deletions modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ - (id) createContext:(dispatch_data_t)shaderLibData
linearSamplerDict = [[NSMutableDictionary alloc] init];
nonLinearSamplerDict = [[NSMutableDictionary alloc] init];
compositeMode = com_sun_prism_mtl_MTLContext_MTL_COMPMODE_SRCOVER; //default
nonMipmappedSamplerState = nil;
mipmappedSamplerState = nil;

currentBufferIndex = 0;
commandQueue = [device newCommandQueue];
Expand Down Expand Up @@ -235,6 +237,35 @@ - (MetalRingBuffer*) getDataRingBuffer
return sampler;
}

- (void) create3DSamplerStates
{
if (mipmappedSamplerState == nil ||
nonMipmappedSamplerState == nil) {
MTLSamplerDescriptor *samplerDescriptor = [MTLSamplerDescriptor new];
samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
samplerDescriptor.rAddressMode = MTLSamplerAddressModeRepeat;
samplerDescriptor.sAddressMode = MTLSamplerAddressModeRepeat;
samplerDescriptor.tAddressMode = MTLSamplerAddressModeRepeat;
samplerDescriptor.mipFilter = MTLSamplerMipFilterNotMipmapped;
nonMipmappedSamplerState = [[self getDevice] newSamplerStateWithDescriptor:samplerDescriptor];

samplerDescriptor.mipFilter = MTLSamplerMipFilterLinear;
mipmappedSamplerState = [[self getDevice] newSamplerStateWithDescriptor:samplerDescriptor];

[samplerDescriptor release];
}
}

- (id<MTLSamplerState>) get3DSamplerState:(bool)mipmapped
{
if (mipmapped) {
return mipmappedSamplerState;
} else {
return nonMipmappedSamplerState;
}
}

- (id<MTLDevice>) getDevice
{
return device;
Expand Down Expand Up @@ -837,6 +868,16 @@ - (void) dealloc
pixelBuffer = nil;
}

if (nonMipmappedSamplerState != nil) {
[nonMipmappedSamplerState release];
nonMipmappedSamplerState = nil;
}

if (mipmappedSamplerState != nil) {
[mipmappedSamplerState release];
mipmappedSamplerState = nil;
}

device = nil;

[super dealloc];
Expand Down Expand Up @@ -1109,6 +1150,18 @@ - (void) blit:(id<MTLTexture>)src srcX0:(int)srcX0 srcY0:(int)srcY0 srcX1:(int)s
[mtlContext setWorldTransformIdentityMatrix];
}

/*
* Class: com_sun_prism_mtl_MTLContext
* Method: nSetDeviceParametersFor3D
* Signature: (J)J
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor : (J)J should be changed to (J)V

*/
JNIEXPORT void JNICALL Java_com_sun_prism_mtl_MTLContext_nSetDeviceParametersFor3D
(JNIEnv *env, jclass jClass, jlong ctx)
{
MetalContext *pCtx = (MetalContext*) jlong_to_ptr(ctx);
[pCtx create3DSamplerStates];
}

/*
* Class: com_sun_prism_mtl_MTLContext
* Method: nCreateMTLMesh
Expand Down Expand Up @@ -1290,12 +1343,12 @@ - (void) blit:(id<MTLTexture>)src srcX0:(int)srcX0 srcY0:(int)srcY0 srcX1:(int)s
*/
JNIEXPORT void JNICALL Java_com_sun_prism_mtl_MTLContext_nSetMap
(JNIEnv *env, jclass jClass, jlong ctx, jlong nativePhongMaterial,
jint mapType, jlong nativeTexture)
jint mapType, jlong nativeTexture, jboolean useMipmap)
{
MetalPhongMaterial *phongMaterial = (MetalPhongMaterial *) jlong_to_ptr(nativePhongMaterial);
MetalTexture *texMap = (MetalTexture *) jlong_to_ptr(nativeTexture);

[phongMaterial setMap:mapType map:[texMap getTexture]];
[phongMaterial setMap:mapType map:[texMap getTexture] useMipmap:useMipmap];
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,20 @@ - (void) render
atIndex:0];
[phongEncoder setFragmentTexture:[material getMap:DIFFUSE]
atIndex:0];
[phongEncoder setFragmentSamplerState:[material getSamplerState:DIFFUSE]
atIndex:0];
[phongEncoder setFragmentTexture:[material getMap:SPECULAR]
atIndex:1];
[phongEncoder setFragmentSamplerState:[material getSamplerState:SPECULAR]
atIndex:0];
[phongEncoder setFragmentTexture:[material getMap:BUMP]
atIndex:2];
[phongEncoder setFragmentSamplerState:[material getSamplerState:BUMP]
atIndex:0];
[phongEncoder setFragmentTexture:[material getMap:SELFILLUMINATION]
atIndex:3];
[phongEncoder setFragmentSamplerState:[material getSamplerState:SELFILLUMINATION]
atIndex:0];
Comment on lines +231 to +244
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. All setFragmentSamplerState are setting at index 0, should it not be 0,1,2,3?
  2. Does this not require any changes to PhongPS.metal, The shader functions in PhongPS.metal do not have a sampler parameter.
  3. Currently the PhongPS.metal file creates two samplers in shader file itself and uses those for sampling. May be that should be modified to use these samplers that are set using setFragmentSamplerState

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the observations @arapte .
Yes these changes are needed and it also has to be tested properly. So moving this PR back to draft stage.


[phongEncoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle
indexCount:[mesh getNumIndices]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
vector_float4 specularColor;
bool specularColorSet;
id<MTLTexture> map[4];
id<MTLSamplerState> samplerState[4];
}

- (MetalPhongMaterial*) createPhongMaterial:(MetalContext*)ctx;
Expand All @@ -66,8 +67,10 @@
- (int) getSpecType;
- (bool) isSelfIllumMap;
- (void) setMap:(int)mapID
map:(id<MTLTexture>)texMap;
map:(id<MTLTexture>)texMap
useMipmap:(bool)mipmapped;
- (id<MTLTexture>) getMap:(int)mapID;
- (id<MTLSamplerState>) getSamplerState:(int)mapID;
@end

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ - (bool) isSelfIllumMap

- (void) setMap:(int)mapID
map:(id<MTLTexture>)texMap
useMipmap:(bool)mipmapped
{
// Within the range of DIFFUSE, SPECULAR, BUMP, SELFILLUMINATION
if (mapID >= 0 && mapID <= 3) {
map[mapID] = texMap;
samplerState[mapID] = [context get3DSamplerState:mipmapped];
} else {
NSLog(@"MetalPhongMaterial.setMap(): mapID is out of range");
}
Expand All @@ -131,4 +133,9 @@ - (void) setMap:(int)mapID
NSLog(@"MetalPhongMaterial.getMap(): mapID is out of range");
return nil;
}

- (id<MTLSamplerState>) getSamplerState:(int)mapID
{
return samplerState[mapID];
}
@end // MetalPhongMaterial