mirror of
https://github.com/Dark98/SliceBeam.git
synced 2026-07-02 16:49:02 +00:00
Use instanced shaders manager
This commit is contained in:
@@ -143,6 +143,7 @@ public class SetupActivity extends AppCompatActivity {
|
||||
|
||||
private GLSurfaceView backgroundView;
|
||||
private GLModel backgroundModel;
|
||||
private GLShadersManager shadersManager;
|
||||
|
||||
private int titleY;
|
||||
private float backgroundProgress;
|
||||
@@ -396,7 +397,8 @@ public class SetupActivity extends AppCompatActivity {
|
||||
super.surfaceDestroyed(holder);
|
||||
backgroundModel.release();
|
||||
backgroundModel = null;
|
||||
GLShadersManager.clearShaders();
|
||||
shadersManager.clearShaders();
|
||||
shadersManager = null;
|
||||
}
|
||||
};
|
||||
backgroundView.setEGLContextClientVersion(3);
|
||||
@@ -410,6 +412,7 @@ public class SetupActivity extends AppCompatActivity {
|
||||
if (backgroundModel == null) {
|
||||
backgroundModel = new GLModel();
|
||||
backgroundModel.initBackgroundTriangles();
|
||||
shadersManager = new GLShadersManager();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,7 +429,7 @@ public class SetupActivity extends AppCompatActivity {
|
||||
|
||||
if (backgroundModel != null) {
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_BEAM_INTRO);
|
||||
GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_BEAM_INTRO);
|
||||
shader.startUsing();
|
||||
int topColor = ThemesRepo.getColor(android.R.attr.colorAccent);
|
||||
int bottomColor = ThemesRepo.getColor(android.R.attr.windowBackground);
|
||||
|
||||
@@ -39,13 +39,13 @@ public class CoordAxes {
|
||||
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()) {
|
||||
arrow.stilizedArrow(tipRadius, tipLength, stemRadius, stemLength);
|
||||
}
|
||||
|
||||
GLShaderProgram currentShader = GLShadersManager.getCurrentShader();
|
||||
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_GOURAUD_LIGHT);
|
||||
GLShaderProgram currentShader = shadersManager.getCurrentShader();
|
||||
GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_GOURAUD_LIGHT);
|
||||
if (currentShader != null) {
|
||||
currentShader.stopUsing();
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
// Instance values, should be released
|
||||
private Bed3D bed;
|
||||
private int lastConfigUid;
|
||||
private GLShadersManager shadersManager;
|
||||
private GLModel backgroundModel;
|
||||
private GLModel selectionModel;
|
||||
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);
|
||||
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_BACKGROUND);
|
||||
GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_BACKGROUND);
|
||||
shader.startUsing();
|
||||
shader.setUniformColor("top_color", ThemesRepo.getColor(R.attr.backgroundColorTop));
|
||||
shader.setUniformColor("bottom_color", ThemesRepo.getColor(R.attr.backgroundColorBottom));
|
||||
@@ -225,7 +226,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
configureBed();
|
||||
}
|
||||
if (bed.isValid()) {
|
||||
bed.render(bottom, camera.getViewModelMatrix(), projectionMatrix, 1f / camera.getZoom());
|
||||
bed.render(shadersManager, bottom, camera.getViewModelMatrix(), projectionMatrix, 1f / camera.getZoom());
|
||||
}
|
||||
|
||||
if (isViewerEnabled) {
|
||||
@@ -239,7 +240,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
viewer.render(camera.getViewModelMatrix(), projectionMatrix);
|
||||
}
|
||||
if (viewer == null && model != null) {
|
||||
shader = GLShadersManager.get(GLShadersManager.SHADER_GOURAUD_LIGHT);
|
||||
shader = shadersManager.get(GLShadersManager.SHADER_GOURAUD_LIGHT);
|
||||
shader.startUsing();
|
||||
int color = ThemesRepo.getColor(android.R.attr.colorAccent);
|
||||
int hoverColor = ThemesRepo.getColor(R.attr.modelHoverColor);
|
||||
@@ -301,7 +302,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
if (selected) {
|
||||
shader.stopUsing();
|
||||
|
||||
GLShaderProgram flat = GLShadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
GLShaderProgram flat = shadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
glLineWidth(ViewUtils.dp(1.5f));
|
||||
|
||||
flat.startUsing();
|
||||
@@ -323,7 +324,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
if (isInFlattenMode) {
|
||||
shader.stopUsing();
|
||||
|
||||
GLShaderProgram flat = GLShadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
GLShaderProgram flat = shadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
|
||||
flat.startUsing();
|
||||
glEnable(GL_BLEND);
|
||||
@@ -603,6 +604,7 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
|
||||
backgroundModel = new GLModel();
|
||||
backgroundModel.initBackgroundTriangles();
|
||||
shadersManager = new GLShadersManager();
|
||||
if (!bed.isValid()) return;
|
||||
|
||||
if (cameraIsDirty) {
|
||||
@@ -625,7 +627,10 @@ public class GLRenderer implements GLSurfaceView.Renderer {
|
||||
}
|
||||
|
||||
public void onDestroy() {
|
||||
GLShadersManager.clearShaders();
|
||||
if (shadersManager != null) {
|
||||
shadersManager.clearShaders();
|
||||
shadersManager = null;
|
||||
}
|
||||
if (backgroundModel != null) {
|
||||
backgroundModel.release();
|
||||
backgroundModel = null;
|
||||
|
||||
@@ -83,7 +83,7 @@ public class Bed3D {
|
||||
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(projectionMatrix.length == 16);
|
||||
|
||||
@@ -92,12 +92,12 @@ public class Bed3D {
|
||||
DoubleMatrix.translateM(modelMatrix, 0, -getVolumeMin().x * 2, -getVolumeMin().y * 2, -getVolumeMin().z);
|
||||
}
|
||||
DoubleMatrix.multiplyMM(outModelMatrix, 0, viewModelMatrix, 0, modelMatrix, 0);
|
||||
renderDefaultBed(bottom, outModelMatrix, projectionMatrix);
|
||||
axes.render(viewModelMatrix, projectionMatrix, 0.25f, invZoom);
|
||||
renderDefaultBed(shadersManager, bottom, outModelMatrix, projectionMatrix);
|
||||
axes.render(shadersManager, viewModelMatrix, projectionMatrix, 0.25f, invZoom);
|
||||
}
|
||||
|
||||
private void renderDefaultBed(boolean bottom, double[] viewModelMatrix, double[] projectionMatrix) {
|
||||
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
private void renderDefaultBed(GLShadersManager shadersManager, boolean bottom, double[] viewModelMatrix, double[] projectionMatrix) {
|
||||
GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_FLAT);
|
||||
shader.startUsing();
|
||||
|
||||
shader.setUniformMatrix4fv("view_model_matrix", viewModelMatrix);
|
||||
@@ -126,8 +126,8 @@ public class Bed3D {
|
||||
shader.stopUsing();
|
||||
}
|
||||
|
||||
private void renderTexturedBed(boolean bottom, float[] viewModelMatrix, float[] projectionMatrix) {
|
||||
GLShaderProgram shader = GLShadersManager.get(GLShadersManager.SHADER_PRINTBED);
|
||||
private void renderTexturedBed(GLShadersManager shadersManager, boolean bottom, float[] viewModelMatrix, float[] projectionMatrix) {
|
||||
GLShaderProgram shader = shadersManager.get(GLShadersManager.SHADER_PRINTBED);
|
||||
shader.startUsing();
|
||||
|
||||
shader.setUniform3f("view_model_matrix", viewModelMatrix);
|
||||
|
||||
@@ -8,7 +8,9 @@ import androidx.annotation.StringDef;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GLShadersManager {
|
||||
@@ -52,7 +54,9 @@ public class GLShadersManager {
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
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
|
||||
public GLShaderProgram get(@Nullable Object 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()) {
|
||||
program.release();
|
||||
}
|
||||
shaders.clear();
|
||||
managers.remove(this);
|
||||
}
|
||||
|
||||
public static GLShaderProgram get(@ShaderType String key) {
|
||||
public GLShaderProgram get(@ShaderType String key) {
|
||||
return shaders.get(key);
|
||||
}
|
||||
|
||||
@Keep
|
||||
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;
|
||||
}
|
||||
|
||||
public static GLShaderProgram getCurrentShader() {
|
||||
public GLShaderProgram getCurrentShader() {
|
||||
int[] idRef = {0};
|
||||
GLES30.glGetIntegerv(GLES30.GL_CURRENT_PROGRAM, idRef, 0);
|
||||
int id = idRef[0];
|
||||
|
||||
Reference in New Issue
Block a user