Skip to content

Commit

Permalink
8304769: Add implementation for integer index buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jayathirthrao authored and aghaisas committed Mar 23, 2023
1 parent 02037aa commit 0bc4f31
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
48 changes: 42 additions & 6 deletions modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.m
Expand Up @@ -794,10 +794,10 @@ - (void)dealloc
return JNI_FALSE;
}

bool result = [mesh buildBuffers:vertexBuffer
vSize:uvbSize
iBuffer:indexBuffer
iSize:uibSize];
bool result = [mesh buildBuffersShort:vertexBuffer
vSize:uvbSize
iBuffer:indexBuffer
iSize:uibSize];
(*env)->ReleasePrimitiveArrayCritical(env, ib, indexBuffer, 0);
(*env)->ReleasePrimitiveArrayCritical(env, vb, vertexBuffer, 0);

Expand All @@ -812,9 +812,45 @@ - (void)dealloc
JNIEXPORT jboolean JNICALL Java_com_sun_prism_mtl_MTLContext_nBuildNativeGeometryInt
(JNIEnv *env, jclass jClass, jlong ctx, jlong nativeMesh, jfloatArray vb, jint vbSize, jintArray ib, jint ibSize)
{
// TODO: MTL: Complete the implementation
CTX_LOG(@"MTLContext_nBuildNativeGeometryInt");
return JNI_TRUE;
CTX_LOG(@"vbSize %d ibSize %d", vbSize, ibSize);
MetalMesh *mesh = (MetalMesh *) jlong_to_ptr(nativeMesh);

if (vbSize < 0 || ibSize < 0) {
return JNI_FALSE;
}

unsigned int uvbSize = (unsigned int) vbSize;
unsigned int uibSize = (unsigned int) ibSize;
unsigned int vertexBufferSize = (*env)->GetArrayLength(env, vb);
unsigned int indexBufferSize = (*env)->GetArrayLength(env, ib);
CTX_LOG(@"vertexBufferSize %d indexBufferSize %d", vertexBufferSize, indexBufferSize);

if (uvbSize > vertexBufferSize || uibSize > indexBufferSize) {
return JNI_FALSE;
}

float *vertexBuffer = (float *) ((*env)->GetPrimitiveArrayCritical(env, vb, 0));
if (vertexBuffer == NULL) {
CTX_LOG(@"MTLContext_nBuildNativeGeometryInt vertexBuffer is NULL");
return JNI_FALSE;
}

unsigned int *indexBuffer = (unsigned int *) ((*env)->GetPrimitiveArrayCritical(env, ib, 0));
if (indexBuffer == NULL) {
CTX_LOG(@"MTLContext_nBuildNativeGeometryInt indexBuffer is NULL");
(*env)->ReleasePrimitiveArrayCritical(env, vb, vertexBuffer, 0);
return JNI_FALSE;
}

bool result = [mesh buildBuffersInt:vertexBuffer
vSize:uvbSize
iBuffer:indexBuffer
iSize:uibSize];
(*env)->ReleasePrimitiveArrayCritical(env, ib, indexBuffer, 0);
(*env)->ReleasePrimitiveArrayCritical(env, vb, vertexBuffer, 0);

return result;
}

/*
Expand Down
Expand Up @@ -43,10 +43,14 @@
}

- (id) createMesh:(MetalContext*)ctx;
- (bool) buildBuffers:(float*)vb
- (bool) buildBuffersShort:(float*)vb
vSize:(unsigned int)vbSize
iBuffer:(unsigned short*)ib
iSize:(unsigned int)ibSize;
- (bool) buildBuffersInt:(float*)vb
vSize:(unsigned int)vbSize
iBuffer:(unsigned int*)ib
iSize:(unsigned int)ibSize;
- (void) releaseVertexBuffer;
- (void) releaseIndexBuffer;
- (id<MTLBuffer>) getVertexBuffer;
Expand Down
37 changes: 35 additions & 2 deletions modules/javafx.graphics/src/main/native-prism-mtl/MetalMesh.m
Expand Up @@ -51,7 +51,7 @@ - (id) createMesh:(MetalContext*)ctx
return self;
}

- (bool) buildBuffers:(float*)vb
- (bool) buildBuffersShort:(float*)vb
vSize:(unsigned int)vbSize
iBuffer:(unsigned short*)ib
iSize:(unsigned int)ibSize
Expand All @@ -62,7 +62,7 @@ - (bool) buildBuffers:(float*)vb
id<MTLDevice> device = [context getDevice];
unsigned int size = vbSize * sizeof (float);
unsigned int vbCount = vbSize / NUM_OF_FLOATS_PER_VERTEX;
MESH_LOG(@"vbCount %d", vbCount);
MESH_LOG(@"vbCount %d", vbCount);
// TODO: MTL: Cleanup this code in future if we think we don't need
// to add padding to float3 data
/*VS_PHONG_INPUT* pVert = vertices;
Expand Down Expand Up @@ -102,6 +102,39 @@ - (bool) buildBuffers:(float*)vb
return true;
}

- (bool) buildBuffersInt:(float*)vb
vSize:(unsigned int)vbSize
iBuffer:(unsigned int*)ib
iSize:(unsigned int)ibSize
{
MESH_LOG(@"MetalMesh->buildBuffers");
MESH_LOG(@"vbsize %d", vbSize);
MESH_LOG(@"ibsize %d", ibSize);
id<MTLDevice> device = [context getDevice];
unsigned int size = vbSize * sizeof (float);
unsigned int vbCount = vbSize / NUM_OF_FLOATS_PER_VERTEX;
MESH_LOG(@"vbCount %d", vbCount);

if (numVertices != vbCount) {
[self releaseVertexBuffer];
vertexBuffer = [[device newBufferWithBytes:vb length:size options:MTLResourceStorageModeShared] autorelease];
numVertices = vbCount;
MESH_LOG(@"numVertices %lu", numVertices);
}

size = ibSize * sizeof (unsigned int);
MESH_LOG(@"IndexBuffer size %d", size);
if (numIndices != ibSize) {
[self releaseIndexBuffer];
indexBuffer = [[device newBufferWithBytes:ib length:size options:MTLResourceStorageModeShared] autorelease];
numIndices = ibSize;
MESH_LOG(@"numIndices %lu", numIndices);
}

MESH_LOG(@"MetalMesh->buildBuffers done");
return true;
}

- (void) releaseVertexBuffer
{
MESH_LOG(@"MetalMesh->releaseVertexBuffer");
Expand Down

0 comments on commit 0bc4f31

Please sign in to comment.