1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Managers/BsTextureManager.h"
4#include "Error/BsException.h"
5#include "Image/BsPixelUtil.h"
6#include "RenderAPI/BsRenderAPI.h"
7
8namespace bs
9{
10 SPtr<Texture> TextureManager::createTexture(const TEXTURE_DESC& desc)
11 {
12 Texture* tex = new (bs_alloc<Texture>()) Texture(desc);
13 SPtr<Texture> ret = bs_core_ptr<Texture>(tex);
14
15 ret->_setThisPtr(ret);
16 ret->initialize();
17
18 return ret;
19 }
20
21 SPtr<Texture> TextureManager::createTexture(const TEXTURE_DESC& desc, const SPtr<PixelData>& pixelData)
22 {
23 Texture* tex = new (bs_alloc<Texture>()) Texture(desc, pixelData);
24 SPtr<Texture> ret = bs_core_ptr<Texture>(tex);
25
26 ret->_setThisPtr(ret);
27 ret->initialize();
28
29 return ret;
30 }
31
32 SPtr<Texture> TextureManager::_createEmpty()
33 {
34 Texture* tex = new (bs_alloc<Texture>()) Texture();
35 SPtr<Texture> texture = bs_core_ptr<Texture>(tex);
36 texture->_setThisPtr(texture);
37
38 return texture;
39 }
40
41 SPtr<RenderTexture> TextureManager::createRenderTexture(const TEXTURE_DESC& colorDesc, bool createDepth,
42 PixelFormat depthStencilFormat)
43 {
44 TEXTURE_DESC textureDesc = colorDesc;
45 textureDesc.usage = TU_RENDERTARGET;
46 textureDesc.numMips = 0;
47
48 HTexture texture = Texture::create(textureDesc);
49
50 HTexture depthStencil;
51 if(createDepth)
52 {
53 textureDesc.format = depthStencilFormat;
54 textureDesc.hwGamma = false;
55 textureDesc.usage = TU_DEPTHSTENCIL;
56
57 depthStencil = Texture::create(textureDesc);
58 }
59
60 RENDER_TEXTURE_DESC desc;
61 desc.colorSurfaces[0].texture = texture;
62 desc.colorSurfaces[0].face = 0;
63 desc.colorSurfaces[0].numFaces = 1;
64 desc.colorSurfaces[0].mipLevel = 0;
65
66 desc.depthStencilSurface.texture = depthStencil;
67 desc.depthStencilSurface.face = 0;
68 desc.depthStencilSurface.numFaces = 1;
69 desc.depthStencilSurface.mipLevel = 0;
70
71 SPtr<RenderTexture> newRT = createRenderTexture(desc);
72
73 return newRT;
74 }
75
76 SPtr<RenderTexture> TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc)
77 {
78 SPtr<RenderTexture> newRT = createRenderTextureImpl(desc);
79 newRT->_setThisPtr(newRT);
80 newRT->initialize();
81
82 return newRT;
83 }
84
85 namespace ct
86 {
87 void TextureManager::onStartUp()
88 {
89 TEXTURE_DESC desc;
90 desc.type = TEX_TYPE_2D;
91 desc.width = 2;
92 desc.height = 2;
93 desc.format = PF_RGBA8;
94 desc.usage = TU_STATIC;
95
96 // White built-in texture
97 SPtr<Texture> whiteTexture = createTexture(desc);
98
99 SPtr<PixelData> whitePixelData = PixelData::create(2, 2, 1, PF_RGBA8);
100 whitePixelData->setColorAt(Color::White, 0, 0);
101 whitePixelData->setColorAt(Color::White, 0, 1);
102 whitePixelData->setColorAt(Color::White, 1, 0);
103 whitePixelData->setColorAt(Color::White, 1, 1);
104
105 whiteTexture->writeData(*whitePixelData);
106 Texture::WHITE = whiteTexture;
107
108 // Black built-in texture
109 SPtr<Texture> blackTexture = createTexture(desc);
110
111 SPtr<PixelData> blackPixelData = PixelData::create(2, 2, 1, PF_RGBA8);
112 blackPixelData->setColorAt(Color::ZERO, 0, 0);
113 blackPixelData->setColorAt(Color::ZERO, 0, 1);
114 blackPixelData->setColorAt(Color::ZERO, 1, 0);
115 blackPixelData->setColorAt(Color::ZERO, 1, 1);
116
117 blackTexture->writeData(*blackPixelData);
118 Texture::BLACK = blackTexture;
119
120 // Normal (Y = Up) built-in texture
121 SPtr<Texture> normalTexture = createTexture(desc);
122 SPtr<PixelData> normalPixelData = PixelData::create(2, 2, 1, PF_RGBA8);
123
124 Color encodedNormal(0.5f, 0.5f, 1.0f);
125 normalPixelData->setColorAt(encodedNormal, 0, 0);
126 normalPixelData->setColorAt(encodedNormal, 0, 1);
127 normalPixelData->setColorAt(encodedNormal, 1, 0);
128 normalPixelData->setColorAt(encodedNormal, 1, 1);
129
130 normalTexture->writeData(*normalPixelData);
131 Texture::NORMAL = normalTexture;
132 }
133
134 void TextureManager::onShutDown()
135 {
136 // Need to make sure these are freed while still on the core thread
137 Texture::WHITE = nullptr;
138 Texture::BLACK = nullptr;
139 Texture::NORMAL = nullptr;
140 }
141
142 SPtr<Texture> TextureManager::createTexture(const TEXTURE_DESC& desc, GpuDeviceFlags deviceMask)
143 {
144 SPtr<Texture> newTex = createTextureInternal(desc, nullptr, deviceMask);
145 newTex->initialize();
146
147 return newTex;
148 }
149
150 SPtr<RenderTexture> TextureManager::createRenderTexture(const RENDER_TEXTURE_DESC& desc,
151 UINT32 deviceIdx)
152 {
153 SPtr<RenderTexture> newRT = createRenderTextureInternal(desc, deviceIdx);
154 newRT->initialize();
155
156 return newRT;
157 }
158 }
159}
160