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