Skip to content

Commit

Permalink
Lighting sample works on both Web and Kinc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Apprentice-Alchemist committed Sep 17, 2023
1 parent f6d4bc4 commit a7ecb72
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "tools/krafix"]
path = tools/krafix
url = https://github.com/Kode/krafix_bin
[submodule "tools/KincTools_linux_x64"]
path = tools/KincTools_linux_x64
url = https://github.com/Kode/KincTools_linux_x64
71 changes: 60 additions & 11 deletions Test.hx
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
import sys.thread.Thread;

function main() {
trace("Hello World!");
Thread.create(() -> {
trace("thread");
throw "Hello?";
});
// hl.Api.breakPoint();
Thread.current().events.repeat(() -> {}, 5);
}
using Test;

class Test {
static function main() {
trace("Haxe is great!");
trace("foo\nbar".lines().filter(s -> s != "foo").collect());
}

static function lines(s:String):Iterator<String> {
return s.split("\n").iterator(); // TODO: bad! should be a lazy iter!
}

static function filter<T>(i:Iterator<T>, fn:T->Bool):Iterator<T> {
return new FilterIter(i, fn);
}

static function collect<T>(iter:Iterator<T>):Array<T> {
return [for (i in iter) i];
}
}

class FilterIter<T> {
var inner:Iterator<T>;
var fn:T->Bool;

var _hasNext:Bool = false;
var _next:T = null;

public inline function new(i:Iterator<T>, fn:T->Bool) {
inner = i;
this.fn = fn;
while (inner.hasNext()) {
var n = inner.next();
if (fn(n)) {
_hasNext = true;
_next = n;
break;
}
}
}

public inline function hasNext():Bool {
return _hasNext;
}

public inline function next():Null<T> {
var ret = if (_hasNext) _next else null;
_hasNext = false;
_next = null;
while (inner.hasNext()) {
var n = inner.next();
if (fn(n)) {
_hasNext = true;
_next = n;
break;
}
}
return ret;
}
}
4 changes: 2 additions & 2 deletions build.hxml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-cp samples
-cp samples/light
-cp src
-cp test
-main Texture
-main Light
-lib kinc
-lib format

Expand Down
16 changes: 1 addition & 15 deletions samples/light/Light.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,16 @@ import arcane.util.Color;
import arcane.util.Math;
import arcane.arrays.Int32Array;
import arcane.arrays.Float32Array;
import arcane.gpu.IVertexBuffer;
import arcane.gpu.IIndexBuffer;
import arcane.gpu.IBindGroup;
import arcane.gpu.IBindGroupLayout;
import arcane.gpu.ICommandBuffer;
import arcane.gpu.ICommandEncoder;
import arcane.gpu.IComputePass;
import arcane.gpu.IComputePipeline;
import arcane.gpu.IRenderPass;
import arcane.gpu.IRenderPipeline;
import arcane.gpu.ISampler;
import arcane.gpu.IShaderModule;
import arcane.gpu.ITexture;
import arcane.gpu.IUniformBuffer;
import arcane.gpu.IGPUDevice;
import arcane.Lib;
import arcane.Image;

function main() {
arcane.Lib.init(() -> {
arcane.Assets.loadBytesAsync("res/parrot.png", bytes -> {
trace(bytes);
arcane.Utils.assert(bytes != null);
final d:IGPUDevice = cast Lib.gdriver;
final d:IGPUDevice = cast Lib.gpu;
final parrot = Image.fromPngBytes(bytes).expect().toTexture(d);
final vertex_attributes = vertex_attribs("pos" => Float3, /*"uv" => Float2,*/ "normal" => Float3);
final vbuf = d.createVertexBuffer({
Expand Down
4 changes: 1 addition & 3 deletions src/arcane/Assets.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Assets {
/**
* All the files present in the resource folder at the moment of compilation
*/
static var manifest:Array<String> = [];
static var manifest:Array<String> = arcane.internal.Macros.initManifest();

/**
* Preload all assets for further use with Assets.getBytes.
Expand All @@ -28,13 +28,11 @@ class Assets {
* @param onComplete called once all assets have been loaded
*/
public static function preload(onProgress:(f:Float) -> Void, handle_error:(error:AssetError) -> Void, onComplete:() -> Void):Void {
Assets.manifest = arcane.internal.Macros.initManifest();
var loaded_files:Int = 0;
var errored_files:Int = 0;
final file_count:Int = manifest.length;
for (x in manifest) {
loadBytesAsync(x, bytes -> {
bytes_cache.set(x, bytes);
loaded_files++;
onProgress(loaded_files / file_count);
if ((file_count + errored_files) == loaded_files) {
Expand Down
3 changes: 2 additions & 1 deletion src/arcane/Geometry.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import arcane.arrays.UInt16Array;
import arcane.arrays.Int32Array;
import arcane.arrays.Float32Array;
import arcane.gpu.IGPUDevice;
import arcane.gpu.IRenderPipeline;

@:nullSafety(Strict)
class Geometry {
Expand Down Expand Up @@ -43,7 +44,7 @@ class Geometry {
}
}

public function makeBuffers(driver:IGraphicsDriver) {
public function makeBuffers(driver:IGPUDevice) {
var attributes = [{name: "pos", kind: Float3}, {name: "uv", kind: Float2}];
var ret = {
vertices: driver.createVertexBuffer({attributes: attributes, size: points.length, dyn: true}),
Expand Down
12 changes: 6 additions & 6 deletions src/arcane/Lib.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Lib {
public static var fps(default, null):Float = 0.0;

public static var system(default, null):ISystem = new arcane.internal.System();
public static var gdriver(default, null):Null<IGPUDevice>;
public static var gpu(default, null):Null<IGPUDevice>;
public static var adriver(default, null):Null<IAudioDevice>;

#if target.threaded
Expand All @@ -43,7 +43,7 @@ class Lib {
mode: Windowed
}
}, () -> {
gdriver = system.getGPUDevice();
gpu = system.getGPUDevice();
adriver = system.getAudioDevice();
cb();
});
Expand Down Expand Up @@ -78,8 +78,8 @@ class Lib {
hl.Profile.event(-1); // pause
#end
#if kinc
if (gdriver != null) {
gdriver.present();
if (gpu != null) {
gpu.present();
}
#end
#if hl_profile
Expand Down Expand Up @@ -109,8 +109,8 @@ class Lib {
* @param code Exit code
*/
public static function exit(code:Int):Void {
if (gdriver != null)
gdriver.dispose();
if (gpu != null)
gpu.dispose();
if (system != null)
system.shutdown();
#if hl_profile
Expand Down
3 changes: 3 additions & 0 deletions src/arcane/internal/html5/WebGL2Driver.hx
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,9 @@ private class CommandBuffer implements ICommandBuffer {
case Load:
}
}
enable(GL.DEPTH_TEST, true);
gl.depthMask(true);
gl.clearDepth(0.0);
gl.clear(GL.DEPTH);
case EndRenderPass:
case SetVertexBuffers(buffers):
Expand Down
Loading

0 comments on commit a7ecb72

Please sign in to comment.