def debug3DTexture(self, tex, dest):

        """ Writes a 3D Texture to disk """

        effectiveHeight = (tex.getYSize()+1) * (tex.getZSize())
        effectiveHeight -= 1
        effectiveWidth = (tex.getXSize()+1)*3

        effectiveHeight = tex.getYSize()
        effectiveWidth = tex.getXSize()

        
        self.debug("Texture Size =",tex.getXSize(),"x",tex.getYSize(),"x",tex.getZSize())
        self.debug("EffectiveHeight = ", effectiveHeight, "Layers=",tex.getZSize())
        
        store = Texture("store")
        store.setup2dTexture(
            effectiveWidth, effectiveHeight, Texture.TFloat, Texture.FRgba16)

        TextureCleaner.clearTexture(store, Vec4(1,0,1,1))

        # Create a dummy node and apply the shader to it
        shader = BetterShader.loadCompute("Shader/Write3DTexture.compute")
        dummy = NodePath("dummy")
        dummy.setShader(shader)
        dummy.setShaderInput("source", tex)
        dummy.setShaderInput("height", tex.getYSize() + 1)
        dummy.setShaderInput("width", tex.getXSize() + 1)
        dummy.setShaderInput("layerCount", tex.getZSize())
        dummy.setShaderInput("destination", store)

        # Retrieve the underlying ShaderAttrib
        sattr = dummy.get_attrib(ShaderAttrib)

        # Dispatch the compute shader, right now!
        Globals.base.graphicsEngine.dispatch_compute(
            (tex.getXSize() / 16, tex.getYSize() / 16, tex.getZSize()), sattr, base.win.get_gsg())

        Globals.base.graphicsEngine.extract_texture_data(
                store, Globals.base.win.getGsg())


        store.write(dest)
Esempio n. 2
0
    def debug3DTexture(self, tex, dest):
        """ Writes a 3D Texture to disk """

        effectiveHeight = (tex.getYSize() + 1) * (tex.getZSize())
        effectiveHeight -= 1
        effectiveWidth = (tex.getXSize() + 1) * 3

        # effectiveHeight = tex.getYSize()
        effectiveWidth = tex.getXSize()

        self.debug("Texture Size =", tex.getXSize(), "x",
                   tex.getYSize(), "x", tex.getZSize())
        self.debug(
            "EffectiveHeight = ", effectiveHeight, "Layers=", tex.getZSize())

        store = Texture("store")
        store.setup2dTexture(
            effectiveWidth, effectiveHeight, Texture.TFloat, Texture.FRgba16)

        TextureCleaner.clearTexture(store, Vec4(1, 0, 1, 1))

        # Create a dummy node and apply the shader to it
        shader = BetterShader.loadCompute("Shader/Write3DTexture.compute")
        dummy = NodePath("dummy")
        dummy.setShader(shader)
        dummy.setShaderInput("source", tex)
        dummy.setShaderInput("height", tex.getYSize() + 1)
        dummy.setShaderInput("width", tex.getXSize() + 1)
        dummy.setShaderInput("layerCount", tex.getZSize())
        dummy.setShaderInput("destination", store)

        # Retrieve the underlying ShaderAttrib
        sattr = dummy.get_attrib(ShaderAttrib)

        # Dispatch the compute shader, right now!
        Globals.base.graphicsEngine.dispatch_compute(
            (tex.getXSize() / 16, tex.getYSize() / 16, tex.getZSize()),
            sattr, Globals.base.win.get_gsg())

        Globals.base.graphicsEngine.extract_texture_data(
            store, Globals.base.win.getGsg())

        store.write(dest)
    def setup(self):
        """ Setups everything for the GI to work """
        self.debug("Setup ..")

        if self.pipeline.settings.useHardwarePCF:
            self.error(
                "Global Illumination does not work in combination with PCF!")
            import sys
            sys.exit(0)
            return

        

        self.settings = VoxelSettingsManager()
        self.settings.loadFromFile(join(self.sceneRoot, "voxels.ini"))

        self.debug(
            "Loaded voxels, grid resolution is", self.settings.GridResolution)

        self.gridScale = self.settings.GridEnd - self.settings.GridStart
        self.voxelSize = self.gridScale / float(self.settings.GridResolution)
        self.entrySize = Vec2(
            1.0 / float(self.settings.StackSizeX), 1.0 / float(self.settings.StackSizeY))
        self.frameIndex = 0

        invVoxelSize = Vec3(
            1.0 / self.voxelSize.x, 1.0 / self.voxelSize.y, 1.0 / self.voxelSize.z)
        invVoxelSize.normalize()
        self.normalizationFactor = invVoxelSize / float(self.settings.GridResolution)

        # Debugging of voxels, VERY slow
        self.debugVoxels = False

        if self.debugVoxels:
            self.createVoxelDebugBox()

        # Load packed voxels
        packedVoxels = Globals.loader.loadTexture(
            join(self.sceneRoot, "voxels.png"))
        packedVoxels.setFormat(Texture.FRgba8)
        packedVoxels.setComponentType(Texture.TUnsignedByte)
        # packedVoxels.setKeepRamImage(False)

        # Create 3D Texture to store unpacked voxels
        self.unpackedVoxels = Texture("Unpacked voxels")
        self.unpackedVoxels.setup3dTexture(self.settings.GridResolution, self.settings.GridResolution, self.settings.GridResolution,
                                           Texture.TFloat, Texture.FRgba8)
        self.unpackedVoxels.setMinfilter(Texture.FTLinearMipmapLinear)
        self.unpackedVoxels.setMagfilter(Texture.FTLinear)

        self.unpackVoxels = NodePath("unpackVoxels")
        self.unpackVoxels.setShader(
            BetterShader.loadCompute("Shader/GI/UnpackVoxels.compute"))
        self.unpackVoxels.setShaderInput("packedVoxels", packedVoxels)
        self.unpackVoxels.setShaderInput(
            "stackSizeX", LVecBase3i(self.settings.StackSizeX))
        self.unpackVoxels.setShaderInput("gridSize", LVecBase3i(self.settings.GridResolution))
        self.unpackVoxels.setShaderInput("destination", self.unpackedVoxels)
        self._executeShader(
            self.unpackVoxels, self.settings.GridResolution / 8, self.settings.GridResolution / 8, self.settings.GridResolution / 8)

        # Create 3D Texture to store direct radiance
        self.directRadiance = Texture("Direct radiance")
        self.directRadiance.setup3dTexture(self.settings.GridResolution, self.settings.GridResolution, self.settings.GridResolution,
                                           Texture.TFloat, Texture.FRgba8)

        for prepare in [self.directRadiance, self.unpackedVoxels]:
            prepare.setMagfilter(Texture.FTLinear)
            prepare.setMinfilter(Texture.FTLinearMipmapLinear)
            prepare.setWrapU(Texture.WMBorderColor)
            prepare.setWrapV(Texture.WMBorderColor)
            prepare.setWrapW(Texture.WMBorderColor)
            prepare.setBorderColor(Vec4(0))

        self.populateVPLNode = NodePath("PopulateVPLs")
        self.clearTextureNode = NodePath("ClearTexture")
        self.copyTextureNode = NodePath("CopyTexture")
        self.generateMipmapsNode = NodePath("GenerateMipmaps")

        # surroundingBox = Globals.loader.loadModel(
        #     "Demoscene.ignore/CubeOpen/Model.egg")
        # surroundingBox.setPos(self.gridStart)
        # surroundingBox.setScale(self.gridScale)
        # surroundingBox.reparentTo(Globals.render)

        self.bindTo(self.populateVPLNode, "giData")
        self.reloadShader()

        self._generateMipmaps(self.unpackedVoxels)
 def _createGenerateMipmapsShader(self):
     shader = BetterShader.loadCompute("Shader/GI/GenerateMipmaps.compute")
     self.generateMipmapsNode.setShader(shader)
 def _createPopulateShader(self):
     shader = BetterShader.loadCompute("Shader/GI/PopulateVPL.compute")
     self.populateVPLNode.setShader(shader)
 def _createCopyShader(self):
     shader = BetterShader.loadCompute("Shader/GI/CopyResult.compute")
     self.copyTextureNode.setShader(shader)
 def _createCleanShader(self):
     shader = BetterShader.loadCompute("Shader/GI/ClearTexture.compute")
     self.clearTextureNode.setShader(shader)
 def _createConvertShader(self):
     shader = BetterShader.loadCompute("Shader/GI/ConvertGrid.compute")
     self.convertGridNode.setShader(shader)
Esempio n. 9
0
 def _createGenerateMipmapsShader(self):
     shader = BetterShader.loadCompute("Shader/GI/GenerateMipmaps.compute")
     self.generateMipmapsNode.setShader(shader)
Esempio n. 10
0
 def _createConvertShader(self):
     shader = BetterShader.loadCompute("Shader/GI/ConvertGrid.compute")
     self.convertGridNode.setShader(shader)
Esempio n. 11
0
 def _createCleanShader(self):
     shader = BetterShader.loadCompute("Shader/GI/ClearTexture.compute")
     self.clearTextureNode.setShader(shader)