Difference between revisions of "Writing Lua Shaders"

From GiderosMobile
Line 3: Line 3:
  
 
== Lua Shaders ==
 
== Lua Shaders ==
=== Creating a Shader ===
+
When you write your shaders in '''Lua''', they will be automatically translated to the relevant shader Language for the platform you are using (eg GLSL, HLSL or MSL). This is the '''recommended''' way to write your shaders in Gideros.
Typical Lua Vertex and Fragment shader code:
+
 
 +
The [[Shader|Shader API]] allows the creation of Shader objects from within '''Lua'''. The 'Shader.new()' constructor takes five arguments:
 +
* the '''Vertex''' shader code (a lua function)
 +
* the '''Fragment''' shader code (a lua function)
 +
* a set of [[Shader Flags|numerical flags]] or 0 if none
 +
* an array of [[Shader Uniform Descriptors|uniforms/constants descriptors]]
 +
* an array of [[Shader Attribute Descriptors|attributes descriptors]]
 +
 
 +
With Lua Shaders, it is assumed that the code is within the '''Vertex''' and '''Fragment''' parameter functions.
 +
 
 +
A typical Lua '''Vertex''' and '''Fragment''' shader code looks like:
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
function vertex(vVertex,vColor,vTexCoord)
 
function vertex(vVertex,vColor,vTexCoord)
Line 22: Line 32:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Changing Uniforms/Constants ====
+
=== Creating a Shader ===
 +
 
 +
=== Changing Uniforms/Constants ===
 
In order to change the value of a uniform from Lua, use the setConstant function, it takes three arguments:
 
In order to change the value of a uniform from Lua, use the setConstant function, it takes three arguments:
 
*the uniform name
 
*the uniform name
Line 28: Line 40:
 
*the actual data to set, either as a table or as multiple arguments
 
*the actual data to set, either as a table or as multiple arguments
  
==== Associating a Shader to a Sprite ====
+
=== Associating a Shader to a Sprite ===
 
The Sprite API has a new call to deal with that: '''''Sprite:setShader(shader)''''' tells Gideros to use the specified shader for rendering the sprite. Setting back the shader to nil actually revert to the default shader.
 
The Sprite API has a new call to deal with that: '''''Sprite:setShader(shader)''''' tells Gideros to use the specified shader for rendering the sprite. Setting back the shader to nil actually revert to the default shader.
  
The API allows the creation of Shader objects from within Lua. The 'Shader.new()' constructor takes five arguments:
 
* The vertex shader code (a lua function).
 
* The fragment shader code (a lua function).
 
* A set of [[Shader Flags|numerical flags]] or 0 if none.
 
* An array of [[Shader Uniform Descriptors|uniforms/constants descriptors]].
 
* An array of [[Shader Attribute Descriptors|attributes descriptors]].
 
 
With Lua Shaders, it is assumed that the code is within the [[Shader types|vertex and fragment]] parameter functions.
 
  
==== Lua Shader Functions ====
+
=== Lua Shader Functions ===
 
'''(Please note that these functions may or may not be available, the Lua shader is still under development)'''
 
'''(Please note that these functions may or may not be available, the Lua shader is still under development)'''
  
Line 70: Line 74:
 
</div>
 
</div>
  
==== Restrictions ====
+
=== Restrictions ===
 
Don't rely on anything external, basically no globals except for attributes, uniforms and varying.
 
Don't rely on anything external, basically no globals except for attributes, uniforms and varying.
 
* No Do..While
 
* No Do..While

Revision as of 03:30, 6 November 2023

Parent: Shaders

Lua Shaders

When you write your shaders in Lua, they will be automatically translated to the relevant shader Language for the platform you are using (eg GLSL, HLSL or MSL). This is the recommended way to write your shaders in Gideros.

The Shader API allows the creation of Shader objects from within Lua. The 'Shader.new()' constructor takes five arguments:

With Lua Shaders, it is assumed that the code is within the Vertex and Fragment parameter functions.

A typical Lua Vertex and Fragment shader code looks like:

function vertex(vVertex,vColor,vTexCoord)
	local vertex = hF4(vVertex,0.0,1.0)
	fTexCoord=vTexCoord
	return vMatrix*vertex
end

function fragment()
	local frag=lF4(fColor)*texture2D(fTexture, fTexCoord)
	local coef=lF3(0.2125, 0.7154, 0.0721)
	local gray=dot(frag.rgb,coef)
	frag.rgb=lF3(gray,gray,gray)
	if (frag.a==0.0) then discard() end
	return frag
end

Creating a Shader

Changing Uniforms/Constants

In order to change the value of a uniform from Lua, use the setConstant function, it takes three arguments:

  • the uniform name
  • the type of data to set (one of the Shader.Cxxx constants)
  • the actual data to set, either as a table or as multiple arguments

Associating a Shader to a Sprite

The Sprite API has a new call to deal with that: Sprite:setShader(shader) tells Gideros to use the specified shader for rendering the sprite. Setting back the shader to nil actually revert to the default shader.


Lua Shader Functions

(Please note that these functions may or may not be available, the Lua shader is still under development)

Restrictions

Don't rely on anything external, basically no globals except for attributes, uniforms and varying.

  • No Do..While
  • No Repeat..Until
  • No tables. The only allowed form of table lookup is for vector component swizzling and access to uniform arrays
  • No strings, userdata, etc. Basically only numbers are supported
  • Mind your types! Although lua is not a typed language, shaders need types
  • Always check on all platforms: Gideros will translate your lua code to GLSL,HLSL or MSL code, but some platforms are more strict than others about what you can write, or how many arguments a common function is supposed to take

Lua Shaders Pebbles and Demos

Rain drops demo - by Hgy29