
com.gempukku.libgdx.graph.plugin.screen.ScreenShaderRendererPipelineNodeProducer Maven / Gradle / Ivy
package com.gempukku.libgdx.graph.plugin.screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.ObjectMap;
import com.gempukku.libgdx.graph.loader.GraphLoader;
import com.gempukku.libgdx.graph.pipeline.RenderPipeline;
import com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer;
import com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext;
import com.gempukku.libgdx.graph.pipeline.producer.node.OncePerFrameJobPipelineNode;
import com.gempukku.libgdx.graph.pipeline.producer.node.PipelineInitializationFeedback;
import com.gempukku.libgdx.graph.pipeline.producer.node.PipelineNode;
import com.gempukku.libgdx.graph.pipeline.producer.node.PipelineNodeProducerImpl;
import com.gempukku.libgdx.graph.pipeline.producer.node.PipelineRequirements;
import com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.ShaderContextImpl;
import com.gempukku.libgdx.graph.plugin.PluginPrivateDataSource;
import com.gempukku.libgdx.graph.plugin.screen.config.ScreenShaderRendererPipelineNodeConfiguration;
import com.gempukku.libgdx.graph.shader.common.CommonShaderConfiguration;
import com.gempukku.libgdx.graph.shader.common.PropertyAsUniformShaderConfiguration;
import com.gempukku.libgdx.graph.shader.config.GraphConfiguration;
import com.gempukku.libgdx.graph.util.WhitePixel;
public class ScreenShaderRendererPipelineNodeProducer extends PipelineNodeProducerImpl {
private static GraphConfiguration[] configurations = new GraphConfiguration[]{new CommonShaderConfiguration(), new PropertyAsUniformShaderConfiguration(), new ScreenShaderConfiguration()};
private PluginPrivateDataSource pluginPrivateDataSource;
public ScreenShaderRendererPipelineNodeProducer(PluginPrivateDataSource pluginPrivateDataSource) {
super(new ScreenShaderRendererPipelineNodeConfiguration());
this.pluginPrivateDataSource = pluginPrivateDataSource;
}
@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap> inputFields) {
final WhitePixel whitePixel = new WhitePixel();
final ShaderContextImpl shaderContext = new ShaderContextImpl(pluginPrivateDataSource);
final Array shaderArray = new Array<>();
final JsonValue shaderDefinitions = data.get("shaders");
for (JsonValue shaderDefinition : shaderDefinitions) {
JsonValue shaderGraph = shaderDefinition.get("shader");
String tag = shaderDefinition.getString("tag");
final ScreenGraphShader shader = GraphLoader.loadGraph(shaderGraph, new ScreenShaderLoaderCallback(whitePixel.texture, configurations));
shader.setTag(tag);
shaderArray.add(shader);
}
final PipelineNode.FieldOutput processorEnabled = (PipelineNode.FieldOutput) inputFields.get("enabled");
final PipelineNode.FieldOutput cameraInput = (PipelineNode.FieldOutput) inputFields.get("camera");
final PipelineNode.FieldOutput renderPipelineInput = (PipelineNode.FieldOutput) inputFields.get("input");
return new OncePerFrameJobPipelineNode(configuration, inputFields) {
@Override
public void initializePipeline(PipelineInitializationFeedback pipelineInitializationFeedback) {
GraphScreenShadersImpl graphScreenShaders = pipelineInitializationFeedback.getPrivatePluginData(GraphScreenShadersImpl.class);
for (ScreenGraphShader shader : shaderArray) {
graphScreenShaders.registerTag(shader.getTag(), shader);
}
}
public boolean needsDepth() {
for (ScreenGraphShader shader : shaderArray) {
if (shader.isUsingDepthTexture())
return true;
}
return false;
}
public boolean isRequiringSceneColor() {
for (ScreenGraphShader shader : shaderArray) {
if (shader.isUsingColorTexture())
return true;
}
return false;
}
@Override
protected void executeJob(PipelineRenderingContext pipelineRenderingContext, PipelineRequirements pipelineRequirements, ObjectMap outputValues) {
boolean usesDepth = false;
boolean enabled = processorEnabled == null || processorEnabled.getValue(pipelineRenderingContext, null);
if (enabled) {
if (needsDepth()) {
usesDepth = true;
pipelineRequirements.setRequiringDepthTexture();
}
}
RenderPipeline renderPipeline = renderPipelineInput.getValue(pipelineRenderingContext, pipelineRequirements);
if (enabled) {
boolean needsSceneColor = isRequiringSceneColor();
RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
if (usesDepth) {
renderPipeline.enrichWithDepthBuffer(currentBuffer);
}
if (cameraInput != null) {
Camera camera = cameraInput.getValue(pipelineRenderingContext, null);
shaderContext.setCamera(camera);
}
shaderContext.setTimeProvider(pipelineRenderingContext.getTimeProvider());
shaderContext.setRenderWidth(currentBuffer.getWidth());
shaderContext.setRenderHeight(currentBuffer.getHeight());
RenderPipelineBuffer sceneColorBuffer = null;
if (needsSceneColor) {
sceneColorBuffer = setupColorTexture(renderPipeline, currentBuffer, pipelineRenderingContext);
}
currentBuffer.beginColor();
for (ScreenGraphShader shader : shaderArray) {
shaderContext.setPropertyContainer(shader.getPropertyContainer());
shader.begin(shaderContext, pipelineRenderingContext.getRenderContext());
shader.render(shaderContext, pipelineRenderingContext.getFullScreenRender());
shader.end();
}
currentBuffer.endColor();
if (sceneColorBuffer != null)
renderPipeline.returnFrameBuffer(sceneColorBuffer);
}
OutputValue output = outputValues.get("output");
if (output != null)
output.setValue(renderPipeline);
}
private RenderPipelineBuffer setupColorTexture(final RenderPipeline renderPipeline, final RenderPipelineBuffer currentBuffer,
PipelineRenderingContext pipelineRenderingContext) {
RenderPipelineBuffer sceneColorBuffer = renderPipeline.getNewFrameBuffer(currentBuffer);
shaderContext.setColorTexture(sceneColorBuffer.getColorBufferTexture());
renderPipeline.drawTexture(currentBuffer, sceneColorBuffer, pipelineRenderingContext);
return sceneColorBuffer;
}
@Override
public void dispose() {
for (ScreenGraphShader shader : shaderArray) {
shader.dispose();
}
whitePixel.dispose();
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy