Browse Source

Remove API-specific Graphics class headers and code duplication. Closes #1422.

Lasse Öörni 9 years ago
parent
commit
eb104906aa

+ 48 - 261
Source/Urho3D/Graphics/Direct3D11/D3D11Graphics.cpp

@@ -25,39 +25,18 @@
 #include "../../Core/Context.h"
 #include "../../Core/Context.h"
 #include "../../Core/ProcessUtils.h"
 #include "../../Core/ProcessUtils.h"
 #include "../../Core/Profiler.h"
 #include "../../Core/Profiler.h"
-#include "../../Graphics/AnimatedModel.h"
-#include "../../Graphics/Animation.h"
-#include "../../Graphics/AnimationController.h"
-#include "../../Graphics/Camera.h"
 #include "../../Graphics/ConstantBuffer.h"
 #include "../../Graphics/ConstantBuffer.h"
-#include "../../Graphics/CustomGeometry.h"
-#include "../../Graphics/DebugRenderer.h"
-#include "../../Graphics/DecalSet.h"
 #include "../../Graphics/Geometry.h"
 #include "../../Graphics/Geometry.h"
 #include "../../Graphics/Graphics.h"
 #include "../../Graphics/Graphics.h"
 #include "../../Graphics/GraphicsEvents.h"
 #include "../../Graphics/GraphicsEvents.h"
 #include "../../Graphics/GraphicsImpl.h"
 #include "../../Graphics/GraphicsImpl.h"
 #include "../../Graphics/IndexBuffer.h"
 #include "../../Graphics/IndexBuffer.h"
-#include "../../Graphics/Material.h"
-#include "../../Graphics/Octree.h"
-#include "../../Graphics/ParticleEffect.h"
-#include "../../Graphics/ParticleEmitter.h"
-#include "../../Graphics/RibbonTrail.h"
 #include "../../Graphics/Renderer.h"
 #include "../../Graphics/Renderer.h"
 #include "../../Graphics/Shader.h"
 #include "../../Graphics/Shader.h"
 #include "../../Graphics/ShaderPrecache.h"
 #include "../../Graphics/ShaderPrecache.h"
 #include "../../Graphics/ShaderProgram.h"
 #include "../../Graphics/ShaderProgram.h"
-#include "../../Graphics/Skybox.h"
-#include "../../Graphics/StaticModelGroup.h"
-#include "../../Graphics/Technique.h"
-#include "../../Graphics/Terrain.h"
-#include "../../Graphics/TerrainPatch.h"
 #include "../../Graphics/Texture2D.h"
 #include "../../Graphics/Texture2D.h"
-#include "../../Graphics/Texture2DArray.h"
-#include "../../Graphics/Texture3D.h"
-#include "../../Graphics/TextureCube.h"
 #include "../../Graphics/VertexBuffer.h"
 #include "../../Graphics/VertexBuffer.h"
-#include "../../Graphics/Zone.h"
 #include "../../IO/File.h"
 #include "../../IO/File.h"
 #include "../../IO/Log.h"
 #include "../../IO/Log.h"
 #include "../../Resource/ResourceCache.h"
 #include "../../Resource/ResourceCache.h"
@@ -214,6 +193,7 @@ static HWND GetWindowHandle(SDL_Window* window)
 }
 }
 
 
 const Vector2 Graphics::pixelUVOffset(0.0f, 0.0f);
 const Vector2 Graphics::pixelUVOffset(0.0f, 0.0f);
+bool Graphics::gl3Support = false;
 
 
 Graphics::Graphics(Context* context) :
 Graphics::Graphics(Context* context) :
     Object(context),
     Object(context),
@@ -231,7 +211,12 @@ Graphics::Graphics(Context* context) :
     vsync_(false),
     vsync_(false),
     tripleBuffer_(false),
     tripleBuffer_(false),
     flushGPU_(false),
     flushGPU_(false),
+    forceGL2_(false),
     sRGB_(false),
     sRGB_(false),
+    anisotropySupport_(false),
+    dxtTextureSupport_(false),
+    etcTextureSupport_(false),
+    pvrtcTextureSupport_(false),
     lightPrepassSupport_(false),
     lightPrepassSupport_(false),
     deferredSupport_(false),
     deferredSupport_(false),
     instancingSupport_(false),
     instancingSupport_(false),
@@ -311,41 +296,6 @@ Graphics::~Graphics()
     SDL_Quit();
     SDL_Quit();
 }
 }
 
 
-void Graphics::SetExternalWindow(void* window)
-{
-    if (!impl_->window_)
-        externalWindow_ = window;
-    else
-        URHO3D_LOGERROR("Window already opened, can not set external window");
-}
-
-void Graphics::SetWindowTitle(const String& windowTitle)
-{
-    windowTitle_ = windowTitle;
-    if (impl_->window_)
-        SDL_SetWindowTitle(impl_->window_, windowTitle_.CString());
-}
-
-void Graphics::SetWindowIcon(Image* windowIcon)
-{
-    windowIcon_ = windowIcon;
-    if (impl_->window_)
-        CreateWindowIcon();
-}
-
-void Graphics::SetWindowPosition(const IntVector2& position)
-{
-    if (impl_->window_)
-        SDL_SetWindowPosition(impl_->window_, position.x_, position.y_);
-    else
-        position_ = position; // Sets as initial position for OpenWindow()
-}
-
-void Graphics::SetWindowPosition(int x, int y)
-{
-    SetWindowPosition(IntVector2(x, y));
-}
-
 bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync, bool tripleBuffer,
 bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync, bool tripleBuffer,
     int multiSample)
     int multiSample)
 {
 {
@@ -506,15 +456,9 @@ void Graphics::SetFlushGPU(bool enable)
     }
     }
 }
 }
 
 
-void Graphics::SetOrientations(const String& orientations)
+void Graphics::SetForceGL2(bool enable)
 {
 {
-    orientations_ = orientations.Trimmed();
-    SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
-}
-
-bool Graphics::ToggleFullscreen()
-{
-    return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, highDPI_, vsync_, tripleBuffer_, multiSample_);
+    // No effect on Direct3D11
 }
 }
 
 
 void Graphics::Close()
 void Graphics::Close()
@@ -1356,6 +1300,11 @@ void Graphics::SetTexture(unsigned index, Texture* texture)
     }
     }
 }
 }
 
 
+void SetTextureForUpdate(Texture* texture)
+{
+    // No-op on Direct3D11
+}
+
 void Graphics::SetDefaultTextureFilterMode(TextureFilterMode mode)
 void Graphics::SetDefaultTextureFilterMode(TextureFilterMode mode)
 {
 {
     if (mode != defaultTextureFilterMode_)
     if (mode != defaultTextureFilterMode_)
@@ -1374,6 +1323,11 @@ void Graphics::SetTextureAnisotropy(unsigned level)
     }
     }
 }
 }
 
 
+void Graphics::Restore()
+{
+    // No-op on Direct3D11
+}
+
 void Graphics::SetTextureParametersDirty()
 void Graphics::SetTextureParametersDirty()
 {
 {
     MutexLock lock(gpuObjectMutex_);
     MutexLock lock(gpuObjectMutex_);
@@ -1715,43 +1669,6 @@ bool Graphics::IsInitialized() const
     return impl_->window_ != 0 && impl_->GetDevice() != 0;
     return impl_->window_ != 0 && impl_->GetDevice() != 0;
 }
 }
 
 
-IntVector2 Graphics::GetWindowPosition() const
-{
-    if (impl_->window_)
-        return position_;
-    return IntVector2::ZERO;
-}
-
-PODVector<IntVector2> Graphics::GetResolutions() const
-{
-    PODVector<IntVector2> ret;
-    unsigned numModes = (unsigned)SDL_GetNumDisplayModes(0);
-
-    for (unsigned i = 0; i < numModes; ++i)
-    {
-        SDL_DisplayMode mode;
-        SDL_GetDisplayMode(0, i, &mode);
-        int width = mode.w;
-        int height = mode.h;
-
-        // Store mode if unique
-        bool unique = true;
-        for (unsigned j = 0; j < ret.Size(); ++j)
-        {
-            if (ret[j].x_ == width && ret[j].y_ == height)
-            {
-                unique = false;
-                break;
-            }
-        }
-
-        if (unique)
-            ret.Push(IntVector2(width, height));
-    }
-
-    return ret;
-}
-
 PODVector<int> Graphics::GetMultiSampleLevels() const
 PODVector<int> Graphics::GetMultiSampleLevels() const
 {
 {
     PODVector<int> ret;
     PODVector<int> ret;
@@ -1772,13 +1689,6 @@ PODVector<int> Graphics::GetMultiSampleLevels() const
     return ret;
     return ret;
 }
 }
 
 
-IntVector2 Graphics::GetDesktopResolution() const
-{
-    SDL_DisplayMode mode;
-    SDL_GetDesktopDisplayMode(0, &mode);
-    return IntVector2(mode.w, mode.h);
-}
-
 unsigned Graphics::GetFormat(CompressedFormat format) const
 unsigned Graphics::GetFormat(CompressedFormat format) const
 {
 {
     switch (format)
     switch (format)
@@ -1828,6 +1738,11 @@ VertexBuffer* Graphics::GetVertexBuffer(unsigned index) const
     return index < MAX_VERTEX_STREAMS ? vertexBuffers_[index] : 0;
     return index < MAX_VERTEX_STREAMS ? vertexBuffers_[index] : 0;
 }
 }
 
 
+ShaderProgram* Graphics::GetShaderProgram() const
+{
+    return impl_->shaderProgram_;
+}
+
 TextureUnit Graphics::GetTextureUnit(const String& name)
 TextureUnit Graphics::GetTextureUnit(const String& name)
 {
 {
     HashMap<String, TextureUnit>::Iterator i = textureUnits_.Find(name);
     HashMap<String, TextureUnit>::Iterator i = textureUnits_.Find(name);
@@ -1880,6 +1795,13 @@ IntVector2 Graphics::GetRenderTargetDimensions() const
     return IntVector2(width, height);
     return IntVector2(width, height);
 }
 }
 
 
+bool Graphics::IsDeviceLost() const
+{
+    // Direct3D11 graphics context is never considered lost
+    /// \todo The device could be lost in case of graphics adapters getting disabled during runtime. This is not handled
+    return false;
+}
+
 void Graphics::OnWindowResized()
 void Graphics::OnWindowResized()
 {
 {
     if (!impl_->device_ || !impl_->window_)
     if (!impl_->device_ || !impl_->window_)
@@ -1933,114 +1855,7 @@ void Graphics::OnWindowMoved()
     SendEvent(E_WINDOWPOS, eventData);
     SendEvent(E_WINDOWPOS, eventData);
 }
 }
 
 
-void Graphics::Maximize()
-{
-    if (!impl_->window_)
-        return;
-
-    SDL_MaximizeWindow(impl_->window_);
-}
-
-void Graphics::Minimize()
-{
-    if (!impl_->window_)
-        return;
-
-    SDL_MinimizeWindow(impl_->window_);
-}
-
-void Graphics::AddGPUObject(GPUObject* object)
-{
-    MutexLock lock(gpuObjectMutex_);
-
-    gpuObjects_.Push(object);
-}
-
-void Graphics::RemoveGPUObject(GPUObject* object)
-{
-    MutexLock lock(gpuObjectMutex_);
-
-    gpuObjects_.Remove(object);
-}
-
-void* Graphics::ReserveScratchBuffer(unsigned size)
-{
-    if (!size)
-        return 0;
-
-    if (size > maxScratchBufferRequest_)
-        maxScratchBufferRequest_ = size;
-
-    // First check for a free buffer that is large enough
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_ && i->size_ >= size)
-        {
-            i->reserved_ = true;
-            return i->data_.Get();
-        }
-    }
-
-    // Then check if a free buffer can be resized
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_)
-        {
-            i->data_ = new unsigned char[size];
-            i->size_ = size;
-            i->reserved_ = true;
-
-            URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(size));
-
-            return i->data_.Get();
-        }
-    }
-
-    // Finally allocate a new buffer
-    ScratchBuffer newBuffer;
-    newBuffer.data_ = new unsigned char[size];
-    newBuffer.size_ = size;
-    newBuffer.reserved_ = true;
-    scratchBuffers_.Push(newBuffer);
-    return newBuffer.data_.Get();
-
-    URHO3D_LOGDEBUG("Allocated scratch buffer with size " + String(size));
-}
-
-void Graphics::FreeScratchBuffer(void* buffer)
-{
-    if (!buffer)
-        return;
-
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (i->reserved_ && i->data_.Get() == buffer)
-        {
-            i->reserved_ = false;
-            return;
-        }
-    }
-
-    URHO3D_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
-}
-
-void Graphics::CleanupScratchBuffers()
-{
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
-        {
-            i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
-            i->size_ = maxScratchBufferRequest_;
-
-            URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
-        }
-    }
-
-    maxScratchBufferRequest_ = 0;
-}
-
-void Graphics::CleanUpShaderPrograms(ShaderVariation* variation)
+void Graphics::CleanupShaderPrograms(ShaderVariation* variation)
 {
 {
     for (ShaderProgramMap::Iterator i = impl_->shaderPrograms_.Begin(); i != impl_->shaderPrograms_.End();)
     for (ShaderProgramMap::Iterator i = impl_->shaderPrograms_.Begin(); i != impl_->shaderPrograms_.End();)
     {
     {
@@ -2054,6 +1869,11 @@ void Graphics::CleanUpShaderPrograms(ShaderVariation* variation)
         impl_->shaderProgram_ = 0;
         impl_->shaderProgram_ = 0;
 }
 }
 
 
+void Graphics::CleanupRenderSurface(RenderSurface* surface)
+{
+    // No-op on Direct3D11
+}
+
 ConstantBuffer* Graphics::GetOrCreateConstantBuffer(ShaderType type, unsigned index, unsigned size)
 ConstantBuffer* Graphics::GetOrCreateConstantBuffer(ShaderType type, unsigned index, unsigned size)
 {
 {
     // Ensure that different shader types and index slots get unique buffers, even if the size is same
     // Ensure that different shader types and index slots get unique buffers, even if the size is same
@@ -2192,6 +2012,16 @@ unsigned Graphics::GetFormat(const String& formatName)
     return GetRGBFormat();
     return GetRGBFormat();
 }
 }
 
 
+unsigned Graphics::GetMaxBones()
+{
+    return 128;
+}
+
+bool Graphics::GetGL3Support()
+{
+    return gl3Support;
+}
+
 bool Graphics::OpenWindow(int width, int height, bool resizable, bool borderless)
 bool Graphics::OpenWindow(int width, int height, bool resizable, bool borderless)
 {
 {
     if (!externalWindow_)
     if (!externalWindow_)
@@ -2220,19 +2050,6 @@ bool Graphics::OpenWindow(int width, int height, bool resizable, bool borderless
     return true;
     return true;
 }
 }
 
 
-void Graphics::CreateWindowIcon()
-{
-    if (windowIcon_)
-    {
-        SDL_Surface* surface = windowIcon_->GetSDLSurface();
-        if (surface)
-        {
-            SDL_SetWindowIcon(impl_->window_, surface);
-            SDL_FreeSurface(surface);
-        }
-    }
-}
-
 void Graphics::AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless)
 void Graphics::AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless)
 {
 {
     if (!externalWindow_)
     if (!externalWindow_)
@@ -2438,6 +2255,8 @@ bool Graphics::UpdateSwapChain(int width, int height)
 
 
 void Graphics::CheckFeatureSupport()
 void Graphics::CheckFeatureSupport()
 {
 {
+    anisotropySupport_ = true;
+    dxtTextureSupport_ = true;
     lightPrepassSupport_ = true;
     lightPrepassSupport_ = true;
     deferredSupport_ = true;
     deferredSupport_ = true;
     hardwareShadowSupport_ = true;
     hardwareShadowSupport_ = true;
@@ -2804,36 +2623,4 @@ void Graphics::SetTextureUnitMappings()
     textureUnits_["ZoneVolumeMap"] = TU_ZONE;
     textureUnits_["ZoneVolumeMap"] = TU_ZONE;
 }
 }
 
 
-void RegisterGraphicsLibrary(Context* context)
-{
-    Animation::RegisterObject(context);
-    Material::RegisterObject(context);
-    Model::RegisterObject(context);
-    Shader::RegisterObject(context);
-    Technique::RegisterObject(context);
-    Texture2D::RegisterObject(context);
-    Texture2DArray::RegisterObject(context);
-    Texture3D::RegisterObject(context);
-    TextureCube::RegisterObject(context);
-    Camera::RegisterObject(context);
-    Drawable::RegisterObject(context);
-    Light::RegisterObject(context);
-    StaticModel::RegisterObject(context);
-    StaticModelGroup::RegisterObject(context);
-    Skybox::RegisterObject(context);
-    AnimatedModel::RegisterObject(context);
-    AnimationController::RegisterObject(context);
-    BillboardSet::RegisterObject(context);
-    ParticleEffect::RegisterObject(context);
-    ParticleEmitter::RegisterObject(context);
-    RibbonTrail::RegisterObject(context);
-    CustomGeometry::RegisterObject(context);
-    DecalSet::RegisterObject(context);
-    Terrain::RegisterObject(context);
-    TerrainPatch::RegisterObject(context);
-    DebugRenderer::RegisterObject(context);
-    Octree::RegisterObject(context);
-    Zone::RegisterObject(context);
-}
-
 }
 }

+ 0 - 674
Source/Urho3D/Graphics/Direct3D11/D3D11Graphics.h

@@ -1,674 +0,0 @@
-//
-// Copyright (c) 2008-2016 the Urho3D project.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-//
-
-#pragma once
-
-#include "../../Container/ArrayPtr.h"
-#include "../../Container/HashSet.h"
-#include "../../Core/Mutex.h"
-#include "../../Core/Object.h"
-#include "../../Graphics/GraphicsDefs.h"
-#include "../../Graphics/ShaderVariation.h"
-#include "../../Math/Color.h"
-#include "../../Math/Plane.h"
-#include "../../Math/Rect.h"
-#include "../../Resource/Image.h"
-
-namespace Urho3D
-{
-
-class ConstantBuffer;
-class File;
-class Image;
-class IndexBuffer;
-class GPUObject;
-class GraphicsImpl;
-class RenderSurface;
-class Shader;
-class ShaderPrecache;
-class ShaderProgram;
-class ShaderVariation;
-class Texture;
-class Texture2D;
-class Texture2DArray;
-class TextureCube;
-class Vector3;
-class Vector4;
-class VertexBuffer;
-class VertexDeclaration;
-
-struct ShaderParameter;
-
-/// CPU-side scratch buffer for vertex data updates.
-struct ScratchBuffer
-{
-    ScratchBuffer() :
-        size_(0),
-        reserved_(false)
-    {
-    }
-
-    /// Buffer data.
-    SharedArrayPtr<unsigned char> data_;
-    /// Data size.
-    unsigned size_;
-    /// Reserved flag.
-    bool reserved_;
-};
-
-/// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
-class URHO3D_API Graphics : public Object
-{
-    URHO3D_OBJECT(Graphics, Object);
-
-public:
-    /// Construct.
-    Graphics(Context* context);
-    /// Destruct. Release the Direct3D11 device and close the window.
-    virtual ~Graphics();
-
-    /// Set external window handle. Only effective before setting the initial screen mode.
-    void SetExternalWindow(void* window);
-    /// Set window title.
-    void SetWindowTitle(const String& windowTitle);
-    /// Set window icon.
-    void SetWindowIcon(Image* windowIcon);
-    /// Set window position. Sets initial position if window is not created yet.
-    void SetWindowPosition(const IntVector2& position);
-    /// Set window position. Sets initial position if window is not created yet.
-    void SetWindowPosition(int x, int y);
-    /// Set screen mode. Return true if successful.
-    bool SetMode
-        (int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync, bool tripleBuffer,
-            int multiSample);
-    /// Set screen resolution only. Return true if successful.
-    bool SetMode(int width, int height);
-    /// Set whether the main window uses sRGB conversion on write.
-    void SetSRGB(bool enable);
-    /// Set whether to flush the GPU command buffer to prevent multiple frames being queued and uneven frame timesteps. Default off, may decrease performance if enabled.
-    void SetFlushGPU(bool enable);
-    /// Set allowed screen orientations as a space-separated list of "LandscapeLeft", "LandscapeRight", "Portrait" and "PortraitUpsideDown". Affects currently only iOS platform.
-    void SetOrientations(const String& orientations);
-    /// Toggle between full screen and windowed mode. Return true if successful.
-    bool ToggleFullscreen();
-    /// Close the window.
-    void Close();
-    /// Take a screenshot. Return true if successful.
-    bool TakeScreenShot(Image& destImage);
-    /// Begin frame rendering. Return true if device available and can render.
-    bool BeginFrame();
-    /// End frame rendering and swap buffers.
-    void EndFrame();
-    /// Clear any or all of rendertarget, depth buffer and stencil buffer.
-    void Clear(unsigned flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, unsigned stencil = 0);
-    /// Resolve multisampled backbuffer to a texture rendertarget. The texture's size should match the viewport size.
-    bool ResolveToTexture(Texture2D* destination, const IntRect& viewport);
-    /// Draw non-indexed geometry.
-    void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
-    /// Draw indexed geometry.
-    void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
-    /// Draw indexed geometry with vertex index offset.
-    void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount);
-    /// Draw indexed, instanced geometry. An instancing vertex buffer must be set.
-    void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
-        unsigned instanceCount);
-    /// Draw indexed, instanced geometry with vertex index offset.
-    void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex,
-        unsigned vertexCount, unsigned instanceCount);
-    /// Set vertex buffer.
-    void SetVertexBuffer(VertexBuffer* buffer);
-    /// Set multiple vertex buffers.
-    bool SetVertexBuffers(const PODVector<VertexBuffer*>& buffers, unsigned instanceOffset = 0);
-    /// Set multiple vertex buffers.
-    bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, unsigned instanceOffset = 0);
-    /// Set index buffer.
-    void SetIndexBuffer(IndexBuffer* buffer);
-    /// Set shaders.
-    void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
-    /// Set shader float constants.
-    void SetShaderParameter(StringHash param, const float* data, unsigned count);
-    /// Set shader float constant.
-    void SetShaderParameter(StringHash param, float value);
-    /// Set shader boolean constant.
-    void SetShaderParameter(StringHash param, bool value);
-    /// Set shader color constant.
-    void SetShaderParameter(StringHash param, const Color& color);
-    /// Set shader 2D vector constant.
-    void SetShaderParameter(StringHash param, const Vector2& vector);
-    /// Set shader 3x3 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix3& matrix);
-    /// Set shader 3D vector constant.
-    void SetShaderParameter(StringHash param, const Vector3& vector);
-    /// Set shader 4x4 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix4& matrix);
-    /// Set shader 4D vector constant.
-    void SetShaderParameter(StringHash param, const Vector4& vector);
-    /// Set shader 3x4 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
-    /// Set shader constant from a variant. Supported variant types: bool, float, vector2, vector3, vector4, color.
-    void SetShaderParameter(StringHash param, const Variant& value);
-    /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
-    bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
-    /// Check whether a shader parameter exists on the currently set shaders.
-    bool HasShaderParameter(StringHash param);
-    /// Check whether the current vertex or pixel shader uses a texture unit.
-    bool HasTextureUnit(TextureUnit unit);
-    /// Clear remembered shader parameter source group.
-    void ClearParameterSource(ShaderParameterGroup group);
-    /// Clear remembered shader parameter sources.
-    void ClearParameterSources();
-    /// Clear remembered transform shader parameter sources.
-    void ClearTransformSources();
-    /// Set texture.
-    void SetTexture(unsigned index, Texture* texture);
-    /// Set default texture filtering mode.
-    void SetDefaultTextureFilterMode(TextureFilterMode mode);
-    /// Set texture anisotropy.
-    void SetTextureAnisotropy(unsigned level);
-    /// Dirty texture parameters of all textures (when global settings change.)
-    void SetTextureParametersDirty();
-    /// Reset all rendertargets, depth-stencil surface and viewport.
-    void ResetRenderTargets();
-    /// Reset specific rendertarget.
-    void ResetRenderTarget(unsigned index);
-    /// Reset depth-stencil surface.
-    void ResetDepthStencil();
-    /// Set rendertarget.
-    void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
-    /// Set rendertarget.
-    void SetRenderTarget(unsigned index, Texture2D* texture);
-    /// Set depth-stencil surface.
-    void SetDepthStencil(RenderSurface* depthStencil);
-    /// Set depth-stencil surface.
-    void SetDepthStencil(Texture2D* texture);
-    /// Set viewport.
-    void SetViewport(const IntRect& rect);
-    /// Set blending mode.
-    void SetBlendMode(BlendMode mode);
-    /// Set color write on/off.
-    void SetColorWrite(bool enable);
-    /// Set hardware culling mode.
-    void SetCullMode(CullMode mode);
-    /// Set depth bias.
-    void SetDepthBias(float constantBias, float slopeScaledBias);
-    /// Set depth compare.
-    void SetDepthTest(CompareMode mode);
-    /// Set depth write on/off.
-    void SetDepthWrite(bool enable);
-    /// Set polygon fill mode.
-    void SetFillMode(FillMode mode);
-    /// Set scissor test.
-    void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
-    /// Set scissor test.
-    void SetScissorTest(bool enable, const IntRect& rect);
-    /// Set stencil test.
-    void SetStencilTest
-        (bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP,
-            unsigned stencilRef = 0, unsigned compareMask = M_MAX_UNSIGNED, unsigned writeMask = M_MAX_UNSIGNED);
-    /// Set a custom clipping plane. The plane is specified in world space, but is dependent on the view and projection matrices.
-    void SetClipPlane(bool enable, const Plane& clipPlane = Plane::UP, const Matrix3x4& view = Matrix3x4::IDENTITY,
-        const Matrix4& projection = Matrix4::IDENTITY);
-    /// Begin dumping shader variation names to an XML file for precaching.
-    void BeginDumpShaders(const String& fileName);
-    /// End dumping shader variations names.
-    void EndDumpShaders();
-    /// Precache shader variations from an XML file generated with BeginDumpShaders().
-    void PrecacheShaders(Deserializer& source);
-
-    /// Return whether rendering initialized.
-    bool IsInitialized() const;
-
-    /// Return graphics implementation, which holds the actual API-specific resources.
-    GraphicsImpl* GetImpl() const { return impl_; }
-
-    /// Return OS-specific external window handle. Null if not in use.
-    void* GetExternalWindow() const { return externalWindow_; }
-
-    /// Return window title.
-    const String& GetWindowTitle() const { return windowTitle_; }
-
-    /// Return graphics API name.
-    const String& GetApiName() const { return apiName_; }
-
-    /// Return window position.
-    IntVector2 GetWindowPosition() const;
-
-    /// Return window width in pixels.
-    int GetWidth() const { return width_; }
-
-    /// Return window height in pixels.
-    int GetHeight() const { return height_; }
-
-    /// Return multisample mode (1 = no multisampling.)
-    int GetMultiSample() const { return multiSample_; }
-
-    /// Return whether window is fullscreen.
-    bool GetFullscreen() const { return fullscreen_; }
-
-    /// Return whether window is borderless.
-    bool GetBorderless() const { return borderless_; }
-
-    /// Return whether window is resizable.
-    bool GetResizable() const { return resizable_; }
-
-    /// Return whether window is high DPI.
-    bool GetHighDPI() const { return highDPI_; }
-
-    /// Return whether vertical sync is on.
-    bool GetVSync() const { return vsync_; }
-
-    /// Return whether triple buffering is enabled.
-    bool GetTripleBuffer() const { return tripleBuffer_; }
-
-    /// Return whether the main window is using sRGB conversion on write.
-    bool GetSRGB() const { return sRGB_; }
-
-    /// Return whether the GPU command buffer is flushed each frame.
-    bool GetFlushGPU() const { return flushGPU_; }
-
-    /// Return allowed screen orientations.
-    const String& GetOrientations() const { return orientations_; }
-
-    /// Return whether Direct3D device is lost, and can not yet render. Always false on D3D11.
-    bool IsDeviceLost() const { return false; }
-
-    /// Return number of primitives drawn this frame.
-    unsigned GetNumPrimitives() const { return numPrimitives_; }
-
-    /// Return number of batches drawn this frame.
-    unsigned GetNumBatches() const { return numBatches_; }
-
-    /// Return dummy color texture format for shadow maps. Is "NULL" (consume no video memory) if supported.
-    unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
-
-    /// Return shadow map depth texture format, or 0 if not supported.
-    unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
-
-    /// Return 24-bit shadow map depth texture format, or 0 if not supported.
-    unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
-
-    /// Return whether hardware instancing is supported.
-    bool GetInstancingSupport() const { return instancingSupport_; }
-
-    /// Return whether light pre-pass rendering is supported.
-    bool GetLightPrepassSupport() const { return lightPrepassSupport_; }
-
-    /// Return whether deferred rendering is supported.
-    bool GetDeferredSupport() const { return deferredSupport_; }
-
-    /// Return whether shadow map depth compare is done in hardware.
-    bool GetHardwareShadowSupport() const { return hardwareShadowSupport_; }
-
-    /// Return whether a readable hardware depth format is available.
-    bool GetReadableDepthSupport() const { return GetReadableDepthFormat() != 0; }
-
-    /// Return whether sRGB conversion on texture sampling is supported.
-    bool GetSRGBSupport() const { return sRGBSupport_; }
-
-    /// Return whether sRGB conversion on rendertarget writing is supported.
-    bool GetSRGBWriteSupport() const { return sRGBWriteSupport_; }
-
-    /// Return supported fullscreen resolutions. Will be empty if listing the resolutions is not supported on the platform (e.g. Web).
-    PODVector<IntVector2> GetResolutions() const;
-    /// Return supported multisampling levels.
-    PODVector<int> GetMultiSampleLevels() const;
-    /// Return the desktop resolution.
-    IntVector2 GetDesktopResolution() const;
-    /// Return hardware format for a compressed image format, or 0 if unsupported.
-    unsigned GetFormat(CompressedFormat format) const;
-    /// Return a shader variation by name and defines.
-    ShaderVariation* GetShader(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
-    /// Return a shader variation by name and defines.
-    ShaderVariation* GetShader(ShaderType type, const char* name, const char* defines) const;
-    /// Return current vertex buffer by index.
-    VertexBuffer* GetVertexBuffer(unsigned index) const;
-
-    /// Return current index buffer.
-    IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
-
-    /// Return current vertex shader.
-    ShaderVariation* GetVertexShader() const { return vertexShader_; }
-
-    /// Return current pixel shader.
-    ShaderVariation* GetPixelShader() const { return pixelShader_; }
-
-    /// Return texture unit index by name.
-    TextureUnit GetTextureUnit(const String& name);
-    /// Return texture unit name by index.
-    const String& GetTextureUnitName(TextureUnit unit);
-    /// Return current texture by texture unit index.
-    Texture* GetTexture(unsigned index) const;
-
-    /// Return default texture filtering mode.
-    TextureFilterMode GetDefaultTextureFilterMode() const { return defaultTextureFilterMode_; }
-
-    /// Return current rendertarget by index.
-    RenderSurface* GetRenderTarget(unsigned index) const;
-
-    /// Return current depth-stencil surface.
-    RenderSurface* GetDepthStencil() const { return depthStencil_; }
-
-    /// Return the viewport coordinates.
-    IntRect GetViewport() const { return viewport_; }
-
-    /// Return texture anisotropy.
-    unsigned GetTextureAnisotropy() const { return textureAnisotropy_; }
-
-    /// Return blending mode.
-    BlendMode GetBlendMode() const { return blendMode_; }
-
-    /// Return whether color write is enabled.
-    bool GetColorWrite() const { return colorWrite_; }
-
-    /// Return hardware culling mode.
-    CullMode GetCullMode() const { return cullMode_; }
-
-    /// Return depth constant bias.
-    float GetDepthConstantBias() const { return constantDepthBias_; }
-
-    /// Return depth slope scaled bias.
-    float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
-
-    /// Return depth compare mode.
-    CompareMode GetDepthTest() const { return depthTestMode_; }
-
-    /// Return whether depth write is enabled.
-    bool GetDepthWrite() const { return depthWrite_; }
-
-    /// Return polygon fill mode.
-    FillMode GetFillMode() const { return fillMode_; }
-
-    /// Return whether stencil test is enabled.
-    bool GetStencilTest() const { return stencilTest_; }
-
-    /// Return whether scissor test is enabled.
-    bool GetScissorTest() const { return scissorTest_; }
-
-    /// Return scissor rectangle coordinates.
-    const IntRect& GetScissorRect() const { return scissorRect_; }
-
-    /// Return stencil compare mode.
-    CompareMode GetStencilTestMode() const { return stencilTestMode_; }
-
-    /// Return stencil operation to do if stencil test passes.
-    StencilOp GetStencilPass() const { return stencilPass_; }
-
-    /// Return stencil operation to do if stencil test fails.
-    StencilOp GetStencilFail() const { return stencilFail_; }
-
-    /// Return stencil operation to do if depth compare fails.
-    StencilOp GetStencilZFail() const { return stencilZFail_; }
-
-    /// Return stencil reference value.
-    unsigned GetStencilRef() const { return stencilRef_; }
-
-    /// Return stencil compare bitmask.
-    unsigned GetStencilCompareMask() const { return stencilCompareMask_; }
-
-    /// Return stencil write bitmask.
-    unsigned GetStencilWriteMask() const { return stencilWriteMask_; }
-
-    /// Return whether a custom clipping plane is in use.
-    bool GetUseClipPlane() const { return useClipPlane_; }
-
-    /// Return rendertarget width and height.
-    IntVector2 GetRenderTargetDimensions() const;
-
-    /// Window was resized through user interaction. Called by Input subsystem.
-    void OnWindowResized();
-    /// Window was moved through user interaction. Called by Input subsystem.
-    void OnWindowMoved();
-    /// Maximize the Window.
-    void Maximize();
-    /// Minimize the Window.
-    void Minimize();
-    /// Add a GPU object to keep track of. Called by GPUObject.
-    void AddGPUObject(GPUObject* object);
-    /// Remove a GPU object. Called by GPUObject.
-    void RemoveGPUObject(GPUObject* object);
-    /// Reserve a CPU-side scratch buffer.
-    void* ReserveScratchBuffer(unsigned size);
-    /// Free a CPU-side scratch buffer.
-    void FreeScratchBuffer(void* buffer);
-    /// Clean up too large scratch buffers.
-    void CleanupScratchBuffers();
-    /// Clean up shader parameters when a shader variation is released or destroyed.
-    void CleanUpShaderPrograms(ShaderVariation* variation);
-    /// Get or create a constant buffer. Will be shared between shaders if possible.
-    ConstantBuffer* GetOrCreateConstantBuffer(ShaderType type, unsigned index, unsigned size);
-
-    /// Return the API-specific alpha texture format.
-    static unsigned GetAlphaFormat();
-    /// Return the API-specific luminance texture format.
-    static unsigned GetLuminanceFormat();
-    /// Return the API-specific luminance alpha texture format.
-    static unsigned GetLuminanceAlphaFormat();
-    /// Return the API-specific RGB texture format.
-    static unsigned GetRGBFormat();
-    /// Return the API-specific RGBA texture format.
-    static unsigned GetRGBAFormat();
-    /// Return the API-specific RGBA 16-bit texture format.
-    static unsigned GetRGBA16Format();
-    /// Return the API-specific RGBA 16-bit float texture format.
-    static unsigned GetRGBAFloat16Format();
-    /// Return the API-specific RGBA 32-bit float texture format.
-    static unsigned GetRGBAFloat32Format();
-    /// Return the API-specific RG 16-bit texture format.
-    static unsigned GetRG16Format();
-    /// Return the API-specific RG 16-bit float texture format.
-    static unsigned GetRGFloat16Format();
-    /// Return the API-specific RG 32-bit float texture format.
-    static unsigned GetRGFloat32Format();
-    /// Return the API-specific single channel 16-bit float texture format.
-    static unsigned GetFloat16Format();
-    /// Return the API-specific single channel 32-bit float texture format.
-    static unsigned GetFloat32Format();
-    /// Return the API-specific linear depth texture format.
-    static unsigned GetLinearDepthFormat();
-    /// Return the API-specific hardware depth-stencil texture format.
-    static unsigned GetDepthStencilFormat();
-    /// Return the API-specific readable hardware depth format, or 0 if not supported.
-    static unsigned GetReadableDepthFormat();
-    /// Return the API-specific texture format from a textual description, for example "rgb".
-    static unsigned GetFormat(const String& formatName);
-
-    /// Return UV offset required for pixel perfect rendering.
-    static const Vector2& GetPixelUVOffset() { return pixelUVOffset; }
-
-    /// Return maximum number of supported bones for skinning.
-    static unsigned GetMaxBones() { return 128; }
-
-private:
-    /// Create the application window.
-    bool OpenWindow(int width, int height, bool resizable, bool borderless);
-    /// Create the application window icon.
-    void CreateWindowIcon();
-    /// Adjust the window for new resolution and fullscreen mode.
-    void AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless);
-    /// Create the D3D11 device and swap chain. Requires an open window. Can also be called again to recreate swap chain. Return true on success.
-    bool CreateDevice(int width, int height, int multiSample);
-    /// Update swap chain state for a new mode and create views for the backbuffer & default depth buffer. Return true on success.
-    bool UpdateSwapChain(int width, int height);
-    /// Check supported rendering features.
-    void CheckFeatureSupport();
-    /// Reset cached rendering state.
-    void ResetCachedState();
-    /// Initialize texture unit mappings.
-    void SetTextureUnitMappings();
-    /// Process dirtied state before draw.
-    void PrepareDraw();
-    /// Create intermediate texture for multisampled backbuffer resolve. No-op if already exists.
-    void CreateResolveTexture();
-
-    /// Mutex for accessing the GPU objects vector from several threads.
-    Mutex gpuObjectMutex_;
-    /// Implementation.
-    GraphicsImpl* impl_;
-    /// Window title.
-    String windowTitle_;
-    /// Window icon image.
-    WeakPtr<Image> windowIcon_;
-    /// External window, null if not in use (default.)
-    void* externalWindow_;
-    /// Window width in pixels.
-    int width_;
-    /// Window height in pixels.
-    int height_;
-    /// Window position.
-    IntVector2 position_;
-    /// Multisampling mode.
-    int multiSample_;
-    /// Fullscreen flag.
-    bool fullscreen_;
-    /// Borderless flag.
-    bool borderless_;
-    /// Resizable flag.
-    bool resizable_;
-    /// High DPI flag.
-    bool highDPI_;
-    /// Vertical sync flag.
-    bool vsync_;
-    /// Triple buffering flag.
-    bool tripleBuffer_;
-    /// Flush GPU command buffer flag.
-    bool flushGPU_;
-    /// sRGB conversion on write flag for the main window.
-    bool sRGB_;
-    /// Light pre-pass rendering support flag.
-    bool lightPrepassSupport_;
-    /// Deferred rendering support flag.
-    bool deferredSupport_;
-    /// Hardware shadow map depth compare support flag.
-    bool hardwareShadowSupport_;
-    /// Instancing support flag.
-    bool instancingSupport_;
-    /// sRGB conversion on read support flag.
-    bool sRGBSupport_;
-    /// sRGB conversion on write support flag.
-    bool sRGBWriteSupport_;
-    /// Number of primitives this frame.
-    unsigned numPrimitives_;
-    /// Number of batches this frame.
-    unsigned numBatches_;
-    /// Largest scratch buffer request this frame.
-    unsigned maxScratchBufferRequest_;
-    /// GPU objects.
-    PODVector<GPUObject*> gpuObjects_;
-    /// Scratch buffers.
-    Vector<ScratchBuffer> scratchBuffers_;
-    /// Shadow map dummy color texture format.
-    unsigned dummyColorFormat_;
-    /// Shadow map depth texture format.
-    unsigned shadowMapFormat_;
-    /// Shadow map 24-bit depth texture format.
-    unsigned hiresShadowMapFormat_;
-    /// Vertex buffers in use.
-    VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS];
-    /// Index buffer in use.
-    IndexBuffer* indexBuffer_;
-    /// Current vertex declaration hash.
-    unsigned long long vertexDeclarationHash_;
-    /// Current primitive type.
-    unsigned primitiveType_;
-    /// Vertex shader in use.
-    ShaderVariation* vertexShader_;
-    /// Pixel shader in use.
-    ShaderVariation* pixelShader_;
-    /// Textures in use.
-    Texture* textures_[MAX_TEXTURE_UNITS];
-    /// Texture unit mappings.
-    HashMap<String, TextureUnit> textureUnits_;
-    /// Rendertargets in use.
-    RenderSurface* renderTargets_[MAX_RENDERTARGETS];
-    /// Depth-stencil surface in use.
-    RenderSurface* depthStencil_;
-    /// Viewport coordinates.
-    IntRect viewport_;
-    /// Default texture filtering mode.
-    TextureFilterMode defaultTextureFilterMode_;
-    /// Texture anisotropy level.
-    unsigned textureAnisotropy_;
-    /// Blending mode.
-    BlendMode blendMode_;
-    /// Color write enable.
-    bool colorWrite_;
-    /// Hardware culling mode.
-    CullMode cullMode_;
-    /// Depth constant bias.
-    float constantDepthBias_;
-    /// Depth slope scaled bias.
-    float slopeScaledDepthBias_;
-    /// Depth compare mode.
-    CompareMode depthTestMode_;
-    /// Depth write enable flag.
-    bool depthWrite_;
-    /// Polygon fill mode.
-    FillMode fillMode_;
-    /// Scissor test rectangle.
-    IntRect scissorRect_;
-    /// Scissor test enable flag.
-    bool scissorTest_;
-    /// Stencil test compare mode.
-    CompareMode stencilTestMode_;
-    /// Stencil operation on pass.
-    StencilOp stencilPass_;
-    /// Stencil operation on fail.
-    StencilOp stencilFail_;
-    /// Stencil operation on depth fail.
-    StencilOp stencilZFail_;
-    /// Stencil test reference value.
-    unsigned stencilRef_;
-    /// Stencil compare bitmask.
-    unsigned stencilCompareMask_;
-    /// Stencil write bitmask.
-    unsigned stencilWriteMask_;
-    /// Current custom clip plane in post-projection space.
-    Vector4 clipPlane_;
-    /// Stencil test enable flag.
-    bool stencilTest_;
-    /// Custom clip plane enable flag.
-    bool useClipPlane_;
-    /// Remembered shader parameter sources.
-    const void* shaderParameterSources_[MAX_SHADER_PARAMETER_GROUPS];
-    /// Base directory for shaders.
-    String shaderPath_;
-    /// File extension for shaders.
-    String shaderExtension_;
-    /// Last used shader in shader variation query.
-    mutable WeakPtr<Shader> lastShader_;
-    /// Last used shader name in shader variation query.
-    mutable String lastShaderName_;
-    /// Shader precache utility.
-    SharedPtr<ShaderPrecache> shaderPrecache_;
-    /// Allowed screen orientations.
-    String orientations_;
-    /// Graphics API name.
-    String apiName_;
-
-    /// Pixel perfect UV offset.
-    static const Vector2 pixelUVOffset;
-};
-
-/// Register Graphics library objects.
-void URHO3D_API RegisterGraphicsLibrary(Context* context);
-
-}

+ 1 - 1
Source/Urho3D/Graphics/Direct3D11/D3D11ShaderVariation.cpp

@@ -127,7 +127,7 @@ void ShaderVariation::Release()
         if (!graphics_)
         if (!graphics_)
             return;
             return;
 
 
-        graphics_->CleanUpShaderPrograms(this);
+        graphics_->CleanupShaderPrograms(this);
 
 
         if (type_ == VS)
         if (type_ == VS)
         {
         {

+ 31 - 268
Source/Urho3D/Graphics/Direct3D9/D3D9Graphics.cpp

@@ -25,37 +25,17 @@
 #include "../../Core/Context.h"
 #include "../../Core/Context.h"
 #include "../../Core/ProcessUtils.h"
 #include "../../Core/ProcessUtils.h"
 #include "../../Core/Profiler.h"
 #include "../../Core/Profiler.h"
-#include "../../Graphics/AnimatedModel.h"
-#include "../../Graphics/Animation.h"
-#include "../../Graphics/AnimationController.h"
-#include "../../Graphics/Camera.h"
-#include "../../Graphics/CustomGeometry.h"
-#include "../../Graphics/DebugRenderer.h"
-#include "../../Graphics/DecalSet.h"
 #include "../../Graphics/Graphics.h"
 #include "../../Graphics/Graphics.h"
 #include "../../Graphics/GraphicsEvents.h"
 #include "../../Graphics/GraphicsEvents.h"
 #include "../../Graphics/GraphicsImpl.h"
 #include "../../Graphics/GraphicsImpl.h"
 #include "../../Graphics/IndexBuffer.h"
 #include "../../Graphics/IndexBuffer.h"
-#include "../../Graphics/Material.h"
-#include "../../Graphics/Octree.h"
-#include "../../Graphics/ParticleEffect.h"
-#include "../../Graphics/ParticleEmitter.h"
-#include "../../Graphics/RibbonTrail.h"
 #include "../../Graphics/Shader.h"
 #include "../../Graphics/Shader.h"
 #include "../../Graphics/ShaderPrecache.h"
 #include "../../Graphics/ShaderPrecache.h"
 #include "../../Graphics/ShaderProgram.h"
 #include "../../Graphics/ShaderProgram.h"
-#include "../../Graphics/Skybox.h"
-#include "../../Graphics/StaticModelGroup.h"
-#include "../../Graphics/Technique.h"
-#include "../../Graphics/Terrain.h"
-#include "../../Graphics/TerrainPatch.h"
 #include "../../Graphics/Texture2D.h"
 #include "../../Graphics/Texture2D.h"
-#include "../../Graphics/Texture2DArray.h"
-#include "../../Graphics/Texture3D.h"
 #include "../../Graphics/TextureCube.h"
 #include "../../Graphics/TextureCube.h"
 #include "../../Graphics/VertexBuffer.h"
 #include "../../Graphics/VertexBuffer.h"
 #include "../../Graphics/VertexDeclaration.h"
 #include "../../Graphics/VertexDeclaration.h"
-#include "../../Graphics/Zone.h"
 #include "../../IO/File.h"
 #include "../../IO/File.h"
 #include "../../IO/Log.h"
 #include "../../IO/Log.h"
 #include "../../Resource/ResourceCache.h"
 #include "../../Resource/ResourceCache.h"
@@ -257,6 +237,7 @@ static HWND GetWindowHandle(SDL_Window* window)
 static unsigned readableDepthFormat = 0;
 static unsigned readableDepthFormat = 0;
 
 
 const Vector2 Graphics::pixelUVOffset(0.5f, 0.5f);
 const Vector2 Graphics::pixelUVOffset(0.5f, 0.5f);
+bool Graphics::gl3Support = false;
 
 
 Graphics::Graphics(Context* context) :
 Graphics::Graphics(Context* context) :
     Object(context),
     Object(context),
@@ -275,8 +256,10 @@ Graphics::Graphics(Context* context) :
     tripleBuffer_(false),
     tripleBuffer_(false),
     flushGPU_(false),
     flushGPU_(false),
     sRGB_(false),
     sRGB_(false),
-    deviceLost_(false),
-    queryIssued_(false),
+    anisotropySupport_(false),
+    dxtTextureSupport_(false),
+    etcTextureSupport_(false),
+    pvrtcTextureSupport_(false),
     lightPrepassSupport_(false),
     lightPrepassSupport_(false),
     deferredSupport_(false),
     deferredSupport_(false),
     instancingSupport_(false),
     instancingSupport_(false),
@@ -333,41 +316,6 @@ Graphics::~Graphics()
     SDL_Quit();
     SDL_Quit();
 }
 }
 
 
-void Graphics::SetExternalWindow(void* window)
-{
-    if (!impl_->window_)
-        externalWindow_ = window;
-    else
-        URHO3D_LOGERROR("Window already opened, can not set external window");
-}
-
-void Graphics::SetWindowTitle(const String& windowTitle)
-{
-    windowTitle_ = windowTitle;
-    if (impl_->window_)
-        SDL_SetWindowTitle(impl_->window_, windowTitle_.CString());
-}
-
-void Graphics::SetWindowIcon(Image* windowIcon)
-{
-    windowIcon_ = windowIcon;
-    if (impl_->window_)
-        CreateWindowIcon();
-}
-
-void Graphics::SetWindowPosition(const IntVector2& position)
-{
-    if (impl_->window_)
-        SDL_SetWindowPosition(impl_->window_, position.x_, position.y_);
-    else
-        position_ = position; // Sets as initial position for OpenWindow()
-}
-
-void Graphics::SetWindowPosition(int x, int y)
-{
-    SetWindowPosition(IntVector2(x, y));
-}
-
 bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync,
 bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync,
     bool tripleBuffer, int multiSample)
     bool tripleBuffer, int multiSample)
 {
 {
@@ -581,15 +529,9 @@ void Graphics::SetFlushGPU(bool enable)
     flushGPU_ = enable;
     flushGPU_ = enable;
 }
 }
 
 
-void Graphics::SetOrientations(const String& orientations)
-{
-    orientations_ = orientations.Trimmed();
-    SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
-}
-
-bool Graphics::ToggleFullscreen()
+void Graphics::SetForceGL2(bool enable)
 {
 {
-    return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, highDPI_, vsync_, tripleBuffer_, multiSample_);
+    // No effect on Direct3D9
 }
 }
 
 
 void Graphics::Close()
 void Graphics::Close()
@@ -759,7 +701,7 @@ bool Graphics::BeginFrame()
     {
     {
         URHO3D_PROFILE(DeviceLost);
         URHO3D_PROFILE(DeviceLost);
 
 
-        deviceLost_ = true;
+        impl_->deviceLost_ = true;
 
 
         // The device can not be reset yet, sleep and try again eventually
         // The device can not be reset yet, sleep and try again eventually
         if (hr == D3DERR_DEVICELOST)
         if (hr == D3DERR_DEVICELOST)
@@ -810,7 +752,7 @@ void Graphics::EndFrame()
     // If a query was issued on the previous frame, first wait for it to finish
     // If a query was issued on the previous frame, first wait for it to finish
     if (impl_->frameQuery_)
     if (impl_->frameQuery_)
     {
     {
-        if (queryIssued_)
+        if (impl_->queryIssued_)
         {
         {
             URHO3D_PROFILE(FlushGPU);
             URHO3D_PROFILE(FlushGPU);
 
 
@@ -818,13 +760,13 @@ void Graphics::EndFrame()
             {
             {
             }
             }
 
 
-            queryIssued_ = false;
+            impl_->queryIssued_ = false;
         }
         }
 
 
         if (flushGPU_)
         if (flushGPU_)
         {
         {
             impl_->frameQuery_->Issue(D3DISSUE_END);
             impl_->frameQuery_->Issue(D3DISSUE_END);
-            queryIssued_ = true;
+            impl_->queryIssued_ = true;
         }
         }
     }
     }
 
 
@@ -1934,43 +1876,6 @@ bool Graphics::IsInitialized() const
     return impl_->window_ != 0 && impl_->GetDevice() != 0;
     return impl_->window_ != 0 && impl_->GetDevice() != 0;
 }
 }
 
 
-IntVector2 Graphics::GetWindowPosition() const
-{
-    if (impl_->window_)
-        return position_;
-    return IntVector2::ZERO;
-}
-
-PODVector<IntVector2> Graphics::GetResolutions() const
-{
-    PODVector<IntVector2> ret;
-    unsigned numModes = (unsigned)SDL_GetNumDisplayModes(0);
-
-    for (unsigned i = 0; i < numModes; ++i)
-    {
-        SDL_DisplayMode mode;
-        SDL_GetDisplayMode(0, i, &mode);
-        int width = mode.w;
-        int height = mode.h;
-
-        // Store mode if unique
-        bool unique = true;
-        for (unsigned j = 0; j < ret.Size(); ++j)
-        {
-            if (ret[j].x_ == width && ret[j].y_ == height)
-            {
-                unique = false;
-                break;
-            }
-        }
-
-        if (unique)
-            ret.Push(IntVector2(width, height));
-    }
-
-    return ret;
-}
-
 PODVector<int> Graphics::GetMultiSampleLevels() const
 PODVector<int> Graphics::GetMultiSampleLevels() const
 {
 {
     PODVector<int> ret;
     PODVector<int> ret;
@@ -1994,13 +1899,6 @@ PODVector<int> Graphics::GetMultiSampleLevels() const
     return ret;
     return ret;
 }
 }
 
 
-IntVector2 Graphics::GetDesktopResolution() const
-{
-    SDL_DisplayMode mode;
-    SDL_GetDesktopDisplayMode(0, &mode);
-    return IntVector2(mode.w, mode.h);
-}
-
 unsigned Graphics::GetFormat(CompressedFormat format) const
 unsigned Graphics::GetFormat(CompressedFormat format) const
 {
 {
     switch (format)
     switch (format)
@@ -2097,6 +1995,11 @@ IntVector2 Graphics::GetRenderTargetDimensions() const
     return IntVector2(width, height);
     return IntVector2(width, height);
 }
 }
 
 
+bool Graphics::IsDeviceLost() const
+{
+    return impl_->deviceLost_;
+}
+
 void Graphics::OnWindowResized()
 void Graphics::OnWindowResized()
 {
 {
     if (!impl_->device_ || !impl_->window_)
     if (!impl_->device_ || !impl_->window_)
@@ -2155,113 +2058,6 @@ void Graphics::OnWindowMoved()
     SendEvent(E_WINDOWPOS, eventData);
     SendEvent(E_WINDOWPOS, eventData);
 }
 }
 
 
-void Graphics::Maximize()
-{
-    if (!impl_->window_)
-        return;
-
-    SDL_MaximizeWindow(impl_->window_);
-}
-
-void Graphics::Minimize()
-{
-    if (!impl_->window_)
-        return;
-
-    SDL_MinimizeWindow(impl_->window_);
-}
-
-void Graphics::AddGPUObject(GPUObject* object)
-{
-    MutexLock lock(gpuObjectMutex_);
-
-    gpuObjects_.Push(object);
-}
-
-void Graphics::RemoveGPUObject(GPUObject* object)
-{
-    MutexLock lock(gpuObjectMutex_);
-
-    gpuObjects_.Remove(object);
-}
-
-void* Graphics::ReserveScratchBuffer(unsigned size)
-{
-    if (!size)
-        return 0;
-
-    if (size > maxScratchBufferRequest_)
-        maxScratchBufferRequest_ = size;
-
-    // First check for a free buffer that is large enough
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_ && i->size_ >= size)
-        {
-            i->reserved_ = true;
-            return i->data_.Get();
-        }
-    }
-
-    // Then check if a free buffer can be resized
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_)
-        {
-            i->data_ = new unsigned char[size];
-            i->size_ = size;
-            i->reserved_ = true;
-
-            URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(size));
-
-            return i->data_.Get();
-        }
-    }
-
-    // Finally allocate a new buffer
-    ScratchBuffer newBuffer;
-    newBuffer.data_ = new unsigned char[size];
-    newBuffer.size_ = size;
-    newBuffer.reserved_ = true;
-    scratchBuffers_.Push(newBuffer);
-    return newBuffer.data_.Get();
-
-    URHO3D_LOGDEBUG("Allocated scratch buffer with size " + String(size));
-}
-
-void Graphics::FreeScratchBuffer(void* buffer)
-{
-    if (!buffer)
-        return;
-
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (i->reserved_ && i->data_.Get() == buffer)
-        {
-            i->reserved_ = false;
-            return;
-        }
-    }
-
-    URHO3D_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
-}
-
-void Graphics::CleanupScratchBuffers()
-{
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
-        {
-            i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
-            i->size_ = maxScratchBufferRequest_;
-
-            URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
-        }
-    }
-
-    maxScratchBufferRequest_ = 0;
-}
-
 void Graphics::CleanupShaderPrograms(ShaderVariation* variation)
 void Graphics::CleanupShaderPrograms(ShaderVariation* variation)
 {
 {
     for (ShaderProgramMap::Iterator i = impl_->shaderPrograms_.Begin(); i != impl_->shaderPrograms_.End();)
     for (ShaderProgramMap::Iterator i = impl_->shaderPrograms_.Begin(); i != impl_->shaderPrograms_.End();)
@@ -2396,6 +2192,16 @@ unsigned Graphics::GetFormat(const String& formatName)
     return GetRGBFormat();
     return GetRGBFormat();
 }
 }
 
 
+unsigned Graphics::GetMaxBones()
+{
+    return 64;
+}
+
+bool Graphics::GetGL3Support()
+{
+    return gl3Support;
+}
+
 void Graphics::SetStreamFrequency(unsigned index, unsigned frequency)
 void Graphics::SetStreamFrequency(unsigned index, unsigned frequency)
 {
 {
     if (index < MAX_VERTEX_STREAMS && impl_->streamFrequencies_[index] != frequency)
     if (index < MAX_VERTEX_STREAMS && impl_->streamFrequencies_[index] != frequency)
@@ -2445,19 +2251,6 @@ bool Graphics::OpenWindow(int width, int height, bool resizable, bool borderless
     return true;
     return true;
 }
 }
 
 
-void Graphics::CreateWindowIcon()
-{
-    if (windowIcon_)
-    {
-        SDL_Surface* surface = windowIcon_->GetSDLSurface();
-        if (surface)
-        {
-            SDL_SetWindowIcon(impl_->window_, surface);
-            SDL_FreeSurface(surface);
-        }
-    }
-}
-
 void Graphics::AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless)
 void Graphics::AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless)
 {
 {
     if (!externalWindow_)
     if (!externalWindow_)
@@ -2556,6 +2349,9 @@ bool Graphics::CreateDevice(unsigned adapter, unsigned deviceType)
 
 
 void Graphics::CheckFeatureSupport()
 void Graphics::CheckFeatureSupport()
 {
 {
+    anisotropySupport_ = true;
+    dxtTextureSupport_ = true;
+    
     // Reset features first
     // Reset features first
     lightPrepassSupport_ = false;
     lightPrepassSupport_ = false;
     deferredSupport_ = false;
     deferredSupport_ = false;
@@ -2643,7 +2439,7 @@ void Graphics::ResetDevice()
 
 
     if (SUCCEEDED(impl_->device_->Reset(&impl_->presentParams_)))
     if (SUCCEEDED(impl_->device_->Reset(&impl_->presentParams_)))
     {
     {
-        deviceLost_ = false;
+        impl_->deviceLost_ = false;
         OnDeviceReset();
         OnDeviceReset();
     }
     }
 }
 }
@@ -2761,8 +2557,7 @@ void Graphics::ResetCachedState()
     impl_->destBlend_ = D3DBLEND_ZERO;
     impl_->destBlend_ = D3DBLEND_ZERO;
     impl_->blendOp_ = D3DBLENDOP_ADD;
     impl_->blendOp_ = D3DBLENDOP_ADD;
     impl_->vertexDeclaration_ = 0;
     impl_->vertexDeclaration_ = 0;
-
-    queryIssued_ = false;
+    impl_->queryIssued_ = false;
 }
 }
 
 
 void Graphics::SetTextureUnitMappings()
 void Graphics::SetTextureUnitMappings()
@@ -2785,36 +2580,4 @@ void Graphics::SetTextureUnitMappings()
     textureUnits_["ZoneVolumeMap"] = TU_ZONE;
     textureUnits_["ZoneVolumeMap"] = TU_ZONE;
 }
 }
 
 
-void RegisterGraphicsLibrary(Context* context)
-{
-    Animation::RegisterObject(context);
-    Material::RegisterObject(context);
-    Model::RegisterObject(context);
-    Shader::RegisterObject(context);
-    Technique::RegisterObject(context);
-    Texture2D::RegisterObject(context);
-    Texture2DArray::RegisterObject(context);
-    Texture3D::RegisterObject(context);
-    TextureCube::RegisterObject(context);
-    Camera::RegisterObject(context);
-    Drawable::RegisterObject(context);
-    Light::RegisterObject(context);
-    StaticModel::RegisterObject(context);
-    StaticModelGroup::RegisterObject(context);
-    Skybox::RegisterObject(context);
-    AnimatedModel::RegisterObject(context);
-    AnimationController::RegisterObject(context);
-    BillboardSet::RegisterObject(context);
-    ParticleEffect::RegisterObject(context);
-    ParticleEmitter::RegisterObject(context);
-    RibbonTrail::RegisterObject(context);
-    CustomGeometry::RegisterObject(context);
-    DecalSet::RegisterObject(context);
-    Terrain::RegisterObject(context);
-    TerrainPatch::RegisterObject(context);
-    DebugRenderer::RegisterObject(context);
-    Octree::RegisterObject(context);
-    Zone::RegisterObject(context);
-}
-
 }
 }

+ 0 - 671
Source/Urho3D/Graphics/Direct3D9/D3D9Graphics.h

@@ -1,671 +0,0 @@
-//
-// Copyright (c) 2008-2016 the Urho3D project.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-//
-
-#pragma once
-
-#include "../../Container/ArrayPtr.h"
-#include "../../Container/HashSet.h"
-#include "../../Core/Mutex.h"
-#include "../../Core/Object.h"
-#include "../../Graphics/GraphicsDefs.h"
-#include "../../Math/Color.h"
-#include "../../Math/Plane.h"
-#include "../../Math/Rect.h"
-#include "../../Resource/Image.h"
-
-namespace Urho3D
-{
-
-class File;
-class Image;
-class IndexBuffer;
-class GPUObject;
-class GraphicsImpl;
-class RenderSurface;
-class Shader;
-class ShaderPrecache;
-class ShaderProgram;
-class ShaderVariation;
-class Texture;
-class Texture2D;
-class TextureCube;
-class Vector3;
-class Vector4;
-class VertexBuffer;
-class VertexDeclaration;
-
-struct ShaderParameter;
-
-/// CPU-side scratch buffer for vertex data updates.
-struct ScratchBuffer
-{
-    ScratchBuffer() :
-        size_(0),
-        reserved_(false)
-    {
-    }
-
-    /// Buffer data.
-    SharedArrayPtr<unsigned char> data_;
-    /// Data size.
-    unsigned size_;
-    /// Reserved flag.
-    bool reserved_;
-};
-
-/// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
-class URHO3D_API Graphics : public Object
-{
-    URHO3D_OBJECT(Graphics, Object);
-
-public:
-    /// Construct.
-    Graphics(Context* context);
-    /// Destruct. Release the Direct3D9 device and close the window.
-    virtual ~Graphics();
-
-    /// Set external window handle. Only effective before setting the initial screen mode.
-    void SetExternalWindow(void* window);
-    /// Set window title.
-    void SetWindowTitle(const String& windowTitle);
-    /// Set window icon.
-    void SetWindowIcon(Image* windowIcon);
-    /// Set window position. Sets initial position if window is not created yet.
-    void SetWindowPosition(const IntVector2& position);
-    /// Set window position. Sets initial position if window is not created yet.
-    void SetWindowPosition(int x, int y);
-    /// Set screen mode. Return true if successful.
-    bool SetMode
-        (int width, int height, bool fullscreen, bool borderless, bool resizable, bool vsync, bool highDPI, bool tripleBuffer,
-            int multiSample);
-    /// Set screen resolution only. Return true if successful.
-    bool SetMode(int width, int height);
-    /// Set whether the main window uses sRGB conversion on write.
-    void SetSRGB(bool enable);
-    /// Set whether to flush the GPU command buffer to prevent multiple frames being queued and uneven frame timesteps. Default off, may decrease performance if enabled.
-    void SetFlushGPU(bool enable);
-    /// Set allowed screen orientations as a space-separated list of "LandscapeLeft", "LandscapeRight", "Portrait" and "PortraitUpsideDown". Affects currently only iOS platform.
-    void SetOrientations(const String& orientations);
-    /// Toggle between full screen and windowed mode. Return true if successful.
-    bool ToggleFullscreen();
-    /// Close the window.
-    void Close();
-    /// Take a screenshot. Return true if successful.
-    bool TakeScreenShot(Image& destImage);
-    /// Begin frame rendering. Return true if device available and can render.
-    bool BeginFrame();
-    /// End frame rendering and swap buffers.
-    void EndFrame();
-    /// Clear any or all of rendertarget, depth buffer and stencil buffer.
-    void Clear(unsigned flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, unsigned stencil = 0);
-    /// Resolve multisampled backbuffer to a texture rendertarget. The texture's size should match the viewport size.
-    bool ResolveToTexture(Texture2D* destination, const IntRect& viewport);
-    /// Draw non-indexed geometry.
-    void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
-    /// Draw indexed geometry with vertex index offset.
-    void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount);
-    /// Draw indexed geometry.
-    void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
-    /// Draw indexed, instanced geometry. An instancing vertex buffer must be set.
-    void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
-        unsigned instanceCount);
-    /// Draw indexed, instanced geometry with vertex index offset.
-    void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex,
-        unsigned vertexCount, unsigned instanceCount);
-    /// Set vertex buffer.
-    void SetVertexBuffer(VertexBuffer* buffer);
-    /// Set multiple vertex buffers.
-    bool SetVertexBuffers(const PODVector<VertexBuffer*>& buffers, unsigned instanceOffset = 0);
-    /// Set multiple vertex buffers.
-    bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, unsigned instanceOffset = 0);
-    /// Set index buffer.
-    void SetIndexBuffer(IndexBuffer* buffer);
-    /// Set shaders.
-    void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
-    /// Set shader float constants.
-    void SetShaderParameter(StringHash param, const float* data, unsigned count);
-    /// Set shader float constant.
-    void SetShaderParameter(StringHash param, float value);
-    /// Set shader boolean constant.
-    void SetShaderParameter(StringHash param, bool value);
-    /// Set shader color constant.
-    void SetShaderParameter(StringHash param, const Color& color);
-    /// Set shader 2D vector constant.
-    void SetShaderParameter(StringHash param, const Vector2& vector);
-    /// Set shader 3x3 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix3& matrix);
-    /// Set shader 3D vector constant.
-    void SetShaderParameter(StringHash param, const Vector3& vector);
-    /// Set shader 4x4 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix4& matrix);
-    /// Set shader 4D vector constant.
-    void SetShaderParameter(StringHash param, const Vector4& vector);
-    /// Set shader 3x4 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
-    /// Set shader constant from a variant. Supported variant types: bool, float, vector2, vector3, vector4, color.
-    void SetShaderParameter(StringHash param, const Variant& value);
-    /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
-    bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
-    /// Check whether a shader parameter exists on the currently set shaders.
-    bool HasShaderParameter(StringHash param);
-    /// Check whether the current pixel shader uses a texture unit.
-    bool HasTextureUnit(TextureUnit unit);
-    /// Clear remembered shader parameter source group.
-    void ClearParameterSource(ShaderParameterGroup group);
-    /// Clear remembered shader parameter sources.
-    void ClearParameterSources();
-    /// Clear remembered transform shader parameter sources.
-    void ClearTransformSources();
-    /// Set texture.
-    void SetTexture(unsigned index, Texture* texture);
-    /// Set default texture filtering mode.
-    void SetDefaultTextureFilterMode(TextureFilterMode mode);
-    /// Set texture anisotropy.
-    void SetTextureAnisotropy(unsigned level);
-    /// Reset all rendertargets, depth-stencil surface and viewport.
-    void ResetRenderTargets();
-    /// Reset specific rendertarget.
-    void ResetRenderTarget(unsigned index);
-    /// Reset depth-stencil surface.
-    void ResetDepthStencil();
-    /// Set rendertarget.
-    void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
-    /// Set rendertarget.
-    void SetRenderTarget(unsigned index, Texture2D* texture);
-    /// Set depth-stencil surface.
-    void SetDepthStencil(RenderSurface* depthStencil);
-    /// Set depth-stencil surface.
-    void SetDepthStencil(Texture2D* texture);
-    /// Set viewport.
-    void SetViewport(const IntRect& rect);
-    /// Set blending mode.
-    void SetBlendMode(BlendMode mode);
-    /// Set color write on/off.
-    void SetColorWrite(bool enable);
-    /// Set hardware culling mode.
-    void SetCullMode(CullMode mode);
-    /// Set depth bias.
-    void SetDepthBias(float constantBias, float slopeScaledBias);
-    /// Set depth compare.
-    void SetDepthTest(CompareMode mode);
-    /// Set depth write on/off.
-    void SetDepthWrite(bool enable);
-    /// Set polygon fill mode.
-    void SetFillMode(FillMode mode);
-    /// Set scissor test.
-    void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
-    /// Set scissor test.
-    void SetScissorTest(bool enable, const IntRect& rect);
-    /// Set stencil test.
-    void SetStencilTest
-        (bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP,
-            unsigned stencilRef = 0, unsigned compareMask = M_MAX_UNSIGNED, unsigned writeMask = M_MAX_UNSIGNED);
-    /// Set a custom clipping plane. The plane is specified in world space, but is dependent on the view and projection matrices.
-    void SetClipPlane(bool enable, const Plane& clipPlane = Plane::UP, const Matrix3x4& view = Matrix3x4::IDENTITY,
-        const Matrix4& projection = Matrix4::IDENTITY);
-    /// Begin dumping shader variation names to an XML file for precaching.
-    void BeginDumpShaders(const String& fileName);
-    /// End dumping shader variations names.
-    void EndDumpShaders();
-    /// Precache shader variations from an XML file generated with BeginDumpShaders().
-    void PrecacheShaders(Deserializer& source);
-
-    /// Return whether rendering initialized.
-    bool IsInitialized() const;
-
-    /// Return graphics implementation, which holds the actual API-specific resources.
-    GraphicsImpl* GetImpl() const { return impl_; }
-
-    /// Return OS-specific external window handle. Null if not in use.
-    void* GetExternalWindow() const { return externalWindow_; }
-
-    /// Return window title.
-    const String& GetWindowTitle() const { return windowTitle_; }
-
-    /// Return graphics API name.
-    const String& GetApiName() const { return apiName_; }
-
-    /// Return window position.
-    IntVector2 GetWindowPosition() const;
-
-    /// Return window width in pixels.
-    int GetWidth() const { return width_; }
-
-    /// Return window height in pixels.
-    int GetHeight() const { return height_; }
-
-    /// Return multisample mode (1 = no multisampling.)
-    int GetMultiSample() const { return multiSample_; }
-
-    /// Return whether window is fullscreen.
-    bool GetFullscreen() const { return fullscreen_; }
-
-    /// Return whether window is borderless.
-    bool GetBorderless() const { return borderless_; }
-
-    /// Return whether window is resizable.
-    bool GetResizable() const { return resizable_; }
-
-    /// Return whether window is high DPI.
-    bool GetHighDPI() const { return highDPI_; }
-
-    /// Return whether vertical sync is on.
-    bool GetVSync() const { return vsync_; }
-
-    /// Return whether triple buffering is enabled.
-    bool GetTripleBuffer() const { return tripleBuffer_; }
-
-    /// Return whether the main window is using sRGB conversion on write.
-    bool GetSRGB() const { return sRGB_; }
-
-    /// Return whether the GPU command buffer is flushed each frame.
-    bool GetFlushGPU() const { return flushGPU_; }
-
-    /// Return allowed screen orientations.
-    const String& GetOrientations() const { return orientations_; }
-
-    /// Return whether Direct3D device is lost, and can not yet render. This happens during fullscreen resolution switching.
-    bool IsDeviceLost() const { return deviceLost_; }
-
-    /// Return number of primitives drawn this frame.
-    unsigned GetNumPrimitives() const { return numPrimitives_; }
-
-    /// Return number of batches drawn this frame.
-    unsigned GetNumBatches() const { return numBatches_; }
-
-    /// Return dummy color texture format for shadow maps. Is "NULL" (consume no video memory) if supported.
-    unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
-
-    /// Return shadow map depth texture format, or 0 if not supported.
-    unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
-
-    /// Return 24-bit shadow map depth texture format, or 0 if not supported.
-    unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
-
-    /// Return whether hardware instancing is supported..
-    bool GetInstancingSupport() const { return instancingSupport_; }
-
-    /// Return whether light pre-pass rendering is supported.
-    bool GetLightPrepassSupport() const { return lightPrepassSupport_; }
-
-    /// Return whether deferred rendering is supported.
-    bool GetDeferredSupport() const { return deferredSupport_; }
-
-    /// Return whether shadow map depth compare is done in hardware.
-    bool GetHardwareShadowSupport() const { return hardwareShadowSupport_; }
-
-    /// Return whether a readable hardware depth format is available.
-    bool GetReadableDepthSupport() const { return GetReadableDepthFormat() != 0; }
-
-    /// Return whether sRGB conversion on texture sampling is supported.
-    bool GetSRGBSupport() const { return sRGBSupport_; }
-
-    /// Return whether sRGB conversion on rendertarget writing is supported.
-    bool GetSRGBWriteSupport() const { return sRGBWriteSupport_; }
-
-    /// Return supported fullscreen resolutions. Will be empty if listing the resolutions is not supported on the platform (e.g. Web).
-    PODVector<IntVector2> GetResolutions() const;
-    /// Return supported multisampling levels.
-    PODVector<int> GetMultiSampleLevels() const;
-    /// Return the desktop resolution.
-    IntVector2 GetDesktopResolution() const;
-    /// Return hardware format for a compressed image format, or 0 if unsupported.
-    unsigned GetFormat(CompressedFormat format) const;
-    /// Return a shader variation by name and defines.
-    ShaderVariation* GetShader(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
-    /// Return a shader variation by name and defines.
-    ShaderVariation* GetShader(ShaderType type, const char* name, const char* defines) const;
-    /// Return current vertex buffer by index.
-    VertexBuffer* GetVertexBuffer(unsigned index) const;
-
-    /// Return current index buffer.
-    IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
-
-    /// Return current vertex shader.
-    ShaderVariation* GetVertexShader() const { return vertexShader_; }
-
-    /// Return current pixel shader.
-    ShaderVariation* GetPixelShader() const { return pixelShader_; }
-
-    /// Return texture unit index by name.
-    TextureUnit GetTextureUnit(const String& name);
-    /// Return texture unit name by index.
-    const String& GetTextureUnitName(TextureUnit unit);
-    /// Return current texture by texture unit index.
-    Texture* GetTexture(unsigned index) const;
-
-    /// Return default texture filtering mode.
-    TextureFilterMode GetDefaultTextureFilterMode() const { return defaultTextureFilterMode_; }
-
-    /// Return current rendertarget by index.
-    RenderSurface* GetRenderTarget(unsigned index) const;
-
-    /// Return current depth-stencil surface.
-    RenderSurface* GetDepthStencil() const { return depthStencil_; }
-
-    /// Return the viewport coordinates.
-    IntRect GetViewport() const { return viewport_; }
-
-    /// Return texture anisotropy.
-    unsigned GetTextureAnisotropy() const { return textureAnisotropy_; }
-
-    /// Return blending mode.
-    BlendMode GetBlendMode() const { return blendMode_; }
-
-    /// Return whether color write is enabled.
-    bool GetColorWrite() const { return colorWrite_; }
-
-    /// Return hardware culling mode.
-    CullMode GetCullMode() const { return cullMode_; }
-
-    /// Return depth constant bias.
-    float GetDepthConstantBias() const { return constantDepthBias_; }
-
-    /// Return depth slope scaled bias.
-    float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
-
-    /// Return depth compare mode.
-    CompareMode GetDepthTest() const { return depthTestMode_; }
-
-    /// Return whether depth write is enabled.
-    bool GetDepthWrite() const { return depthWrite_; }
-
-    /// Return polygon fill mode.
-    FillMode GetFillMode() const { return fillMode_; }
-
-    /// Return whether stencil test is enabled.
-    bool GetStencilTest() const { return stencilTest_; }
-
-    /// Return whether scissor test is enabled.
-    bool GetScissorTest() const { return scissorTest_; }
-
-    /// Return scissor rectangle coordinates.
-    const IntRect& GetScissorRect() const { return scissorRect_; }
-
-    /// Return stencil compare mode.
-    CompareMode GetStencilTestMode() const { return stencilTestMode_; }
-
-    /// Return stencil operation to do if stencil test passes.
-    StencilOp GetStencilPass() const { return stencilPass_; }
-
-    /// Return stencil operation to do if stencil test fails.
-    StencilOp GetStencilFail() const { return stencilFail_; }
-
-    /// Return stencil operation to do if depth compare fails.
-    StencilOp GetStencilZFail() const { return stencilZFail_; }
-
-    /// Return stencil reference value.
-    unsigned GetStencilRef() const { return stencilRef_; }
-
-    /// Return stencil compare bitmask.
-    unsigned GetStencilCompareMask() const { return stencilCompareMask_; }
-
-    /// Return stencil write bitmask.
-    unsigned GetStencilWriteMask() const { return stencilWriteMask_; }
-
-    /// Return whether a custom clipping plane is in use.
-    bool GetUseClipPlane() const { return useClipPlane_; }
-
-    /// Return rendertarget width and height.
-    IntVector2 GetRenderTargetDimensions() const;
-
-    /// Window was resized through user interaction. Called by Input subsystem.
-    void OnWindowResized();
-    /// Window was moved through user interaction. Called by Input subsystem.
-    void OnWindowMoved();
-    /// Maximize the Window.
-    void Maximize();
-    /// Minimize the Window.
-    void Minimize();
-    /// Add a GPU object to keep track of. Called by GPUObject.
-    void AddGPUObject(GPUObject* object);
-    /// Remove a GPU object. Called by GPUObject.
-    void RemoveGPUObject(GPUObject* object);
-    /// Reserve a CPU-side scratch buffer.
-    void* ReserveScratchBuffer(unsigned size);
-    /// Free a CPU-side scratch buffer.
-    void FreeScratchBuffer(void* buffer);
-    /// Clean up too large scratch buffers.
-    void CleanupScratchBuffers();
-    /// Clean up shader programs when a shader variation is released or destroyed.
-    void CleanupShaderPrograms(ShaderVariation* variation);
-
-    /// Return the API-specific alpha texture format.
-    static unsigned GetAlphaFormat();
-    /// Return the API-specific luminance texture format.
-    static unsigned GetLuminanceFormat();
-    /// Return the API-specific luminance alpha texture format.
-    static unsigned GetLuminanceAlphaFormat();
-    /// Return the API-specific RGB texture format.
-    static unsigned GetRGBFormat();
-    /// Return the API-specific RGBA texture format.
-    static unsigned GetRGBAFormat();
-    /// Return the API-specific RGBA 16-bit texture format.
-    static unsigned GetRGBA16Format();
-    /// Return the API-specific RGBA 16-bit float texture format.
-    static unsigned GetRGBAFloat16Format();
-    /// Return the API-specific RGBA 32-bit float texture format.
-    static unsigned GetRGBAFloat32Format();
-    /// Return the API-specific RG 16-bit texture format.
-    static unsigned GetRG16Format();
-    /// Return the API-specific RG 16-bit float texture format.
-    static unsigned GetRGFloat16Format();
-    /// Return the API-specific RG 32-bit float texture format.
-    static unsigned GetRGFloat32Format();
-    /// Return the API-specific single channel 16-bit float texture format.
-    static unsigned GetFloat16Format();
-    /// Return the API-specific single channel 32-bit float texture format.
-    static unsigned GetFloat32Format();
-    /// Return the API-specific linear depth texture format.
-    static unsigned GetLinearDepthFormat();
-    /// Return the API-specific hardware depth-stencil texture format.
-    static unsigned GetDepthStencilFormat();
-    /// Return the API-specific readable hardware depth format, or 0 if not supported.
-    static unsigned GetReadableDepthFormat();
-    /// Return the API-specific texture format from a textual description, for example "rgb".
-    static unsigned GetFormat(const String& formatName);
-
-    /// Return UV offset required for pixel perfect rendering.
-    static const Vector2& GetPixelUVOffset() { return pixelUVOffset; }
-
-    /// Return maximum number of supported bones for skinning.
-    static unsigned GetMaxBones() { return 64; }
-
-private:
-    /// Set vertex buffer stream frequency.
-    void SetStreamFrequency(unsigned index, unsigned frequency);
-    /// Reset stream frequencies.
-    void ResetStreamFrequencies();
-    /// Create the application window.
-    bool OpenWindow(int width, int height, bool resizable, bool borderless);
-    /// Create the application window icon.
-    void CreateWindowIcon();
-    /// Adjust the window for new resolution and fullscreen mode.
-    void AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless);
-    /// Create the Direct3D interface.
-    bool CreateInterface();
-    /// Create the Direct3D device.
-    bool CreateDevice(unsigned adapter, unsigned deviceType);
-    /// Check supported rendering features.
-    void CheckFeatureSupport();
-    /// Reset the Direct3D device.
-    void ResetDevice();
-    /// Notify all GPU resources so they can release themselves as needed.
-    void OnDeviceLost();
-    /// Notify all GPU resources so they can recreate themselves as needed.
-    void OnDeviceReset();
-    /// Reset cached rendering state.
-    void ResetCachedState();
-    /// Initialize texture unit mappings.
-    void SetTextureUnitMappings();
-
-    /// Mutex for accessing the GPU objects vector from several threads.
-    Mutex gpuObjectMutex_;
-    /// Implementation.
-    GraphicsImpl* impl_;
-    /// Window title.
-    String windowTitle_;
-    /// Window icon image.
-    WeakPtr<Image> windowIcon_;
-    /// External window, null if not in use (default.)
-    void* externalWindow_;
-    /// Window width in pixels.
-    int width_;
-    /// Window height in pixels.
-    int height_;
-    /// Window position.
-    IntVector2 position_;
-    /// Multisampling mode.
-    int multiSample_;
-    /// Fullscreen flag.
-    bool fullscreen_;
-    /// Borderless flag.
-    bool borderless_;
-    /// Resizable flag.
-    bool resizable_;
-    /// High DPI flag.
-    bool highDPI_;
-    /// Vertical sync flag.
-    bool vsync_;
-    /// Triple buffering flag.
-    bool tripleBuffer_;
-    /// Flush GPU command buffer flag.
-    bool flushGPU_;
-    /// sRGB conversion on write flag for the main window.
-    bool sRGB_;
-    /// Direct3D device lost flag.
-    bool deviceLost_;
-    /// Flush query issued flag.
-    bool queryIssued_;
-    /// Light pre-pass rendering support flag.
-    bool lightPrepassSupport_;
-    /// Deferred rendering support flag.
-    bool deferredSupport_;
-    /// Hardware shadow map depth compare support flag.
-    bool hardwareShadowSupport_;
-    /// Instancing support flag.
-    bool instancingSupport_;
-    /// sRGB conversion on read support flag.
-    bool sRGBSupport_;
-    /// sRGB conversion on write support flag.
-    bool sRGBWriteSupport_;
-    /// Number of primitives this frame.
-    unsigned numPrimitives_;
-    /// Number of batches this frame.
-    unsigned numBatches_;
-    /// Largest scratch buffer request this frame.
-    unsigned maxScratchBufferRequest_;
-    /// GPU objects.
-    PODVector<GPUObject*> gpuObjects_;
-    /// Scratch buffers.
-    Vector<ScratchBuffer> scratchBuffers_;
-    /// Shadow map dummy color texture format.
-    unsigned dummyColorFormat_;
-    /// Shadow map depth texture format.
-    unsigned shadowMapFormat_;
-    /// Shadow map 24-bit depth texture format.
-    unsigned hiresShadowMapFormat_;
-    /// Vertex buffers in use.
-    VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS];
-    /// Index buffer in use.
-    IndexBuffer* indexBuffer_;
-    /// Vertex shader in use.
-    ShaderVariation* vertexShader_;
-    /// Pixel shader in use.
-    ShaderVariation* pixelShader_;
-    /// Textures in use.
-    Texture* textures_[MAX_TEXTURE_UNITS];
-    /// Texture unit mappings.
-    HashMap<String, TextureUnit> textureUnits_;
-    /// Rendertargets in use.
-    RenderSurface* renderTargets_[MAX_RENDERTARGETS];
-    /// Depth-stencil surface in use.
-    RenderSurface* depthStencil_;
-    /// Viewport coordinates.
-    IntRect viewport_;
-    /// Default texture filtering mode.
-    TextureFilterMode defaultTextureFilterMode_;
-    /// Texture anisotropy level.
-    unsigned textureAnisotropy_;
-    /// Blending mode.
-    BlendMode blendMode_;
-    /// Color write enable.
-    bool colorWrite_;
-    /// Hardware culling mode.
-    CullMode cullMode_;
-    /// Depth constant bias.
-    float constantDepthBias_;
-    /// Depth slope scaled bias.
-    float slopeScaledDepthBias_;
-    /// Depth compare mode.
-    CompareMode depthTestMode_;
-    /// Depth write enable flag.
-    bool depthWrite_;
-    /// Polygon fill mode.
-    FillMode fillMode_;
-    /// Scissor test rectangle.
-    IntRect scissorRect_;
-    /// Scissor test enable flag.
-    bool scissorTest_;
-    /// Stencil test compare mode.
-    CompareMode stencilTestMode_;
-    /// Stencil operation on pass.
-    StencilOp stencilPass_;
-    /// Stencil operation on fail.
-    StencilOp stencilFail_;
-    /// Stencil operation on depth fail.
-    StencilOp stencilZFail_;
-    /// Stencil test reference value.
-    unsigned stencilRef_;
-    /// Stencil compare bitmask.
-    unsigned stencilCompareMask_;
-    /// Stencil write bitmask.
-    unsigned stencilWriteMask_;
-    /// Stencil test enable flag.
-    bool stencilTest_;
-    /// Custom clip plane enable flag.
-    bool useClipPlane_;
-    /// Remembered shader parameter sources.
-    const void* shaderParameterSources_[MAX_SHADER_PARAMETER_GROUPS];
-    /// Base directory for shaders.
-    String shaderPath_;
-    /// File extension for shaders.
-    String shaderExtension_;
-    /// Last used shader in shader variation query.
-    mutable WeakPtr<Shader> lastShader_;
-    /// Last used shader name in shader variation query.
-    mutable String lastShaderName_;
-    /// Shader precache utility.
-    SharedPtr<ShaderPrecache> shaderPrecache_;
-    /// Allowed screen orientations.
-    String orientations_;
-    /// Graphics API name.
-    String apiName_;
-
-    /// Pixel perfect UV offset.
-    static const Vector2 pixelUVOffset;
-};
-
-/// Register Graphics library objects.
-void URHO3D_API RegisterGraphicsLibrary(Context* context);
-
-}

+ 3 - 1
Source/Urho3D/Graphics/Direct3D9/D3D9GraphicsImpl.cpp

@@ -39,7 +39,9 @@ GraphicsImpl::GraphicsImpl() :
     frameQuery_(0),
     frameQuery_(0),
     adapter_(D3DADAPTER_DEFAULT),
     adapter_(D3DADAPTER_DEFAULT),
     deviceType_(D3DDEVTYPE_HAL),
     deviceType_(D3DDEVTYPE_HAL),
-    shaderProgram_(0)
+    shaderProgram_(0),
+    deviceLost_(false),
+    queryIssued_(false)
 {
 {
     memset(&presentParams_, 0, sizeof presentParams_);
     memset(&presentParams_, 0, sizeof presentParams_);
 }
 }

+ 4 - 0
Source/Urho3D/Graphics/Direct3D9/D3D9GraphicsImpl.h

@@ -99,6 +99,10 @@ private:
     D3DTEXTUREADDRESS wAddressModes_[MAX_TEXTURE_UNITS];
     D3DTEXTUREADDRESS wAddressModes_[MAX_TEXTURE_UNITS];
     /// Texture border colors in use.
     /// Texture border colors in use.
     Color borderColors_[MAX_TEXTURE_UNITS];
     Color borderColors_[MAX_TEXTURE_UNITS];
+    /// Device lost flag.
+    bool deviceLost_;
+    /// Frame query issued flag.
+    bool queryIssued_;
     /// sRGB mode in use.
     /// sRGB mode in use.
     bool sRGBModes_[MAX_TEXTURE_UNITS];
     bool sRGBModes_[MAX_TEXTURE_UNITS];
     /// sRGB write flag.
     /// sRGB write flag.

+ 310 - 0
Source/Urho3D/Graphics/Graphics.cpp

@@ -0,0 +1,310 @@
+//
+// Copyright (c) 2008-2016 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "../Precompiled.h"
+
+#include "../Graphics/AnimatedModel.h"
+#include "../Graphics/Animation.h"
+#include "../Graphics/AnimationController.h"
+#include "../Graphics/Camera.h"
+#include "../Graphics/CustomGeometry.h"
+#include "../Graphics/DebugRenderer.h"
+#include "../Graphics/DecalSet.h"
+#include "../Graphics/Graphics.h"
+#include "../Graphics/GraphicsImpl.h"
+#include "../Graphics/Material.h"
+#include "../Graphics/Octree.h"
+#include "../Graphics/ParticleEffect.h"
+#include "../Graphics/ParticleEmitter.h"
+#include "../Graphics/RibbonTrail.h"
+#include "../Graphics/Shader.h"
+#include "../Graphics/Skybox.h"
+#include "../Graphics/StaticModelGroup.h"
+#include "../Graphics/Technique.h"
+#include "../Graphics/Terrain.h"
+#include "../Graphics/TerrainPatch.h"
+#include "../Graphics/Texture2D.h"
+#include "../Graphics/Texture2DArray.h"
+#include "../Graphics/Texture3D.h"
+#include "../Graphics/TextureCube.h"
+#include "../Graphics/Zone.h"
+#include "../IO/Log.h"
+
+#include <SDL/SDL.h>
+#include <SDL/SDL_syswm.h>
+
+#include "../DebugNew.h"
+
+namespace Urho3D
+{
+
+void Graphics::SetExternalWindow(void* window)
+{
+    if (!impl_->window_)
+        externalWindow_ = window;
+    else
+        URHO3D_LOGERROR("Window already opened, can not set external window");
+}
+
+void Graphics::SetWindowTitle(const String& windowTitle)
+{
+    windowTitle_ = windowTitle;
+    if (impl_->window_)
+        SDL_SetWindowTitle(impl_->window_, windowTitle_.CString());
+}
+
+void Graphics::SetWindowIcon(Image* windowIcon)
+{
+    windowIcon_ = windowIcon;
+    if (impl_->window_)
+        CreateWindowIcon();
+}
+
+void Graphics::SetWindowPosition(const IntVector2& position)
+{
+    if (impl_->window_)
+        SDL_SetWindowPosition(impl_->window_, position.x_, position.y_);
+    else
+        position_ = position; // Sets as initial position for OpenWindow()
+}
+
+void Graphics::SetWindowPosition(int x, int y)
+{
+    SetWindowPosition(IntVector2(x, y));
+}
+
+void Graphics::SetOrientations(const String& orientations)
+{
+    orientations_ = orientations.Trimmed();
+    SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
+}
+
+bool Graphics::ToggleFullscreen()
+{
+    return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, highDPI_, vsync_, tripleBuffer_, multiSample_);
+}
+
+IntVector2 Graphics::GetWindowPosition() const
+{
+    if (impl_->window_)
+        return position_;
+    return IntVector2::ZERO;
+}
+
+PODVector<IntVector2> Graphics::GetResolutions() const
+{
+    PODVector<IntVector2> ret;
+    // Emscripten is not able to return a valid list
+#ifndef __EMSCRIPTEN__
+    unsigned numModes = (unsigned)SDL_GetNumDisplayModes(0);
+
+    for (unsigned i = 0; i < numModes; ++i)
+    {
+        SDL_DisplayMode mode;
+        SDL_GetDisplayMode(0, i, &mode);
+        int width = mode.w;
+        int height = mode.h;
+
+        // Store mode if unique
+        bool unique = true;
+        for (unsigned j = 0; j < ret.Size(); ++j)
+        {
+            if (ret[j].x_ == width && ret[j].y_ == height)
+            {
+                unique = false;
+                break;
+            }
+        }
+
+        if (unique)
+            ret.Push(IntVector2(width, height));
+    }
+#endif
+
+    return ret;
+}
+
+IntVector2 Graphics::GetDesktopResolution() const
+{
+#if !defined(__ANDROID__) && !defined(IOS)
+    SDL_DisplayMode mode;
+    SDL_GetDesktopDisplayMode(0, &mode);
+    return IntVector2(mode.w, mode.h);
+#else
+    // SDL_GetDesktopDisplayMode() may not work correctly on mobile platforms. Rather return the window size
+    return IntVector2(width_, height_);
+#endif
+}
+
+void Graphics::Maximize()
+{
+    if (!impl_->window_)
+        return;
+
+    SDL_MaximizeWindow(impl_->window_);
+}
+
+void Graphics::Minimize()
+{
+    if (!impl_->window_)
+        return;
+
+    SDL_MinimizeWindow(impl_->window_);
+}
+
+void Graphics::AddGPUObject(GPUObject* object)
+{
+    MutexLock lock(gpuObjectMutex_);
+
+    gpuObjects_.Push(object);
+}
+
+void Graphics::RemoveGPUObject(GPUObject* object)
+{
+    MutexLock lock(gpuObjectMutex_);
+
+    gpuObjects_.Remove(object);
+}
+
+void* Graphics::ReserveScratchBuffer(unsigned size)
+{
+    if (!size)
+        return 0;
+
+    if (size > maxScratchBufferRequest_)
+        maxScratchBufferRequest_ = size;
+
+    // First check for a free buffer that is large enough
+    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
+    {
+        if (!i->reserved_ && i->size_ >= size)
+        {
+            i->reserved_ = true;
+            return i->data_.Get();
+        }
+    }
+
+    // Then check if a free buffer can be resized
+    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
+    {
+        if (!i->reserved_)
+        {
+            i->data_ = new unsigned char[size];
+            i->size_ = size;
+            i->reserved_ = true;
+
+            URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(size));
+
+            return i->data_.Get();
+        }
+    }
+
+    // Finally allocate a new buffer
+    ScratchBuffer newBuffer;
+    newBuffer.data_ = new unsigned char[size];
+    newBuffer.size_ = size;
+    newBuffer.reserved_ = true;
+    scratchBuffers_.Push(newBuffer);
+    return newBuffer.data_.Get();
+
+    URHO3D_LOGDEBUG("Allocated scratch buffer with size " + String(size));
+}
+
+void Graphics::FreeScratchBuffer(void* buffer)
+{
+    if (!buffer)
+        return;
+
+    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
+    {
+        if (i->reserved_ && i->data_.Get() == buffer)
+        {
+            i->reserved_ = false;
+            return;
+        }
+    }
+
+    URHO3D_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
+}
+
+void Graphics::CleanupScratchBuffers()
+{
+    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
+    {
+        if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
+        {
+            i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
+            i->size_ = maxScratchBufferRequest_;
+
+            URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
+        }
+    }
+
+    maxScratchBufferRequest_ = 0;
+}
+
+void Graphics::CreateWindowIcon()
+{
+    if (windowIcon_)
+    {
+        SDL_Surface* surface = windowIcon_->GetSDLSurface();
+        if (surface)
+        {
+            SDL_SetWindowIcon(impl_->window_, surface);
+            SDL_FreeSurface(surface);
+        }
+    }
+}
+
+void RegisterGraphicsLibrary(Context* context)
+{
+    Animation::RegisterObject(context);
+    Material::RegisterObject(context);
+    Model::RegisterObject(context);
+    Shader::RegisterObject(context);
+    Technique::RegisterObject(context);
+    Texture2D::RegisterObject(context);
+    Texture2DArray::RegisterObject(context);
+    Texture3D::RegisterObject(context);
+    TextureCube::RegisterObject(context);
+    Camera::RegisterObject(context);
+    Drawable::RegisterObject(context);
+    Light::RegisterObject(context);
+    StaticModel::RegisterObject(context);
+    StaticModelGroup::RegisterObject(context);
+    Skybox::RegisterObject(context);
+    AnimatedModel::RegisterObject(context);
+    AnimationController::RegisterObject(context);
+    BillboardSet::RegisterObject(context);
+    ParticleEffect::RegisterObject(context);
+    ParticleEmitter::RegisterObject(context);
+    RibbonTrail::RegisterObject(context);
+    CustomGeometry::RegisterObject(context);
+    DecalSet::RegisterObject(context);
+    Terrain::RegisterObject(context);
+    TerrainPatch::RegisterObject(context);
+    DebugRenderer::RegisterObject(context);
+    Octree::RegisterObject(context);
+    Zone::RegisterObject(context);
+}
+
+}

+ 721 - 7
Source/Urho3D/Graphics/Graphics.h

@@ -22,10 +22,724 @@
 
 
 #pragma once
 #pragma once
 
 
-#if defined(URHO3D_OPENGL)
-#include "OpenGL/OGLGraphics.h"
-#elif defined(URHO3D_D3D11)
-#include "Direct3D11/D3D11Graphics.h"
-#else
-#include "Direct3D9/D3D9Graphics.h"
-#endif
+#include "../Container/ArrayPtr.h"
+#include "../Container/HashSet.h"
+#include "../Core/Mutex.h"
+#include "../Core/Object.h"
+#include "../Graphics/GraphicsDefs.h"
+#include "../Graphics/ShaderVariation.h"
+#include "../Math/Color.h"
+#include "../Math/Plane.h"
+#include "../Math/Rect.h"
+#include "../Resource/Image.h"
+
+namespace Urho3D
+{
+
+class ConstantBuffer;
+class File;
+class Image;
+class IndexBuffer;
+class GPUObject;
+class GraphicsImpl;
+class RenderSurface;
+class Shader;
+class ShaderPrecache;
+class ShaderProgram;
+class ShaderVariation;
+class Texture;
+class Texture2D;
+class Texture2DArray;
+class TextureCube;
+class Vector3;
+class Vector4;
+class VertexBuffer;
+class VertexDeclaration;
+
+struct ShaderParameter;
+
+/// CPU-side scratch buffer for vertex data updates.
+struct ScratchBuffer
+{
+    ScratchBuffer() :
+        size_(0),
+        reserved_(false)
+    {
+    }
+
+    /// Buffer data.
+    SharedArrayPtr<unsigned char> data_;
+    /// Data size.
+    unsigned size_;
+    /// Reserved flag.
+    bool reserved_;
+};
+
+/// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
+class URHO3D_API Graphics : public Object
+{
+    URHO3D_OBJECT(Graphics, Object);
+
+public:
+    /// Construct.
+    Graphics(Context* context);
+    /// Destruct. Release the Direct3D11 device and close the window.
+    virtual ~Graphics();
+
+    /// Set external window handle. Only effective before setting the initial screen mode.
+    void SetExternalWindow(void* window);
+    /// Set window title.
+    void SetWindowTitle(const String& windowTitle);
+    /// Set window icon.
+    void SetWindowIcon(Image* windowIcon);
+    /// Set window position. Sets initial position if window is not created yet.
+    void SetWindowPosition(const IntVector2& position);
+    /// Set window position. Sets initial position if window is not created yet.
+    void SetWindowPosition(int x, int y);
+    /// Set screen mode. Return true if successful.
+    bool SetMode
+        (int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync, bool tripleBuffer,
+            int multiSample);
+    /// Set screen resolution only. Return true if successful.
+    bool SetMode(int width, int height);
+    /// Set whether the main window uses sRGB conversion on write.
+    void SetSRGB(bool enable);
+    /// Set whether to flush the GPU command buffer to prevent multiple frames being queued and uneven frame timesteps. Default off, may decrease performance if enabled. Not currently implemented on OpenGL.
+    void SetFlushGPU(bool enable);
+    /// Set forced use of OpenGL 2 even if OpenGL 3 is available. Must be called before setting the screen mode for the first time. Default false. No effect on Direct3D9 & 11.
+    void SetForceGL2(bool enable);
+    /// Set allowed screen orientations as a space-separated list of "LandscapeLeft", "LandscapeRight", "Portrait" and "PortraitUpsideDown". Affects currently only iOS platform.
+    void SetOrientations(const String& orientations);
+    /// Toggle between full screen and windowed mode. Return true if successful.
+    bool ToggleFullscreen();
+    /// Close the window.
+    void Close();
+    /// Take a screenshot. Return true if successful.
+    bool TakeScreenShot(Image& destImage);
+    /// Begin frame rendering. Return true if device available and can render.
+    bool BeginFrame();
+    /// End frame rendering and swap buffers.
+    void EndFrame();
+    /// Clear any or all of rendertarget, depth buffer and stencil buffer.
+    void Clear(unsigned flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, unsigned stencil = 0);
+    /// Resolve multisampled backbuffer to a texture rendertarget. The texture's size should match the viewport size.
+    bool ResolveToTexture(Texture2D* destination, const IntRect& viewport);
+    /// Draw non-indexed geometry.
+    void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
+    /// Draw indexed geometry.
+    void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
+    /// Draw indexed geometry with vertex index offset.
+    void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount);
+    /// Draw indexed, instanced geometry. An instancing vertex buffer must be set.
+    void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
+        unsigned instanceCount);
+    /// Draw indexed, instanced geometry with vertex index offset.
+    void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex,
+        unsigned vertexCount, unsigned instanceCount);
+    /// Set vertex buffer.
+    void SetVertexBuffer(VertexBuffer* buffer);
+    /// Set multiple vertex buffers.
+    bool SetVertexBuffers(const PODVector<VertexBuffer*>& buffers, unsigned instanceOffset = 0);
+    /// Set multiple vertex buffers.
+    bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, unsigned instanceOffset = 0);
+    /// Set index buffer.
+    void SetIndexBuffer(IndexBuffer* buffer);
+    /// Set shaders.
+    void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
+    /// Set shader float constants.
+    void SetShaderParameter(StringHash param, const float* data, unsigned count);
+    /// Set shader float constant.
+    void SetShaderParameter(StringHash param, float value);
+    /// Set shader boolean constant.
+    void SetShaderParameter(StringHash param, bool value);
+    /// Set shader color constant.
+    void SetShaderParameter(StringHash param, const Color& color);
+    /// Set shader 2D vector constant.
+    void SetShaderParameter(StringHash param, const Vector2& vector);
+    /// Set shader 3x3 matrix constant.
+    void SetShaderParameter(StringHash param, const Matrix3& matrix);
+    /// Set shader 3D vector constant.
+    void SetShaderParameter(StringHash param, const Vector3& vector);
+    /// Set shader 4x4 matrix constant.
+    void SetShaderParameter(StringHash param, const Matrix4& matrix);
+    /// Set shader 4D vector constant.
+    void SetShaderParameter(StringHash param, const Vector4& vector);
+    /// Set shader 3x4 matrix constant.
+    void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
+    /// Set shader constant from a variant. Supported variant types: bool, float, vector2, vector3, vector4, color.
+    void SetShaderParameter(StringHash param, const Variant& value);
+    /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
+    bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
+    /// Check whether a shader parameter exists on the currently set shaders.
+    bool HasShaderParameter(StringHash param);
+    /// Check whether the current vertex or pixel shader uses a texture unit.
+    bool HasTextureUnit(TextureUnit unit);
+    /// Clear remembered shader parameter source group.
+    void ClearParameterSource(ShaderParameterGroup group);
+    /// Clear remembered shader parameter sources.
+    void ClearParameterSources();
+    /// Clear remembered transform shader parameter sources.
+    void ClearTransformSources();
+    /// Set texture.
+    void SetTexture(unsigned index, Texture* texture);
+    /// Bind texture unit 0 for update. Called by Texture. Used only on OpenGL.
+    void SetTextureForUpdate(Texture* texture);
+    /// Set default texture filtering mode.
+    void SetDefaultTextureFilterMode(TextureFilterMode mode);
+    /// Set texture anisotropy.
+    void SetTextureAnisotropy(unsigned level);
+    /// Dirty texture parameters of all textures (when global settings change.)
+    void SetTextureParametersDirty();
+    /// Reset all rendertargets, depth-stencil surface and viewport.
+    void ResetRenderTargets();
+    /// Reset specific rendertarget.
+    void ResetRenderTarget(unsigned index);
+    /// Reset depth-stencil surface.
+    void ResetDepthStencil();
+    /// Set rendertarget.
+    void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
+    /// Set rendertarget.
+    void SetRenderTarget(unsigned index, Texture2D* texture);
+    /// Set depth-stencil surface.
+    void SetDepthStencil(RenderSurface* depthStencil);
+    /// Set depth-stencil surface.
+    void SetDepthStencil(Texture2D* texture);
+    /// Set viewport.
+    void SetViewport(const IntRect& rect);
+    /// Set blending mode.
+    void SetBlendMode(BlendMode mode);
+    /// Set color write on/off.
+    void SetColorWrite(bool enable);
+    /// Set hardware culling mode.
+    void SetCullMode(CullMode mode);
+    /// Set depth bias.
+    void SetDepthBias(float constantBias, float slopeScaledBias);
+    /// Set depth compare.
+    void SetDepthTest(CompareMode mode);
+    /// Set depth write on/off.
+    void SetDepthWrite(bool enable);
+    /// Set polygon fill mode.
+    void SetFillMode(FillMode mode);
+    /// Set scissor test.
+    void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
+    /// Set scissor test.
+    void SetScissorTest(bool enable, const IntRect& rect);
+    /// Set stencil test.
+    void SetStencilTest
+        (bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP,
+            unsigned stencilRef = 0, unsigned compareMask = M_MAX_UNSIGNED, unsigned writeMask = M_MAX_UNSIGNED);
+    /// Set a custom clipping plane. The plane is specified in world space, but is dependent on the view and projection matrices.
+    void SetClipPlane(bool enable, const Plane& clipPlane = Plane::UP, const Matrix3x4& view = Matrix3x4::IDENTITY,
+        const Matrix4& projection = Matrix4::IDENTITY);
+    /// Begin dumping shader variation names to an XML file for precaching.
+    void BeginDumpShaders(const String& fileName);
+    /// End dumping shader variations names.
+    void EndDumpShaders();
+    /// Precache shader variations from an XML file generated with BeginDumpShaders().
+    void PrecacheShaders(Deserializer& source);
+
+    /// Return whether rendering initialized.
+    bool IsInitialized() const;
+
+    /// Return graphics implementation, which holds the actual API-specific resources.
+    GraphicsImpl* GetImpl() const { return impl_; }
+
+    /// Return OS-specific external window handle. Null if not in use.
+    void* GetExternalWindow() const { return externalWindow_; }
+
+    /// Return window title.
+    const String& GetWindowTitle() const { return windowTitle_; }
+
+    /// Return graphics API name.
+    const String& GetApiName() const { return apiName_; }
+
+    /// Return window position.
+    IntVector2 GetWindowPosition() const;
+
+    /// Return window width in pixels.
+    int GetWidth() const { return width_; }
+
+    /// Return window height in pixels.
+    int GetHeight() const { return height_; }
+
+    /// Return multisample mode (1 = no multisampling.)
+    int GetMultiSample() const { return multiSample_; }
+
+    /// Return whether window is fullscreen.
+    bool GetFullscreen() const { return fullscreen_; }
+
+    /// Return whether window is borderless.
+    bool GetBorderless() const { return borderless_; }
+
+    /// Return whether window is resizable.
+    bool GetResizable() const { return resizable_; }
+
+    /// Return whether window is high DPI.
+    bool GetHighDPI() const { return highDPI_; }
+
+    /// Return whether vertical sync is on.
+    bool GetVSync() const { return vsync_; }
+
+    /// Return whether triple buffering is enabled.
+    bool GetTripleBuffer() const { return tripleBuffer_; }
+
+    /// Return whether the main window is using sRGB conversion on write.
+    bool GetSRGB() const { return sRGB_; }
+
+    /// Return whether the GPU command buffer is flushed each frame.
+    bool GetFlushGPU() const { return flushGPU_; }
+
+    /// Return whether OpenGL 2 use is forced. Effective only on OpenGL.
+    bool GetForceGL2() const { return forceGL2_; }
+
+    /// Return allowed screen orientations.
+    const String& GetOrientations() const { return orientations_; }
+
+    /// Return whether graphics context is lost and can not render or load GPU resources.
+    bool IsDeviceLost() const;
+
+    /// Return number of primitives drawn this frame.
+    unsigned GetNumPrimitives() const { return numPrimitives_; }
+
+    /// Return number of batches drawn this frame.
+    unsigned GetNumBatches() const { return numBatches_; }
+
+    /// Return dummy color texture format for shadow maps. Is "NULL" (consume no video memory) if supported.
+    unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
+
+    /// Return shadow map depth texture format, or 0 if not supported.
+    unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
+
+    /// Return 24-bit shadow map depth texture format, or 0 if not supported.
+    unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
+
+    /// Return whether hardware instancing is supported.
+    bool GetInstancingSupport() const { return instancingSupport_; }
+
+    /// Return whether light pre-pass rendering is supported.
+    bool GetLightPrepassSupport() const { return lightPrepassSupport_; }
+
+    /// Return whether deferred rendering is supported.
+    bool GetDeferredSupport() const { return deferredSupport_; }
+
+    /// Return whether anisotropic texture filtering is supported.
+    bool GetAnisotropySupport() const { return anisotropySupport_; }
+
+    /// Return whether shadow map depth compare is done in hardware.
+    bool GetHardwareShadowSupport() const { return hardwareShadowSupport_; }
+
+    /// Return whether a readable hardware depth format is available.
+    bool GetReadableDepthSupport() const { return GetReadableDepthFormat() != 0; }
+
+    /// Return whether sRGB conversion on texture sampling is supported.
+    bool GetSRGBSupport() const { return sRGBSupport_; }
+
+    /// Return whether sRGB conversion on rendertarget writing is supported.
+    bool GetSRGBWriteSupport() const { return sRGBWriteSupport_; }
+
+    /// Return supported fullscreen resolutions. Will be empty if listing the resolutions is not supported on the platform (e.g. Web).
+    PODVector<IntVector2> GetResolutions() const;
+    /// Return supported multisampling levels.
+    PODVector<int> GetMultiSampleLevels() const;
+    /// Return the desktop resolution.
+    IntVector2 GetDesktopResolution() const;
+    /// Return hardware format for a compressed image format, or 0 if unsupported.
+    unsigned GetFormat(CompressedFormat format) const;
+    /// Return a shader variation by name and defines.
+    ShaderVariation* GetShader(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
+    /// Return a shader variation by name and defines.
+    ShaderVariation* GetShader(ShaderType type, const char* name, const char* defines) const;
+    /// Return current vertex buffer by index.
+    VertexBuffer* GetVertexBuffer(unsigned index) const;
+
+    /// Return current index buffer.
+    IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
+
+    /// Return current vertex shader.
+    ShaderVariation* GetVertexShader() const { return vertexShader_; }
+
+    /// Return current pixel shader.
+    ShaderVariation* GetPixelShader() const { return pixelShader_; }
+
+    /// Return shader program. This is an API-specific class and should not be used by applications.
+    ShaderProgram* GetShaderProgram() const;
+
+    /// Return texture unit index by name.
+    TextureUnit GetTextureUnit(const String& name);
+    /// Return texture unit name by index.
+    const String& GetTextureUnitName(TextureUnit unit);
+    /// Return current texture by texture unit index.
+    Texture* GetTexture(unsigned index) const;
+
+    /// Return default texture filtering mode.
+    TextureFilterMode GetDefaultTextureFilterMode() const { return defaultTextureFilterMode_; }
+
+    /// Return current rendertarget by index.
+    RenderSurface* GetRenderTarget(unsigned index) const;
+
+    /// Return current depth-stencil surface.
+    RenderSurface* GetDepthStencil() const { return depthStencil_; }
+
+    /// Return the viewport coordinates.
+    IntRect GetViewport() const { return viewport_; }
+
+    /// Return texture anisotropy.
+    unsigned GetTextureAnisotropy() const { return textureAnisotropy_; }
+
+    /// Return blending mode.
+    BlendMode GetBlendMode() const { return blendMode_; }
+
+    /// Return whether color write is enabled.
+    bool GetColorWrite() const { return colorWrite_; }
+
+    /// Return hardware culling mode.
+    CullMode GetCullMode() const { return cullMode_; }
+
+    /// Return depth constant bias.
+    float GetDepthConstantBias() const { return constantDepthBias_; }
+
+    /// Return depth slope scaled bias.
+    float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
+
+    /// Return depth compare mode.
+    CompareMode GetDepthTest() const { return depthTestMode_; }
+
+    /// Return whether depth write is enabled.
+    bool GetDepthWrite() const { return depthWrite_; }
+
+    /// Return polygon fill mode.
+    FillMode GetFillMode() const { return fillMode_; }
+
+    /// Return whether stencil test is enabled.
+    bool GetStencilTest() const { return stencilTest_; }
+
+    /// Return whether scissor test is enabled.
+    bool GetScissorTest() const { return scissorTest_; }
+
+    /// Return scissor rectangle coordinates.
+    const IntRect& GetScissorRect() const { return scissorRect_; }
+
+    /// Return stencil compare mode.
+    CompareMode GetStencilTestMode() const { return stencilTestMode_; }
+
+    /// Return stencil operation to do if stencil test passes.
+    StencilOp GetStencilPass() const { return stencilPass_; }
+
+    /// Return stencil operation to do if stencil test fails.
+    StencilOp GetStencilFail() const { return stencilFail_; }
+
+    /// Return stencil operation to do if depth compare fails.
+    StencilOp GetStencilZFail() const { return stencilZFail_; }
+
+    /// Return stencil reference value.
+    unsigned GetStencilRef() const { return stencilRef_; }
+
+    /// Return stencil compare bitmask.
+    unsigned GetStencilCompareMask() const { return stencilCompareMask_; }
+
+    /// Return stencil write bitmask.
+    unsigned GetStencilWriteMask() const { return stencilWriteMask_; }
+
+    /// Return whether a custom clipping plane is in use.
+    bool GetUseClipPlane() const { return useClipPlane_; }
+
+    /// Return current rendertarget width and height.
+    IntVector2 GetRenderTargetDimensions() const;
+
+    /// Window was resized through user interaction. Called by Input subsystem.
+    void OnWindowResized();
+    /// Window was moved through user interaction. Called by Input subsystem.
+    void OnWindowMoved();
+    /// Restore GPU objects and reinitialize state. Requires an open window. Used only on OpenGL.
+    void Restore();
+    /// Maximize the window.
+    void Maximize();
+    /// Minimize the window.
+    void Minimize();
+    /// Add a GPU object to keep track of. Called by GPUObject.
+    void AddGPUObject(GPUObject* object);
+    /// Remove a GPU object. Called by GPUObject.
+    void RemoveGPUObject(GPUObject* object);
+    /// Reserve a CPU-side scratch buffer.
+    void* ReserveScratchBuffer(unsigned size);
+    /// Free a CPU-side scratch buffer.
+    void FreeScratchBuffer(void* buffer);
+    /// Clean up too large scratch buffers.
+    void CleanupScratchBuffers();
+    /// Clean up shader parameters when a shader variation is released or destroyed.
+    void CleanupShaderPrograms(ShaderVariation* variation);
+    /// Clean up a render surface from all FBOs. Used only on OpenGL.
+    void CleanupRenderSurface(RenderSurface* surface);
+    /// Get or create a constant buffer. Will be shared between shaders if possible.
+    ConstantBuffer* GetOrCreateConstantBuffer(ShaderType type, unsigned index, unsigned size);
+    /// Mark the FBO needing an update. Used only on OpenGL.
+    void MarkFBODirty();
+    /// Bind a VBO, avoiding redundant operation. Used only on OpenGL.
+    void SetVBO(unsigned object);
+    /// Bind a UBO, avoiding redundant operation. Used only on OpenGL.
+    void SetUBO(unsigned object);
+
+    /// Return the API-specific alpha texture format.
+    static unsigned GetAlphaFormat();
+    /// Return the API-specific luminance texture format.
+    static unsigned GetLuminanceFormat();
+    /// Return the API-specific luminance alpha texture format.
+    static unsigned GetLuminanceAlphaFormat();
+    /// Return the API-specific RGB texture format.
+    static unsigned GetRGBFormat();
+    /// Return the API-specific RGBA texture format.
+    static unsigned GetRGBAFormat();
+    /// Return the API-specific RGBA 16-bit texture format.
+    static unsigned GetRGBA16Format();
+    /// Return the API-specific RGBA 16-bit float texture format.
+    static unsigned GetRGBAFloat16Format();
+    /// Return the API-specific RGBA 32-bit float texture format.
+    static unsigned GetRGBAFloat32Format();
+    /// Return the API-specific RG 16-bit texture format.
+    static unsigned GetRG16Format();
+    /// Return the API-specific RG 16-bit float texture format.
+    static unsigned GetRGFloat16Format();
+    /// Return the API-specific RG 32-bit float texture format.
+    static unsigned GetRGFloat32Format();
+    /// Return the API-specific single channel 16-bit float texture format.
+    static unsigned GetFloat16Format();
+    /// Return the API-specific single channel 32-bit float texture format.
+    static unsigned GetFloat32Format();
+    /// Return the API-specific linear depth texture format.
+    static unsigned GetLinearDepthFormat();
+    /// Return the API-specific hardware depth-stencil texture format.
+    static unsigned GetDepthStencilFormat();
+    /// Return the API-specific readable hardware depth format, or 0 if not supported.
+    static unsigned GetReadableDepthFormat();
+    /// Return the API-specific texture format from a textual description, for example "rgb".
+    static unsigned GetFormat(const String& formatName);
+
+    /// Return UV offset required for pixel perfect rendering.
+    static const Vector2& GetPixelUVOffset() { return pixelUVOffset; }
+
+    /// Return maximum number of supported bones for skinning.
+    static unsigned GetMaxBones();
+    /// Return whether is using an OpenGL 3 context. Return always false on Direct3D9 & Direct3D11.
+    static bool GetGL3Support();
+
+private:
+    /// Create the application window.
+    bool OpenWindow(int width, int height, bool resizable, bool borderless);
+    /// Create the application window icon.
+    void CreateWindowIcon();
+    /// Adjust the window for new resolution and fullscreen mode.
+    void AdjustWindow(int& newWidth, int& newHeight, bool& newFullscreen, bool& newBorderless);
+    /// Create the Direct3D11 device and swap chain. Requires an open window. Can also be called again to recreate swap chain. Return true on success.
+    bool CreateDevice(int width, int height, int multiSample);
+    /// Update Direct3D11 swap chain state for a new mode and create views for the backbuffer & default depth buffer. Return true on success.
+    bool UpdateSwapChain(int width, int height);
+    /// Create the Direct3D9 interface.
+    bool CreateInterface();
+    /// Create the Direct3D9 device.
+    bool CreateDevice(unsigned adapter, unsigned deviceType);
+    /// Reset the Direct3D9 device.
+    void ResetDevice();
+    /// Notify all GPU resources so they can release themselves as needed. Used only on Direct3D9.
+    void OnDeviceLost();
+    /// Notify all GPU resources so they can recreate themselves as needed. Used only on Direct3D9.
+    void OnDeviceReset();
+    /// Set vertex buffer stream frequency. Used only on Direct3D9.
+    void SetStreamFrequency(unsigned index, unsigned frequency);
+    /// Reset stream frequencies. Used only on Direct3D9.
+    void ResetStreamFrequencies();
+    /// Check supported rendering features.
+    void CheckFeatureSupport();
+    /// Reset cached rendering state.
+    void ResetCachedState();
+    /// Initialize texture unit mappings.
+    void SetTextureUnitMappings();
+    /// Process dirtied state before draw.
+    void PrepareDraw();
+    /// Create intermediate texture for multisampled backbuffer resolve. No-op if already exists.
+    void CreateResolveTexture();
+    /// Clean up all framebuffers. Called when destroying the context. Used only on OpenGL.
+    void CleanupFramebuffers();
+    /// Create a framebuffer using either extension or core functionality. Used only on OpenGL.
+    unsigned CreateFramebuffer();
+    /// Delete a framebuffer using either extension or core functionality. Used only on OpenGL.
+    void DeleteFramebuffer(unsigned fbo);
+    /// Bind a framebuffer using either extension or core functionality. Used only on OpenGL.
+    void BindFramebuffer(unsigned fbo);
+    /// Bind a framebuffer color attachment using either extension or core functionality. Used only on OpenGL.
+    void BindColorAttachment(unsigned index, unsigned target, unsigned object);
+    /// Bind a framebuffer depth attachment using either extension or core functionality. Used only on OpenGL.
+    void BindDepthAttachment(unsigned object, bool isRenderBuffer);
+    /// Bind a framebuffer stencil attachment using either extension or core functionality. Used only on OpenGL.
+    void BindStencilAttachment(unsigned object, bool isRenderBuffer);
+    /// Check FBO completeness using either extension or core functionality. Used only on OpenGL.
+    bool CheckFramebuffer();
+    /// Set vertex attrib divisor. No-op if unsupported. Used only on OpenGL.
+    void SetVertexAttribDivisor(unsigned location, unsigned divisor);
+    /// Release/clear GPU objects and optionally close the window. Used only on OpenGL.
+    void Release(bool clearGPUObjects, bool closeWindow);
+
+    /// Mutex for accessing the GPU objects vector from several threads.
+    Mutex gpuObjectMutex_;
+    /// Implementation.
+    GraphicsImpl* impl_;
+    /// Window title.
+    String windowTitle_;
+    /// Window icon image.
+    WeakPtr<Image> windowIcon_;
+    /// External window, null if not in use (default.)
+    void* externalWindow_;
+    /// Window width in pixels.
+    int width_;
+    /// Window height in pixels.
+    int height_;
+    /// Window position.
+    IntVector2 position_;
+    /// Multisampling mode.
+    int multiSample_;
+    /// Fullscreen flag.
+    bool fullscreen_;
+    /// Borderless flag.
+    bool borderless_;
+    /// Resizable flag.
+    bool resizable_;
+    /// High DPI flag.
+    bool highDPI_;
+    /// Vertical sync flag.
+    bool vsync_;
+    /// Triple buffering flag.
+    bool tripleBuffer_;
+    /// Flush GPU command buffer flag.
+    bool flushGPU_;
+    /// Force OpenGL 2 flag. Only used on OpenGL.
+    bool forceGL2_;
+    /// sRGB conversion on write flag for the main window.
+    bool sRGB_;
+    /// Light pre-pass rendering support flag.
+    bool lightPrepassSupport_;
+    /// Deferred rendering support flag.
+    bool deferredSupport_;
+    /// Anisotropic filtering support flag.
+    bool anisotropySupport_;
+    /// DXT format support flag.
+    bool dxtTextureSupport_;
+    /// ETC1 format support flag.
+    bool etcTextureSupport_;
+    /// PVRTC formats support flag.
+    bool pvrtcTextureSupport_;
+    /// Hardware shadow map depth compare support flag.
+    bool hardwareShadowSupport_;
+    /// Instancing support flag.
+    bool instancingSupport_;
+    /// sRGB conversion on read support flag.
+    bool sRGBSupport_;
+    /// sRGB conversion on write support flag.
+    bool sRGBWriteSupport_;
+    /// Number of primitives this frame.
+    unsigned numPrimitives_;
+    /// Number of batches this frame.
+    unsigned numBatches_;
+    /// Largest scratch buffer request this frame.
+    unsigned maxScratchBufferRequest_;
+    /// GPU objects.
+    PODVector<GPUObject*> gpuObjects_;
+    /// Scratch buffers.
+    Vector<ScratchBuffer> scratchBuffers_;
+    /// Shadow map dummy color texture format.
+    unsigned dummyColorFormat_;
+    /// Shadow map depth texture format.
+    unsigned shadowMapFormat_;
+    /// Shadow map 24-bit depth texture format.
+    unsigned hiresShadowMapFormat_;
+    /// Vertex buffers in use.
+    VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS];
+    /// Index buffer in use.
+    IndexBuffer* indexBuffer_;
+    /// Current vertex declaration hash.
+    unsigned long long vertexDeclarationHash_;
+    /// Current primitive type.
+    unsigned primitiveType_;
+    /// Vertex shader in use.
+    ShaderVariation* vertexShader_;
+    /// Pixel shader in use.
+    ShaderVariation* pixelShader_;
+    /// Textures in use.
+    Texture* textures_[MAX_TEXTURE_UNITS];
+    /// Texture unit mappings.
+    HashMap<String, TextureUnit> textureUnits_;
+    /// Rendertargets in use.
+    RenderSurface* renderTargets_[MAX_RENDERTARGETS];
+    /// Depth-stencil surface in use.
+    RenderSurface* depthStencil_;
+    /// Viewport coordinates.
+    IntRect viewport_;
+    /// Default texture filtering mode.
+    TextureFilterMode defaultTextureFilterMode_;
+    /// Texture anisotropy level.
+    unsigned textureAnisotropy_;
+    /// Blending mode.
+    BlendMode blendMode_;
+    /// Color write enable.
+    bool colorWrite_;
+    /// Hardware culling mode.
+    CullMode cullMode_;
+    /// Depth constant bias.
+    float constantDepthBias_;
+    /// Depth slope scaled bias.
+    float slopeScaledDepthBias_;
+    /// Depth compare mode.
+    CompareMode depthTestMode_;
+    /// Depth write enable flag.
+    bool depthWrite_;
+    /// Polygon fill mode.
+    FillMode fillMode_;
+    /// Scissor test rectangle.
+    IntRect scissorRect_;
+    /// Scissor test enable flag.
+    bool scissorTest_;
+    /// Stencil test compare mode.
+    CompareMode stencilTestMode_;
+    /// Stencil operation on pass.
+    StencilOp stencilPass_;
+    /// Stencil operation on fail.
+    StencilOp stencilFail_;
+    /// Stencil operation on depth fail.
+    StencilOp stencilZFail_;
+    /// Stencil test reference value.
+    unsigned stencilRef_;
+    /// Stencil compare bitmask.
+    unsigned stencilCompareMask_;
+    /// Stencil write bitmask.
+    unsigned stencilWriteMask_;
+    /// Current custom clip plane in post-projection space.
+    Vector4 clipPlane_;
+    /// Stencil test enable flag.
+    bool stencilTest_;
+    /// Custom clip plane enable flag.
+    bool useClipPlane_;
+    /// Remembered shader parameter sources.
+    const void* shaderParameterSources_[MAX_SHADER_PARAMETER_GROUPS];
+    /// Base directory for shaders.
+    String shaderPath_;
+    /// File extension for shaders.
+    String shaderExtension_;
+    /// Last used shader in shader variation query.
+    mutable WeakPtr<Shader> lastShader_;
+    /// Last used shader name in shader variation query.
+    mutable String lastShaderName_;
+    /// Shader precache utility.
+    SharedPtr<ShaderPrecache> shaderPrecache_;
+    /// Allowed screen orientations.
+    String orientations_;
+    /// Graphics API name.
+    String apiName_;
+
+    /// Pixel perfect UV offset.
+    static const Vector2 pixelUVOffset;
+    /// OpenGL3 support flag.
+    static bool gl3Support;
+};
+
+/// Register Graphics library objects.
+void URHO3D_API RegisterGraphicsLibrary(Context* context);
+
+}

+ 32 - 274
Source/Urho3D/Graphics/OpenGL/OGLGraphics.cpp

@@ -26,40 +26,18 @@
 #include "../../Core/Mutex.h"
 #include "../../Core/Mutex.h"
 #include "../../Core/ProcessUtils.h"
 #include "../../Core/ProcessUtils.h"
 #include "../../Core/Profiler.h"
 #include "../../Core/Profiler.h"
-#include "../../Graphics/AnimatedModel.h"
-#include "../../Graphics/Animation.h"
-#include "../../Graphics/AnimationController.h"
-#include "../../Graphics/BillboardSet.h"
-#include "../../Graphics/Camera.h"
 #include "../../Graphics/ConstantBuffer.h"
 #include "../../Graphics/ConstantBuffer.h"
-#include "../../Graphics/CustomGeometry.h"
-#include "../../Graphics/DebugRenderer.h"
-#include "../../Graphics/DecalSet.h"
 #include "../../Graphics/Graphics.h"
 #include "../../Graphics/Graphics.h"
 #include "../../Graphics/GraphicsEvents.h"
 #include "../../Graphics/GraphicsEvents.h"
 #include "../../Graphics/GraphicsImpl.h"
 #include "../../Graphics/GraphicsImpl.h"
 #include "../../Graphics/IndexBuffer.h"
 #include "../../Graphics/IndexBuffer.h"
-#include "../../Graphics/Material.h"
-#include "../../Graphics/Octree.h"
-#include "../../Graphics/ParticleEffect.h"
-#include "../../Graphics/ParticleEmitter.h"
-#include "../../Graphics/RibbonTrail.h"
 #include "../../Graphics/RenderSurface.h"
 #include "../../Graphics/RenderSurface.h"
 #include "../../Graphics/Shader.h"
 #include "../../Graphics/Shader.h"
 #include "../../Graphics/ShaderPrecache.h"
 #include "../../Graphics/ShaderPrecache.h"
 #include "../../Graphics/ShaderProgram.h"
 #include "../../Graphics/ShaderProgram.h"
 #include "../../Graphics/ShaderVariation.h"
 #include "../../Graphics/ShaderVariation.h"
-#include "../../Graphics/Skybox.h"
-#include "../../Graphics/StaticModelGroup.h"
-#include "../../Graphics/Technique.h"
-#include "../../Graphics/Terrain.h"
-#include "../../Graphics/TerrainPatch.h"
 #include "../../Graphics/Texture2D.h"
 #include "../../Graphics/Texture2D.h"
-#include "../../Graphics/Texture2DArray.h"
-#include "../../Graphics/Texture3D.h"
-#include "../../Graphics/TextureCube.h"
 #include "../../Graphics/VertexBuffer.h"
 #include "../../Graphics/VertexBuffer.h"
-#include "../../Graphics/Zone.h"
 #include "../../IO/File.h"
 #include "../../IO/File.h"
 #include "../../IO/Log.h"
 #include "../../IO/Log.h"
 #include "../../Resource/ResourceCache.h"
 #include "../../Resource/ResourceCache.h"
@@ -304,41 +282,6 @@ Graphics::~Graphics()
     SDL_Quit();
     SDL_Quit();
 }
 }
 
 
-void Graphics::SetExternalWindow(void* window)
-{
-    if (!impl_->window_)
-        externalWindow_ = window;
-    else
-        URHO3D_LOGERROR("Window already opened, can not set external window");
-}
-
-void Graphics::SetWindowTitle(const String& windowTitle)
-{
-    windowTitle_ = windowTitle;
-    if (impl_->window_)
-        SDL_SetWindowTitle(impl_->window_, windowTitle_.CString());
-}
-
-void Graphics::SetWindowIcon(Image* windowIcon)
-{
-    windowIcon_ = windowIcon;
-    if (impl_->window_)
-        CreateWindowIcon();
-}
-
-void Graphics::SetWindowPosition(const IntVector2& position)
-{
-    if (impl_->window_)
-        SDL_SetWindowPosition(impl_->window_, position.x_, position.y_);
-    else
-        position_ = position; // Sets as initial position for future window creation
-}
-
-void Graphics::SetWindowPosition(int x, int y)
-{
-    SetWindowPosition(IntVector2(x, y));
-}
-
 bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync,
 bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync,
     bool tripleBuffer, int multiSample)
     bool tripleBuffer, int multiSample)
 {
 {
@@ -622,17 +565,6 @@ void Graphics::SetForceGL2(bool enable)
     forceGL2_ = enable;
     forceGL2_ = enable;
 }
 }
 
 
-void Graphics::SetOrientations(const String& orientations)
-{
-    orientations_ = orientations.Trimmed();
-    SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
-}
-
-bool Graphics::ToggleFullscreen()
-{
-    return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, highDPI_, vsync_, tripleBuffer_, multiSample_);
-}
-
 void Graphics::Close()
 void Graphics::Close()
 {
 {
     if (!IsInitialized())
     if (!IsInitialized())
@@ -1174,6 +1106,29 @@ void Graphics::SetShaderParameter(StringHash param, float value)
     }
     }
 }
 }
 
 
+void Graphics::SetShaderParameter(StringHash param, bool value)
+{
+    // \todo Not tested
+
+    if (impl_->shaderProgram_)
+    {
+        const ShaderParameter* info = impl_->shaderProgram_->GetParameter(param);
+        if (info)
+        {
+            if (info->bufferPtr_)
+            {
+                ConstantBuffer* buffer = info->bufferPtr_;
+                if (!buffer->IsDirty())
+                    impl_->dirtyConstantBuffers_.Push(buffer);
+                buffer->SetParameter(info->offset_, sizeof(bool), &value);
+                return;
+            }
+
+            glUniform1i(info->location_, (int)value);
+        }
+    }
+}
+
 void Graphics::SetShaderParameter(StringHash param, const Color& color)
 void Graphics::SetShaderParameter(StringHash param, const Color& color)
 {
 {
     SetShaderParameter(param, color.Data(), 4);
     SetShaderParameter(param, color.Data(), 4);
@@ -1971,46 +1926,6 @@ bool Graphics::IsDeviceLost() const
     return impl_->context_ == 0;
     return impl_->context_ == 0;
 }
 }
 
 
-IntVector2 Graphics::GetWindowPosition() const
-{
-    if (impl_->window_)
-        return position_;
-    return IntVector2::ZERO;
-}
-
-PODVector<IntVector2> Graphics::GetResolutions() const
-{
-    PODVector<IntVector2> ret;
-    // Emscripten is not able to return a valid list
-#ifndef __EMSCRIPTEN__
-    unsigned numModes = (unsigned)SDL_GetNumDisplayModes(0);
-
-    for (unsigned i = 0; i < numModes; ++i)
-    {
-        SDL_DisplayMode mode;
-        SDL_GetDisplayMode(0, i, &mode);
-        int width = mode.w;
-        int height = mode.h;
-
-        // Store mode if unique
-        bool unique = true;
-        for (unsigned j = 0; j < ret.Size(); ++j)
-        {
-            if (ret[j].x_ == width && ret[j].y_ == height)
-            {
-                unique = false;
-                break;
-            }
-        }
-
-        if (unique)
-            ret.Push(IntVector2(width, height));
-    }
-#endif
-
-    return ret;
-}
-
 PODVector<int> Graphics::GetMultiSampleLevels() const
 PODVector<int> Graphics::GetMultiSampleLevels() const
 {
 {
     PODVector<int> ret;
     PODVector<int> ret;
@@ -2021,18 +1936,6 @@ PODVector<int> Graphics::GetMultiSampleLevels() const
     return ret;
     return ret;
 }
 }
 
 
-IntVector2 Graphics::GetDesktopResolution() const
-{
-#if !defined(__ANDROID__) && !defined(IOS)
-    SDL_DisplayMode mode;
-    SDL_GetDesktopDisplayMode(0, &mode);
-    return IntVector2(mode.w, mode.h);
-#else
-    // SDL_GetDesktopDisplayMode() may not work correctly on mobile platforms. Rather return the window size
-    return IntVector2(width_, height_);
-#endif
-}
-
 unsigned Graphics::GetFormat(CompressedFormat format) const
 unsigned Graphics::GetFormat(CompressedFormat format) const
 {
 {
     switch (format)
     switch (format)
@@ -2082,6 +1985,11 @@ unsigned Graphics::GetMaxBones()
 #endif
 #endif
 }
 }
 
 
+bool Graphics::GetGL3Support()
+{
+    return gl3Support;
+}
+
 ShaderVariation* Graphics::GetShader(ShaderType type, const String& name, const String& defines) const
 ShaderVariation* Graphics::GetShader(ShaderType type, const String& name, const String& defines) const
 {
 {
     return GetShader(type, name.CString(), defines.CString());
     return GetShader(type, name.CString(), defines.CString());
@@ -2222,95 +2130,6 @@ void Graphics::OnWindowMoved()
     SendEvent(E_WINDOWPOS, eventData);
     SendEvent(E_WINDOWPOS, eventData);
 }
 }
 
 
-void Graphics::AddGPUObject(GPUObject* object)
-{
-    MutexLock lock(gpuObjectMutex_);
-
-    gpuObjects_.Push(object);
-}
-
-void Graphics::RemoveGPUObject(GPUObject* object)
-{
-    MutexLock lock(gpuObjectMutex_);
-
-    gpuObjects_.Remove(object);
-}
-
-void* Graphics::ReserveScratchBuffer(unsigned size)
-{
-    if (!size)
-        return 0;
-
-    if (size > maxScratchBufferRequest_)
-        maxScratchBufferRequest_ = size;
-
-    // First check for a free buffer that is large enough
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_ && i->size_ >= size)
-        {
-            i->reserved_ = true;
-            return i->data_.Get();
-        }
-    }
-
-    // Then check if a free buffer can be resized
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_)
-        {
-            i->data_ = new unsigned char[size];
-            i->size_ = size;
-            i->reserved_ = true;
-
-            URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(size));
-
-            return i->data_.Get();
-        }
-    }
-
-    // Finally allocate a new buffer
-    ScratchBuffer newBuffer;
-    newBuffer.data_ = new unsigned char[size];
-    newBuffer.size_ = size;
-    newBuffer.reserved_ = true;
-    scratchBuffers_.Push(newBuffer);
-    return newBuffer.data_.Get();
-}
-
-void Graphics::FreeScratchBuffer(void* buffer)
-{
-    if (!buffer)
-        return;
-
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (i->reserved_ && i->data_.Get() == buffer)
-        {
-            i->reserved_ = false;
-            return;
-        }
-    }
-
-    URHO3D_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
-}
-
-void Graphics::CleanupScratchBuffers()
-{
-    for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
-    {
-        if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
-        {
-            i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
-            i->size_ = maxScratchBufferRequest_;
-
-            URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
-        }
-    }
-
-    maxScratchBufferRequest_ = 0;
-}
-
 void Graphics::CleanupRenderSurface(RenderSurface* surface)
 void Graphics::CleanupRenderSurface(RenderSurface* surface)
 {
 {
     if (!surface)
     if (!surface)
@@ -2372,8 +2191,11 @@ void Graphics::CleanupShaderPrograms(ShaderVariation* variation)
         impl_->shaderProgram_ = 0;
         impl_->shaderProgram_ = 0;
 }
 }
 
 
-ConstantBuffer* Graphics::GetOrCreateConstantBuffer(unsigned bindingIndex, unsigned size)
+ConstantBuffer* Graphics::GetOrCreateConstantBuffer(ShaderType /*type*/,  unsigned bindingIndex, unsigned size)
 {
 {
+    // Note: shaderType parameter is not used on OpenGL, instead binding index should already use the PS range
+    // for PS constant buffers
+
     unsigned key = (bindingIndex << 16) | size;
     unsigned key = (bindingIndex << 16) | size;
     HashMap<unsigned, SharedPtr<ConstantBuffer> >::Iterator i = impl_->allConstantBuffers_.Find(key);
     HashMap<unsigned, SharedPtr<ConstantBuffer> >::Iterator i = impl_->allConstantBuffers_.Find(key);
     if (i == impl_->allConstantBuffers_.End())
     if (i == impl_->allConstantBuffers_.End())
@@ -2555,22 +2377,6 @@ void Graphics::Restore()
     SendEvent(E_DEVICERESET);
     SendEvent(E_DEVICERESET);
 }
 }
 
 
-void Graphics::Maximize()
-{
-    if (!impl_->window_)
-        return;
-
-    SDL_MaximizeWindow(impl_->window_);
-}
-
-void Graphics::Minimize()
-{
-    if (!impl_->window_)
-        return;
-
-    SDL_MinimizeWindow(impl_->window_);
-}
-
 void Graphics::MarkFBODirty()
 void Graphics::MarkFBODirty()
 {
 {
     impl_->fboDirty_ = true;
     impl_->fboDirty_ = true;
@@ -2780,19 +2586,6 @@ unsigned Graphics::GetFormat(const String& formatName)
     return GetRGBFormat();
     return GetRGBFormat();
 }
 }
 
 
-void Graphics::CreateWindowIcon()
-{
-    if (windowIcon_)
-    {
-        SDL_Surface* surface = windowIcon_->GetSDLSurface();
-        if (surface)
-        {
-            SDL_SetWindowIcon(impl_->window_, surface);
-            SDL_FreeSurface(surface);
-        }
-    }
-}
-
 void Graphics::CheckFeatureSupport()
 void Graphics::CheckFeatureSupport()
 {
 {
     // Check supported features: light pre-pass, deferred rendering and hardware depth texture
     // Check supported features: light pre-pass, deferred rendering and hardware depth texture
@@ -3205,10 +2998,7 @@ void Graphics::CleanupFramebuffers()
 void Graphics::ResetCachedState()
 void Graphics::ResetCachedState()
 {
 {
     for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
     for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
-    {
         vertexBuffers_[i] = 0;
         vertexBuffers_[i] = 0;
-        elementMasks_[i] = 0;
-    }
 
 
     for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
     for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
     {
     {
@@ -3408,36 +3198,4 @@ void Graphics::SetVertexAttribDivisor(unsigned location, unsigned divisor)
 #endif
 #endif
 }
 }
 
 
-void RegisterGraphicsLibrary(Context* context)
-{
-    Animation::RegisterObject(context);
-    Material::RegisterObject(context);
-    Model::RegisterObject(context);
-    Shader::RegisterObject(context);
-    Technique::RegisterObject(context);
-    Texture2D::RegisterObject(context);
-    Texture2DArray::RegisterObject(context);
-    Texture3D::RegisterObject(context);
-    TextureCube::RegisterObject(context);
-    Camera::RegisterObject(context);
-    Drawable::RegisterObject(context);
-    Light::RegisterObject(context);
-    StaticModel::RegisterObject(context);
-    StaticModelGroup::RegisterObject(context);
-    Skybox::RegisterObject(context);
-    AnimatedModel::RegisterObject(context);
-    AnimationController::RegisterObject(context);
-    BillboardSet::RegisterObject(context);
-    ParticleEffect::RegisterObject(context);
-    ParticleEmitter::RegisterObject(context);
-    RibbonTrail::RegisterObject(context);
-    CustomGeometry::RegisterObject(context);
-    DecalSet::RegisterObject(context);
-    Terrain::RegisterObject(context);
-    TerrainPatch::RegisterObject(context);
-    DebugRenderer::RegisterObject(context);
-    Octree::RegisterObject(context);
-    Zone::RegisterObject(context);
-}
-
 }
 }

+ 0 - 715
Source/Urho3D/Graphics/OpenGL/OGLGraphics.h

@@ -1,715 +0,0 @@
-//
-// Copyright (c) 2008-2016 the Urho3D project.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-//
-
-#pragma once
-
-#include "../../Container/ArrayPtr.h"
-#include "../../Core/Mutex.h"
-#include "../../Core/Object.h"
-#include "../../Graphics/GraphicsDefs.h"
-#include "../../Math/Color.h"
-#include "../../Math/Plane.h"
-#include "../../Math/Rect.h"
-#include "../../Resource/Image.h"
-
-namespace Urho3D
-{
-
-class ConstantBuffer;
-class File;
-class Image;
-class IndexBuffer;
-class GPUObject;
-class GraphicsImpl;
-class RenderSurface;
-class Shader;
-class ShaderPrecache;
-class ShaderProgram;
-class ShaderVariation;
-class Texture;
-class Texture2D;
-class Texture2DArray;
-class TextureCube;
-class Vector3;
-class Vector4;
-class VertexBuffer;
-
-static const unsigned NUM_SCREEN_BUFFERS = 2;
-
-/// CPU-side scratch buffer for vertex data updates.
-struct ScratchBuffer
-{
-    ScratchBuffer() :
-        size_(0),
-        reserved_(false)
-    {
-    }
-
-    /// Buffer data.
-    SharedArrayPtr<unsigned char> data_;
-    /// Data size.
-    unsigned size_;
-    /// Reserved flag.
-    bool reserved_;
-};
-
-/// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
-class URHO3D_API Graphics : public Object
-{
-    URHO3D_OBJECT(Graphics, Object);
-
-public:
-    /// Construct.
-    Graphics(Context* context_);
-    /// Destruct. Release the OpenGL context and close the window.
-    virtual ~Graphics();
-
-    /// Set external window handle. Only effective before setting the initial screen mode. On Windows it is necessary to set up OpenGL pixel format manually for the window.
-    void SetExternalWindow(void* window);
-    /// Set window icon.
-    void SetWindowIcon(Image* windowIcon);
-    /// Set window title.
-    void SetWindowTitle(const String& windowTitle);
-    /// Set window position.
-    void SetWindowPosition(const IntVector2& position);
-    /// Set window position.
-    void SetWindowPosition(int x, int y);
-    /// Set screen mode. Return true if successful.
-    bool SetMode
-        (int width, int height, bool fullscreen, bool borderless, bool resizable, bool highDPI, bool vsync, bool tripleBuffer,
-            int multiSample);
-    /// Set screen resolution only. Return true if successful.
-    bool SetMode(int width, int height);
-    /// Set whether the main window uses sRGB conversion on write.
-    void SetSRGB(bool enable);
-    /// Set whether to flush the GPU command buffer to prevent multiple frames being queued and uneven frame timesteps. Not yet implemented on OpenGL.
-    void SetFlushGPU(bool enable);
-    /// Set forced use of OpenGL 2 even if OpenGL 3 is available. Must be called before setting the screen mode for the first time. Default false.
-    void SetForceGL2(bool enable);
-    /// Set allowed screen orientations as a space-separated list of "LandscapeLeft", "LandscapeRight", "Portrait" and "PortraitUpsideDown". Affects currently only iOS platform.
-    void SetOrientations(const String& orientations);
-    /// Toggle between full screen and windowed mode. Return true if successful.
-    bool ToggleFullscreen();
-    /// Close the window.
-    void Close();
-    /// Take a screenshot. Return true if successful.
-    bool TakeScreenShot(Image& destImage);
-    /// Begin frame rendering. Return true if device available and can render.
-    bool BeginFrame();
-    /// End frame rendering and swap buffers.
-    void EndFrame();
-    /// Clear any or all of rendertarget, depth buffer and stencil buffer.
-    void Clear(unsigned flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, unsigned stencil = 0);
-    /// Resolve multisampled backbuffer to a texture rendertarget. The texture's size should match the viewport size.
-    bool ResolveToTexture(Texture2D* destination, const IntRect& viewport);
-    /// Draw non-indexed geometry.
-    void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
-    /// Draw indexed geometry.
-    void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
-    /// Draw indexed geometry with vertex index offset. Only supported on desktop OpenGL 3.2 or greater.
-    void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex, unsigned vertexCount);
-    /// Draw indexed, instanced geometry.
-    void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
-        unsigned instanceCount);
-    /// Draw indexed, instanced geometry with vertex index offset. Only supported on desktop OpenGL 3.2 or greater.
-    void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned baseVertexIndex, unsigned minVertex,
-        unsigned vertexCount, unsigned instanceCount);
-    /// Set vertex buffer.
-    void SetVertexBuffer(VertexBuffer* buffer);
-    /// Set multiple vertex buffers.
-    bool SetVertexBuffers
-        (const PODVector<VertexBuffer*>& buffers, unsigned instanceOffset = 0);
-    /// Set multiple vertex buffers.
-    bool SetVertexBuffers
-        (const Vector<SharedPtr<VertexBuffer> >& buffers, unsigned instanceOffset = 0);
-    /// Set index buffer.
-    void SetIndexBuffer(IndexBuffer* buffer);
-    /// Set shaders.
-    void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
-    /// Set shader float constants.
-    void SetShaderParameter(StringHash param, const float* data, unsigned count);
-    /// Set shader float constant.
-    void SetShaderParameter(StringHash param, float value);
-    /// Set shader color constant.
-    void SetShaderParameter(StringHash param, const Color& color);
-    /// Set shader 2D vector constant.
-    void SetShaderParameter(StringHash param, const Vector2& vector);
-    /// Set shader 3x3 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix3& matrix);
-    /// Set shader 3D vector constant.
-    void SetShaderParameter(StringHash param, const Vector3& vector);
-    /// Set shader 4x4 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix4& matrix);
-    /// Set shader 4D vector constant.
-    void SetShaderParameter(StringHash param, const Vector4& vector);
-    /// Set shader 4x3 matrix constant.
-    void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
-    /// Set shader constant from a variant. Supported variant types: bool, float, vector2, vector3, vector4, color.
-    void SetShaderParameter(StringHash param, const Variant& value);
-    /// Check whether a shader parameter group needs update. Does not actually check whether parameters exist in the shaders.
-    bool NeedParameterUpdate(ShaderParameterGroup group, const void* source);
-    /// Check whether a shader parameter exists on the currently set shaders.
-    bool HasShaderParameter(StringHash param);
-    /// Check whether the current shader program uses a texture unit.
-    bool HasTextureUnit(TextureUnit unit);
-    /// Clear remembered shader parameter source group.
-    void ClearParameterSource(ShaderParameterGroup group);
-    /// Clear remembered shader parameter sources.
-    void ClearParameterSources();
-    /// Clear remembered transform shader parameter sources.
-    void ClearTransformSources();
-    /// Set texture.
-    void SetTexture(unsigned index, Texture* texture);
-    /// Bind texture unit 0 for update. Called by Texture.
-    void SetTextureForUpdate(Texture* texture);
-    /// Set default texture filtering mode.
-    void SetDefaultTextureFilterMode(TextureFilterMode mode);
-    /// Set texture anisotropy.
-    void SetTextureAnisotropy(unsigned level);
-    /// Dirty texture parameters of all textures (when global settings change.)
-    void SetTextureParametersDirty();
-    /// Reset all rendertargets, depth-stencil surface and viewport.
-    void ResetRenderTargets();
-    /// Reset specific rendertarget.
-    void ResetRenderTarget(unsigned index);
-    /// Reset depth-stencil surface.
-    void ResetDepthStencil();
-    /// Set rendertarget.
-    void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
-    /// Set rendertarget.
-    void SetRenderTarget(unsigned index, Texture2D* texture);
-    /// Set depth-stencil surface.
-    void SetDepthStencil(RenderSurface* depthStencil);
-    /// Set depth-stencil surface.
-    void SetDepthStencil(Texture2D* texture);
-    /// Set viewport.
-    void SetViewport(const IntRect& rect);
-    /// Set blending mode.
-    void SetBlendMode(BlendMode mode);
-    /// Set color write on/off.
-    void SetColorWrite(bool enable);
-    /// Set hardware culling mode.
-    void SetCullMode(CullMode mode);
-    /// Set depth bias.
-    void SetDepthBias(float constantBias, float slopeScaledBias);
-    /// Set depth compare.
-    void SetDepthTest(CompareMode mode);
-    /// Set depth write on/off.
-    void SetDepthWrite(bool enable);
-    /// Set polygon fill mode.
-    void SetFillMode(FillMode mode);
-    /// Set scissor test.
-    void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
-    /// Set scissor test.
-    void SetScissorTest(bool enable, const IntRect& rect);
-    /// Set stencil test.
-    void SetStencilTest
-        (bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP,
-            unsigned stencilRef = 0, unsigned compareMask = M_MAX_UNSIGNED, unsigned writeMask = M_MAX_UNSIGNED);
-    /// Set a custom clipping plane. The plane is specified in world space, but is dependent on the view and projection matrices.
-    void SetClipPlane(bool enable, const Plane& clipPlane = Plane::UP, const Matrix3x4& view = Matrix3x4::IDENTITY,
-        const Matrix4& projection = Matrix4::IDENTITY);
-    /// Begin dumping shader variation names to an XML file for precaching.
-    void BeginDumpShaders(const String& fileName);
-    /// End dumping shader variations names.
-    void EndDumpShaders();
-    /// Precache shader variations from an XML file generated with BeginDumpShaders().
-    void PrecacheShaders(Deserializer& source);
-
-    /// Return whether rendering initialized.
-    bool IsInitialized() const;
-
-    /// Return graphics implementation, which holds the actual API-specific resources.
-    GraphicsImpl* GetImpl() const { return impl_; }
-
-    /// Return OS-specific external window handle. Null if not in use.
-    void* GetExternalWindow() const { return externalWindow_; }
-
-    /// Return window title.
-    const String& GetWindowTitle() const { return windowTitle_; }
-
-    /// Return graphics API name.
-    const String& GetApiName() const { return apiName_; }
-
-    /// Return window position.
-    IntVector2 GetWindowPosition() const;
-
-    /// Return window width in pixels.
-    int GetWidth() const { return width_; }
-
-    /// Return window height in pixels.
-    int GetHeight() const { return height_; }
-
-    /// Return multisample mode (1 = no multisampling.)
-    int GetMultiSample() const { return multiSample_; }
-
-    /// Return whether window is fullscreen.
-    bool GetFullscreen() const { return fullscreen_; }
-
-    /// Return whether window is borderless.
-    bool GetBorderless() const { return borderless_; }
-
-    /// Return whether window is resizable.
-    bool GetResizable() const { return resizable_; }
-
-    /// Return whether window is high DPI.
-    bool GetHighDPI() const { return highDPI_; }
-
-    /// Return whether vertical sync is on.
-    bool GetVSync() const { return vsync_; }
-
-    /// Return whether triple buffering is enabled.
-    bool GetTripleBuffer() const { return tripleBuffer_; }
-
-    /// Return whether the main window is using sRGB conversion on write.
-    bool GetSRGB() const { return sRGB_; }
-
-    /// Return whether the GPU command buffer is flushed each frame. Not yet implemented on OpenGL.
-    bool GetFlushGPU() const { return false; }
-
-    /// Return whether OpenGL 2 use is forced.
-    bool GetForceGL2() const { return forceGL2_; }
-
-    /// Return allowed screen orientations.
-    const String& GetOrientations() const { return orientations_; }
-
-    /// Return whether device is lost, and can not yet render.
-    bool IsDeviceLost() const;
-
-    /// Return number of primitives drawn this frame.
-    unsigned GetNumPrimitives() const { return numPrimitives_; }
-
-    /// Return number of batches drawn this frame.
-    unsigned GetNumBatches() const { return numBatches_; }
-
-    /// Return dummy color texture format for shadow maps. 0 if not needed, may be nonzero on OS X to work around an Intel driver issue.
-    unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
-
-    /// Return shadow map depth texture format, or 0 if not supported.
-    unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
-
-    /// Return 24-bit shadow map depth texture format, or 0 if not supported.
-    unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
-
-    /// Return whether hardware instancing is supported.
-    bool GetInstancingSupport() const { return instancingSupport_; }
-
-    /// Return whether light pre-pass rendering is supported.
-    bool GetLightPrepassSupport() const { return lightPrepassSupport_; }
-
-    /// Return whether deferred rendering is supported.
-    bool GetDeferredSupport() const { return deferredSupport_; }
-
-    /// Return whether anisotropic texture filtering is supported.
-    bool GetAnisotropySupport() const { return anisotropySupport_; }
-
-    /// Return whether shadow map depth compare is done in hardware. Always true on OpenGL.
-    bool GetHardwareShadowSupport() const { return true; }
-
-    /// Return whether a readable hardware depth format is available.
-    bool GetReadableDepthSupport() const { return GetReadableDepthFormat() != 0; }
-
-    /// Return whether sRGB conversion on texture sampling is supported.
-    bool GetSRGBSupport() const { return sRGBSupport_; }
-
-    /// Return whether sRGB conversion on rendertarget writing is supported.
-    bool GetSRGBWriteSupport() const { return sRGBWriteSupport_; }
-
-    /// Return supported fullscreen resolutions. Will be empty if listing the resolutions is not supported on the platform (e.g. Web).
-    PODVector<IntVector2> GetResolutions() const;
-    /// Return supported multisampling levels.
-    PODVector<int> GetMultiSampleLevels() const;
-    /// Return the desktop resolution.
-    IntVector2 GetDesktopResolution() const;
-    /// Return hardware format for a compressed image format, or 0 if unsupported.
-    unsigned GetFormat(CompressedFormat format) const;
-    /// Return a shader variation by name and defines.
-    ShaderVariation* GetShader(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
-    /// Return a shader variation by name and defines.
-    ShaderVariation* GetShader(ShaderType type, const char* name, const char* defines) const;
-    /// Return current vertex buffer by index.
-    VertexBuffer* GetVertexBuffer(unsigned index) const;
-
-    /// Return index buffer.
-    IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
-
-    /// Return vertex shader.
-    ShaderVariation* GetVertexShader() const { return vertexShader_; }
-
-    /// Return pixel shader.
-    ShaderVariation* GetPixelShader() const { return pixelShader_; }
-
-    /// Return shader program. This is an API-specific class and should not be used by applications.
-    ShaderProgram* GetShaderProgram() const;
-
-    /// Return texture unit index by name.
-    TextureUnit GetTextureUnit(const String& name);
-    /// Return texture unit name by index.
-    const String& GetTextureUnitName(TextureUnit unit);
-    /// Return texture by texture unit index.
-    Texture* GetTexture(unsigned index) const;
-
-    /// Return default texture filtering mode.
-    TextureFilterMode GetDefaultTextureFilterMode() const { return defaultTextureFilterMode_; }
-
-    /// Return rendertarget by index.
-    RenderSurface* GetRenderTarget(unsigned index) const;
-
-    /// Return depth-stencil surface.
-    RenderSurface* GetDepthStencil() const { return depthStencil_; }
-
-    /// Return readable depth-stencil texture. Not created automatically on OpenGL.
-    Texture2D* GetDepthTexture() const { return 0; }
-
-    /// Return the viewport coordinates.
-    IntRect GetViewport() const { return viewport_; }
-
-    /// Return texture anisotropy.
-    unsigned GetTextureAnisotropy() const { return textureAnisotropy_; }
-
-    /// Return blending mode.
-    BlendMode GetBlendMode() const { return blendMode_; }
-
-    /// Return whether color write is enabled.
-    bool GetColorWrite() const { return colorWrite_; }
-
-    /// Return hardware culling mode.
-    CullMode GetCullMode() const { return cullMode_; }
-
-    /// Return depth constant bias.
-    float GetDepthConstantBias() const { return constantDepthBias_; }
-
-    /// Return depth slope scaled bias.
-    float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
-
-    /// Return depth compare mode.
-    CompareMode GetDepthTest() const { return depthTestMode_; }
-
-    /// Return whether depth write is enabled.
-    bool GetDepthWrite() const { return depthWrite_; }
-
-    /// Return polygon fill mode.
-    FillMode GetFillMode() const { return fillMode_; }
-
-    /// Return whether stencil test is enabled.
-    bool GetStencilTest() const { return stencilTest_; }
-
-    /// Return whether scissor test is enabled.
-    bool GetScissorTest() const { return scissorTest_; }
-
-    /// Return scissor rectangle coordinates.
-    const IntRect& GetScissorRect() const { return scissorRect_; }
-
-    /// Return stencil compare mode.
-    CompareMode GetStencilTestMode() const { return stencilTestMode_; }
-
-    /// Return stencil operation to do if stencil test passes.
-    StencilOp GetStencilPass() const { return stencilPass_; }
-
-    /// Return stencil operation to do if stencil test fails.
-    StencilOp GetStencilFail() const { return stencilFail_; }
-
-    /// Return stencil operation to do if depth compare fails.
-    StencilOp GetStencilZFail() const { return stencilZFail_; }
-
-    /// Return stencil reference value.
-    unsigned GetStencilRef() const { return stencilRef_; }
-
-    /// Return stencil compare bitmask.
-    unsigned GetStencilCompareMask() const { return stencilCompareMask_; }
-
-    /// Return stencil write bitmask.
-    unsigned GetStencilWriteMask() const { return stencilWriteMask_; }
-
-    /// Return whether a custom clipping plane is in use.
-    bool GetUseClipPlane() const { return useClipPlane_; }
-
-    /// Return rendertarget width and height.
-    IntVector2 GetRenderTargetDimensions() const;
-
-    /// Window was resized through user interaction. Called by Input subsystem.
-    void OnWindowResized();
-    /// Window was moved through user interaction. Called by Input subsystem.
-    void OnWindowMoved();
-    /// Add a GPU object to keep track of. Called by GPUObject.
-    void AddGPUObject(GPUObject* object);
-    /// Remove a GPU object. Called by GPUObject.
-    void RemoveGPUObject(GPUObject* object);
-    /// Reserve a CPU-side scratch buffer.
-    void* ReserveScratchBuffer(unsigned size);
-    /// Free a CPU-side scratch buffer.
-    void FreeScratchBuffer(void* buffer);
-    /// Clean up too large scratch buffers.
-    void CleanupScratchBuffers();
-    /// Clean up a render surface from all FBOs.
-    void CleanupRenderSurface(RenderSurface* surface);
-    /// Clean up shader programs when a shader variation is released or destroyed.
-    void CleanupShaderPrograms(ShaderVariation* variation);
-    /// Reserve a constant buffer.
-    ConstantBuffer* GetOrCreateConstantBuffer(unsigned bindingIndex, unsigned size);
-    /// Release/clear GPU objects and optionally close the window.
-    void Release(bool clearGPUObjects, bool closeWindow);
-    /// Restore GPU objects and reinitialize state. Requires an open window.
-    void Restore();
-    /// Maximize the Window.
-    void Maximize();
-    /// Minimize the Window.
-    void Minimize();
-    /// Mark the FBO needing an update.
-    void MarkFBODirty();
-    /// Bind a VBO, avoiding redundant operation.
-    void SetVBO(unsigned object);
-    /// Bind a UBO, avoiding redundant operation.
-    void SetUBO(unsigned object);
-
-    /// Return the API-specific alpha texture format.
-    static unsigned GetAlphaFormat();
-    /// Return the API-specific luminance texture format.
-    static unsigned GetLuminanceFormat();
-    /// Return the API-specific luminance alpha texture format.
-    static unsigned GetLuminanceAlphaFormat();
-    /// Return the API-specific RGB texture format.
-    static unsigned GetRGBFormat();
-    /// Return the API-specific RGBA texture format.
-    static unsigned GetRGBAFormat();
-    /// Return the API-specific RGBA 16-bit texture format.
-    static unsigned GetRGBA16Format();
-    /// Return the API-specific RGBA 16-bit float texture format.
-    static unsigned GetRGBAFloat16Format();
-    /// Return the API-specific RGBA 32-bit float texture format.
-    static unsigned GetRGBAFloat32Format();
-    /// Return the API-specific RG 16-bit texture format.
-    static unsigned GetRG16Format();
-    /// Return the API-specific RG 16-bit float texture format.
-    static unsigned GetRGFloat16Format();
-    /// Return the API-specific RG 32-bit float texture format.
-    static unsigned GetRGFloat32Format();
-    /// Return the API-specific single channel 16-bit float texture format.
-    static unsigned GetFloat16Format();
-    /// Return the API-specific single channel 32-bit float texture format.
-    static unsigned GetFloat32Format();
-    /// Return the API-specific linear depth texture format.
-    static unsigned GetLinearDepthFormat();
-    /// Return the API-specific hardware depth-stencil texture format.
-    static unsigned GetDepthStencilFormat();
-    /// Return the API-specific readable hardware depth format, or 0 if not supported.
-    static unsigned GetReadableDepthFormat();
-    /// Return the API-specific texture format from a textual description, for example "rgb".
-    static unsigned GetFormat(const String& formatName);
-
-    /// Return UV offset required for pixel perfect rendering.
-    static const Vector2& GetPixelUVOffset() { return pixelUVOffset; }
-
-    /// Return maximum number of supported bones for skinning.
-    static unsigned GetMaxBones();
-
-    /// Return whether is using an OpenGL 3 context.
-    static bool GetGL3Support() { return gl3Support; }
-
-private:
-    /// Create the application window icon.
-    void CreateWindowIcon();
-    /// Check supported rendering features.
-    void CheckFeatureSupport();
-    /// Prepare for draw call. Update constant buffers and setup the FBO.
-    void PrepareDraw();
-    /// Clean up all framebuffers. Called when destroying the context.
-    void CleanupFramebuffers();
-    /// Reset cached rendering state.
-    void ResetCachedState();
-    /// Initialize texture unit mappings.
-    void SetTextureUnitMappings();
-    /// Create a framebuffer using either extension or core functionality.
-    unsigned CreateFramebuffer();
-    /// Delete a framebuffer using either extension or core functionality.
-    void DeleteFramebuffer(unsigned fbo);
-    /// Bind a framebuffer using either extension or core functionality.
-    void BindFramebuffer(unsigned fbo);
-    /// Bind a framebuffer color attachment using either extension or core functionality.
-    void BindColorAttachment(unsigned index, unsigned target, unsigned object);
-    /// Bind a framebuffer depth attachment using either extension or core functionality.
-    void BindDepthAttachment(unsigned object, bool isRenderBuffer);
-    /// Bind a framebuffer stencil attachment using either extension or core functionality.
-    void BindStencilAttachment(unsigned object, bool isRenderBuffer);
-    /// Check FBO completeness using either extension or core functionality.
-    bool CheckFramebuffer();
-    /// Set vertex attrib divisor. No-op if unsupported.
-    void SetVertexAttribDivisor(unsigned location, unsigned divisor);
-
-    /// Mutex for accessing the GPU objects vector from several threads.
-    Mutex gpuObjectMutex_;
-    /// Implementation.
-    GraphicsImpl* impl_;
-    /// Window title.
-    String windowTitle_;
-    /// Window icon image.
-    WeakPtr<Image> windowIcon_;
-    /// External window, null if not in use (default.)
-    void* externalWindow_;
-    /// Window width in pixels.
-    int width_;
-    /// Window height in pixels.
-    int height_;
-    /// Window position.
-    IntVector2 position_;
-    /// Multisampling mode.
-    int multiSample_;
-    /// Fullscreen flag.
-    bool fullscreen_;
-    /// Borderless flag.
-    bool borderless_;
-    /// Resizable flag.
-    bool resizable_;
-    /// High DPI flag.
-    bool highDPI_;
-    /// Vertical sync flag.
-    bool vsync_;
-    /// Triple buffering flag.
-    bool tripleBuffer_;
-    /// sRGB conversion on write flag for the main window.
-    bool sRGB_;
-    /// Force OpenGL 2 use flag.
-    bool forceGL2_;
-    /// Instancing support flag.
-    bool instancingSupport_;
-    /// Light prepass support flag.
-    bool lightPrepassSupport_;
-    /// Deferred rendering support flag.
-    bool deferredSupport_;
-    /// Anisotropic filtering support flag.
-    bool anisotropySupport_;
-    /// DXT format support flag.
-    bool dxtTextureSupport_;
-    /// ETC1 format support flag.
-    bool etcTextureSupport_;
-    /// PVRTC formats support flag.
-    bool pvrtcTextureSupport_;
-    /// sRGB conversion on read support flag.
-    bool sRGBSupport_;
-    /// sRGB conversion on write support flag.
-    bool sRGBWriteSupport_;
-    /// Number of primitives this frame.
-    unsigned numPrimitives_;
-    /// Number of batches this frame.
-    unsigned numBatches_;
-    /// Largest scratch buffer request this frame.
-    unsigned maxScratchBufferRequest_;
-    /// GPU objects.
-    PODVector<GPUObject*> gpuObjects_;
-    /// Scratch buffers.
-    Vector<ScratchBuffer> scratchBuffers_;
-    /// Shadow map dummy color texture format.
-    unsigned dummyColorFormat_;
-    /// Shadow map depth texture format.
-    unsigned shadowMapFormat_;
-    /// Shadow map 24-bit depth texture format.
-    unsigned hiresShadowMapFormat_;
-    /// Vertex buffers in use.
-    VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS];
-    /// Element mask in use.
-    unsigned elementMasks_[MAX_VERTEX_STREAMS];
-    /// Index buffer in use.
-    IndexBuffer* indexBuffer_;
-    /// Vertex shader in use.
-    ShaderVariation* vertexShader_;
-    /// Pixel shader in use.
-    ShaderVariation* pixelShader_;
-    /// Textures in use.
-    Texture* textures_[MAX_TEXTURE_UNITS];
-
-    /// Texture unit mappings.
-    HashMap<String, TextureUnit> textureUnits_;
-    /// Rendertargets in use.
-    RenderSurface* renderTargets_[MAX_RENDERTARGETS];
-    /// Depth-stencil surface in use.
-    RenderSurface* depthStencil_;
-    /// Viewport coordinates.
-    IntRect viewport_;
-    /// Default texture filtering mode.
-    TextureFilterMode defaultTextureFilterMode_;
-    /// Texture anisotropy level.
-    unsigned textureAnisotropy_;
-    /// Blending mode.
-    BlendMode blendMode_;
-    /// Color write enable.
-    bool colorWrite_;
-    /// Hardware culling mode.
-    CullMode cullMode_;
-    /// Depth constant bias.
-    float constantDepthBias_;
-    /// Depth slope scaled bias.
-    float slopeScaledDepthBias_;
-    /// Depth compare mode.
-    CompareMode depthTestMode_;
-    /// Depth write enable flag.
-    bool depthWrite_;
-    /// Polygon fill mode.
-    FillMode fillMode_;
-    /// Scissor test rectangle.
-    IntRect scissorRect_;
-    /// Scissor test enable flag.
-    bool scissorTest_;
-    /// Current custom clip plane in post-projection space.
-    Vector4 clipPlane_;
-    /// Stencil test compare mode.
-    CompareMode stencilTestMode_;
-    /// Stencil operation on pass.
-    StencilOp stencilPass_;
-    /// Stencil operation on fail.
-    StencilOp stencilFail_;
-    /// Stencil operation on depth fail.
-    StencilOp stencilZFail_;
-    /// Stencil test reference value.
-    unsigned stencilRef_;
-    /// Stencil compare bitmask.
-    unsigned stencilCompareMask_;
-    /// Stencil write bitmask.
-    unsigned stencilWriteMask_;
-    /// Stencil test enable flag.
-    bool stencilTest_;
-    /// Custom clip plane enable flag.
-    bool useClipPlane_;
-    /// Base directory for shaders.
-    String shaderPath_;
-    /// File extension for shaders.
-    String shaderExtension_;
-    /// Last used shader in shader variation query.
-    mutable WeakPtr<Shader> lastShader_;
-    /// Last used shader name in shader variation query.
-    mutable String lastShaderName_;
-    /// Shader precache utility.
-    SharedPtr<ShaderPrecache> shaderPrecache_;
-    /// Allowed screen orientations.
-    String orientations_;
-    /// Graphics API name.
-    String apiName_;
-
-    /// Pixel perfect UV offset.
-    static const Vector2 pixelUVOffset;
-    /// Flag for OpenGL 3 support.
-    static bool gl3Support;
-};
-
-/// Register Graphics library objects.
-void URHO3D_API RegisterGraphicsLibrary(Context* context_);
-
-}

+ 5 - 1
Source/Urho3D/Graphics/OpenGL/OGLShaderProgram.cpp

@@ -239,13 +239,17 @@ bool ShaderProgram::Link()
             unsigned bindingIndex = group;
             unsigned bindingIndex = group;
             // Vertex shader constant buffer bindings occupy slots starting from zero to maximum supported, pixel shader bindings
             // Vertex shader constant buffer bindings occupy slots starting from zero to maximum supported, pixel shader bindings
             // from that point onward
             // from that point onward
+            ShaderType shaderType = VS;
             if (name.Contains("PS", false))
             if (name.Contains("PS", false))
+            {
                 bindingIndex += MAX_SHADER_PARAMETER_GROUPS;
                 bindingIndex += MAX_SHADER_PARAMETER_GROUPS;
+                shaderType = PS;
+            }
 
 
             glUniformBlockBinding(object_.name_, blockIndex, bindingIndex);
             glUniformBlockBinding(object_.name_, blockIndex, bindingIndex);
             blockToBinding[blockIndex] = bindingIndex;
             blockToBinding[blockIndex] = bindingIndex;
 
 
-            constantBuffers_[bindingIndex] = graphics_->GetOrCreateConstantBuffer(bindingIndex, (unsigned)dataSize);
+            constantBuffers_[bindingIndex] = graphics_->GetOrCreateConstantBuffer(shaderType, bindingIndex, (unsigned)dataSize);
         }
         }
     }
     }
 #endif
 #endif