Use instanced shaders manager

This commit is contained in:
utkabobr
2025-04-06 21:24:47 +03:00
parent 4c4469b1fd
commit bc1007aa59
5 changed files with 46 additions and 23 deletions
@@ -143,6 +143,7 @@ public class SetupActivity extends AppCompatActivity {
private GLSurfaceView backgroundView; private GLSurfaceView backgroundView;
private GLModel backgroundModel; private GLModel backgroundModel;
private GLShadersManager shadersManager;
private int titleY; private int titleY;
private float backgroundProgress; private float backgroundProgress;
@@ -396,7 +397,8 @@ public class SetupActivity extends AppCompatActivity {
super.surfaceDestroyed(holder); super.surfaceDestroyed(holder);
backgroundModel.release(); backgroundModel.release();
backgroundModel = null; backgroundModel = null;
GLShadersManager.clearShaders(); shadersManager.clearShaders();
shadersManager = null;
} }
}; };
backgroundView.setEGLContextClientVersion(3); backgroundView.setEGLContextClientVersion(3);
@@ -410,6 +412,7 @@ public class SetupActivity extends AppCompatActivity {
if (backgroundModel == null) { if (backgroundModel == null) {
backgroundModel = new GLModel(); backgroundModel = new GLModel();
backgroundModel.initBackgroundTriangles(); backgroundModel.initBackgroundTriangles();
shadersManager = new GLShadersManager();
} }
} }
@@ -426,7 +429,7 @@ public class SetupActivity extends AppCompatActivity {
if (backgroundModel != null) { if (backgroundModel != null) {
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_BEAM_INTRO); GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_BEAM_INTRO);
shader.startUsing(); shader.startUsing();
int topColor = ThemesRepo.getColor(android.R.attr.colorAccent); int topColor = ThemesRepo.getColor(android.R.attr.colorAccent);
int bottomColor = ThemesRepo.getColor(android.R.attr.windowBackground); int bottomColor = ThemesRepo.getColor(android.R.attr.windowBackground);
@@ -39,13 +39,13 @@ public class CoordAxes {
arrow.render(); arrow.render();
} }
public void render(double[] viewMatrix, double[] projectionMatrix, float emissionFactor, float invZoom) { public void render(GLShadersManager shadersManager, double[] viewMatrix, double[] projectionMatrix, float emissionFactor, float invZoom) {
if (!arrow.isInitialized()) { if (!arrow.isInitialized()) {
arrow.stilizedArrow(tipRadius, tipLength, stemRadius, stemLength); arrow.stilizedArrow(tipRadius, tipLength, stemRadius, stemLength);
} }
GLShaderProgram currentShader = GLShadersManager.getCurrentShader(); GLShaderProgram currentShader = shadersManager.getCurrentShader();
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_GOURAUD_LIGHT); GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_GOURAUD_LIGHT);
if (currentShader != null) { if (currentShader != null) {
currentShader.stopUsing(); currentShader.stopUsing();
} }
@@ -52,6 +52,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
// Instance values, should be released // Instance values, should be released
private Bed3D bed; private Bed3D bed;
private int lastConfigUid; private int lastConfigUid;
private GLShadersManager shadersManager;
private GLModel backgroundModel; private GLModel backgroundModel;
private GLModel selectionModel; private GLModel selectionModel;
private List<GLModel> glModels = new ArrayList<>(); private List<GLModel> glModels = new ArrayList<>();
@@ -212,7 +213,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_BACKGROUND); GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_BACKGROUND);
shader.startUsing(); shader.startUsing();
shader.setUniformColor("top_color", ThemesRepo.getColor(R.attr.backgroundColorTop)); shader.setUniformColor("top_color", ThemesRepo.getColor(R.attr.backgroundColorTop));
shader.setUniformColor("bottom_color", ThemesRepo.getColor(R.attr.backgroundColorBottom)); shader.setUniformColor("bottom_color", ThemesRepo.getColor(R.attr.backgroundColorBottom));
@@ -225,7 +226,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
configureBed(); configureBed();
} }
if (bed.isValid()) { if (bed.isValid()) {
bed.render(bottom, camera.getViewModelMatrix(), projectionMatrix, 1f / camera.getZoom()); bed.render(shadersManager, bottom, camera.getViewModelMatrix(), projectionMatrix, 1f / camera.getZoom());
} }
if (isViewerEnabled) { if (isViewerEnabled) {
@@ -239,7 +240,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
viewer.render(camera.getViewModelMatrix(), projectionMatrix); viewer.render(camera.getViewModelMatrix(), projectionMatrix);
} }
if (viewer == null && model != null) { if (viewer == null && model != null) {
shader = GLShadersManager.get(GLShadersManager.SHADER_GOURAUD_LIGHT); shader = shadersManager.get(GLShadersManager.SHADER_GOURAUD_LIGHT);
shader.startUsing(); shader.startUsing();
int color = ThemesRepo.getColor(android.R.attr.colorAccent); int color = ThemesRepo.getColor(android.R.attr.colorAccent);
int hoverColor = ThemesRepo.getColor(R.attr.modelHoverColor); int hoverColor = ThemesRepo.getColor(R.attr.modelHoverColor);
@@ -301,7 +302,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
if (selected) { if (selected) {
shader.stopUsing(); shader.stopUsing();
GLShaderProgram flat = GLShadersManager.get(GLShadersManager.SHADER_FLAT); GLShaderProgram flat = shadersManager.get(GLShadersManager.SHADER_FLAT);
glLineWidth(ViewUtils.dp(1.5f)); glLineWidth(ViewUtils.dp(1.5f));
flat.startUsing(); flat.startUsing();
@@ -323,7 +324,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
if (isInFlattenMode) { if (isInFlattenMode) {
shader.stopUsing(); shader.stopUsing();
GLShaderProgram flat = GLShadersManager.get(GLShadersManager.SHADER_FLAT); GLShaderProgram flat = shadersManager.get(GLShadersManager.SHADER_FLAT);
flat.startUsing(); flat.startUsing();
glEnable(GL_BLEND); glEnable(GL_BLEND);
@@ -603,6 +604,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
backgroundModel = new GLModel(); backgroundModel = new GLModel();
backgroundModel.initBackgroundTriangles(); backgroundModel.initBackgroundTriangles();
shadersManager = new GLShadersManager();
if (!bed.isValid()) return; if (!bed.isValid()) return;
if (cameraIsDirty) { if (cameraIsDirty) {
@@ -625,7 +627,10 @@ public class GLRenderer implements GLSurfaceView.Renderer {
} }
public void onDestroy() { public void onDestroy() {
GLShadersManager.clearShaders(); if (shadersManager != null) {
shadersManager.clearShaders();
shadersManager = null;
}
if (backgroundModel != null) { if (backgroundModel != null) {
backgroundModel.release(); backgroundModel.release();
backgroundModel = null; backgroundModel = null;
@@ -83,7 +83,7 @@ public class Bed3D {
return triangles.getRaycaster(); return triangles.getRaycaster();
} }
public void render(boolean bottom, double[] viewModelMatrix, double[] projectionMatrix, float invZoom) { public void render(GLShadersManager shadersManager, boolean bottom, double[] viewModelMatrix, double[] projectionMatrix, float invZoom) {
assertTrue(viewModelMatrix.length == 16); assertTrue(viewModelMatrix.length == 16);
assertTrue(projectionMatrix.length == 16); assertTrue(projectionMatrix.length == 16);
@@ -92,12 +92,12 @@ public class Bed3D {
DoubleMatrix.translateM(modelMatrix, 0, -getVolumeMin().x * 2, -getVolumeMin().y * 2, -getVolumeMin().z); DoubleMatrix.translateM(modelMatrix, 0, -getVolumeMin().x * 2, -getVolumeMin().y * 2, -getVolumeMin().z);
} }
DoubleMatrix.multiplyMM(outModelMatrix, 0, viewModelMatrix, 0, modelMatrix, 0); DoubleMatrix.multiplyMM(outModelMatrix, 0, viewModelMatrix, 0, modelMatrix, 0);
renderDefaultBed(bottom, outModelMatrix, projectionMatrix); renderDefaultBed(shadersManager, bottom, outModelMatrix, projectionMatrix);
axes.render(viewModelMatrix, projectionMatrix, 0.25f, invZoom); axes.render(shadersManager, viewModelMatrix, projectionMatrix, 0.25f, invZoom);
} }
private void renderDefaultBed(boolean bottom, double[] viewModelMatrix, double[] projectionMatrix) { private void renderDefaultBed(GLShadersManager shadersManager, boolean bottom, double[] viewModelMatrix, double[] projectionMatrix) {
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_FLAT); GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_FLAT);
shader.startUsing(); shader.startUsing();
shader.setUniformMatrix4fv("view_model_matrix", viewModelMatrix); shader.setUniformMatrix4fv("view_model_matrix", viewModelMatrix);
@@ -126,8 +126,8 @@ public class Bed3D {
shader.stopUsing(); shader.stopUsing();
} }
private void renderTexturedBed(boolean bottom, float[] viewModelMatrix, float[] projectionMatrix) { private void renderTexturedBed(GLShadersManager shadersManager, boolean bottom, float[] viewModelMatrix, float[] projectionMatrix) {
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_PRINTBED); GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_PRINTBED);
shader.startUsing(); shader.startUsing();
shader.setUniform3f("view_model_matrix", viewModelMatrix); shader.setUniform3f("view_model_matrix", viewModelMatrix);
@@ -8,7 +8,9 @@ import androidx.annotation.StringDef;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class GLShadersManager { public class GLShadersManager {
@@ -52,7 +54,9 @@ public class GLShadersManager {
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface ShaderType {} public @interface ShaderType {}
private final static Map<String, GLShaderProgram> shaders = new HashMap<String, GLShaderProgram>() { private final static List<GLShadersManager> managers = new ArrayList<>();
private final Map<String, GLShaderProgram> shaders = new HashMap<String, GLShaderProgram>() {
@Override @Override
public GLShaderProgram get(@Nullable Object key) { public GLShaderProgram get(@Nullable Object key) {
GLShaderProgram shader = super.get(key); GLShaderProgram shader = super.get(key);
@@ -61,24 +65,35 @@ public class GLShadersManager {
} }
}; };
public static void clearShaders() { public GLShadersManager() {
managers.add(this);
}
public void clearShaders() {
for (GLShaderProgram program : shaders.values()) { for (GLShaderProgram program : shaders.values()) {
program.release(); program.release();
} }
shaders.clear(); shaders.clear();
managers.remove(this);
} }
public static GLShaderProgram get(@ShaderType String key) { public GLShaderProgram get(@ShaderType String key) {
return shaders.get(key); return shaders.get(key);
} }
@Keep @Keep
private static long getCurrentShaderPointer() { private static long getCurrentShaderPointer() {
GLShaderProgram prog = getCurrentShader(); GLShaderProgram prog = null;
for (GLShadersManager manager : managers) {
prog = manager.getCurrentShader();
if (prog != null) {
break;
}
}
return prog != null ? prog.pointer : 0; return prog != null ? prog.pointer : 0;
} }
public static GLShaderProgram getCurrentShader() { public GLShaderProgram getCurrentShader() {
int[] idRef = {0}; int[] idRef = {0};
GLES30.glGetIntegerv(GLES30.GL_CURRENT_PROGRAM, idRef, 0); GLES30.glGetIntegerv(GLES30.GL_CURRENT_PROGRAM, idRef, 0);
int id = idRef[0]; int id = idRef[0];