Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: npe in depth shader diffuseTexture #7267

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

dar-dev
Copy link
Contributor

@dar-dev dar-dev commented Oct 31, 2023

Line: https://github.com/libgdx/libgdx/blob/7fb396541a8c2777c92a5062aaa49afcab0bec1f/gdx/src/com/badlogic/gdx/graphics/g3d/shaders/DefaultShader.java#L237C32-L237C50

This is throwing an npe because the DepthShader canRender() is recycling the shader without the diffuse texture attribute.

Step to replicate:

Copy link
Contributor

@mgsx-dev mgsx-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fixes the issue you have but it introduces a regression : let say you have 2 models without blending, one with a diffuse texture and another one without diffuse texture, 2 shaders will be generated instead of one. And will then produce some unnecessary shader switches when rendering.

@dar-dev
Copy link
Contributor Author

dar-dev commented Jan 17, 2024

It fixes the issue you have but it introduces a regression : let say you have 2 models without blending, one with a diffuse texture and another one without diffuse texture, 2 shaders will be generated instead of one. And will then produce some unnecessary shader switches when rendering.

If we use the same shader an npe is thrown, so.. it never worked (at least in our tests).
How can we avoid this shader switch ? Do we need to introduce an npe check in diffuseTexture local setter ?

--

@Tom-Ski
Copy link
Member

Tom-Ski commented May 28, 2024

What is the minimal pure libgdx repro steps here? Having a demo with gltf dependency is not very suitable for the tracker.
Is it two renderables with diffuse texture? One with diffuse texture, one not? Whats the minimum combination required for this to die?

@dar-dev
Copy link
Contributor Author

dar-dev commented May 28, 2024

To replicate in libgdx you need to render 2 different renderables using depthshader: one with diffuse texture and the other without.

--

@Tom-Ski
Copy link
Member

Tom-Ski commented May 28, 2024

Using a slightly modified version of the ShadowMappingTest does not reproduce this.

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.tests.g3d;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.DepthShaderProvider;
import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.tests.utils.GdxTest;

public class ShadowMappingTest extends GdxTest {
    PerspectiveCamera cam;
    CameraInputController camController;
    ModelBatch modelBatch;
    Model model;
    ModelInstance instance;
    ModelInstance instance2;
    Environment environment;
    DirectionalShadowLight shadowLight;
    ModelBatch shadowBatch;
    private Model model2;

    @Override
    public void create () {
        modelBatch = new ModelBatch();
        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
        environment
                .add((shadowLight = new DirectionalShadowLight(1024, 1024, 30f, 30f, 1f, 100f)).set(0.8f, 0.8f, 0.8f, -1f, -.8f, -.2f));
        environment.shadowMap = shadowLight;

        cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.position.set(0f, 7f, 10f);
        cam.lookAt(0, 0, 0);
        cam.near = 1f;
        cam.far = 50f;
        cam.update();

        ModelBuilder modelBuilder = new ModelBuilder();

        modelBuilder.begin();
        MeshPartBuilder mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.ColorUnpacked | Usage.TextureCoordinates,
                new Material(TextureAttribute.createDiffuse(new Texture(Gdx.files.internal("data/badlogic.jpg")))));
        mpb.setColor(1f, 1f, 1f, 1f);
        mpb.sphere(2f, 2f, 2f, 10, 10);
        model = modelBuilder.end();
        instance = new ModelInstance(model);

        modelBuilder.begin();
        mpb = modelBuilder.part("parts2", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.ColorUnpacked,
            new Material());
        mpb.setColor(1f, 1f, 1f, 1f);
        mpb.sphere(4f, 2f, 2f, 10, 10);
        model2 = modelBuilder.end();
        instance2 = new ModelInstance(model2);


        shadowBatch = new ModelBatch(new DepthShaderProvider());

        Gdx.input.setInputProcessor(camController = new CameraInputController(cam));
    }

    @Override
    public void render () {
        camController.update();

        Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        instance2.transform.setToTranslation(4, 0, 0);

        shadowLight.begin(Vector3.Zero, cam.direction);
        shadowBatch.begin(shadowLight.getCamera());
        shadowBatch.render(instance);
        shadowBatch.render(instance2);
        shadowBatch.end();
        shadowLight.end();

        modelBatch.begin(cam);
        modelBatch.render(instance, environment);
        modelBatch.render(instance2, environment);
        modelBatch.end();
    }

    @Override
    public void dispose () {
        modelBatch.dispose();
        model.dispose();
        model2.dispose();
    }

    public boolean needsGL20 () {
        return true;
    }

    public void resume () {
    }

    public void resize (int width, int height) {
    }

    public void pause () {
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants