PageRenderTime 72ms CodeModel.GetById 18ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 1ms

/xbmc/guilib/D3DResource.h

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