xbmc
D3DResource.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "GUIColorManager.h"
12 #include "utils/ColorUtils.h"
13 #include "utils/Geometry.h"
14 
15 #include <map>
16 
17 #include <set>
18 #include <d3dx11effect.h>
19 #include <DirectXMath.h>
20 #include <d3dx11effect.h>
21 #include <wrl/client.h>
22 
23 #define KODI_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT 4
24 
25 typedef enum SHADER_METHOD {
26  SHADER_METHOD_RENDER_DEFAULT,
27  SHADER_METHOD_RENDER_TEXTURE_NOBLEND,
28  SHADER_METHOD_RENDER_FONT,
29  SHADER_METHOD_RENDER_TEXTURE_BLEND,
30  SHADER_METHOD_RENDER_MULTI_TEXTURE_BLEND,
31  SHADER_METHOD_RENDER_STEREO_INTERLACED_LEFT,
32  SHADER_METHOD_RENDER_STEREO_INTERLACED_RIGHT,
33  SHADER_METHOD_RENDER_STEREO_CHECKERBOARD_LEFT,
34  SHADER_METHOD_RENDER_STEREO_CHECKERBOARD_RIGHT,
35  SHADER_METHOD_RENDER_COUNT
36 } _SHADER_METHOD;
37 
39 {
40 public:
41  virtual ~ID3DResource() {}
42 
43  virtual void OnDestroyDevice(bool fatal)=0;
44  virtual void OnCreateDevice()=0;
45 
46 protected:
47  void Register();
48  void Unregister();
49 
50  bool m_bRegistered = false;
51 };
52 
54 {
55 public:
56  static inline void XMStoreColor(float* floats, DWORD dword)
57  {
58  floats[0] = float((dword >> 16) & 0xFF) * (1.0f / 255.0f); // r
59  floats[1] = float((dword >> 8) & 0xFF) * (1.0f / 255.0f); // g
60  floats[2] = float((dword >> 0) & 0xFF) * (1.0f / 255.0f); // b
61  floats[3] = float((dword >> 24) & 0xFF) * (1.0f / 255.0f); // a
62  }
63 
64  static inline void XMStoreColor(DirectX::XMFLOAT4* floats, DWORD dword)
65  {
66  XMStoreColor(reinterpret_cast<float*>(floats), dword);
67  }
68 
69  static inline void XMStoreColor(float* floats, unsigned char a, unsigned char r, unsigned char g, unsigned char b)
70  {
71  floats[0] = r * (1.0f / 255.0f);
72  floats[1] = g * (1.0f / 255.0f);
73  floats[2] = b * (1.0f / 255.0f);
74  floats[3] = a * (1.0f / 255.0f);
75  }
76 
77  static inline void XMStoreColor(DirectX::XMFLOAT4* floats, unsigned char a, unsigned char r, unsigned char g, unsigned char b)
78  {
79  XMStoreColor(reinterpret_cast<float*>(floats), a, r, g, b);
80  }
81 
82  // helper function to properly "clear" shader resources
83  static inline void PSClearShaderResources(ID3D11DeviceContext* pContext)
84  {
85  ID3D11ShaderResourceView* shader_resource_views[KODI_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] = {};
86  pContext->PSSetShaderResources(0, ARRAYSIZE(shader_resource_views), shader_resource_views);
87  }
88 
89  static size_t BitsPerPixel(DXGI_FORMAT fmt);
90 };
91 
92 class CD3DTexture : public ID3DResource
93 {
94 public:
95  CD3DTexture();
96  virtual ~CD3DTexture();
97 
98  bool Create(UINT width, UINT height, UINT mipLevels, D3D11_USAGE usage, DXGI_FORMAT format, const void* pInitData = nullptr, unsigned int srcPitch = 0);
99 
100  void Release();
101  bool GetDesc(D3D11_TEXTURE2D_DESC *desc) const;
102  bool LockRect(UINT subresource, D3D11_MAPPED_SUBRESOURCE *res, D3D11_MAP mapType) const;
103  bool UnlockRect(UINT subresource) const;
104 
105  // Accessors
106  ID3D11Texture2D* Get() const { return m_texture.Get(); }
107  ID3D11ShaderResourceView* GetShaderResource(DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN);
108  ID3D11ShaderResourceView** GetAddressOfSRV(DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN);
109  ID3D11RenderTargetView* GetRenderTarget();
110  ID3D11RenderTargetView** GetAddressOfRTV();
111  UINT GetWidth() const { return m_width; }
112  UINT GetHeight() const { return m_height; }
113  DXGI_FORMAT GetFormat() const { return m_format; }
114  void GenerateMipmaps();
115 
116  // static methods
117  static void DrawQuad(const CPoint points[4],
118  UTILS::COLOR::Color color,
119  CD3DTexture* texture,
120  const CRect* texCoords,
121  SHADER_METHOD options = SHADER_METHOD_RENDER_TEXTURE_BLEND);
122 
123  static void DrawQuad(const CPoint points[4],
124  UTILS::COLOR::Color color,
125  unsigned numViews,
126  ID3D11ShaderResourceView** view,
127  const CRect* texCoords,
128  SHADER_METHOD options = SHADER_METHOD_RENDER_TEXTURE_BLEND);
129 
130  static void DrawQuad(const CRect& coords,
131  UTILS::COLOR::Color color,
132  CD3DTexture* texture,
133  const CRect* texCoords,
134  SHADER_METHOD options = SHADER_METHOD_RENDER_TEXTURE_BLEND);
135 
136  static void DrawQuad(const CRect& coords,
137  UTILS::COLOR::Color color,
138  unsigned numViews,
139  ID3D11ShaderResourceView** view,
140  const CRect* texCoords,
141  SHADER_METHOD options = SHADER_METHOD_RENDER_TEXTURE_BLEND);
142 
143  void OnDestroyDevice(bool fatal) override;
144  void OnCreateDevice() override;
145 
146 protected:
147  ID3D11RenderTargetView* GetRenderTargetInternal(unsigned idx = 0);
148  unsigned int GetMemoryUsage(unsigned int pitch) const;
149  bool CreateInternal(const void* pInitData = nullptr, unsigned int srcPitch = 0);
150 
151  void SaveTexture();
152  void RestoreTexture();
153 
154  // saved data
155  BYTE* m_data;
156  // creation parameters
157  UINT m_width;
158  UINT m_height;
159  UINT m_mipLevels;
160  UINT m_pitch;
161  UINT m_bindFlags;
162  UINT m_cpuFlags;
163  UINT m_viewIdx;
164  D3D11_USAGE m_usage;
165  DXGI_FORMAT m_format;
166 
167  // created texture
168  Microsoft::WRL::ComPtr<ID3D11Texture2D> m_texture;
169  Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_renderTargets[2];
170  // store views in different formats
171  std::map<DXGI_FORMAT, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>> m_views;
172 };
173 
174 typedef std::map<std::string, std::string> DefinesMap;
175 
176 class CD3DEffect : public ID3DResource, public ID3DInclude
177 {
178 public:
179  CD3DEffect();
180  virtual ~CD3DEffect();
181  bool Create(const std::string &effectString, DefinesMap* defines);
182  void Release();
183  bool SetFloatArray(LPCSTR handle, const float* val, unsigned int count);
184  bool SetMatrix(LPCSTR handle, const float* mat);
185  bool SetTechnique(LPCSTR handle);
186  bool SetTexture(LPCSTR handle, CD3DTexture &texture);
187  bool SetTexture(LPCSTR handle, ID3D11ShaderResourceView* resourceView);
188  bool SetResources(LPCSTR handle, ID3D11ShaderResourceView** ppSRViews, size_t count);
189  bool SetConstantBuffer(LPCSTR handle, ID3D11Buffer *buffer);
190  bool SetScalar(LPCSTR handle, float value);
191  void AddIncludePath(const std::string& includePath);
192  bool Begin(UINT *passes, DWORD flags);
193  bool BeginPass(UINT pass);
194  bool EndPass();
195  bool End();
196 
197  ID3DX11Effect* Get() const { return m_effect.Get(); }
198 
199  void OnDestroyDevice(bool fatal) override;
200  void OnCreateDevice() override;
201 
202  // ID3DInclude interface
203  __declspec(nothrow) HRESULT __stdcall Open(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes) override;
204  __declspec(nothrow) HRESULT __stdcall Close(LPCVOID pData) override;
205 
206 private:
207  bool CreateEffect();
208 
209  std::string m_effectString;
210  DefinesMap m_defines;
211  Microsoft::WRL::ComPtr<ID3DX11Effect> m_effect;
212  Microsoft::WRL::ComPtr<ID3DX11EffectTechnique> m_techniquie;
213  Microsoft::WRL::ComPtr<ID3DX11EffectPass> m_currentPass;
214  std::set<std::string> m_includePaths;
215 };
216 
217 class CD3DBuffer : public ID3DResource
218 {
219 public:
220  CD3DBuffer();
221  virtual ~CD3DBuffer();
222  bool Create(D3D11_BIND_FLAG type, UINT count, UINT stride, DXGI_FORMAT format, D3D11_USAGE usage, const void* initData = nullptr);
223  bool Map(void** resource);
224  bool Unmap();
225  void Release();
226  unsigned int GetStride() { return m_stride; }
227  DXGI_FORMAT GetFormat() { return m_format; }
228  ID3D11Buffer* Get() const { return m_buffer.Get(); }
229 
230  void OnDestroyDevice(bool fatal) override;
231  void OnCreateDevice() override;
232 
233 private:
234  bool CreateBuffer(const void *pData);
235 
236  // saved data
237  BYTE* m_data;
238  UINT m_length;
239  UINT m_stride;
240  UINT m_cpuFlags;
241  DXGI_FORMAT m_format;
242  D3D11_USAGE m_usage;
243  D3D11_BIND_FLAG m_type;
244  Microsoft::WRL::ComPtr<ID3D11Buffer> m_buffer;
245 };
246 
248 {
249 public:
251  ~CD3DVertexShader();
252 
253  bool Create(const std::wstring& vertexFile, D3D11_INPUT_ELEMENT_DESC* vertexLayout, unsigned int vertexLayoutSize);
254  bool Create(const void* code, size_t codeLength, D3D11_INPUT_ELEMENT_DESC* vertexLayout, unsigned int vertexLayoutSize);
255  void ReleaseShader();
256  void BindShader();
257  void UnbindShader();
258  void Release();
259  bool IsInited() { return m_inited; }
260 
261  void OnDestroyDevice(bool fatal) override;
262  void OnCreateDevice() override;
263 
264 private:
265  bool CreateInternal();
266 
267  bool m_inited;
268  unsigned int m_vertexLayoutSize;
269  D3D11_INPUT_ELEMENT_DESC* m_vertexLayout;
270  Microsoft::WRL::ComPtr<ID3DBlob> m_VSBuffer;
271  Microsoft::WRL::ComPtr<ID3D11VertexShader> m_VS;
272  Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
273 };
274 
276 {
277 public:
278  CD3DPixelShader();
279  ~CD3DPixelShader();
280 
281  bool Create(const std::wstring& wstrFile);
282  bool Create(const void* code, size_t codeLength);
283  void ReleaseShader();
284  void BindShader();
285  void UnbindShader();
286  void Release();
287  bool IsInited() { return m_inited; }
288 
289  void OnDestroyDevice(bool fatal) override;
290  void OnCreateDevice() override;
291 
292 private:
293  bool CreateInternal();
294 
295  bool m_inited;
296  Microsoft::WRL::ComPtr<ID3DBlob> m_PSBuffer;
297  Microsoft::WRL::ComPtr<ID3D11PixelShader> m_PS;
298 };
Definition: D3DResource.h:92
Definition: D3DResource.h:176
Definition: D3DResource.h:275
Definition: D3DResource.h:38
Definition: inftrees.h:24
Definition: D3DResource.h:53
Definition: D3DResource.h:247
Definition: D3DResource.h:217