#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 // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_GRAPHICS_SOURCE 1 #include "Direct3D11Texture2Observer.h" #include "../Direct3D11RasterizerResources.h" #include "../Direct3D11Converters.h" #include "Nuclex/Graphics/Rasterization/PixelFormat.h" #include "Nuclex/Graphics/Rasterization/Usage.h" #include namespace { // ------------------------------------------------------------------------------------------- // /// Counts the number of mip map levels required for the texture /// Width of the texture /// Height of the texture /// The number of mip map levels the texture requires std::size_t CountRequiredMipMaps(std::size_t width, std::size_t height) { std::size_t requiredMipMapCount = 0; while((width > 0) && (height > 0)) { ++requiredMipMapCount; width >>= 1; height >>= 1; } return requiredMipMapCount; } // ------------------------------------------------------------------------------------------- // } // anonymous namespace namespace Nuclex { namespace Graphics { namespace Rasterization { // ------------------------------------------------------------------------------------------- // Direct3D11Texture2Observer::Direct3D11Texture2Observer( Direct3D11RasterizerResources &resources, Texture2 *texture2 ) : resources(resources), texture2(texture2) { // TODO: Disabled mip maps because there are no auto-generated mip maps // Unsure whether I should generate mipmaps in software (since hardware // generation requires access to the Direct3D device from the wrong thread) // or expose mipmaps to the user. Or both. D3D11_TEXTURE2D_DESC textureDescription; textureDescription.Width = static_cast(texture2->GetWidth()); textureDescription.Height = static_cast(texture2->GetHeight()); textureDescription.MipLevels = 1; // 0 = auto textureDescription.ArraySize = 1; // could be higher for cubemaps, etc. textureDescription.Format = direct3DFormatFromPixelFormat(texture2->GetPixelFormat()); textureDescription.SampleDesc.Count = 1; textureDescription.SampleDesc.Quality = 0; textureDescription.Usage = direct3DUsageFromUsage(texture2->GetUsage()); textureDescription.BindFlags = D3D11_BIND_SHADER_RESOURCE; textureDescription.CPUAccessFlags = direct3DCpuAccessFromUsage(texture2->GetUsage()); textureDescription.MiscFlags = D3D11_RESOURCE_MISC_FLAG(0); D3D11_SUBRESOURCE_DATA initialData; initialData.pSysMem = texture2->AccessMemory(); initialData.SysMemPitch = static_cast( CountBitsPerPixel(texture2->GetPixelFormat()) * texture2->GetWidth() / 8 ); initialData.SysMemSlicePitch = 0; HRESULT resultHandle = resources.GetDevice()->CreateTexture2D( &textureDescription, &initialData, &this->direct3DTexture ); if(FAILED(resultHandle)) { throw std::runtime_error("Could not create Direct3D 11 texture"); } D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDescription; resourceViewDescription.Format = textureDescription.Format; resourceViewDescription.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; resourceViewDescription.Texture2D.MipLevels = 1; resourceViewDescription.Texture2D.MostDetailedMip = 0; resultHandle = resources.GetDevice()->CreateShaderResourceView( this->direct3DTexture, &resourceViewDescription, &this->direct3DResourceView ); if(FAILED(resultHandle)) { throw std::runtime_error( "Could not create Direct3D 11 shader resource view for texture" ); } } // ------------------------------------------------------------------------------------------- // void Direct3D11Texture2Observer::Destroying() { this->resources.Texture2Holder.Evict(this->texture2, this); // Do not do anything below Evict(). It will delete this instance! } // ------------------------------------------------------------------------------------------- // void Direct3D11Texture2Observer::Cleared() { throw std::runtime_error("Not implemented yet"); } // ------------------------------------------------------------------------------------------- // void Direct3D11Texture2Observer::Changed(const Box &) { throw std::runtime_error("Not implemented yet"); } // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Graphics::Rasterization