Skip to content

Commit

Permalink
started on rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
codescapade committed Sep 10, 2024
1 parent 4126e3d commit 46a887c
Show file tree
Hide file tree
Showing 20 changed files with 1,416 additions and 1 deletion.
1 change: 1 addition & 0 deletions extraParams.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--macro jume.utils.Macros.init()
4 changes: 4 additions & 0 deletions hxml/build.hxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
-cp src
-cp build

--js dist/jume.js

--debug

--macro jume.utils.Macros.init()

--main Build
3 changes: 2 additions & 1 deletion hxml/test.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

--main UnitTests

--macro jume.utils.Macros.init()

--debug
-D headless
# -D headless
# -D UTEST_PATTERN=TimeStepTests

--js tests/out/tests.js
Expand Down
179 changes: 179 additions & 0 deletions src/jume/graphics/Pipeline.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package jume.graphics;

import jume.graphics.gl.Context;
import jume.graphics.gl.BlendOperation;
import jume.graphics.gl.BlendMode;

import js.html.webgl.GL;
import js.html.webgl.Program;
import js.html.webgl.UniformLocation;

import jume.di.Injectable;

/**
* The pipeline class to store and use shaders.
*/
class Pipeline implements Injectable {
/**
* Defaults to BlendOne.
*/
public var blendSource: BlendMode;

/**
* Defaults to InverseSourceAlpha.
*/
public var blendDestination: BlendMode;

/**
* Defaults to Add.
*/
public var blendOperation: BlendOperation;

/**
* Defaults to BlendOne.
*/
public var alphaBlendSource: BlendMode;

/**
* Defaults to InverseSourceAlpha.
*/
public var alphaBlendDestination: BlendMode;

/**
* Defaults to Add.
*/
public var alphaBlendOperation: BlendOperation;

/**
* Matrix projection location.
*/
public var projectionLocation: UniformLocation;

/**
* Texture location for shaders that use a texture.
*/
public var textureLocation: UniformLocation;

/**
* The vertex position in the shader.
*/
public var vertexPositionLocation: Int;

/**
* The vertex color in the shader.
*/
public var vertexColorLocation: Int;

/**
* The vertex uv in the shader.
*/
public var vertexUVLocation: Int;

// The shaders.
var vertexShader: Shader;
var fragmentShader: Shader;

// The shader program.
var program: Program;

@:inject
var context: Context;

/**
* Create a new shader pipeline.
* @param vertexShader
* @param fragmentShader
* @param useTexture Does this shader use a texture.
*/
public function new(vertexShader: Shader, fragmentShader: Shader, useTexture: Bool) {
this.vertexShader = vertexShader;
this.fragmentShader = fragmentShader;
program = createProgram(useTexture);

#if !headless
final projection = context.gl.getUniformLocation(program, 'projectionMatrix');
if (projection == null) {
throw 'projectionMatrix not available in the vertex shader';
}
projectionLocation = projection;

textureLocation = null;
if (useTexture) {
final tex = context.gl.getUniformLocation(program, 'tex');
if (tex == null) {
throw 'tex not available in the fragment shader';
}
textureLocation = tex;
}
#end

blendSource = BLEND_ONE;
blendDestination = INVERSE_SOURCE_ALPHA;
blendOperation = ADD;

alphaBlendSource = BLEND_ONE;
alphaBlendDestination = INVERSE_SOURCE_ALPHA;
alphaBlendOperation = ADD;
}

/**
* Start rendering using this shader. This gets called by Graphics.
*/
public inline function use() {
#if !headless
context.gl.useProgram(this.program);
#end
}

/**
* Get a WebGL uniform location for this pipeline.
* @param location The location name
* @returns The uniform location or null if not found.
*/
public function getUniformLocation(location: String): UniformLocation {
#if !headless
return context.gl.getUniformLocation(this.program, location);
#end

return null;
}

/**
* Create a new shader program.
* @param useTexture
* @returns The shader program.
*/
function createProgram(useTexture: Bool): Program {
#if !headless
final gl = context.gl;
final program = gl.createProgram();
if (program != null) {
gl.attachShader(program, vertexShader.glShader);
gl.attachShader(program, fragmentShader.glShader);
gl.linkProgram(program);

final success: Bool = gl.getProgramParameter(program, GL.LINK_STATUS);
if (!success) {
var error = gl.getProgramInfoLog(program);
if (error == null) {
error = '';
}
throw 'Error while linking shader program: ${error}';
}

gl.bindAttribLocation(program, vertexPositionLocation, 'vertexPosition');
gl.bindAttribLocation(program, vertexColorLocation, 'vertexColor');

if (useTexture) {
gl.bindAttribLocation(program, vertexUVLocation, 'vertexUV');
}

return program;
} else {
throw 'Unable to create shader program';
}
#end

return null;
}
}
58 changes: 58 additions & 0 deletions src/jume/graphics/Shader.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package jume.graphics;

import jume.graphics.gl.Context;

import haxe.Exception;

import js.html.webgl.GL;
import js.html.webgl.Shader as WebGLShader;

import jume.di.Injectable;

/**
* The shader class is used to create custom shaders.
*/
class Shader implements Injectable {
/**
* The shader id.
*/
public var glShader(default, null): WebGLShader;

/**
* The rendering context.
*/
@:inject
final context: Context;

/**
* Create a new Shader.
* @param source The shader source text.
* @param type The shader type (Vertex or Fragment.)
*/
public function new(source: String, type: ShaderType) {
#if !headless
final gl = context.gl;
final shaderType = type == ShaderType.VERTEX ? GL.VERTEX_SHADER : GL.FRAGMENT_SHADER;
final shader = gl.createShader(shaderType);
if (shader == null) {
throw new Exception('Unable to load shader:\n ${source}');
}
glShader = shader;

gl.shaderSource(glShader, source);
gl.compileShader(glShader);
if (!gl.getShaderParameter(glShader, GL.COMPILE_STATUS)) {
throw new Exception('Could not compile shader:\n, ${gl.getShaderInfoLog(glShader)}');
}
#end
}

/**
* Delete the shader.
*/
public inline function destroy() {
#if !headless
context.gl.deleteShader(glShader);
#end
}
}
9 changes: 9 additions & 0 deletions src/jume/graphics/ShaderType.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package jume.graphics;

/**
* Shader type options.
*/
enum abstract ShaderType(String) {
var VERTEX = 'vertex';
var FRAGMENT = 'fragment';
}
18 changes: 18 additions & 0 deletions src/jume/graphics/gl/BlendMode.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jume.graphics.gl;

/**
* Blend mode options.
*/
enum abstract BlendMode(String) {
var UNDEFINED = 'undefined';
var BLEND_ONE = 'blend_one';
var BLEND_ZERO = 'blend_zero';
var SOURCE_ALPHA = 'source_alpha';
var DESTINATION_ALPHA = 'destination_alpha';
var INVERSE_SOURCE_ALPHA = 'inverse_source_alpha';
var INVERSE_DESTINATION_ALPHA = 'inverse_destination_alpha';
var SOURCE_COLOR = 'source_color';
var DESTINATION_COLOR = 'destination_color';
var INVERSE_SOURCE_COLOR = 'inverse_source_color';
var INVERSE_DESTINATION_COLOR = 'inverse_destination_color';
}
10 changes: 10 additions & 0 deletions src/jume/graphics/gl/BlendOperation.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package jume.graphics.gl;

/**
* Blend operation options.
*/
enum abstract BlendOperation(String) {
var ADD = 'add';
var SUBTRACT = 'subtract';
var REVERSE_SUBTRACT = 'reverse_subtract';
}
Loading

0 comments on commit 46a887c

Please sign in to comment.