Skip to content

Commit

Permalink
Merge pull request #1041 from RblSb/patch-3
Browse files Browse the repository at this point in the history
Optimize android-java
  • Loading branch information
RobDangerous authored May 21, 2019
2 parents 1ccb976 + 2b968e1 commit eeb9f6b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
32 changes: 16 additions & 16 deletions Backends/Android/kha/arrays/Float32Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

abstract Float32Array(FloatBuffer) {
abstract Float32Array(java.NativeArray<Single>) {
private static inline var elementSize = 4;

public inline function new(elements: Int) {
this = ByteBuffer.allocateDirect(elements * elementSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
this = new java.NativeArray<Single>(elements * elementSize);
}

public var length(get, never): Int;

inline function get_length(): Int {
return this.remaining();
return this.length;
}
public function set(index: Int, value: FastFloat): FastFloat {
this.put(index, value);

public inline function set(index: Int, value: FastFloat): FastFloat {
this[index] = value;
return value;
}

public inline function get(index: Int): FastFloat {
return this.get(index);
return this[index];
}
public inline function data(): FloatBuffer {
return this;

public inline function data(count:Int): FloatBuffer {
return FloatBuffer.wrap(this, 0, count);
}

@:arrayAccess
public inline function arrayRead(index: Int): FastFloat {
return this.get(index);
return get(index);
}

@:arrayAccess
public inline function arrayWrite(index: Int, value: FastFloat): FastFloat {
this.put(index, value);
set(index, value);
return value;
}

Expand Down
23 changes: 12 additions & 11 deletions Backends/Android/kha/graphics4/VertexBuffer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class VertexBuffer {
private var sizes: Array<Int>;
private var offsets: Array<Int>;
private var usage: Usage;

public function new(vertexCount: Int, structure: VertexStructure, usage: Usage, canRead: Bool = false) {
this.usage = usage;
mySize = vertexCount;
Expand All @@ -41,15 +41,15 @@ class VertexBuffer {
myStride += 2 * 4;
}
}

buffer = createBuffer();
data = new Float32Array(Std.int(vertexCount * myStride / 4));

sizes = new Array<Int>();
offsets = new Array<Int>();
sizes[structure.elements.length - 1] = 0;
offsets[structure.elements.length - 1] = 0;

var offset = 0;
var index = 0;
for (element in structure.elements) {
Expand Down Expand Up @@ -91,30 +91,31 @@ class VertexBuffer {
++index;
}
}

private static function createBuffer(): Int {
var buffers = new NativeArray<Int>(1);
GLES20.glGenBuffers(1, buffers, 0);
return buffers[0];
}

public function lock(?start: Int, ?count: Int): Float32Array {
return data;
}

public function unlock(?count: Int): Void {
var count = (count != null ? count : mySize) * myStride;
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffer);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, (count != null ? count : mySize) * myStride, data.data(), usage == Usage.DynamicUsage ? GLES20.GL_DYNAMIC_DRAW : GLES20.GL_STATIC_DRAW);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, count, data.data(count), usage == Usage.DynamicUsage ? GLES20.GL_DYNAMIC_DRAW : GLES20.GL_STATIC_DRAW);
}

public function stride(): Int {
return myStride;
}

public function count(): Int {
return mySize;
}

public function set(): Void {
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffer);
for (i in 0...sizes.length) {
Expand Down

0 comments on commit eeb9f6b

Please sign in to comment.