Before a ShaderBlock can be used in a Shader, the Shader has to have the block's variables defined in its source code. This can be a tedious process, so you can call this function to return a GLSL string that contains this definition. Roughly, it will look something like this:

layout(std140) uniform <label> {
  <type> <name>[<count>];
} <namespace>;
code = ShaderBlock:getShaderCode(label, namespace)


labelstringThe label of the block in the shader code. This will be used to identify it when using Shader:sendBlock.
namespacestringnilThe namespace to use when accessing the block's variables in the shader code. This can be used to prevent naming conflicts if two blocks have variables with the same name. If the namespace is nil, the block's variables will be available in the global scope.


codestringThe code that can be prepended to Shader code.


block ='uniform', {
  sizes = { 'float', 10 }

code = [[
  #ifdef VERTEX
    ]] .. block:getShaderCode('MyBlock', 'sizeBlock') .. [[

    // vertex shader goes here,
    // it can access sizeBlock.sizes

  #ifdef PIXEL
    // fragment shader goes here

shader =, code)
shader:sendBlock('MyBlock', block)

See also