java.lang.Object
net.ixdarklord.coolcatcanvas.internal.client.render.CanvasProgram
All Implemented Interfaces:
AutoCloseable

public final class CanvasProgram extends Object implements AutoCloseable
A GLSL program linked from a vertex and a fragment shader in resources. Sources go through vanilla's preprocessor, with namespaced imports: #moj_import <namespace:file.glsl> reads assets/<namespace>/shaders/include/, #moj_import "file.glsl" a path next to the importing shader. Extra #defines go right after #version. The Position attribute is bound to location 0, where 1.20.1's vertex formats put it; compile(ResourceManager, String, ResourceLocation, ResourceLocation, List, List) binds more.

Kept out of vanilla's program caches, so its names never collide with vanilla's and reloading vanilla's shaders never frees it. Binding one tells vanilla its own shader is no longer bound (use()). Uniform locations and the values last uploaded are remembered, so setting a uniform to the value it already has (samplers, matrices that didn't change, every frame) costs no GL call. Render thread only.

  • Method Details

    • compile

      public static CanvasProgram compile(net.minecraft.server.packs.resources.ResourceManager resources, String name, net.minecraft.resources.ResourceLocation vertex, net.minecraft.resources.ResourceLocation fragment, List<String> defines) throws IOException
      Parameters:
      vertex - the vertex shader's full path, e.g. minecraft:shaders/program/sobel.vsh
      fragment - the fragment shader's full path
      defines - names to #define in both shaders
      Throws:
      IOException - if a file is missing, or a shader doesn't compile or link
    • compile

      public static CanvasProgram compile(net.minecraft.server.packs.resources.ResourceManager resources, String name, net.minecraft.resources.ResourceLocation vertex, net.minecraft.resources.ResourceLocation fragment, List<String> defines, List<String> attributes) throws IOException
      Parameters:
      attributes - vertex attribute names, bound to locations 0, 1, ... in order (as the vertex format's elements)
      Throws:
      IOException
    • name

      public String name()
    • id

      public int id()
    • use

      public void use()
      Binds the program. Vanilla remembers the core and post shaders it bound last and skips binding the same one again, so binding this one behind its back would have its next draw with that shader run this program instead (or none, after unuse()), with its uniforms uploaded to the wrong program. Its records are cleared here, so it binds its own again.
    • unuse

      public static void unuse()
    • location

      public int location(String uniform)
      The uniform's location, or -1 if the program doesn't use it.
    • has

      public boolean has(String uniform)
    • setInt

      public void setInt(String uniform, int value)
    • setFloat

      public void setFloat(String uniform, float value)
    • setVec2

      public void setVec2(String uniform, float x, float y)
    • setVec4

      public void setVec4(String uniform, float x, float y, float z, float w)
    • setVec4

      public void setVec4(String uniform, float[] xyzw)
    • setVec4Array

      public void setVec4Array(String uniform, float[] values)
      A vec4[], from 4 floats per element.
    • setMat4

      public void setMat4(String uniform, org.joml.Matrix4fc matrix)
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable