-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4126e3d
commit 46a887c
Showing
20 changed files
with
1,416 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--macro jume.utils.Macros.init() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
Oops, something went wrong.