Skip to content

Commit

Permalink
fix: Automatically add version statements to the GL32C shaders. Also …
Browse files Browse the repository at this point in the history
…add a few convenience methods to GL32MacIssueHandler.
  • Loading branch information
crykn committed Mar 2, 2024
1 parent ee4c75d commit a117c8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import de.damios.guacamole.Exceptions;
import de.damios.guacamole.concurrent.ThreadHandler;
import de.damios.guacamole.gdx.graphics.ShaderCompatibilityHelper;
import de.damios.guacamole.gdx.log.Logger;
import de.damios.guacamole.gdx.log.LoggerService;
import de.damios.guacamole.gdx.reflection.ReflectionUtils;
Expand Down Expand Up @@ -183,10 +182,7 @@ public void create() {

// Sprite batch
injector.bindToInstance(SpriteBatch.class,
new SpriteBatch(1000,
ShaderCompatibilityHelper.mustUse32CShader()
? GL32CMacIssueHandler.createSpriteBatchShader()
: null));
GL32CMacIssueHandler.createSpriteBatch());

// Start args
injector.bindToInstance(StartArguments.class, startArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

package de.eskalon.commons.utils;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import de.damios.guacamole.gdx.graphics.ShaderCompatibilityHelper;
import de.damios.guacamole.gdx.graphics.ShaderProgramFactory;

/**
Expand All @@ -26,6 +29,8 @@
* This is needed when OpenGL 3+ features (e.g. multiple render targets) are
* used on mac, as mac only supports core profiles and those are not backward
* compatible.
* <p>
* Please note that prepends are ignored for shaders created by this class.
*
* @author damios
* @see <a href=
Expand All @@ -42,7 +47,8 @@ private GL32CMacIssueHandler() {

public static ShaderProgram createSpriteBatchShader() {
// @formatter:off
String vertexShader = "in vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
String vertexShader = "#version 150\n" //
+ "in vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
+ "in vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
+ "in vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
+ "uniform mat4 u_projTrans;\n" //
Expand All @@ -58,7 +64,8 @@ public static ShaderProgram createSpriteBatchShader() {
+ " gl_Position = u_projTrans * "
+ ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
+ "}\n";
String fragmentShader = "#ifdef GL_ES\n" //
String fragmentShader = "#version 150\n" //
+ "#ifdef GL_ES\n" //
+ "#define LOWP lowp\n" //
+ "precision mediump float;\n" //
+ "#else\n" //
Expand All @@ -72,12 +79,21 @@ public static ShaderProgram createSpriteBatchShader() {
+ " fragColor = v_color * texture(u_texture, v_texCoords);\n" //
+ "}";
// @formatter:on
return ShaderProgramFactory.fromString(vertexShader, fragmentShader);
return ShaderProgramFactory.fromString(vertexShader, fragmentShader,
true, true);
}

public static SpriteBatch createSpriteBatch() {
return new SpriteBatch(1000,
ShaderCompatibilityHelper.mustUse32CShader()
? createSpriteBatchShader()
: null);
}

private static String createImmediateModeRenderer20VertexShader(
boolean hasNormals, boolean hasColors, int numTexCoords) {
String shader = "in vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n"
String shader = "#version 150\n" + "in vec4 "
+ ShaderProgram.POSITION_ATTRIBUTE + ";\n"
+ (hasNormals
? "in vec3 " + ShaderProgram.NORMAL_ATTRIBUTE + ";\n"
: "")
Expand Down Expand Up @@ -113,8 +129,8 @@ private static String createImmediateModeRenderer20VertexShader(

private static String createImmediateModeRenderer20FragmentShader(
boolean hasNormals, boolean hasColors, int numTexCoords) {
String shader = "#ifdef GL_ES\n" + "precision mediump float;\n"
+ "#endif\n";
String shader = "#version 150\n" + "#ifdef GL_ES\n"
+ "precision mediump float;\n" + "#endif\n";

if (hasColors)
shader += "in vec4 v_col;\n";
Expand Down Expand Up @@ -143,18 +159,25 @@ private static String createImmediateModeRenderer20FragmentShader(
return shader;
}

/**
* Returns a new instance of the default shader used by SpriteBatch for GL2
* when no shader is specified.
*/
public static ShaderProgram createImmediateModeRenderer20DefaultShader(
boolean hasNormals, boolean hasColors, int numTexCoords) {
String vertexShader = createImmediateModeRenderer20VertexShader(
hasNormals, hasColors, numTexCoords);
String fragmentShader = createImmediateModeRenderer20FragmentShader(
hasNormals, hasColors, numTexCoords);
ShaderProgram program = new ShaderProgram(vertexShader, fragmentShader);
return program;
return ShaderProgramFactory.fromString(vertexShader, fragmentShader,
true, true);
}

public static ShaderProgram createImmediateModeRenderer20DefaultShader() {
return createImmediateModeRenderer20DefaultShader(false, true, 0);
}

public static ShapeRenderer createShapeRenderer() {
return new ShapeRenderer(5000,
ShaderCompatibilityHelper.mustUse32CShader()
? createImmediateModeRenderer20DefaultShader()
: null);
}

}

0 comments on commit a117c8e

Please sign in to comment.