use of com.gempukku.libgdx.graph.shader.GraphShader in project gdx-graph by MarcinSc.
the class ShadowShaderRendererPipelineNodeProducer method createNodeForSingleInputs.
@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
final ShaderContextImpl shaderContext = new ShaderContextImpl(pluginPrivateDataSource);
final ObjectMap<String, GraphShader> shaders = new ObjectMap<>();
final Array<String> allShaderTags = new Array<>();
final JsonValue shaderDefinitions = data.get("shaders");
RenderOrder renderOrder = RenderOrder.valueOf(data.getString("renderOrder", "Shader_Unordered"));
final ModelRenderingStrategy renderingStrategy = createRenderingStrategy(renderOrder);
final String environmentId = data.getString("id", "");
final RenderingStrategyCallback depthStrategyCallback = new RenderingStrategyCallback(shaderContext, new Function<String, GraphShader>() {
@Override
public GraphShader apply(String s) {
return shaders.get(s);
}
});
final Array<RenderPipelineBuffer> createdPipelineBuffers = new Array<>();
final Array<Directional3DLight> shadowDirectionalLights = new Array<>();
final ObjectMap<String, PipelineNode.FieldOutput<?>> result = new ObjectMap<>();
final DefaultFieldOutput<RenderPipeline> output = new DefaultFieldOutput<>(PipelineFieldType.RenderPipeline);
result.put("output", output);
return new SingleInputsPipelineNode(result) {
private Lighting3DPrivateData lighting;
private TimeProvider timeProvider;
private GraphModelsImpl models;
private RenderPipeline pipeline;
@Override
public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
lighting = pipelineDataProvider.getPrivatePluginData(Lighting3DPrivateData.class);
timeProvider = pipelineDataProvider.getTimeProvider();
models = pipelineDataProvider.getPrivatePluginData(GraphModelsImpl.class);
for (JsonValue shaderDefinition : shaderDefinitions) {
GraphShader depthGraphShader = ShadowShaderRendererPipelineNodeProducer.createDepthShader(shaderDefinition, pipelineDataProvider.getWhitePixel().texture);
allShaderTags.add(depthGraphShader.getTag());
shaders.put(depthGraphShader.getTag(), depthGraphShader);
}
for (ObjectMap.Entry<String, GraphShader> shaderEntry : shaders.entries()) {
models.registerTag(shaderEntry.key, shaderEntry.value);
}
}
private boolean needsDepth() {
for (GraphShader shader : shaders.values()) {
if (shader.isUsingDepthTexture() && models.hasModelWithTag(shader.getTag()))
return true;
}
return false;
}
private boolean isRequiringSceneColor() {
for (GraphShader shader : shaders.values()) {
if (shader.isUsingColorTexture() && models.hasModelWithTag(shader.getTag()))
return true;
}
return false;
}
@Override
public void processPipelineRequirements(PipelineRequirements pipelineRequirements) {
if (needsDepth())
pipelineRequirements.setRequiringDepthTexture();
}
@Override
public void executeNode(PipelineRenderingContext pipelineRenderingContext, PipelineRequirementsCallback pipelineRequirementsCallback) {
final PipelineNode.FieldOutput<Boolean> processorEnabled = (PipelineNode.FieldOutput<Boolean>) inputs.get("enabled");
final PipelineNode.FieldOutput<RenderPipeline> renderPipelineInput = (PipelineNode.FieldOutput<RenderPipeline>) inputs.get("input");
boolean enabled = processorEnabled == null || processorEnabled.getValue();
boolean usesDepth = enabled && needsDepth();
RenderPipeline renderPipeline = renderPipelineInput.getValue();
this.pipeline = renderPipeline;
if (enabled) {
boolean needsDrawing = false;
Lighting3DEnvironment environment = lighting.getEnvironment(environmentId);
// Initialize directional light cameras and textures
for (Directional3DLight directionalLight : environment.getDirectionalLights()) {
if (directionalLight.isShadowsEnabled()) {
needsDrawing = true;
directionalLight.updateCamera(environment.getSceneCenter(), environment.getSceneDiameter());
if (directionalLight.getShadowFrameBuffer() == null) {
RenderPipelineBuffer shadowFrameBuffer = renderPipeline.getNewFrameBuffer(directionalLight.getShadowBufferSize(), directionalLight.getShadowBufferSize(), Pixmap.Format.RGB888, Color.WHITE);
directionalLight.setShadowFrameBuffer(shadowFrameBuffer);
createdPipelineBuffers.add(shadowFrameBuffer);
shadowDirectionalLights.add(directionalLight);
}
}
}
if (needsDrawing) {
boolean needsSceneColor = isRequiringSceneColor();
RenderPipelineBuffer drawBuffer = renderPipeline.getDefaultBuffer();
for (Directional3DLight directionalLight : environment.getDirectionalLights()) {
if (directionalLight.isShadowsEnabled()) {
RenderPipelineBuffer shadowBuffer = directionalLight.getShadowFrameBuffer();
Camera camera = directionalLight.getShadowCamera();
shaderContext.setCamera(camera);
shaderContext.setTimeProvider(timeProvider);
shaderContext.setRenderWidth(shadowBuffer.getWidth());
shaderContext.setRenderHeight(shadowBuffer.getHeight());
if (usesDepth) {
renderPipeline.enrichWithDepthBuffer(drawBuffer);
shaderContext.setDepthTexture(drawBuffer.getDepthBufferTexture());
}
if (needsSceneColor)
shaderContext.setColorTexture(drawBuffer.getColorBufferTexture());
// Drawing models on color buffer
depthStrategyCallback.prepare(pipelineRenderingContext, models);
shadowBuffer.beginColor();
renderingStrategy.processModels(models, allShaderTags, camera, depthStrategyCallback);
shadowBuffer.endColor();
}
}
}
}
output.setValue(renderPipeline);
}
@Override
public void endFrame() {
for (RenderPipelineBuffer createdPipelineBuffer : createdPipelineBuffers) {
pipeline.returnFrameBuffer(createdPipelineBuffer);
}
createdPipelineBuffers.clear();
for (Directional3DLight shadowDirectionalLight : shadowDirectionalLights) {
shadowDirectionalLight.setShadowFrameBuffer(null);
}
shadowDirectionalLights.clear();
}
@Override
public void dispose() {
for (GraphShader shader : shaders.values()) {
shader.dispose();
}
}
};
}
use of com.gempukku.libgdx.graph.shader.GraphShader in project gdx-graph by MarcinSc.
the class ModelShaderRendererPipelineNodeProducer method createNodeForSingleInputs.
@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
final ShaderContextImpl shaderContext = new ShaderContextImpl(pluginPrivateDataSource);
final ObjectMap<String, ShaderGroup> shaderGroups = new ObjectMap<>();
final Array<String> allShaderTags = new Array<>();
final Array<String> depthDrawingShaderTags = new Array<>();
final JsonValue shaderDefinitions = data.get("shaders");
RenderOrder renderOrder = RenderOrder.valueOf(data.getString("renderOrder", "Shader_Unordered"));
final ModelRenderingStrategy renderingStrategy = createRenderingStrategy(renderOrder);
final RenderingStrategyCallback colorStrategyCallback = new RenderingStrategyCallback(shaderContext, new Function<String, GraphShader>() {
@Override
public GraphShader apply(String s) {
return shaderGroups.get(s).getColorShader();
}
});
final RenderingStrategyCallback depthStrategyCallback = new RenderingStrategyCallback(shaderContext, new Function<String, GraphShader>() {
@Override
public GraphShader apply(String s) {
return shaderGroups.get(s).getDepthShader();
}
});
final ObjectMap<String, PipelineNode.FieldOutput<?>> result = new ObjectMap<>();
final DefaultFieldOutput<RenderPipeline> output = new DefaultFieldOutput<>(PipelineFieldType.RenderPipeline);
result.put("output", output);
return new SingleInputsPipelineNode(result) {
private FullScreenRender fullScreenRender;
private TimeProvider timeProvider;
private GraphModelsImpl models;
private WhitePixel whitePixel;
@Override
public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
fullScreenRender = pipelineDataProvider.getFullScreenRender();
timeProvider = pipelineDataProvider.getTimeProvider();
models = pipelineDataProvider.getPrivatePluginData(GraphModelsImpl.class);
whitePixel = pipelineDataProvider.getWhitePixel();
for (JsonValue shaderDefinition : shaderDefinitions) {
ShaderGroup shaderGroup = new ShaderGroup(shaderDefinition);
shaderGroup.initialize(whitePixel);
allShaderTags.add(shaderGroup.getTag());
shaderGroups.put(shaderGroup.getTag(), shaderGroup);
if (shaderGroup.getColorShader().isDepthWriting())
depthDrawingShaderTags.add(shaderGroup.getTag());
}
for (ShaderGroup shaderGroup : shaderGroups.values()) {
GraphShader shader = shaderGroup.getColorShader();
models.registerTag(shader.getTag(), shader);
}
}
private void initializeDepthShaders() {
for (String depthDrawingShaderTag : depthDrawingShaderTags) {
shaderGroups.get(depthDrawingShaderTag).initializeDepthShader(whitePixel);
}
}
private boolean needsDepth() {
for (ShaderGroup shaderGroup : shaderGroups.values()) {
GraphShader colorShader = shaderGroup.getColorShader();
if (colorShader.isUsingDepthTexture() && models.hasModelWithTag(colorShader.getTag()))
return true;
}
return false;
}
private boolean isRequiringSceneColor() {
for (ShaderGroup shaderGroup : shaderGroups.values()) {
GraphShader colorShader = shaderGroup.getColorShader();
if (colorShader.isUsingColorTexture() && models.hasModelWithTag(colorShader.getTag()))
return true;
}
return false;
}
@Override
public void processPipelineRequirements(PipelineRequirements pipelineRequirements) {
if (needsDepth())
pipelineRequirements.setRequiringDepthTexture();
}
@Override
public void executeNode(PipelineRenderingContext pipelineRenderingContext, PipelineRequirementsCallback pipelineRequirementsCallback) {
final PipelineNode.FieldOutput<Boolean> processorEnabled = (PipelineNode.FieldOutput<Boolean>) inputs.get("enabled");
final PipelineNode.FieldOutput<Camera> cameraInput = (PipelineNode.FieldOutput<Camera>) inputs.get("camera");
final PipelineNode.FieldOutput<RenderPipeline> renderPipelineInput = (PipelineNode.FieldOutput<RenderPipeline>) inputs.get("input");
RenderPipeline renderPipeline = renderPipelineInput.getValue();
boolean enabled = processorEnabled == null || processorEnabled.getValue();
if (enabled) {
boolean needsToDrawDepth = pipelineRequirementsCallback.getPipelineRequirements().isRequiringDepthTexture();
if (needsToDrawDepth)
initializeDepthShaders();
boolean usesDepth = needsDepth();
boolean needsSceneColor = isRequiringSceneColor();
RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
Camera camera = cameraInput.getValue();
shaderContext.setCamera(camera);
shaderContext.setTimeProvider(timeProvider);
shaderContext.setRenderWidth(currentBuffer.getWidth());
shaderContext.setRenderHeight(currentBuffer.getHeight());
if (needsToDrawDepth || usesDepth) {
renderPipeline.enrichWithDepthBuffer(currentBuffer);
shaderContext.setDepthTexture(currentBuffer.getDepthBufferTexture());
}
RenderPipelineBuffer sceneColorBuffer = null;
if (needsSceneColor)
sceneColorBuffer = setupColorTexture(renderPipeline, currentBuffer, pipelineRenderingContext);
// Drawing models on color buffer
colorStrategyCallback.prepare(pipelineRenderingContext, models);
currentBuffer.beginColor();
renderingStrategy.processModels(models, allShaderTags, camera, colorStrategyCallback);
currentBuffer.endColor();
if (needsToDrawDepth) {
// Drawing models on depth buffer
depthStrategyCallback.prepare(pipelineRenderingContext, models);
currentBuffer.beginDepth();
renderingStrategy.processModels(models, depthDrawingShaderTags, camera, depthStrategyCallback);
currentBuffer.endDepth();
}
if (sceneColorBuffer != null)
renderPipeline.returnFrameBuffer(sceneColorBuffer);
}
output.setValue(renderPipeline);
}
private RenderPipelineBuffer setupColorTexture(final RenderPipeline renderPipeline, final RenderPipelineBuffer currentBuffer, PipelineRenderingContext pipelineRenderingContext) {
RenderPipelineBuffer sceneColorBuffer = renderPipeline.getNewFrameBuffer(currentBuffer, Color.BLACK);
shaderContext.setColorTexture(sceneColorBuffer.getColorBufferTexture());
renderPipeline.drawTexture(currentBuffer, sceneColorBuffer, pipelineRenderingContext, fullScreenRender);
return sceneColorBuffer;
}
@Override
public void dispose() {
for (ShaderGroup shaderGroup : shaderGroups.values()) {
shaderGroup.dispose();
}
}
};
}
Aggregations