#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2013 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_GRAPHICS_RASTERIZATION_DIRECT3D11RASTERIZERRESOURCES_H #define NUCLEX_GRAPHICS_RASTERIZATION_DIRECT3D11RASTERIZERRESOURCES_H #include "Nuclex/Graphics/Config.h" #include "Nuclex/Graphics/Rasterization/RasterizerResources.h" #include "Direct3D11Api.h" #include "Direct3D11ObserverManager.h" #include namespace Nuclex { namespace Graphics { namespace Rasterization { // ------------------------------------------------------------------------------------------- // class GenericVertexBuffer; class GenericIndexBuffer; class GenericConstantBuffer; class VertexShader; class PixelShader; class Texture2; class RenderTarget2; class PolygonOptions; class SamplerOptions; class Direct3D11VertexBufferObserver; class Direct3D11IndexBufferObserver; class Direct3D11ConstantBufferObserver; class Direct3D11VertexShaderObserver; class Direct3D11PixelShaderObserver; class Direct3D11Texture2Observer; class Direct3D11RenderTarget2Observer; class Direct3D11PolygonOptionsObserver; class Direct3D11SamplerOptionsObserver; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Graphics::Rasterization namespace Nuclex { namespace Graphics { namespace Rasterization { // ------------------------------------------------------------------------------------------- // ///Controls various settings the rasterizer can be configured with class Direct3D11RasterizerResources : public RasterizerResources { #pragma region class ResourceHolder /// Manages the observers for a specific resource type /// /// API-neutral class for which observers are managed /// /// /// Observers that will be attached to the neutral objects /// private: template class ResourceHolder : public Direct3D11ObserverManager { /// Initializes a new holder for the resource /// Resource manager the resource holder belongs to public: ResourceHolder(Direct3D11RasterizerResources *resources) : Direct3D11ObserverManager(resources) {} /// Frees all resources owned by the resource holder public: ~ResourceHolder() {} /// Kills all observers of the resource holder public: void EvictAll() { std::lock_guard lock(this->mutex); Direct3D11ObserverManager::EvictAll(); } /// Retrieves the resource's attached observer /// /// Resource whose currently attached observer will be returned /// /// The observer currently attached to the specified resource /// /// This method does not lock the mutex and must only be called from the rendering /// thread for resources that are known to be pinned and therefore cannot be /// evicted by another thread while this method executes. /// public: TObserver *Get(TAgnostic *resource) { return Direct3D11ObserverManager::Get(resource); } /// Attaches an observer to the resource or returns its current one /// /// Resource an observer will be attached to or whose current observer will be returned /// /// The observer for the specified resource public: TObserver *Prepare(TAgnostic *resource) { std::lock_guard lock(this->mutex); return Direct3D11ObserverManager::Prepare(resource); } /// Prepares a resource and pins it so it cannot be evicted /// Resource that will be prepared and pinned /// The observer that has been created for the specified resource public: TObserver *PrepareAndPin(TAgnostic *resource) { std::lock_guard lock(this->mutex); TObserver *observer = Direct3D11ObserverManager::Prepare(resource); this->pinnedResources.insert(resource); return observer; } /// Unpins a resource, allowing it to be evicted /// Resource that will be unpinned public: void Unpin(TAgnostic *resource) { std::lock_guard lock(this->mutex); std::set::iterator iterator = this->pinnedResources.find(resource); this->pinnedResources.erase(iterator); } /// Kills the observer for the specified resource /// Resource whose observer will be killed /// Observer that will be killed, if known public: void Evict(TAgnostic *resource, TObserver *observer = nullptr) { std::lock_guard lock(this->mutex); // Never evict a pinned resource if(this->pinnedResources.find(resource) != this->pinnedResources.end()) { return; } Direct3D11ObserverManager::Evict(resource, observer); } /// Retrieves the mutex used to synchronize accesses to the resource public: std::mutex &GetMutex() const { return this->mutex; } /// Resources that must not be evicted because they're in use private: std::set pinnedResources; /// Mutex that has to be used to synchronize acccess to the resource private: mutable std::mutex mutex; }; #pragma endregion // class ResourceHolder /// Initializes a new Direct3D 11 resource controller public: Direct3D11RasterizerResources(); /// Frees all resources owned by the rasterizer resource manager public: virtual ~Direct3D11RasterizerResources(); /// Sets the Direct3D device for which resources will be managed /// Direct3D 11 device resources will be managed for public: void SetDirect3DDevice(const ID3D11DeviceNPtr &device) { this->device = device; } /// Sets the Direct3D device context for which settings will be managed /// Direct3D 11 device context settings will be managed for public: void SetDirect3DDeviceContext(const ID3D11DeviceContextNPtr &context) { this->context = context; } /// Prepares a resource for usage by this rasterizer /// Resource the rasterizer will prepare to use public: void Prepare(const std::shared_ptr &resource); /// Evicts the resource from the rasterizer /// Resource that will be evicted from the rasterizer public: void Evict(const std::shared_ptr &resource); /// Retrieves the Direct3D device the resources are created on /// The Direct3D device used to create the resources public: const ID3D11DeviceNPtr &GetDevice() const { return this->device; } /// Retrieves the Direct3D device context managing the device's state /// The Direct3D device context managing the device's state public: const ID3D11DeviceContextNPtr &GetDeviceContext() const { return this->context; } /// Manages the observers for index buffers public: ResourceHolder IndexBufferHolder; /// Manages the observers for vertex buffers public: ResourceHolder< GenericVertexBuffer, Direct3D11VertexBufferObserver > VertexBufferHolder; /// Manages the observers for constant buffers public: ResourceHolder< GenericConstantBuffer, Direct3D11ConstantBufferObserver > ConstantBufferHolder; /// Manages the observers for vertex shaders public: ResourceHolder VertexShaderHolder; /// Manages the observers for pixel shaders public: ResourceHolder PixelShaderHolder; /// Manages the observers for 2D textures public: ResourceHolder Texture2Holder; /// Manages the observers for render targets public: ResourceHolder RenderTarget2Holder; /// Manages the observers for polygon options public: ResourceHolder< PolygonOptions, Direct3D11PolygonOptionsObserver > PolygonOptionsHolder; /// Manages the observers for sampler options public: ResourceHolder< SamplerOptions, Direct3D11SamplerOptionsObserver > SamplerOptionsHolder; /// Direct3D device the resources will be managed for private: ID3D11DeviceNPtr device; /// Direct3D device context that contains the device's state private: ID3D11DeviceContextNPtr context; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Graphics::Rasterization #endif // NUCLEX_GRAPHICS_RASTERIZATION_DIRECT3D11RASTERIZERRESOURCES_H