diff --git a/away3d/materials/MaterialBase.hx b/away3d/materials/MaterialBase.hx index 23146c6..733d3e5 100644 --- a/away3d/materials/MaterialBase.hx +++ b/away3d/materials/MaterialBase.hx @@ -27,6 +27,7 @@ import away3d.materials.lightpickers.LightPickerBase; import away3d.materials.passes.DepthMapPass; import away3d.materials.passes.DistanceMapPass; import away3d.materials.passes.MaterialPassBase; +import away3d.textures.Anisotropy; import openfl.display.BlendMode; import openfl.display3D.Context3D; import openfl.display3D.Context3DCompareMode; @@ -40,7 +41,7 @@ class MaterialBase extends NamedAssetBase implements IAsset { public var smooth(get_smooth, set_smooth):Bool; public var depthCompareMode(get_depthCompareMode, set_depthCompareMode):Context3DCompareMode; public var repeat(get_repeat, set_repeat):Bool; - public var maxAnisotropy(get_maxAnisotropy, set_maxAnisotropy):Float; + public var anisotropy(get_anisotropy, set_anisotropy):Anisotropy; public var bothSides(get_bothSides, set_bothSides):Bool; public var blendMode(get_blendMode, set_blendMode):BlendMode; public var alphaPremultiplied(get_alphaPremultiplied, set_alphaPremultiplied):Bool; @@ -99,7 +100,7 @@ class MaterialBase extends NamedAssetBase implements IAsset { private var _mipmap:Bool; private var _smooth:Bool; private var _repeat:Bool; - private var _maxAnisotropy:Float; + private var _anisotropy:Anisotropy; private var _depthPass:DepthMapPass; private var _distancePass:DistanceMapPass; private var _lightPicker:LightPickerBase; @@ -113,7 +114,7 @@ class MaterialBase extends NamedAssetBase implements IAsset { _blendMode = BlendMode.NORMAL; _mipmap = true; _smooth = true; - _maxAnisotropy = 1; + _anisotropy = Anisotropy.ANISOTROPIC2X; _depthCompareMode = Context3DCompareMode.LESS_EQUAL; _owners = new Array(); _passes = new Array(); @@ -229,18 +230,18 @@ class MaterialBase extends NamedAssetBase implements IAsset { /** * Indicates the number of Anisotropic filtering samples to take for mipmapping */ - public function get_maxAnisotropy():Float { - return _maxAnisotropy; + public function get_anisotropy():Anisotropy { + return _anisotropy; } - public function set_maxAnisotropy(value:Float):Float { - _maxAnisotropy = value; + public function set_anisotropy(value:Anisotropy):Anisotropy { + _anisotropy = value; var i:Int = 0; while (i < _numPasses) { - _passes[i].maxAnisotropy = maxAnisotropy; + _passes[i].anisotropy = _anisotropy; ++i; } - return maxAnisotropy; + return _anisotropy; } /** @@ -615,7 +616,7 @@ class MaterialBase extends NamedAssetBase implements IAsset { pass.mipmap = _mipmap; pass.smooth = _smooth; pass.repeat = _repeat; - pass.maxAnisotropy = _maxAnisotropy; + pass.anisotropy = _anisotropy; pass.lightPicker = _lightPicker; pass.bothSides = _bothSides; pass.addEventListener(Event.CHANGE, onPassChange); diff --git a/away3d/materials/TextureMaterial.hx b/away3d/materials/TextureMaterial.hx index 7834f2f..acb5652 100644 --- a/away3d/materials/TextureMaterial.hx +++ b/away3d/materials/TextureMaterial.hx @@ -6,6 +6,7 @@ package away3d.materials; import openfl.display.BlendMode; import openfl.geom.ColorTransform; import away3d.textures.Texture2DBase; +import away3d.textures.Anisotropy; class TextureMaterial extends SinglePassMaterialBase { public var animateUVs(get_animateUVs, set_animateUVs):Bool; @@ -19,15 +20,15 @@ class TextureMaterial extends SinglePassMaterialBase { * @param smooth Indicates whether the texture should be filtered when sampled. Defaults to true. * @param repeat Indicates whether the texture should be tiled when sampled. Defaults to true. * @param mipmap Indicates whether or not any used textures should use mipmapping. Defaults to true. - * @param maxAnisotropy Indicates the number of samples to use if Anisotropic mipmap filtering is applied + * @param anisotropy Indicates the number of samples to use if Anisotropic mipmap filtering is applied */ - public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true, maxAnisotropy:Float = 1) { + public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true, anisotropy:Anisotropy = null ) { super(); this.texture = texture; this.smooth = smooth; this.repeat = repeat; this.mipmap = mipmap; - this.maxAnisotropy = maxAnisotropy; + this.anisotropy = (anisotropy == null ? Anisotropy.ANISOTROPIC2X : anisotropy); } /** diff --git a/away3d/materials/TextureMultiPassMaterial.hx b/away3d/materials/TextureMultiPassMaterial.hx index 9442f2e..7400119 100644 --- a/away3d/materials/TextureMultiPassMaterial.hx +++ b/away3d/materials/TextureMultiPassMaterial.hx @@ -4,6 +4,7 @@ package away3d.materials; import away3d.textures.Texture2DBase; +import away3d.textures.Anisotropy; class TextureMultiPassMaterial extends MultiPassMaterialBase { public var animateUVs(get_animateUVs, set_animateUVs):Bool; @@ -18,15 +19,15 @@ class TextureMultiPassMaterial extends MultiPassMaterialBase { * @param smooth Indicates whether the texture should be filtered when sampled. Defaults to true. * @param repeat Indicates whether the texture should be tiled when sampled. Defaults to true. * @param mipmap Indicates whether or not any used textures should use mipmapping. Defaults to true. - * @param maxAnisotropy Indicates the number of samples to use if Anisotropic mipmap filtering is applied + * @param anisotropy Indicates the number of samples to use if Anisotropic mipmap filtering is applied */ - public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true, maxAnisotropy:Float = 1) { + public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true, anisotropy:Anisotropy = null) { super(); this.texture = texture; this.smooth = smooth; this.repeat = repeat; this.mipmap = mipmap; - this.maxAnisotropy = maxAnisotropy; + this.anisotropy = (anisotropy==null ? Anisotropy.ANISOTROPIC2X : anisotropy); } /** diff --git a/away3d/materials/compilation/ShaderCompiler.hx b/away3d/materials/compilation/ShaderCompiler.hx index 7560904..ac8021b 100644 --- a/away3d/materials/compilation/ShaderCompiler.hx +++ b/away3d/materials/compilation/ShaderCompiler.hx @@ -13,6 +13,7 @@ import away3d.materials.methods.MethodVO; import away3d.materials.methods.MethodVOSet; import away3d.materials.methods.ShaderMethodSetup; import away3d.materials.methods.ShadingMethodBase; +import away3d.textures.Anisotropy; import openfl.Vector; @@ -66,7 +67,7 @@ class ShaderCompiler { private var _smooth:Bool; private var _repeat:Bool; private var _mipmap:Bool; - private var _maxAnisotropy:Float; + private var _anisotropy:Anisotropy; private var _enableLightFallOff:Bool; private var _preserveAlpha:Bool; private var _animateUVs:Bool; @@ -231,11 +232,11 @@ class ShaderCompiler { * @param repeat Indicates whether the texture should be tiled when sampled. Defaults to true. * @param mipmap Indicates whether or not any used textures should use mipmapping. Defaults to true. */ - public function setTextureSampling(smooth:Bool, repeat:Bool, mipmap:Bool, maxAnisotropy:Float = 1):Void { + public function setTextureSampling(smooth:Bool, repeat:Bool, mipmap:Bool, anisotropy:Anisotropy ):Void { _smooth = smooth; _repeat = repeat; _mipmap = mipmap; - _maxAnisotropy = maxAnisotropy; + _anisotropy = anisotropy; } /** @@ -479,7 +480,7 @@ class ShaderCompiler { methodVO.useSmoothTextures = _smooth; methodVO.repeatTextures = _repeat; methodVO.useMipmapping = _mipmap; - methodVO.maxAnisotropy = _maxAnisotropy; + methodVO.anisotropy = _anisotropy; methodVO.useLightFallOff = _enableLightFallOff && _profile != "baselineConstrained"; methodVO.numLights = _numLights + _numLightProbes; method.initVO(methodVO); diff --git a/away3d/materials/methods/BasicAmbientMethod.hx b/away3d/materials/methods/BasicAmbientMethod.hx index 15e1520..e332120 100644 --- a/away3d/materials/methods/BasicAmbientMethod.hx +++ b/away3d/materials/methods/BasicAmbientMethod.hx @@ -148,7 +148,10 @@ class BasicAmbientMethod extends ShadingMethodBase { override public function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):Void { if (_useTexture) { #if !flash - stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE, vo.maxAnisotropy ); + stage3DProxy._context3D.setSamplerStateAt( + vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, + getSmoothingFilter(vo.useSmoothTextures, vo.anisotropy), + vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE ); #end stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy)); } diff --git a/away3d/materials/methods/BasicDiffuseMethod.hx b/away3d/materials/methods/BasicDiffuseMethod.hx index 613d740..f81b70f 100644 --- a/away3d/materials/methods/BasicDiffuseMethod.hx +++ b/away3d/materials/methods/BasicDiffuseMethod.hx @@ -299,7 +299,10 @@ class BasicDiffuseMethod extends LightingMethodBase { override public function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):Void { if (_useTexture) { #if !flash - stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE, vo.maxAnisotropy ); + stage3DProxy._context3D.setSamplerStateAt( + vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, + getSmoothingFilter(vo.useSmoothTextures, vo.anisotropy), + vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE ); #end stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy)); if (_alphaThreshold > 0) vo.fragmentData[vo.fragmentConstantsIndex] = _alphaThreshold; diff --git a/away3d/materials/methods/BasicNormalMethod.hx b/away3d/materials/methods/BasicNormalMethod.hx index a39930b..29965d4 100644 --- a/away3d/materials/methods/BasicNormalMethod.hx +++ b/away3d/materials/methods/BasicNormalMethod.hx @@ -96,7 +96,10 @@ class BasicNormalMethod extends ShadingMethodBase { override public function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):Void { if (vo.texturesIndex >= 0) { #if !flash - stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE, vo.maxAnisotropy ); + stage3DProxy._context3D.setSamplerStateAt( + vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, + getSmoothingFilter(vo.useSmoothTextures, vo.anisotropy), + vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE ); #end stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy)); } diff --git a/away3d/materials/methods/BasicSpecularMethod.hx b/away3d/materials/methods/BasicSpecularMethod.hx index cc71b91..30fdbd5 100644 --- a/away3d/materials/methods/BasicSpecularMethod.hx +++ b/away3d/materials/methods/BasicSpecularMethod.hx @@ -248,7 +248,10 @@ class BasicSpecularMethod extends LightingMethodBase { if (vo.numLights == 0) return; if (_useTexture) { #if !flash - stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE, vo.maxAnisotropy ); + stage3DProxy._context3D.setSamplerStateAt( + vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, + getSmoothingFilter(vo.useSmoothTextures, vo.anisotropy), + vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE ); #end stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy)); } diff --git a/away3d/materials/methods/MethodVO.hx b/away3d/materials/methods/MethodVO.hx index f0b7aff..a852eb1 100644 --- a/away3d/materials/methods/MethodVO.hx +++ b/away3d/materials/methods/MethodVO.hx @@ -5,6 +5,7 @@ package away3d.materials.methods; import openfl.Vector; +import away3d.textures.Anisotropy; class MethodVO { @@ -23,7 +24,7 @@ class MethodVO { public var useMipmapping:Bool; public var useSmoothTextures:Bool; public var repeatTextures:Bool; - public var maxAnisotropy:Float; + public var anisotropy:Anisotropy; // internal stuff for the material to know before assembling code public var needsProjection:Bool; public var needsView:Bool; @@ -51,7 +52,7 @@ class MethodVO { vertexConstantsIndex = -1; fragmentConstantsIndex = -1; useMipmapping = true; - maxAnisotropy = 1; + anisotropy = Anisotropy.ANISOTROPIC2X; useSmoothTextures = true; repeatTextures = false; needsProjection = false; diff --git a/away3d/materials/methods/ShadingMethodBase.hx b/away3d/materials/methods/ShadingMethodBase.hx index d24e80d..ad02329 100644 --- a/away3d/materials/methods/ShadingMethodBase.hx +++ b/away3d/materials/methods/ShadingMethodBase.hx @@ -6,7 +6,6 @@ package away3d.materials.methods; import away3d.events.ShadingMethodEvent; import away3d.events.ShadingMethodEvent; -import openfl.display3D.Context3DTextureFormat; import away3d.textures.TextureProxyBase; import away3d.materials.compilation.ShaderRegisterElement; import away3d.cameras.Camera3D; @@ -16,6 +15,10 @@ import away3d.materials.compilation.ShaderRegisterCache; import away3d.library.assets.NamedAssetBase; import away3d.materials.compilation.ShaderRegisterData; import away3d.materials.passes.MaterialPassBase; +import away3d.textures.Anisotropy; + +import openfl.display3D.Context3DTextureFormat; +import openfl.display3D.Context3DTextureFilter; class ShadingMethodBase extends NamedAssetBase { public var sharedRegisters(get_sharedRegisters, set_sharedRegisters):ShaderRegisterData; @@ -203,5 +206,25 @@ class ShadingMethodBase extends NamedAssetBase { */ public function copyFrom(method:ShadingMethodBase):Void { } + + /* + * Set the smoothing dependent on smooth property and anisotropy property from the VO + */ + private function getSmoothingFilter(smooth:Bool, anisotropy:Anisotropy) { + #if flash + return smooth ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST; + #else + if (smooth) { + switch (anisotropy) { + case Anisotropy.ANISOTROPIC2X : return Context3DTextureFilter.ANISOTROPIC2X; + case Anisotropy.ANISOTROPIC4X : return Context3DTextureFilter.ANISOTROPIC4X; + case Anisotropy.ANISOTROPIC8X : return Context3DTextureFilter.ANISOTROPIC8X; + case Anisotropy.ANISOTROPIC16X : return Context3DTextureFilter.ANISOTROPIC16X; + case Anisotropy.NONE : return Context3DTextureFilter.LINEAR; + } + } else + return Context3DTextureFilter.NEAREST; + #end + } } diff --git a/away3d/materials/passes/CompiledPass.hx b/away3d/materials/passes/CompiledPass.hx index 8b74949..8d039fc 100644 --- a/away3d/materials/passes/CompiledPass.hx +++ b/away3d/materials/passes/CompiledPass.hx @@ -21,6 +21,7 @@ import away3d.materials.methods.MethodVOSet; import away3d.materials.methods.ShaderMethodSetup; import away3d.materials.methods.ShadowMapMethodBase; import away3d.textures.Texture2DBase; +import away3d.textures.Anisotropy; import openfl.display3D.Context3D; import openfl.display3D.Context3DProgramType; import openfl.geom.Matrix; @@ -209,7 +210,7 @@ class CompiledPass extends MaterialPassBase { _compiler.methodSetup = _methodSetup; _compiler.diffuseLightSources = _diffuseLightSources; _compiler.specularLightSources = _specularLightSources; - _compiler.setTextureSampling(_smooth, _repeat, _mipmap, _maxAnisotropy); + _compiler.setTextureSampling(_smooth, _repeat, _mipmap, _anisotropy); _compiler.setConstantDataBuffers(_vertexConstantData, _fragmentConstantData); _compiler.animateUVs = _animateUVs; _compiler.alphaPremultiplied = _alphaPremultiplied && _enableBlending; @@ -305,9 +306,9 @@ class CompiledPass extends MaterialPassBase { /** * @inheritDoc */ - override public function set_maxAnisotropy(value:Float):Float { - if (_maxAnisotropy == value) return value; - super.maxAnisotropy = value; + override public function set_anisotropy(value:Anisotropy):Anisotropy { + if (_anisotropy == value) return value; + super.anisotropy = value; return value; } diff --git a/away3d/materials/passes/MaterialPassBase.hx b/away3d/materials/passes/MaterialPassBase.hx index e43a5a5..4296f1a 100644 --- a/away3d/materials/passes/MaterialPassBase.hx +++ b/away3d/materials/passes/MaterialPassBase.hx @@ -1,29 +1,31 @@ package away3d.materials.passes; - import away3d.utils.ArrayUtils; - import openfl.errors.ArgumentError; - import openfl.errors.Error; - import away3d.animators.data.AnimationRegisterCache; - import away3d.animators.IAnimationSet; - import away3d.cameras.Camera3D; - import away3d.core.base.IRenderable; - import away3d.core.managers.AGALProgram3DCache; - import away3d.core.managers.Stage3DProxy; - import away3d.debug.Debug; - import away3d.errors.AbstractMethodError; - import away3d.materials.MaterialBase; - import away3d.materials.lightpickers.LightPickerBase; - import openfl.display.BlendMode; - import openfl.display3D.Context3D; - import openfl.display3D.Context3DBlendFactor; - import openfl.display3D.Context3DCompareMode; - import openfl.display3D.Context3DTriangleFace; - import openfl.display3D.Program3D; - import openfl.display3D.textures.TextureBase; - import openfl.events.Event; - import openfl.events.EventDispatcher; - import openfl.geom.Matrix3D; - import openfl.geom.Rectangle; +import away3d.utils.ArrayUtils; +import openfl.errors.ArgumentError; +import openfl.errors.Error; +import away3d.animators.data.AnimationRegisterCache; +import away3d.animators.IAnimationSet; +import away3d.cameras.Camera3D; +import away3d.core.base.IRenderable; +import away3d.core.managers.AGALProgram3DCache; +import away3d.core.managers.Stage3DProxy; +import away3d.debug.Debug; +import away3d.errors.AbstractMethodError; +import away3d.materials.MaterialBase; +import away3d.materials.lightpickers.LightPickerBase; +import away3d.textures.Anisotropy; + +import openfl.display.BlendMode; +import openfl.display3D.Context3D; +import openfl.display3D.Context3DBlendFactor; +import openfl.display3D.Context3DCompareMode; +import openfl.display3D.Context3DTriangleFace; +import openfl.display3D.Program3D; +import openfl.display3D.textures.TextureBase; +import openfl.events.Event; +import openfl.events.EventDispatcher; +import openfl.geom.Matrix3D; +import openfl.geom.Rectangle; class MaterialPassBase extends EventDispatcher { public var material(get_material, set_material):MaterialBase; @@ -31,7 +33,7 @@ class MaterialPassBase extends EventDispatcher { public var mipmap(get_mipmap, set_mipmap):Bool; public var smooth(get_smooth, set_smooth):Bool; public var repeat(get_repeat, set_repeat):Bool; - public var maxAnisotropy(get_maxAnisotropy, set_maxAnisotropy):Float; + public var anisotropy(get_anisotropy, set_anisotropy):Anisotropy; public var bothSides(get_bothSides, set_bothSides):Bool; public var depthCompareMode(get_depthCompareMode, set_depthCompareMode):Context3DCompareMode; public var animationSet(get_animationSet, set_animationSet):IAnimationSet; @@ -61,7 +63,7 @@ class MaterialPassBase extends EventDispatcher { private var _smooth:Bool; private var _repeat:Bool; private var _mipmap:Bool; - private var _maxAnisotropy:Float; + private var _anisotropy:Anisotropy; private var _depthCompareMode:Context3DCompareMode; private var _blendFactorSource:Context3DBlendFactor; private var _blendFactorDest:Context3DBlendFactor; @@ -107,7 +109,7 @@ class MaterialPassBase extends EventDispatcher { _smooth = true; _repeat = false; _mipmap = true; - _maxAnisotropy = 1; + _anisotropy = Anisotropy.ANISOTROPIC2X; _depthCompareMode = Context3DCompareMode.LESS_EQUAL; _blendFactorSource = Context3DBlendFactor.ONE; @@ -174,16 +176,16 @@ class MaterialPassBase extends EventDispatcher { /** * Indicates the number of Anisotropic filtering samples to take for mipmapping */ - public function get_maxAnisotropy():Float { - return _maxAnisotropy; + public function get_anisotropy():Anisotropy { + return _anisotropy; } - public function set_maxAnisotropy(value:Float):Float { - if (_maxAnisotropy == value) - return _maxAnisotropy; - _maxAnisotropy = value; + public function set_anisotropy(value:Anisotropy):Anisotropy { + if (_anisotropy == value) + return _anisotropy; + _anisotropy = value; invalidateShaderProgram(); - return maxAnisotropy; + return _anisotropy; } /** diff --git a/away3d/textures/Anisotropy.hx b/away3d/textures/Anisotropy.hx new file mode 100644 index 0000000..fd09e85 --- /dev/null +++ b/away3d/textures/Anisotropy.hx @@ -0,0 +1,9 @@ +package away3d.textures; + +enum Anisotropy { + NONE; + ANISOTROPIC2X; + ANISOTROPIC4X; + ANISOTROPIC8X; + ANISOTROPIC16X; +} \ No newline at end of file diff --git a/backends/flash/flash/display3D/Context3DTextureFilter.hx b/backends/flash/flash/display3D/Context3DTextureFilter.hx new file mode 100644 index 0000000..6b63dc7 --- /dev/null +++ b/backends/flash/flash/display3D/Context3DTextureFilter.hx @@ -0,0 +1,10 @@ +package flash.display3D; + +enum Context3DTextureFilter { + ANISOTROPIC2X; + ANISOTROPIC4X; + ANISOTROPIC8X; + ANISOTROPIC16X; + LINEAR; + NEAREST; +} \ No newline at end of file diff --git a/backends/html5/openfl/display3D/Context3D.hx b/backends/html5/openfl/display3D/Context3D.hx index 0703dea..0391911 100755 --- a/backends/html5/openfl/display3D/Context3D.hx +++ b/backends/html5/openfl/display3D/Context3D.hx @@ -26,6 +26,9 @@ typedef Location = Int; class Context3D { + public static var TEXTURE_MAX_ANISOTROPY_EXT : UInt = 0x84FE; + public static var MAX_TEXTURE_MAX_ANISOTROPY_EXT : UInt = 0x84FF; + public var driverInfo(default, null):String; // TODO public var enableErrorChecking:Bool; // TODO ( use GL.getError() and GL.validateProgram(program) ) @@ -53,15 +56,19 @@ class Context3D private var texturesCreated : Array; private var framebuffer : GLFramebuffer; - private var renderbuffer : GLRenderbuffer; + private var renderbuffer : GLRenderbuffer; private var depthbuffer : GLRenderbuffer; private var stencilbuffer : GLRenderbuffer; private var defaultFrameBuffer : GLFramebuffer; private var samplerParameters :Array; //TODO : use Tupple3 - private var scrollRect:Rectangle; + private var scrollRect:Rectangle; + + private static var anisotropySupportTested:Bool = false; + private static var supportsAnisotropy:Bool = false; + private static var maxSupportedAnisotropy:UInt = 256; - public static var MAX_SAMPLERS:Int = 8; + public static var MAX_SAMPLERS:Int = 8; public function new() { @@ -71,13 +78,12 @@ class Context3D programsCreated = new Array(); texturesCreated = new Array(); samplerParameters = new Array(); - for ( i in 0...MAX_SAMPLERS) { - this.samplerParameters[ i ] = new SamplerState(); - this.samplerParameters[ i ].wrap = Context3DWrapMode.REPEAT; - this.samplerParameters[ i ].filter = Context3DTextureFilter.LINEAR; - this.samplerParameters[ i ].mipfilter = Context3DMipFilter.MIPNONE; - this.samplerParameters[ i ].maxAnisotropy = 1; - } + for ( i in 0...MAX_SAMPLERS) { + this.samplerParameters[ i ] = new SamplerState(); + this.samplerParameters[ i ].wrap = Context3DWrapMode.REPEAT; + this.samplerParameters[ i ].filter = Context3DTextureFilter.LINEAR; + this.samplerParameters[ i ].mipfilter =Context3DMipFilter.MIPNONE; + } var stage = Lib.current.stage; @@ -88,11 +94,11 @@ class Context3D ogl.height = stage.stageHeight; //todo html something - //#if html5 - //stage.addChild(ogl); - //#else - stage.addChildAt(ogl, 0); - //#end + //#if html5 + //stage.addChild(ogl); + //#else + stage.addChildAt(ogl, 0); + //#end } public function clear(red:Float = 0, green:Float = 0, blue:Float = 0, alpha:Float = 1, depth:Float = 1, stencil:Int = 0, mask:Int = Context3DClearMask.ALL):Void @@ -124,9 +130,6 @@ class Context3D ogl.scrollRect = new Rectangle(0, 0, width, height); scrollRect = ogl.scrollRect.clone(); GL.viewport(Std.int(scrollRect.x),Std.int(scrollRect.y),Std.int(scrollRect.width),Std.int(scrollRect.height)); - #if ios - defaultFrameBuffer = new GLFramebuffer(GL.version, 1); //TODO: GL.getParameter(GL.FRAMEBUFFER_BINDING)); - #end } public function createCubeTexture(size:Int, format:Context3DTextureFormat, optimizeForRenderToTexture:Bool, streamingLevels:Int = 0):CubeTexture @@ -205,12 +208,12 @@ class Context3D GL.deleteFramebuffer(framebuffer); framebuffer = null; } - + if(renderbuffer != null){ GL.deleteRenderbuffer(renderbuffer); renderbuffer = null; } - + disposed = true; } @@ -352,7 +355,7 @@ class Context3D data.position = byteArrayOffset; } var location = GL.getUniformLocation(currentProgram.glProgram, locationName); - GL.uniform4f(location, data.readFloat(),data.readFloat(),data.readFloat(),data.readFloat()); + GL.uniform4f(location, data.readFloat(), data.readFloat(), data.readFloat(), data.readFloat()); } public function setGLSLProgramConstantsFromMatrix(locationName : String, matrix:Matrix3D, transposedMatrix:Bool = false):Void @@ -382,7 +385,7 @@ class Context3D } // TODO : currently does not work (framebufferStatus always return zero) - public function setRenderToTexture (texture:TextureBase, enableDepthAndStencil:Bool = false, antiAlias:Int = 0, surfaceSelector:Int = 0):Void { + public function setRenderToTexture (texture:TextureBase, enableDepthAndStencil:Bool = false, antiAlias:Int = 0, surfaceSelector:Int = 0):Void { if (framebuffer == null) framebuffer = GL.createFramebuffer(); @@ -392,14 +395,8 @@ class Context3D if (renderbuffer == null) renderbuffer = GL.createRenderbuffer(); - GL.bindRenderbuffer(GL.RENDERBUFFER, renderbuffer); - //#if ios - //GL.renderbufferStorage(GL.RENDERBUFFER, 0x88F0, texture.width, texture.height); - //#else GL.renderbufferStorage(GL.RENDERBUFFER, GL.DEPTH_STENCIL, texture.width, texture.height); - //#end - // GL.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0, GL.TEXTURE_2D, texture.glTexture, 0); if (enableDepthAndStencil) { @@ -415,25 +412,34 @@ class Context3D GL.viewport(0, 0, texture.width, texture.height); } - public function setSamplerStateAt(sampler:Int, wrap:Context3DWrapMode, filter:Context3DTextureFilter, mipfilter:Context3DMipFilter, maxAnisotropy:Float = 1 ):Void + public function setSamplerStateAt(sampler:Int, wrap:Context3DWrapMode, filter:Context3DTextureFilter, mipfilter:Context3DMipFilter ):Void { //TODO for flash < 11.6 : patch the AGAL (using specific opcodes) and rebuild the program? - if (0 <= sampler && sampler < MAX_SAMPLERS) { - this.samplerParameters[ sampler ].wrap = wrap; - this.samplerParameters[ sampler ].filter = filter; - this.samplerParameters[ sampler ].mipfilter = mipfilter; - this.samplerParameters[ sampler ].maxAnisotropy = maxAnisotropy; - } else { - throw "Sampler is out of bounds."; - } + if (0 <= sampler && sampler < MAX_SAMPLERS) { + this.samplerParameters[ sampler ].wrap = wrap; + this.samplerParameters[ sampler ].filter = filter; + this.samplerParameters[ sampler ].mipfilter = mipfilter; + } else { + throw "Sampler is out of bounds."; + } } - private function setTextureParameters(texture : TextureBase, wrap : Context3DWrapMode, filter : Context3DTextureFilter, mipfilter : Context3DMipFilter, maxAnisotropy:Float = 1 ):Void{ + private function setTextureParameters(texture : TextureBase, wrap : Context3DWrapMode, filter : Context3DTextureFilter, mipfilter : Context3DMipFilter ):Void{ + if ( !anisotropySupportTested ) { + var ext = GL.getExtension("EXT_texture_filter_anisotropic") || GL.getExtension("MOZ_EXT_texture_filter_anisotropic") || GL.getExtension("WEBKIT_EXT_texture_filter_anisotropic"); + supportsAnisotropy = (ext != null); + anisotropySupportTested = true; + + GL.texParameterf(GL.TEXTURE_2D, 0x84FE, maxSupportedAnisotropy); + maxSupportedAnisotropy = GL.getTexParameter(GL.TEXTURE_2D, 0x84FE); + } + if (Std.is (texture, openfl.display3D.textures.Texture)) { - switch(wrap){ + //GL.bindTexture(GL.TEXTURE_2D, cast(texture, openfl.display3D.textures.Texture).glTexture); + switch ( wrap ) { case Context3DWrapMode.CLAMP: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE); @@ -442,66 +448,87 @@ class Context3D GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.REPEAT); } - switch(filter){ + // Currently using TEXTURE_MAX_ANISOTROPY_EXT instead of GL.TEXTURE_MAX_ANISOTROPY_EXT + // until it is implemented. + switch ( filter ) { case Context3DTextureFilter.LINEAR: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); case Context3DTextureFilter.NEAREST: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); + + case Context3DTextureFilter.ANISOTROPIC2X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 2) ? maxSupportedAnisotropy : 2 ); + + case Context3DTextureFilter.ANISOTROPIC4X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 4) ? maxSupportedAnisotropy : 4 ); + + case Context3DTextureFilter.ANISOTROPIC8X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 8) ? maxSupportedAnisotropy : 8 ); + + case Context3DTextureFilter.ANISOTROPIC16X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 16) ? maxSupportedAnisotropy : 16 ); + } - //TODO CHECK the mipmap filters - switch(mipfilter){ + switch ( mipfilter ) { case Context3DMipFilter.MIPLINEAR: GL.generateMipmap(GL.TEXTURE_2D); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR); - var ext = GL.getExtension("EXT_texture_filter_anisotropic") || GL.getExtension("MOZ_EXT_texture_filter_anisotropic") || GL.getExtension("WEBKIT_EXT_texture_filter_anisotropic"); - if (ext!=null) { - // I found that setting the filtering to a higher level then querying the texParameter - // returns the maximum capability for the device (as far as I can tell) - GL.texParameterf(GL.TEXTURE_2D, 0x84FE, maxAnisotropy); - var actualMaxAnisotropy = GL.getTexParameter(GL.TEXTURE_2D, 0x84FE); - - // Currently hard coded 0x84FE as the GL.TEXTURE_MAX_ANISOTROPY_EXT enum - // GL.texParameterf(GL_TEXTURE_2D, GL.TEXTURE_MAX_ANISOTROPY_EXT, 8.0); - GL.texParameterf(GL.TEXTURE_2D, 0x84FE, (actualMaxAnisotropy < maxAnisotropy) ? actualMaxAnisotropy : maxAnisotropy ); - } - case Context3DMipFilter.MIPNEAREST: GL.generateMipmap(GL.TEXTURE_2D); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST_MIPMAP_NEAREST); - var ext = GL.getExtension("EXT_texture_filter_anisotropic") || GL.getExtension("MOZ_EXT_texture_filter_anisotropic") || GL.getExtension("WEBKIT_EXT_texture_filter_anisotropic"); - if (ext!=null) { - // I found that setting the filtering to a higher level then querying the texParameter - // returns the maximum capability for the device (as far as I can tell) - GL.texParameterf(GL.TEXTURE_2D, 0x84FE, maxAnisotropy); - var actualMaxAnisotropy = GL.getTexParameter(GL.TEXTURE_2D, 0x84FE); - - // Currently hard coded 0x84FE as the GL.TEXTURE_MAX_ANISOTROPY_EXT enum - // GL.texParameterf(GL_TEXTURE_2D, GL.TEXTURE_MAX_ANISOTROPY_EXT, 8.0); - GL.texParameterf(GL.TEXTURE_2D, 0x84FE, (actualMaxAnisotropy < maxAnisotropy) ? actualMaxAnisotropy : maxAnisotropy ); - } - case Context3DMipFilter.MIPNONE: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); } - } else if (Std.is (texture, openfl.display3D.textures.RectangleTexture)) { + + } else if ( Std.is (texture, openfl.display3D.textures.RectangleTexture) ) { + + //GL.bindTexture(GL.TEXTURE_2D, cast(texture, openfl.display3D.textures.RectangleTexture).glTexture); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE); - switch(filter){ + switch ( filter ) { case Context3DTextureFilter.LINEAR: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); case Context3DTextureFilter.NEAREST: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); + + case Context3DTextureFilter.ANISOTROPIC2X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 2) ? maxSupportedAnisotropy : 2 ); + + case Context3DTextureFilter.ANISOTROPIC4X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 4) ? maxSupportedAnisotropy : 4 ); + + case Context3DTextureFilter.ANISOTROPIC8X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 8) ? maxSupportedAnisotropy : 8 ); + + case Context3DTextureFilter.ANISOTROPIC16X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 16) ? maxSupportedAnisotropy : 16 ); } GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); - } - else if (Std.is (texture, openfl.display3D.textures.CubeTexture)) { + } else if (Std.is (texture, openfl.display3D.textures.CubeTexture)) { + //GL.bindTexture(GL.TEXTURE_CUBE_MAP, cast(texture, openfl.display3D.textures.CubeTexture).glTexture); switch(wrap){ case Context3DWrapMode.CLAMP: @@ -512,16 +539,35 @@ class Context3D GL.texParameteri(GL.TEXTURE_CUBE_MAP, GL.TEXTURE_WRAP_T, GL.REPEAT); } - switch(filter){ + switch ( filter ) { case Context3DTextureFilter.LINEAR: GL.texParameteri(GL.TEXTURE_CUBE_MAP, GL.TEXTURE_MAG_FILTER, GL.LINEAR); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); case Context3DTextureFilter.NEAREST: GL.texParameteri(GL.TEXTURE_CUBE_MAP, GL.TEXTURE_MAG_FILTER, GL.NEAREST); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); + + case Context3DTextureFilter.ANISOTROPIC2X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 2) ? maxSupportedAnisotropy : 2 ); + + case Context3DTextureFilter.ANISOTROPIC4X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 4) ? maxSupportedAnisotropy : 4 ); + + case Context3DTextureFilter.ANISOTROPIC8X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 8) ? maxSupportedAnisotropy : 8 ); + + case Context3DTextureFilter.ANISOTROPIC16X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 16) ? maxSupportedAnisotropy : 16 ); } - //TODO CHECK the mipmap filters - switch(mipfilter){ + switch ( mipfilter ) { case Context3DMipFilter.MIPLINEAR: GL.texParameteri(GL.TEXTURE_CUBE_MAP, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR); @@ -540,12 +586,12 @@ class Context3D public function setScissorRectangle(rectangle:Rectangle):Void { // TODO test it - if (rectangle == null) { - GL.disable(GL.SCISSOR_TEST); - return; - } + if (rectangle == null) { + GL.disable(GL.SCISSOR_TEST); + return; + } - GL.enable(GL.SCISSOR_TEST); + GL.enable(GL.SCISSOR_TEST); GL.scissor(Std.int(rectangle.x), Std.int(scrollRect.height - rectangle.y - rectangle.height), Std.int(rectangle.width), Std.int(rectangle.height)); } @@ -596,7 +642,6 @@ class Context3D if ( Std.is (texture, openfl.display3D.textures.Texture) ) { GL.bindTexture(GL.TEXTURE_2D, cast(texture, openfl.display3D.textures.Texture).glTexture); GL.uniform1i(location, textureIndex); - } else if ( Std.is(texture, RectangleTexture) ) { GL.bindTexture(GL.TEXTURE_2D, cast(texture, openfl.display3D.textures.RectangleTexture).glTexture); GL.uniform1i(location, textureIndex); @@ -609,9 +654,9 @@ class Context3D var parameters:SamplerState= samplerParameters[textureIndex]; if (parameters != null) { - setTextureParameters(texture, parameters.wrap, parameters.filter, parameters.mipfilter, parameters.maxAnisotropy); + setTextureParameters(texture, parameters.wrap, parameters.filter, parameters.mipfilter); } else { - setTextureParameters(texture, Context3DWrapMode.REPEAT, Context3DTextureFilter.NEAREST, Context3DMipFilter.MIPNONE, 1); + setTextureParameters(texture, Context3DWrapMode.REPEAT, Context3DTextureFilter.NEAREST, Context3DMipFilter.MIPNONE); } } diff --git a/backends/html5/openfl/display3D/Context3DTextureFilter.hx b/backends/html5/openfl/display3D/Context3DTextureFilter.hx index f066301..5af07b0 100755 --- a/backends/html5/openfl/display3D/Context3DTextureFilter.hx +++ b/backends/html5/openfl/display3D/Context3DTextureFilter.hx @@ -5,6 +5,10 @@ package openfl.display3D; enum Context3DTextureFilter { + ANISOTROPIC2X; + ANISOTROPIC4X; + ANISOTROPIC8X; + ANISOTROPIC16X; LINEAR; NEAREST; } diff --git a/backends/html5/openfl/display3D/SamplerState.hx b/backends/html5/openfl/display3D/SamplerState.hx index da40969..27adbfe 100755 --- a/backends/html5/openfl/display3D/SamplerState.hx +++ b/backends/html5/openfl/display3D/SamplerState.hx @@ -5,7 +5,6 @@ class SamplerState public var wrap:Context3DWrapMode ; public var filter:Context3DTextureFilter ; public var mipfilter:Context3DMipFilter ; - public var maxAnisotropy:Float ; public function new():Void { diff --git a/backends/native/openfl/display3D/Context3D.hx b/backends/native/openfl/display3D/Context3D.hx index 4fa6673..fae151b 100755 --- a/backends/native/openfl/display3D/Context3D.hx +++ b/backends/native/openfl/display3D/Context3D.hx @@ -26,6 +26,9 @@ typedef Location = Int; class Context3D { + public static var TEXTURE_MAX_ANISOTROPY_EXT : UInt = 0x84FE; + public static var MAX_TEXTURE_MAX_ANISOTROPY_EXT : UInt = 0x84FF; + public var driverInfo(default, null):String; // TODO public var enableErrorChecking:Bool; // TODO ( use GL.getError() and GL.validateProgram(program) ) @@ -53,15 +56,19 @@ class Context3D private var texturesCreated : Array; private var framebuffer : GLFramebuffer; - private var renderbuffer : GLRenderbuffer; + private var renderbuffer : GLRenderbuffer; private var depthbuffer : GLRenderbuffer; private var stencilbuffer : GLRenderbuffer; private var defaultFrameBuffer : GLFramebuffer; private var samplerParameters :Array; //TODO : use Tupple3 - private var scrollRect:Rectangle; + private var scrollRect:Rectangle; + + private static var anisotropySupportTested:Bool = false; + private static var supportsAnisotropy:Bool = false; + private static var maxSupportedAnisotropy:UInt = 256; - public static var MAX_SAMPLERS:Int = 8; + public static var MAX_SAMPLERS:Int = 8; public function new() { @@ -71,13 +78,12 @@ class Context3D programsCreated = new Array(); texturesCreated = new Array(); samplerParameters = new Array(); - for ( i in 0...MAX_SAMPLERS) { - this.samplerParameters[ i ] = new SamplerState(); - this.samplerParameters[ i ].wrap = Context3DWrapMode.REPEAT; - this.samplerParameters[ i ].filter = Context3DTextureFilter.LINEAR; - this.samplerParameters[ i ].mipfilter =Context3DMipFilter.MIPNONE; - this.samplerParameters[ i ].maxAnisotropy = 1; - } + for ( i in 0...MAX_SAMPLERS) { + this.samplerParameters[ i ] = new SamplerState(); + this.samplerParameters[ i ].wrap = Context3DWrapMode.REPEAT; + this.samplerParameters[ i ].filter = Context3DTextureFilter.LINEAR; + this.samplerParameters[ i ].mipfilter =Context3DMipFilter.MIPNONE; + } var stage = Lib.current.stage; @@ -88,11 +94,11 @@ class Context3D ogl.height = stage.stageHeight; //todo html something - //#if html5 - //stage.addChild(ogl); - //#else - stage.addChildAt(ogl, 0); - //#end + //#if html5 + //stage.addChild(ogl); + //#else + stage.addChildAt(ogl, 0); + //#end } public function clear(red:Float = 0, green:Float = 0, blue:Float = 0, alpha:Float = 1, depth:Float = 1, stencil:Int = 0, mask:Int = Context3DClearMask.ALL):Void @@ -152,7 +158,7 @@ class Context3D public function createTexture(width:Int, height:Int, format:Context3DTextureFormat, optimizeForRenderToTexture:Bool, streamingLevels:Int = 0):openfl.display3D.textures.Texture { - var texture = new openfl.display3D.textures.Texture (GL.createTexture (), optimizeForRenderToTexture,width, height); // TODO use format, optimizeForRenderToTexture and streamingLevels? + var texture = new openfl.display3D.textures.Texture(GL.createTexture(), optimizeForRenderToTexture, width, height); // TODO use format, optimizeForRenderToTexture and streamingLevels? texturesCreated.push(texture); return texture; } @@ -205,12 +211,12 @@ class Context3D GL.deleteFramebuffer(framebuffer); framebuffer = null; } - + if(renderbuffer != null){ GL.deleteRenderbuffer(renderbuffer); renderbuffer = null; } - + disposed = true; } @@ -379,7 +385,7 @@ class Context3D } // TODO : currently does not work (framebufferStatus always return zero) - public function setRenderToTexture (texture:TextureBase, enableDepthAndStencil:Bool = false, antiAlias:Int = 0, surfaceSelector:Int = 0):Void { + public function setRenderToTexture (texture:TextureBase, enableDepthAndStencil:Bool = false, antiAlias:Int = 0, surfaceSelector:Int = 0):Void { if (framebuffer == null) framebuffer = GL.createFramebuffer(); @@ -411,26 +417,33 @@ class Context3D GL.viewport(0, 0, texture.width, texture.height); } - public function setSamplerStateAt(sampler:Int, wrap:Context3DWrapMode, filter:Context3DTextureFilter, mipfilter:Context3DMipFilter, maxAnisotropy:Float = 1 ):Void + public function setSamplerStateAt(sampler:Int, wrap:Context3DWrapMode, filter:Context3DTextureFilter, mipfilter:Context3DMipFilter ):Void { //TODO for flash < 11.6 : patch the AGAL (using specific opcodes) and rebuild the program? - if (0 <= sampler && sampler < MAX_SAMPLERS) { - this.samplerParameters[ sampler ].wrap = wrap; - this.samplerParameters[ sampler ].filter = filter; + if (0 <= sampler && sampler < MAX_SAMPLERS) { + this.samplerParameters[ sampler ].wrap = wrap; + this.samplerParameters[ sampler ].filter = filter; this.samplerParameters[ sampler ].mipfilter = mipfilter; - this.samplerParameters[ sampler ].maxAnisotropy = maxAnisotropy; - } else { - throw "Sampler is out of bounds."; - } + } else { + throw "Sampler is out of bounds."; + } } - private function setTextureParameters(texture : TextureBase, wrap : Context3DWrapMode, filter : Context3DTextureFilter, mipfilter : Context3DMipFilter, maxAnisotropy:Float = 1 ):Void{ + private function setTextureParameters(texture : TextureBase, wrap : Context3DWrapMode, filter : Context3DTextureFilter, mipfilter : Context3DMipFilter ):Void{ + + if ( !anisotropySupportTested ) { + supportsAnisotropy = (GL.getSupportedExtensions().indexOf("GL_EXT_texture_filter_anisotropic")!=-1); + anisotropySupportTested = true; + + GL.texParameterf(GL.TEXTURE_2D, 0x84FE, maxSupportedAnisotropy); + maxSupportedAnisotropy = GL.getTexParameter(GL.TEXTURE_2D, 0x84FE); + } if (Std.is (texture, openfl.display3D.textures.Texture)) { GL.bindTexture(GL.TEXTURE_2D, cast(texture, openfl.display3D.textures.Texture).glTexture); - switch(wrap){ + switch ( wrap ) { case Context3DWrapMode.CLAMP: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE); @@ -439,61 +452,81 @@ class Context3D GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.REPEAT); } - switch(filter){ + // Currently using TEXTURE_MAX_ANISOTROPY_EXT instead of GL.TEXTURE_MAX_ANISOTROPY_EXT + // until it is implemented. + switch ( filter ) { case Context3DTextureFilter.LINEAR: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); case Context3DTextureFilter.NEAREST: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); + + case Context3DTextureFilter.ANISOTROPIC2X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 2) ? maxSupportedAnisotropy : 2 ); + + case Context3DTextureFilter.ANISOTROPIC4X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 4) ? maxSupportedAnisotropy : 4 ); + + case Context3DTextureFilter.ANISOTROPIC8X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 8) ? maxSupportedAnisotropy : 8 ); + + case Context3DTextureFilter.ANISOTROPIC16X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 16) ? maxSupportedAnisotropy : 16 ); } - //TODO CHECK the mipmap filters - switch(mipfilter){ - case Context3DMipFilter.MIPLINEAR: + switch ( mipfilter ) { + case Context3DMipFilter.MIPLINEAR: GL.generateMipmap(GL.TEXTURE_2D); - GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR); - - if (GL.getSupportedExtensions().indexOf("GL_EXT_texture_filter_anisotropic")!=-1) { - // I found that setting the filtering to a higher level then querying the texParameter - // returns the maximum capability for the device (as far as I can tell) - GL.texParameterf(GL.TEXTURE_2D, 0x84FE, maxAnisotropy); - var actualMaxAnisotropy = GL.getTexParameter(GL.TEXTURE_2D, 0x84FE); - - // Currently hard coded 0x84FE as the GL.TEXTURE_MAX_ANISOTROPY_EXT enum - // GL.texParameterf(GL_TEXTURE_2D, GL.TEXTURE_MAX_ANISOTROPY_EXT, 8.0); - GL.texParameterf(GL.TEXTURE_2D, 0x84FE, (actualMaxAnisotropy < maxAnisotropy) ? actualMaxAnisotropy : maxAnisotropy ); - } + GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR); case Context3DMipFilter.MIPNEAREST: GL.generateMipmap(GL.TEXTURE_2D); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST_MIPMAP_NEAREST); - if (GL.getSupportedExtensions().indexOf("GL_EXT_texture_filter_anisotropic")!=-1) { - // I found that setting the filtering to a higher level then querying the texParameter - // returns the maximum capability for the device (as far as I can tell) - GL.texParameterf(GL.TEXTURE_2D, 0x84FE, maxAnisotropy); - var actualMaxAnisotropy = GL.getTexParameter(GL.TEXTURE_2D, 0x84FE); - - // Currently hard coded 0x84FE as the GL.TEXTURE_MAX_ANISOTROPY_EXT enum - // GL.texParameterf(GL_TEXTURE_2D, GL.TEXTURE_MAX_ANISOTROPY_EXT, 8.0); - GL.texParameterf(GL.TEXTURE_2D, 0x84FE, (actualMaxAnisotropy < maxAnisotropy) ? actualMaxAnisotropy : maxAnisotropy ); - } - case Context3DMipFilter.MIPNONE: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); } - } else if (Std.is (texture, openfl.display3D.textures.RectangleTexture)) { + + } else if ( Std.is (texture, openfl.display3D.textures.RectangleTexture) ) { GL.bindTexture(GL.TEXTURE_2D, cast(texture, openfl.display3D.textures.RectangleTexture).glTexture); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE); GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE); - switch(filter){ + switch ( filter ) { case Context3DTextureFilter.LINEAR: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); case Context3DTextureFilter.NEAREST: GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); + + case Context3DTextureFilter.ANISOTROPIC2X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 2) ? maxSupportedAnisotropy : 2 ); + + case Context3DTextureFilter.ANISOTROPIC4X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 4) ? maxSupportedAnisotropy : 4 ); + + case Context3DTextureFilter.ANISOTROPIC8X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 8) ? maxSupportedAnisotropy : 8 ); + + case Context3DTextureFilter.ANISOTROPIC16X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_2D, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 16) ? maxSupportedAnisotropy : 16 ); } GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR); @@ -509,16 +542,35 @@ class Context3D GL.texParameteri(GL.TEXTURE_CUBE_MAP, GL.TEXTURE_WRAP_T, GL.REPEAT); } - switch(filter){ + switch ( filter ) { case Context3DTextureFilter.LINEAR: GL.texParameteri(GL.TEXTURE_CUBE_MAP, GL.TEXTURE_MAG_FILTER, GL.LINEAR); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); case Context3DTextureFilter.NEAREST: GL.texParameteri(GL.TEXTURE_CUBE_MAP, GL.TEXTURE_MAG_FILTER, GL.NEAREST); + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, 1 ); + + case Context3DTextureFilter.ANISOTROPIC2X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 2) ? maxSupportedAnisotropy : 2 ); + + case Context3DTextureFilter.ANISOTROPIC4X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 4) ? maxSupportedAnisotropy : 4 ); + + case Context3DTextureFilter.ANISOTROPIC8X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 8) ? maxSupportedAnisotropy : 8 ); + + case Context3DTextureFilter.ANISOTROPIC16X: + if (supportsAnisotropy) + GL.texParameterf(GL.TEXTURE_CUBE_MAP, TEXTURE_MAX_ANISOTROPY_EXT, (maxSupportedAnisotropy < 16) ? maxSupportedAnisotropy : 16 ); } - //TODO CHECK the mipmap filters - switch(mipfilter){ + switch ( mipfilter ) { case Context3DMipFilter.MIPLINEAR: GL.texParameteri(GL.TEXTURE_CUBE_MAP, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR); @@ -537,12 +589,12 @@ class Context3D public function setScissorRectangle(rectangle:Rectangle):Void { // TODO test it - if (rectangle == null) { - GL.disable(GL.SCISSOR_TEST); - return; - } + if (rectangle == null) { + GL.disable(GL.SCISSOR_TEST); + return; + } - GL.enable(GL.SCISSOR_TEST); + GL.enable(GL.SCISSOR_TEST); GL.scissor(Std.int(rectangle.x), Std.int(scrollRect.height - rectangle.y - rectangle.height), Std.int(rectangle.width), Std.int(rectangle.height)); } @@ -593,11 +645,9 @@ class Context3D if ( Std.is (texture, openfl.display3D.textures.Texture) ) { GL.bindTexture(GL.TEXTURE_2D, cast(texture, openfl.display3D.textures.Texture).glTexture); GL.uniform1i(location, textureIndex); - } else if ( Std.is(texture, RectangleTexture) ) { GL.bindTexture(GL.TEXTURE_2D, cast(texture, openfl.display3D.textures.RectangleTexture).glTexture); - GL.uniform1i(location, textureIndex); - + GL.uniform1i(location, textureIndex); } else if ( Std.is(texture, CubeTexture) ) { GL.bindTexture( GL.TEXTURE_CUBE_MAP, cast(texture, openfl.display3D.textures.CubeTexture).glTexture ); GL.uniform1i( location, textureIndex ); @@ -607,9 +657,9 @@ class Context3D var parameters:SamplerState= samplerParameters[textureIndex]; if (parameters != null) { - setTextureParameters(texture, parameters.wrap, parameters.filter, parameters.mipfilter, parameters.maxAnisotropy); + setTextureParameters(texture, parameters.wrap, parameters.filter, parameters.mipfilter); } else { - setTextureParameters(texture, Context3DWrapMode.REPEAT, Context3DTextureFilter.NEAREST, Context3DMipFilter.MIPNONE, 1); + setTextureParameters(texture, Context3DWrapMode.REPEAT, Context3DTextureFilter.NEAREST, Context3DMipFilter.MIPNONE); } } diff --git a/backends/native/openfl/display3D/Context3DTextureFilter.hx b/backends/native/openfl/display3D/Context3DTextureFilter.hx index f066301..5af07b0 100755 --- a/backends/native/openfl/display3D/Context3DTextureFilter.hx +++ b/backends/native/openfl/display3D/Context3DTextureFilter.hx @@ -5,6 +5,10 @@ package openfl.display3D; enum Context3DTextureFilter { + ANISOTROPIC2X; + ANISOTROPIC4X; + ANISOTROPIC8X; + ANISOTROPIC16X; LINEAR; NEAREST; } diff --git a/backends/native/openfl/display3D/SamplerState.hx b/backends/native/openfl/display3D/SamplerState.hx index da40969..27adbfe 100755 --- a/backends/native/openfl/display3D/SamplerState.hx +++ b/backends/native/openfl/display3D/SamplerState.hx @@ -5,7 +5,6 @@ class SamplerState public var wrap:Context3DWrapMode ; public var filter:Context3DTextureFilter ; public var mipfilter:Context3DMipFilter ; - public var maxAnisotropy:Float ; public function new():Void { diff --git a/openfl/display3D/SamplerState.hx b/openfl/display3D/SamplerState.hx index da40969..27adbfe 100755 --- a/openfl/display3D/SamplerState.hx +++ b/openfl/display3D/SamplerState.hx @@ -5,7 +5,6 @@ class SamplerState public var wrap:Context3DWrapMode ; public var filter:Context3DTextureFilter ; public var mipfilter:Context3DMipFilter ; - public var maxAnisotropy:Float ; public function new():Void {