//********************************** Banshee Engine (www.banshee3d.com) **************************************************// //**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************// #include "Managers/BsTextureManager.h" #include "Error/BsException.h" #include "Image/BsPixelUtil.h" #include "RenderAPI/BsRenderAPI.h" namespace bs { SPtr TextureManager::createTexture(const TEXTURE_DESC& desc) { Texture* tex = new (bs_alloc()) Texture(desc); SPtr ret = bs_core_ptr(tex); ret->_setThisPtr(ret); ret->initialize(); return ret; } SPtr TextureManager::createTexture(const TEXTURE_DESC& desc, const SPtr& pixelData) { Texture* tex = new (bs_alloc()) Texture(desc, pixelData); SPtr ret = bs_core_ptr(tex); ret->_setThisPtr(ret); ret->initialize(); return ret; } SPtr TextureManager::_createEmpty() { Texture* tex = new (bs_alloc()) Texture(); SPtr texture = bs_core_ptr(tex); texture->_setThisPtr(texture); return texture; } SPtr TextureManager::createRenderTexture(const TEXTURE_DESC& colorDesc, bool createDepth, PixelFormat depthStencilFormat) { TEXTURE_DESC textureDesc = colorDesc; textureDesc.usage = TU_RENDERTARGET; textureDesc.numMips = 0; HTexture texture = Texture::create(textureDesc); HTexture depthStencil; if(createDepth) { textureDesc.format = depthStencilFormat; textureDesc.hwGamma = false; textureDesc.usage = TU_DEPTHSTENCIL; depthStencil = Texture::create(textureDesc); } RENDER_TEXTURE_DESC desc; desc.colorSurfaces[0].texture = texture; desc.colorSurfaces[0].face = 0; desc.colorSurfaces[0].numFaces = 1; desc.colorSurfaces[0].mipLevel = 0; desc.depthStencilSurface.texture = depthStencil; desc.depthStencilSurface.face = 0; desc.depthStencilSurface.numFaces = 1; desc.depthStencilSurface.mipLevel = 0; SPtr newRT = createRenderTexture(desc); return newRT; } SPtr TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc) { SPtr newRT = createRenderTextureImpl(desc); newRT->_setThisPtr(newRT); newRT->initialize(); return newRT; } namespace ct { void TextureManager::onStartUp() { TEXTURE_DESC desc; desc.type = TEX_TYPE_2D; desc.width = 2; desc.height = 2; desc.format = PF_RGBA8; desc.usage = TU_STATIC; // White built-in texture SPtr whiteTexture = createTexture(desc); SPtr whitePixelData = PixelData::create(2, 2, 1, PF_RGBA8); whitePixelData->setColorAt(Color::White, 0, 0); whitePixelData->setColorAt(Color::White, 0, 1); whitePixelData->setColorAt(Color::White, 1, 0); whitePixelData->setColorAt(Color::White, 1, 1); whiteTexture->writeData(*whitePixelData); Texture::WHITE = whiteTexture; // Black built-in texture SPtr blackTexture = createTexture(desc); SPtr blackPixelData = PixelData::create(2, 2, 1, PF_RGBA8); blackPixelData->setColorAt(Color::ZERO, 0, 0); blackPixelData->setColorAt(Color::ZERO, 0, 1); blackPixelData->setColorAt(Color::ZERO, 1, 0); blackPixelData->setColorAt(Color::ZERO, 1, 1); blackTexture->writeData(*blackPixelData); Texture::BLACK = blackTexture; // Normal (Y = Up) built-in texture SPtr normalTexture = createTexture(desc); SPtr normalPixelData = PixelData::create(2, 2, 1, PF_RGBA8); Color encodedNormal(0.5f, 0.5f, 1.0f); normalPixelData->setColorAt(encodedNormal, 0, 0); normalPixelData->setColorAt(encodedNormal, 0, 1); normalPixelData->setColorAt(encodedNormal, 1, 0); normalPixelData->setColorAt(encodedNormal, 1, 1); normalTexture->writeData(*normalPixelData); Texture::NORMAL = normalTexture; } void TextureManager::onShutDown() { // Need to make sure these are freed while still on the core thread Texture::WHITE = nullptr; Texture::BLACK = nullptr; Texture::NORMAL = nullptr; } SPtr TextureManager::createTexture(const TEXTURE_DESC& desc, GpuDeviceFlags deviceMask) { SPtr newTex = createTextureInternal(desc, nullptr, deviceMask); newTex->initialize(); return newTex; } SPtr TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx) { SPtr newRT = createRenderTextureInternal(desc, deviceIdx); newRT->initialize(); return newRT; } } }