Search in sources :

Example 1 with RenderPipelineBuffer

use of com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer in project gdx-graph by MarcinSc.

the class ShadowBlinnPhongLightingShaderNodeBuilder method buildFragmentNodeSingleInputs.

@Override
public ObjectMap<String, ? extends FieldOutput> buildFragmentNodeSingleInputs(boolean designTime, String nodeId, final JsonValue data, ObjectMap<String, FieldOutput> inputs, ObjectSet<String> producedOutputs, VertexShaderBuilder vertexShaderBuilder, FragmentShaderBuilder fragmentShaderBuilder, final GraphShaderContext graphShaderContext, final GraphShader graphShader) {
    final String environmentId = data.getString("id", "");
    fragmentShaderBuilder.addStructure("Lighting", "  vec3 diffuse;\n" + "  vec3 specular;\n");
    // Lighting3DUtils.configureShadowInformation(fragmentShaderBuilder, nodeId, environmentId, maxNumberOfDirectionalLights, graphShader.getDefaultTexture());
    // 
    // Lighting3DUtils.configureAmbientLighting(fragmentShaderBuilder, nodeId, environmentId);
    final float[] shadowCameras = new float[16 * maxNumberOfDirectionalLights];
    fragmentShaderBuilder.addArrayUniformVariable("u_shadowCamera_" + nodeId, maxNumberOfDirectionalLights, "mat4", true, new UniformRegistry.UniformSetter() {

        @Override
        public void set(BasicShader shader, int location, ShaderContext shaderContext) {
            Lighting3DEnvironment environment = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class).getEnvironment(data.getString("id", ""));
            if (environment != null) {
                Array<Directional3DLight> directionalLights = environment.getDirectionalLights();
                for (int i = 0; i < directionalLights.size; i++) {
                    OrthographicCamera shadowCamera = directionalLights.get(i).getShadowCamera();
                    if (shadowCamera != null) {
                        System.arraycopy(shadowCamera.combined.val, 0, shadowCameras, i * 16, 16);
                    }
                }
            }
            shader.setUniformMatrix4Array(location, shadowCameras);
        }
    }, "Shadow camera matrix");
    final float[] shadowCamerasBufferSize = new float[1 * maxNumberOfDirectionalLights];
    fragmentShaderBuilder.addArrayUniformVariable("u_shadowCameraBufferSize_" + nodeId, maxNumberOfDirectionalLights, "float", true, new UniformRegistry.UniformSetter() {

        @Override
        public void set(BasicShader shader, int location, ShaderContext shaderContext) {
            Lighting3DEnvironment environment = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class).getEnvironment(data.getString("id", ""));
            if (environment != null) {
                Array<Directional3DLight> directionalLights = environment.getDirectionalLights();
                for (int i = 0; i < directionalLights.size; i++) {
                    OrthographicCamera shadowCamera = directionalLights.get(i).getShadowCamera();
                    if (shadowCamera != null) {
                        shadowCamerasBufferSize[i] = directionalLights.get(i).getShadowBufferSize();
                    }
                }
            }
            shader.setUniformFloatArray(location, shadowCamerasBufferSize);
        }
    }, "Shadow camera buffer size");
    final float[] shadowCamerasClipping = new float[2 * maxNumberOfDirectionalLights];
    fragmentShaderBuilder.addArrayUniformVariable("u_shadowCameraClipping_" + nodeId, maxNumberOfDirectionalLights, "vec2", true, new UniformRegistry.UniformSetter() {

        @Override
        public void set(BasicShader shader, int location, ShaderContext shaderContext) {
            Lighting3DEnvironment environment = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class).getEnvironment(data.getString("id", ""));
            if (environment != null) {
                Array<Directional3DLight> directionalLights = environment.getDirectionalLights();
                for (int i = 0; i < directionalLights.size; i++) {
                    OrthographicCamera shadowCamera = directionalLights.get(i).getShadowCamera();
                    if (shadowCamera != null) {
                        shadowCamerasClipping[i * 2 + 0] = shadowCamera.near;
                        shadowCamerasClipping[i * 2 + 1] = shadowCamera.far;
                    }
                }
            }
            shader.setUniformVector2Array(location, shadowCamerasClipping);
        }
    }, "Shadow camera clipping");
    final float[] shadowCamerasPosition = new float[3 * maxNumberOfDirectionalLights];
    fragmentShaderBuilder.addArrayUniformVariable("u_shadowCameraPosition_" + nodeId, maxNumberOfDirectionalLights, "vec3", true, new UniformRegistry.UniformSetter() {

        @Override
        public void set(BasicShader shader, int location, ShaderContext shaderContext) {
            Lighting3DEnvironment environment = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class).getEnvironment(data.getString("id", ""));
            if (environment != null) {
                Array<Directional3DLight> directionalLights = environment.getDirectionalLights();
                for (int i = 0; i < directionalLights.size; i++) {
                    OrthographicCamera shadowCamera = directionalLights.get(i).getShadowCamera();
                    if (shadowCamera != null) {
                        shadowCamerasPosition[i * 3 + 0] = shadowCamera.position.x;
                        shadowCamerasPosition[i * 3 + 1] = shadowCamera.position.y;
                        shadowCamerasPosition[i * 3 + 2] = shadowCamera.position.z;
                    }
                }
            }
            shader.setUniformVector3Array(location, shadowCamerasPosition);
        }
    }, "Shadow camera position");
    for (int i = 0; i < maxNumberOfDirectionalLights; i++) {
        final int lightIndex = i;
        final TextureDescriptor<Texture> textureDescriptor = new TextureDescriptor<>(null, Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge);
        fragmentShaderBuilder.addUniformVariable("u_shadowMap_" + nodeId + "_" + i, "sampler2D", true, new UniformRegistry.UniformSetter() {

            @Override
            public void set(BasicShader shader, int location, ShaderContext shaderContext) {
                Lighting3DEnvironment environment = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class).getEnvironment(data.getString("id", ""));
                if (environment != null) {
                    Array<Directional3DLight> directionalLights = environment.getDirectionalLights();
                    RenderPipelineBuffer shadowFrameBuffer = null;
                    if (directionalLights.size > lightIndex)
                        shadowFrameBuffer = directionalLights.get(lightIndex).getShadowFrameBuffer();
                    if (shadowFrameBuffer != null) {
                        textureDescriptor.texture = shadowFrameBuffer.getColorBufferTexture();
                    } else {
                        textureDescriptor.texture = graphShader.getDefaultTexture();
                    }
                } else {
                    textureDescriptor.texture = graphShader.getDefaultTexture();
                }
                shader.setUniform(location, textureDescriptor);
            }
        }, "Shadow map " + i);
    }
    fragmentShaderBuilder.addUniformVariable("u_ambientLight_" + nodeId, "vec3", true, new UniformRegistry.UniformSetter() {

        @Override
        public void set(BasicShader shader, int location, ShaderContext shaderContext) {
            Lighting3DEnvironment environment = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class).getEnvironment(data.getString("id", ""));
            if (environment != null && environment.getAmbientColor() != null) {
                LightColor ambientColor = environment.getAmbientColor();
                shader.setUniform(location, ambientColor.getRed(), ambientColor.getGreen(), ambientColor.getBlue());
            } else {
                shader.setUniform(location, 0f, 0f, 0f);
            }
        }
    }, "Ambient light");
    loadFragmentIfNotDefined(fragmentShaderBuilder, "unpackVec3ToFloat");
    String isLightedFunctionName = "isLighted_" + nodeId;
    String probeShadowMapFunctionName = "probeShadowMap_" + nodeId;
    fragmentShaderBuilder.addFunction(probeShadowMapFunctionName, createProbeShadowMapFunction(nodeId));
    ObjectMap<String, String> variables = new ObjectMap<>();
    variables.put("NUM_SPOT_LIGHTS", String.valueOf(maxNumberOfSpotlights));
    variables.put("NUM_POINT_LIGHTS", String.valueOf(maxNumberOfPointLights));
    variables.put("NUM_DIRECTIONAL_LIGHTS", String.valueOf(maxNumberOfDirectionalLights));
    variables.put("SHADOW_ACNE_VALUE", SimpleNumberFormatter.format(shadowAcneValue));
    variables.put("SHADOW_SOFTNESS", String.valueOf(shadowSoftness));
    variables.put("SHADOW_PROBE_COUNT", SimpleNumberFormatter.format((shadowSoftness + 1) * (shadowSoftness + 1)));
    variables.put("NODE_ID", nodeId);
    fragmentShaderBuilder.addFunction(isLightedFunctionName, GLSLFragmentReader.getFragment("isLighted", variables));
    if (maxNumberOfDirectionalLights > 0)
        passDirectionalLights(environmentId, fragmentShaderBuilder, nodeId, variables);
    if (maxNumberOfPointLights > 0)
        passPointLights(environmentId, fragmentShaderBuilder, nodeId, variables);
    if (maxNumberOfSpotlights > 0)
        passSpotLights(environmentId, fragmentShaderBuilder, nodeId, variables);
    FieldOutput positionValue = inputs.get("position");
    FieldOutput normalValue = inputs.get("normal");
    FieldOutput albedoValue = inputs.get("albedo");
    FieldOutput emissionValue = inputs.get("emission");
    FieldOutput specularValue = inputs.get("specular");
    FieldOutput ambientOcclusionValue = inputs.get("ambientOcclusion");
    FieldOutput shininessValue = inputs.get("shininess");
    String position = positionValue.getRepresentation();
    String normal = normalValue.getRepresentation();
    String albedo = albedoValue != null ? albedoValue.getRepresentation() : "vec3(0.0)";
    String emission = emissionValue != null ? emissionValue.getRepresentation() : "vec3(0.0)";
    String specular = specularValue != null ? specularValue.getRepresentation() : "vec3(1.0)";
    String ambientOcclusion = ambientOcclusionValue != null ? ambientOcclusionValue.getRepresentation() : "1.0";
    String shininess = shininessValue != null ? shininessValue.getRepresentation() : "32.0";
    fragmentShaderBuilder.addMainLine("// Blinn-Phong Lighting node");
    String calculateLightingFunctionName = "calculateBlinnPhongLightingFunction_" + nodeId;
    fragmentShaderBuilder.addFunction(calculateLightingFunctionName, createCalculateLightingFunction(nodeId));
    String lightingVariable = "lighting_" + nodeId;
    fragmentShaderBuilder.addMainLine("Lighting " + lightingVariable + " = " + calculateLightingFunctionName + "(" + position + ", " + normal + ", " + shininess + ");");
    ShaderFieldType resultType = ShaderFieldTypeRegistry.findShaderFieldType(ShaderFieldType.Vector3);
    ObjectMap<String, DefaultFieldOutput> result = new ObjectMap<>();
    if (producedOutputs.contains("output")) {
        String name = "color_" + nodeId;
        fragmentShaderBuilder.addMainLine(resultType.getShaderType() + " " + name + " = " + emission + ".rgb + " + ambientOcclusion + " * u_ambientLight_" + nodeId + " * " + albedo + ".rgb;");
        fragmentShaderBuilder.addMainLine(name + " += " + lightingVariable + ".diffuse * " + albedo + ".rgb + " + lightingVariable + ".specular * " + specular + ".rgb;");
        result.put("output", new DefaultFieldOutput(resultType.getName(), name));
    }
    if (producedOutputs.contains("diffuse")) {
        result.put("diffuse", new DefaultFieldOutput(resultType.getName(), lightingVariable + ".diffuse"));
    }
    if (producedOutputs.contains("specularOut")) {
        result.put("specularOut", new DefaultFieldOutput(resultType.getName(), lightingVariable + ".specular"));
    }
    return result;
}
Also used : RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) Texture(com.badlogic.gdx.graphics.Texture) DefaultFieldOutput(com.gempukku.libgdx.graph.shader.node.DefaultFieldOutput) Array(com.badlogic.gdx.utils.Array) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Lighting3DEnvironment(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DEnvironment) DefaultFieldOutput(com.gempukku.libgdx.graph.shader.node.DefaultFieldOutput) TextureDescriptor(com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor) LightColor(com.gempukku.libgdx.graph.plugin.lighting3d.LightColor) ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 2 with RenderPipelineBuffer

use of com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer 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();
            }
        }
    };
}
Also used : ShaderContextImpl(com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.ShaderContextImpl) Directional3DLight(com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight) GraphModelsImpl(com.gempukku.libgdx.graph.plugin.models.impl.GraphModelsImpl) ObjectMap(com.badlogic.gdx.utils.ObjectMap) GraphShader(com.gempukku.libgdx.graph.shader.GraphShader) Camera(com.badlogic.gdx.graphics.Camera) TimeProvider(com.gempukku.libgdx.graph.time.TimeProvider) RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer) JsonValue(com.badlogic.gdx.utils.JsonValue) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) Lighting3DPrivateData(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData) Array(com.badlogic.gdx.utils.Array) Lighting3DEnvironment(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DEnvironment) RenderOrder(com.gempukku.libgdx.graph.pipeline.RenderOrder) RenderPipeline(com.gempukku.libgdx.graph.pipeline.RenderPipeline)

Example 3 with RenderPipelineBuffer

use of com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer in project gdx-graph by MarcinSc.

the class MapsLayerIdsRendererPipelineNodeProducer method createNodeForSingleInputs.

@Override
public PipelineNode createNodeForSingleInputs(final JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
    final String mapId = data.getString("id");
    final String[] layerIds = data.getString("layers").split(",");
    final int[] ids = new int[layerIds.length];
    for (int i = 0; i < layerIds.length; i++) {
        ids[i] = Integer.parseInt(layerIds[i]);
    }
    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 MapsPluginPrivateData mapsPluginData;

        @Override
        public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
            mapsPluginData = pipelineDataProvider.getPrivatePluginData(MapsPluginPrivateData.class);
        }

        @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();
            Camera camera = cameraInput.getValue();
            boolean enabled = processorEnabled == null || processorEnabled.getValue();
            Map map = mapsPluginData.getMap(mapId);
            MapRenderer mapRenderer = mapsPluginData.getMapRenderer(mapId);
            if (enabled && map != null) {
                // Sadly need to switch off (and then on) the RenderContext
                pipelineRenderingContext.getRenderContext().end();
                RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
                currentBuffer.beginColor();
                mapRenderer.setView((OrthographicCamera) camera);
                mapRenderer.render(ids);
                currentBuffer.endColor();
                pipelineRenderingContext.getRenderContext().begin();
            }
            output.setValue(renderPipeline);
        }
    };
}
Also used : ObjectMap(com.badlogic.gdx.utils.ObjectMap) Camera(com.badlogic.gdx.graphics.Camera) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) MapsPluginPrivateData(com.gempukku.libgdx.graph.plugin.maps.MapsPluginPrivateData) RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) MapRenderer(com.badlogic.gdx.maps.MapRenderer) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Map(com.badlogic.gdx.maps.Map) RenderPipeline(com.gempukku.libgdx.graph.pipeline.RenderPipeline)

Example 4 with RenderPipelineBuffer

use of com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer in project gdx-graph by MarcinSc.

the class ParticlesShaderRendererPipelineNodeProducer method createNodeForSingleInputs.

@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
    final ShaderContextImpl shaderContext = new ShaderContextImpl(pluginPrivateDataSource);
    final Array<ParticlesGraphShader> particleShaders = new Array<>();
    final JsonValue shaderDefinitions = data.get("shaders");
    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 GraphParticleEffectsImpl particleEffects;

        @Override
        public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
            fullScreenRender = pipelineDataProvider.getFullScreenRender();
            timeProvider = pipelineDataProvider.getTimeProvider();
            particleEffects = pipelineDataProvider.getPrivatePluginData(GraphParticleEffectsImpl.class);
            for (JsonValue shaderDefinition : shaderDefinitions) {
                String tag = shaderDefinition.getString("tag");
                JsonValue shaderGraph = shaderDefinition.get("shader");
                Gdx.app.debug("Shader", "Building shader with tag: " + tag);
                final ParticlesGraphShader graphShader = GraphLoader.loadGraph(shaderGraph, new ParticlesShaderLoaderCallback(tag, pipelineDataProvider.getWhitePixel().texture, configurations), PropertyLocation.Uniform);
                particleShaders.add(graphShader);
            }
            for (ParticlesGraphShader particleShader : particleShaders) {
                particleEffects.registerEffect(particleShader.getTag(), particleShader);
            }
        }

        private boolean usesDepth() {
            for (ParticlesGraphShader particleShader : particleShaders) {
                if (particleShader.isUsingDepthTexture()) {
                    return true;
                }
            }
            return false;
        }

        @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");
            boolean enabled = processorEnabled == null || processorEnabled.getValue();
            RenderPipeline renderPipeline = renderPipelineInput.getValue();
            if (enabled) {
                boolean usesDepth = usesDepth();
                boolean needsSceneColor = false;
                for (ParticlesGraphShader particleShader : particleShaders) {
                    if (particleShader.isUsingColorTexture()) {
                        needsSceneColor = true;
                        break;
                    }
                }
                RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
                if (usesDepth) {
                    renderPipeline.enrichWithDepthBuffer(currentBuffer);
                }
                if (cameraInput != null) {
                    Camera camera = cameraInput.getValue();
                    shaderContext.setCamera(camera);
                }
                shaderContext.setTimeProvider(timeProvider);
                shaderContext.setRenderWidth(currentBuffer.getWidth());
                shaderContext.setRenderHeight(currentBuffer.getHeight());
                RenderPipelineBuffer sceneColorBuffer = null;
                if (needsSceneColor) {
                    sceneColorBuffer = setupColorTexture(renderPipeline, currentBuffer, pipelineRenderingContext);
                }
                currentBuffer.beginColor();
                for (ParticlesGraphShader particleShader : particleShaders) {
                    String tag = particleShader.getTag();
                    if (particleEffects.hasEffects(tag)) {
                        shaderContext.setGlobalPropertyContainer(particleEffects.getGlobalPropertyContainer(tag));
                        particleShader.begin(shaderContext, pipelineRenderingContext.getRenderContext());
                        for (GraphParticleEffectImpl particleEffect : particleEffects.getParticleEffects(tag)) {
                            if (particleEffect.getRenderableParticleEffect().isRendered(shaderContext.getCamera(), tag)) {
                                shaderContext.setLocalPropertyContainer(particleEffect.getPropertyContainer());
                                particleEffect.render(particleShader, shaderContext);
                            }
                        }
                        particleShader.end();
                    }
                }
                currentBuffer.endColor();
                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 (ParticlesGraphShader particleShader : particleShaders) {
                particleShader.dispose();
            }
        }
    };
}
Also used : ShaderContextImpl(com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.ShaderContextImpl) FullScreenRender(com.gempukku.libgdx.graph.pipeline.producer.FullScreenRender) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Camera(com.badlogic.gdx.graphics.Camera) TimeProvider(com.gempukku.libgdx.graph.time.TimeProvider) RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer) JsonValue(com.badlogic.gdx.utils.JsonValue) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) Array(com.badlogic.gdx.utils.Array) GraphParticleEffectImpl(com.gempukku.libgdx.graph.plugin.particles.impl.GraphParticleEffectImpl) GraphParticleEffectsImpl(com.gempukku.libgdx.graph.plugin.particles.impl.GraphParticleEffectsImpl) RenderPipeline(com.gempukku.libgdx.graph.pipeline.RenderPipeline)

Example 5 with RenderPipelineBuffer

use of com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer in project gdx-graph by MarcinSc.

the class DepthOfFieldPipelineNodeProducer method executeBlur.

private RenderPipelineBuffer executeBlur(ShaderProgram shaderProgram, RenderPipeline renderPipeline, RenderPipelineBuffer depthBuffer, RenderPipelineBuffer sourceBuffer, OpenGLContext renderContext, FullScreenRender fullScreenRender) {
    RenderPipelineBuffer resultBuffer = renderPipeline.getNewFrameBuffer(sourceBuffer, Color.BLACK);
    resultBuffer.beginColor();
    shaderProgram.setUniformi("u_sourceTexture", renderContext.bindTexture(sourceBuffer.getColorBufferTexture()));
    shaderProgram.setUniformi("u_depthTexture", renderContext.bindTexture(depthBuffer.getDepthBufferTexture()));
    fullScreenRender.renderFullScreen(shaderProgram);
    resultBuffer.endColor();
    return resultBuffer;
}
Also used : RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer)

Aggregations

RenderPipelineBuffer (com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer)21 ObjectMap (com.badlogic.gdx.utils.ObjectMap)14 RenderPipeline (com.gempukku.libgdx.graph.pipeline.RenderPipeline)13 PipelineRenderingContext (com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext)13 Camera (com.badlogic.gdx.graphics.Camera)8 FullScreenRender (com.gempukku.libgdx.graph.pipeline.producer.FullScreenRender)8 Array (com.badlogic.gdx.utils.Array)5 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)4 ShaderProgram (com.badlogic.gdx.graphics.glutils.ShaderProgram)4 Vector2 (com.badlogic.gdx.math.Vector2)4 JsonValue (com.badlogic.gdx.utils.JsonValue)4 OpenGLContext (com.gempukku.libgdx.graph.libgdx.context.OpenGLContext)4 ShaderContextImpl (com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.ShaderContextImpl)4 TimeProvider (com.gempukku.libgdx.graph.time.TimeProvider)4 Map (com.badlogic.gdx.maps.Map)3 MapRenderer (com.badlogic.gdx.maps.MapRenderer)3 MapsPluginPrivateData (com.gempukku.libgdx.graph.plugin.maps.MapsPluginPrivateData)3 RenderOrder (com.gempukku.libgdx.graph.pipeline.RenderOrder)2 Lighting3DEnvironment (com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DEnvironment)2 GraphModelsImpl (com.gempukku.libgdx.graph.plugin.models.impl.GraphModelsImpl)2