Skip to content

Commit

Permalink
Remove generic setUniform method - switch pattern matching slow?
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Jun 28, 2024
1 parent 8b3157c commit 501be38
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package nz.net.ultraq.redhorizon.classic.shaders

import nz.net.ultraq.redhorizon.engine.graphics.Attribute
import nz.net.ultraq.redhorizon.engine.graphics.Material
import nz.net.ultraq.redhorizon.engine.graphics.ShaderConfig
import nz.net.ultraq.redhorizon.engine.graphics.Uniform

Expand All @@ -40,9 +39,11 @@ class PalettedSpriteShader extends ShaderConfig {
shader.setUniform('adjustmentMap', material.adjustmentMap)
},
{ shader, material, window ->
Material.KEYS_SPRITES.each { key ->
shader.setUniformGeneric(key, material.attributes[key])
}
shader.setUniform('frame', material.frame)
shader.setUniform('framesHorizontal', material.framesHorizontal)
shader.setUniform('framesVertical', material.framesVertical)
shader.setUniform('frameStepX', material.frameStepX)
shader.setUniform('frameStepY', material.frameStepY)
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,29 @@ class Material {
private static final String KEY_FRAME_STEP_X = 'frameStepX'
private static final String KEY_FRAME_STEP_Y = 'frameStepY'

static final String[] KEYS_SPRITES = [
KEY_FRAME, KEY_FRAMES_HORIZONTAL, KEY_FRAMES_VERTICAL, KEY_FRAME_STEP_X, KEY_FRAME_STEP_Y
]

final Map<String, Object> attributes = [:]
Texture texture

int getFrame() {
return attributes[KEY_FRAME]
}

float getFrameStepX() {
return attributes[KEY_FRAME_STEP_X]
}

float getFrameStepY() {
return attributes[KEY_FRAME_STEP_Y]
}

int getFramesHorizontal() {
return attributes[KEY_FRAMES_HORIZONTAL]
}

int getFramesVertical() {
return attributes[KEY_FRAMES_VERTICAL]
}

void setFrame(int frame) {
attributes[KEY_FRAME] = frame
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ abstract class Shader implements GraphicsResource {
final Attribute[] attributes
final Uniform[] uniforms

private final Map<String, Object> lastValues = [:]

/**
* Update a shader's uniforms using the given context.
*/
Expand All @@ -47,31 +45,6 @@ abstract class Shader implements GraphicsResource {
uniforms*.apply(this, material, window)
}

/**
* Apply a data uniform to the shader. If {@code data} is an array, it can be
* used to determine the shader type (eg: 2 floats = vec2).
*/
void setUniformGeneric(String name, Object data) {

if (data == null) {
throw new IllegalArgumentException("Data value for key ${name} was null")
}

// Only perform an update if there was a change in the value since last time
if (lastValues[name] != data) {
lastValues[name] = data

switch (data) {
case Float -> setUniform(name, data)
case Float[] -> setUniform(name, (float[])data)
case Integer -> setUniform(name, data)
case Integer[] -> setUniform(name, (int[])data)
case Matrix4f -> setUniform(name, data)
default -> throw new UnsupportedOperationException("Data type of ${data.class.simpleName} not supported")
}
}
}

/**
* Apply a data uniform to the shader. The type of data is determined by the
* size of the data array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package nz.net.ultraq.redhorizon.engine.graphics.opengl

import nz.net.ultraq.redhorizon.engine.graphics.Attribute
import nz.net.ultraq.redhorizon.engine.graphics.Material
import nz.net.ultraq.redhorizon.engine.graphics.ShaderConfig
import nz.net.ultraq.redhorizon.engine.graphics.Uniform

Expand All @@ -37,9 +36,11 @@ class SpriteShader extends ShaderConfig {
shader.setUniformTexture('mainTexture', 0, material.texture)
},
{ shader, material, window ->
Material.KEYS_SPRITES.each { key ->
shader.setUniformGeneric(key, material[key])
}
shader.setUniform('frame', material.frame)
shader.setUniform('framesHorizontal', material.framesHorizontal)
shader.setUniform('framesVertical', material.framesVertical)
shader.setUniform('frameStepX', material.frameStepX)
shader.setUniform('frameStepY', material.frameStepY)
}
]
}

0 comments on commit 501be38

Please sign in to comment.