Search in sources :

Example 1 with Texture

use of io.xol.chunkstories.api.rendering.textures.Texture in project chunkstories by Hugobros3.

the class TextureGL method destroyPendingTextureObjects.

private static int destroyPendingTextureObjects() {
    int destroyedVerticesObjects = 0;
    synchronized (objectsToDestroy) {
        Iterator<TextureGL> i = objectsToDestroy.iterator();
        while (i.hasNext()) {
            Texture object = i.next();
            if (object.destroy())
                destroyedVerticesObjects++;
            i.remove();
        }
    }
    Iterator<Entry<Integer, WeakReference<TextureGL>>> i = allocatedIds.entrySet().iterator();
    while (i.hasNext()) {
        Entry<Integer, WeakReference<TextureGL>> entry = i.next();
        int id = entry.getKey();
        WeakReference<TextureGL> weakReference = entry.getValue();
        Texture texture = weakReference.get();
        if (texture == null) {
            logger.info("Disallocated orphan openGL texture ID " + id);
            glDeleteTextures(id);
            destroyedVerticesObjects++;
            i.remove();
        }
    }
    return destroyedVerticesObjects;
}
Also used : Entry(java.util.Map.Entry) WeakReference(java.lang.ref.WeakReference) Texture(io.xol.chunkstories.api.rendering.textures.Texture)

Example 2 with Texture

use of io.xol.chunkstories.api.rendering.textures.Texture in project chunkstories by Hugobros3.

the class TextureGL method getTotalVramUsage.

public static long getTotalVramUsage() {
    long vram = 0;
    // Iterates over every instance reference, removes null ones and add up valid ones
    Iterator<WeakReference<TextureGL>> i = allTextureObjects.iterator();
    while (i.hasNext()) {
        WeakReference<TextureGL> reference = i.next();
        Texture object = reference.get();
        if (object != null)
            vram += object.getVramUsage();
        else
            i.remove();
    }
    return vram;
}
Also used : WeakReference(java.lang.ref.WeakReference) Texture(io.xol.chunkstories.api.rendering.textures.Texture)

Example 3 with Texture

use of io.xol.chunkstories.api.rendering.textures.Texture in project chunkstories by Hugobros3.

the class TexturingConfigurationImplementation method setup.

/**
 * Setups the required texturing units and links the shaders uniforms to them
 */
public void setup(RenderingInterface renderingInterface) throws NotEnoughtTextureUnitsException {
    ShaderGL shaderProgram = (ShaderGL) renderingInterface.currentShader();
    // Drop the older bound textures when a new shader is used
    if (lastShaderConfigured != shaderProgram) {
        alreadyBound.clear();
        this.resetBoundTextures();
    } else {
        // Go through the already bound textures
        Iterator<Entry<Texture, Integer>> i = alreadyBound.entrySet().iterator();
        while (i.hasNext()) {
            Entry<Texture, Integer> e = i.next();
            Texture texture = e.getKey();
            // If one texture is no longer used, remove it
            if (!allTextures.values().contains(texture)) {
                i.remove();
                boundTextures[e.getValue()] = null;
            }
        }
    }
    // For each sampler defined in the shader we will try to bind the necessary texture
    for (Entry<String, SamplerType> e : shaderProgram.samplers().entrySet()) {
        String samplerName = e.getKey();
        // System.out.println("figuring out texture for"+samplerName);
        Texture texture = allTextures.get(samplerName);
        if (// No texture or the wrong type supplied ? No worries
        texture == null || texture.getSamplerType() != e.getValue())
            texture = defaultTextures[e.getValue().ordinal()];
        // System.out.println(texture);
        int alreadyBoundTextureUnit = alreadyBound.getOrDefault(texture, -1);
        if (alreadyBoundTextureUnit == -1) {
            int freeTextureUnit = findFreeTextureUnit();
            this.selectTextureUnit(freeTextureUnit);
            texture.bind();
            alreadyBound.put(texture, freeTextureUnit);
            boundTextures[freeTextureUnit] = texture;
            int uniform = shaderProgram.getUniformLocation(samplerName);
            if (uniform == -1)
                continue;
            glUniform1i(uniform, freeTextureUnit);
        // if(shaderProgram.getShaderName().contains("postprocess"))
        // System.out.println("bound " + samplerName + " to " + freeTextureUnit + " ("+uniform+") :" + texture);
        } else {
            glUniform1i(shaderProgram.getUniformLocation(samplerName), alreadyBoundTextureUnit);
        }
    }
    lastShaderConfigured = shaderProgram;
}
Also used : Entry(java.util.Map.Entry) ShaderGL(io.xol.chunkstories.renderer.opengl.shader.ShaderGL) SamplerType(io.xol.chunkstories.api.rendering.shader.Shader.SamplerType) GL11.glBindTexture(org.lwjgl.opengl.GL11.glBindTexture) Texture(io.xol.chunkstories.api.rendering.textures.Texture) ArrayTexture(io.xol.chunkstories.api.rendering.textures.ArrayTexture) GL13.glActiveTexture(org.lwjgl.opengl.GL13.glActiveTexture)

Example 4 with Texture

use of io.xol.chunkstories.api.rendering.textures.Texture in project chunkstories by Hugobros3.

the class WorldRendererImplementation method blitFinalImage.

public void blitFinalImage(RenderingInterface renderingContext, boolean hideGui) {
    Texture finalBuffer = this.renderingGraph.getRenderPass("final").resolvedOutputs.get("finalBuffer");
    if (finalBuffer != null && finalBuffer instanceof Texture2D) {
        final Texture2D finalTexture = (Texture2D) (finalBuffer);
        // We render to the screen.
        renderingContext.getRenderTargetManager().setConfiguration(null);
        renderingContext.setDepthTestMode(DepthTestMode.DISABLED);
        renderingContext.setBlendMode(BlendMode.DISABLED);
        renderingContext.useShader("blit");
        renderingContext.bindTexture2D("diffuseTexture", finalTexture);
        renderingContext.drawFSQuad();
        if (!hideGui) {
            world.entitiesLock.readLock().lock();
            Iterator<Entity> ei = world.getAllLoadedEntities();
            Entity e;
            while (ei.hasNext()) {
                e = ei.next();
                if (e instanceof EntityOverlay) {
                    ((EntityOverlay) e).drawEntityOverlay(renderingContext);
                }
            }
            world.entitiesLock.readLock().unlock();
        }
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Texture(io.xol.chunkstories.api.rendering.textures.Texture) HeightmapArrayTexture(io.xol.chunkstories.renderer.terrain.HeightmapArrayTexture) EntityOverlay(io.xol.chunkstories.api.entity.interfaces.EntityOverlay)

Example 5 with Texture

use of io.xol.chunkstories.api.rendering.textures.Texture in project chunkstories by Hugobros3.

the class RenderingGraph method resolveInputs.

/**
 * Passes the output buffers of the early passes to the inputs of the later ones
 */
private void resolveInputs() {
    if (executionOrder != null) {
        for (int i = 0; i < executionOrder.length; i++) {
            RenderPass pass = executionOrder[i];
            Map<String, Texture> inputs = new HashMap<>();
            // For each buffer we depend on
            for (String requirement : pass.requires) {
                // System.out.println("resolving requirement "+requirement+" for pass "+pass.name);
                // parse pass and buffer name
                String[] s = requirement.split("\\.");
                String requiredPassName = s[0];
                String requiredBufferName = s.length > 0 ? s[1] : "invalid";
                boolean forward = requiredBufferName.endsWith("!");
                requiredBufferName = requiredBufferName.replace("!", "");
                // System.out.println(requiredPassName+"."+requiredBufferName+" forward:"+forward);
                RenderPass requiredPass = this.getRenderPass(requiredPassName);
                // assumes all previous passes were resolved ok
                Texture requiredBuffer = requiredPass == null ? null : requiredPass.resolvedOutputs.get(requiredBufferName);
                if (forward) {
                    // System.out.println("auto-forwarding buffer:"+requiredBufferName);
                    pass.resolvedOutputs.put(requiredBufferName, requiredBuffer);
                }
                // return it the buffer he asked under multiple names
                inputs.put(requiredPassName + "." + requiredBufferName, requiredBuffer);
                inputs.put(requiredBufferName, requiredBuffer);
                // may alias !
                inputs.put(requirement, requiredBuffer);
            }
            // notify the pass we found it's buffers
            pass.resolvedInputs.clear();
            pass.resolvedInputs.putAll(inputs);
            pass.onResolvedInputs();
        }
    }
}
Also used : HashMap(java.util.HashMap) RenderPass(io.xol.chunkstories.api.rendering.pass.RenderPass) Texture(io.xol.chunkstories.api.rendering.textures.Texture)

Aggregations

Texture (io.xol.chunkstories.api.rendering.textures.Texture)6 HeightmapArrayTexture (io.xol.chunkstories.renderer.terrain.HeightmapArrayTexture)2 WeakReference (java.lang.ref.WeakReference)2 Entry (java.util.Map.Entry)2 Entity (io.xol.chunkstories.api.entity.Entity)1 EntityOverlay (io.xol.chunkstories.api.entity.interfaces.EntityOverlay)1 RenderPass (io.xol.chunkstories.api.rendering.pass.RenderPass)1 SamplerType (io.xol.chunkstories.api.rendering.shader.Shader.SamplerType)1 ArrayTexture (io.xol.chunkstories.api.rendering.textures.ArrayTexture)1 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)1 ShaderGL (io.xol.chunkstories.renderer.opengl.shader.ShaderGL)1 Texture2DRenderTargetGL (io.xol.chunkstories.renderer.opengl.texture.Texture2DRenderTargetGL)1 HashMap (java.util.HashMap)1 GL11.glBindTexture (org.lwjgl.opengl.GL11.glBindTexture)1 GL13.glActiveTexture (org.lwjgl.opengl.GL13.glActiveTexture)1