Move sampler state setting to the Renderer

Trac #21727

git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1336 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/build_angle.gypi b/src/build_angle.gypi
index 1287d9d..5b94850 100644
--- a/src/build_angle.gypi
+++ b/src/build_angle.gypi
@@ -230,6 +230,7 @@
             'libGLESv2/Context.h',
             'libGLESv2/D3DConstantTable.cpp',
             'libGLESv2/D3DConstantTable.h',
+            'libGLESv2/EnumTypes.h',
             'libGLESv2/Fence.cpp',
             'libGLESv2/Fence.h',
             'libGLESv2/Float16ToFloat32.cpp',
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index d11ca66..cd3f7dd 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -277,7 +277,7 @@
         mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
         mMaxRenderbufferDimension = mMaxTextureDimension;
         mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
-        mMaxTextureAnisotropy = mRenderer->getTextureFilterAnisotropySupport();
+        mMaxTextureAnisotropy = mRenderer->getTextureMaxAnisotropy();
         TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d, MaxTextureAnisotropy=%f",
               mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel, mMaxTextureAnisotropy);
 
@@ -317,7 +317,7 @@
         mSupportsLuminanceTextures = mRenderer->getLuminanceTextureSupport();
         mSupportsLuminanceAlphaTextures = mRenderer->getLuminanceAlphaTextureSupport();
         mSupportsDepthTextures = mRenderer->getDepthTextureSupport();
-        mSupportsTextureFilterAnisotropy = mMaxTextureAnisotropy >= 2.0f;
+        mSupportsTextureFilterAnisotropy = mRenderer->getTextureFilterAnisotropySupport();
 
         mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
 
@@ -2441,20 +2441,7 @@
                         SamplerState samplerState;
                         texture->getSamplerState(&samplerState);
 
-                        mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(samplerState.wrapS));
-                        mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(samplerState.wrapT));
-
-                        mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
-                        D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
-                        es2dx::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
-                        mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
-                        mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
-                        mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
-
-                        if (supportsTextureFilterAnisotropy())
-                        {
-                            mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
-                        }
+                        mRenderer->setSamplerState(type, samplerIndex, samplerState);
                     }
 
                     if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
diff --git a/src/libGLESv2/EnumTypes.h b/src/libGLESv2/EnumTypes.h
new file mode 100644
index 0000000..c554523
--- /dev/null
+++ b/src/libGLESv2/EnumTypes.h
@@ -0,0 +1,32 @@
+//
+// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+
+// EnumTypes.h : Defines a variety of enum types that are used throughout libGLESv2
+
+#ifndef LIBGLESV2_ENUMTYPES_H_
+#define LIBGLESV2_ENUMTYPES_H_
+
+namespace gl
+{
+
+enum TextureType
+{
+    TEXTURE_2D,
+    TEXTURE_CUBE,
+
+    TEXTURE_TYPE_COUNT,
+    TEXTURE_UNKNOWN
+};
+
+enum SamplerType
+{
+    SAMPLER_PIXEL,
+    SAMPLER_VERTEX
+};
+
+}
+
+#endif // LIBGLESV2_ENUMTYPES_H_
diff --git a/src/libGLESv2/ResourceManager.h b/src/libGLESv2/ResourceManager.h
index ae4f1b0..3c9c568 100644
--- a/src/libGLESv2/ResourceManager.h
+++ b/src/libGLESv2/ResourceManager.h
@@ -20,6 +20,7 @@
 #endif
 
 #include "common/angleutils.h"
+#include "libGLESv2/EnumTypes.h"
 #include "libGLESv2/HandleAllocator.h"
 
 namespace gl
@@ -30,21 +31,6 @@
 class Texture;
 class Renderbuffer;
 
-enum TextureType
-{
-    TEXTURE_2D,
-    TEXTURE_CUBE,
-
-    TEXTURE_TYPE_COUNT,
-    TEXTURE_UNKNOWN
-};
-
-enum SamplerType
-{
-    SAMPLER_PIXEL,
-    SAMPLER_VERTEX
-};
-
 class ResourceManager
 {
   public:
diff --git a/src/libGLESv2/libGLESv2.vcxproj b/src/libGLESv2/libGLESv2.vcxproj
index 3e45606..4374bdb 100644
--- a/src/libGLESv2/libGLESv2.vcxproj
+++ b/src/libGLESv2/libGLESv2.vcxproj
@@ -259,6 +259,7 @@
     <ClInclude Include="Buffer.h" />

     <ClInclude Include="Context.h" />

     <ClInclude Include="D3DConstantTable.h" />

+    <ClInclude Include="EnumTypes.h" />

     <ClInclude Include="Fence.h" />

     <ClInclude Include="Framebuffer.h" />

     <ClInclude Include="..\..\include\GLES2\gl2.h" />

diff --git a/src/libGLESv2/renderer/Renderer.cpp b/src/libGLESv2/renderer/Renderer.cpp
index baacc80..a6b75d7 100644
--- a/src/libGLESv2/renderer/Renderer.cpp
+++ b/src/libGLESv2/renderer/Renderer.cpp
@@ -168,6 +168,9 @@
         !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
         !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
 
+    // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
+    mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
+
     static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
     static const TCHAR className[] = TEXT("STATIC");
 
@@ -357,6 +360,27 @@
     return mPixelShaderCache.create(function, length);
 }
 
+
+void Renderer::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
+{
+    int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
+    int d3dSampler = index + d3dSamplerOffset;
+
+    mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(samplerState.wrapS));
+    mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(samplerState.wrapT));
+
+    mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
+    D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
+    es2dx::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
+    mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
+    mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
+    mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
+    if (mSupportsTextureFilterAnisotropy)
+    {
+        mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
+    }
+}
+
 void Renderer::releaseDeviceResources()
 {
     while (!mEventQueryPool.empty())
@@ -610,10 +634,14 @@
     return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
 }
 
-float Renderer::getTextureFilterAnisotropySupport() const
+bool Renderer::getTextureFilterAnisotropySupport() const
 {
-    // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
-    if ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2))
+    return mSupportsTextureFilterAnisotropy;
+}
+
+float Renderer::getTextureMaxAnisotropy() const
+{
+    if (mSupportsTextureFilterAnisotropy)
     {
         return mDeviceCaps.MaxAnisotropy;
     }
diff --git a/src/libGLESv2/renderer/Renderer.h b/src/libGLESv2/renderer/Renderer.h
index 70cf76f..d0eefd7 100644
--- a/src/libGLESv2/renderer/Renderer.h
+++ b/src/libGLESv2/renderer/Renderer.h
@@ -23,6 +23,8 @@
 
 #include "common/angleutils.h"
 #include "libGLESv2/renderer/ShaderCache.h"
+#include "libGLESv2/EnumTypes.h"
+#include "libGLESv2/Texture.h"
 
 const int versionWindowsVista = MAKEWORD(0x00, 0x06);
 const int versionWindows7 = MAKEWORD(0x01, 0x06);
@@ -78,6 +80,7 @@
     virtual void applyRenderTargets();
     virtual void applyState();
 #endif
+    virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler);
 
     // lost device
     virtual void markDeviceLost();
@@ -108,7 +111,8 @@
     virtual bool getDepthTextureSupport() const;
     virtual bool getOcclusionQuerySupport() const;
     virtual bool getInstancingSupport() const;
-    virtual float getTextureFilterAnisotropySupport() const;
+    virtual bool getTextureFilterAnisotropySupport() const;
+    virtual float getTextureMaxAnisotropy() const;
     virtual bool getShareHandleSupport() const;
 
     virtual D3DPOOL getBufferPool(DWORD usage) const;
@@ -140,6 +144,7 @@
 
     bool mSceneStarted;
     bool mSupportsNonPower2Textures;
+    bool mSupportsTextureFilterAnisotropy;
 
     // A pool of event queries that are currently unused.
     std::vector<IDirect3DQuery9*> mEventQueryPool;