-
Notifications
You must be signed in to change notification settings - Fork 21
/
VideoTextureNodePub.cpp
322 lines (301 loc) · 12.2 KB
/
VideoTextureNodePub.cpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright (c) 2020-2024 WangBin <wbsecg1 at gmail.com>
* MDK SDK in QtQuick RHI
*/
#include "VideoTextureNode.h"
#include <QQuickWindow>
#if (__APPLE__+0)
#include <Metal/Metal.h>
#endif
#if (_WIN32+0)
#include <d3d11.h>
#include <d3d12.h>
#include <wrl/client.h>
using namespace Microsoft::WRL; //ComPtr
#endif
#if QT_CONFIG(opengl)
#include <QOpenGLFramebufferObject>
#endif
#if __has_include(<vulkan/vulkan_core.h>)
# if QT_CONFIG(vulkan)
#include <QVulkanInstance>
#include <QVulkanFunctions>
# endif
#endif
#include "mdk/Player.h"
#include "mdk/RenderAPI.h"
using namespace std;
#define VK_ENSURE(x, ...) VK_RUN_CHECK(x, return __VA_ARGS__)
#define VK_WARN(x, ...) VK_RUN_CHECK(x)
#define VK_RUN_CHECK(x, ...) do { \
const VkResult __vkret__ = x; \
if (__vkret__ != VK_SUCCESS) { \
qDebug() << #x " ERROR: " << __vkret__ << " @" << __LINE__ << __func__; \
__VA_ARGS__; \
} \
} while(false)
class VideoTextureNodePub final: public VideoTextureNode
{
public:
VideoTextureNodePub(VideoTextureItem *item): VideoTextureNode(item) {}
~VideoTextureNodePub() override {
// release gfx resources
#if QT_CONFIG(opengl)
fbo_gl.reset();
#endif
#if (VK_VERSION_1_0+0) && QT_CONFIG(vulkan)
freeTexture();
#endif
}
private:
QSGTexture* ensureTexture(Player* player, const QSize& size) override;
#if (__APPLE__+0)
id<MTLTexture> m_texture_mtl = nil;
#endif
#if QT_CONFIG(opengl)
unique_ptr<QOpenGLFramebufferObject> fbo_gl;
#endif
#if (_WIN32+0)
ComPtr<ID3D11Texture2D> m_texture_d3d11;
ComPtr<ID3D12Resource> m_texture_d3d12;
#endif
#if (VK_VERSION_1_0+0) && QT_CONFIG(vulkan)
bool buildTexture(const QSize &size);
void freeTexture();
VkImage m_texture_vk = VK_NULL_HANDLE;
VkDeviceMemory m_textureMemory = VK_NULL_HANDLE;
VkPhysicalDevice m_physDev = VK_NULL_HANDLE;
VkDevice m_dev = VK_NULL_HANDLE;
QVulkanDeviceFunctions *m_devFuncs = nullptr;
#endif
};
VideoTextureNode* createNode(VideoTextureItem* item)
{
return new VideoTextureNodePub(item);
}
QSGTexture* VideoTextureNodePub::ensureTexture(Player* player, const QSize& size)
{
if (qgetenv("QSG_RHI_HDR") == "hdr10") {
player->set(ColorSpaceBT2100_PQ, this);
// qsg texture fromNative() only supports rgba8, fortunately works fine with hdr10 on windows
} else {
player->set(ColorSpaceBT709, this);
}
QSGRendererInterface *rif = m_window->rendererInterface();
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
intmax_t nativeObj = 0;
int nativeLayout = 0;
#endif
switch (rif->graphicsApi()) {
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
case QSGRendererInterface::OpenGL: // same as OpenGLRhi in qt6
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
case QSGRendererInterface::OpenGLRhi:
#endif // QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
{
#if QT_CONFIG(opengl)
m_tx = TextureCoordinatesTransformFlag::MirrorVertically;
fbo_gl.reset(new QOpenGLFramebufferObject(size));
GLRenderAPI ra;
ra.fbo = fbo_gl->handle();
player->setRenderAPI(&ra, this);
auto tex = fbo_gl->texture();
# if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
nativeObj = decltype(nativeObj)(tex);
# if QT_VERSION <= QT_VERSION_CHECK(5, 14, 0)
return m_window->createTextureFromId(tex, size);
# endif // QT_VERSION <= QT_VERSION_CHECK(5, 14, 0)
# else
if (tex)
return QNativeInterface::QSGOpenGLTexture::fromNative(tex, m_window, size, QQuickWindow::TextureHasAlphaChannel);
# endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#endif // if QT_CONFIG(opengl)
}
break;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
#if (_WIN32)
case QSGRendererInterface::Direct3D11Rhi: {
auto dev = (ID3D11Device*)rif->getResource(m_window, QSGRendererInterface::DeviceResource);
D3D11_TEXTURE2D_DESC desc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, size.width(), size.height(), 1, 1
, D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET
, D3D11_USAGE_DEFAULT, 0, 1, 0, 0);
if (FAILED(dev->CreateTexture2D(&desc, nullptr, &m_texture_d3d11))) {
}
D3D11RenderAPI ra;
ra.rtv = m_texture_d3d11.Get();
player->setRenderAPI(&ra, this);
# if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
nativeObj = decltype(nativeObj)(m_texture_d3d11.Get());
# else
if (m_texture_d3d11)
return QNativeInterface::QSGD3D11Texture::fromNative(m_texture_d3d11.Get(), m_window, size, QQuickWindow::TextureHasAlphaChannel);
# endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
}
break;
# if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
case QSGRendererInterface::Direct3D12: {
auto dev = (ID3D12Device*)rif->getResource(m_window, QSGRendererInterface::DeviceResource);
D3D12_RESOURCE_DESC desc{};
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
desc.Width = size.width();
desc.Height = size.height();
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
D3D12_HEAP_PROPERTIES prop{};
prop.Type = D3D12_HEAP_TYPE_DEFAULT;
if (HRESULT hr = dev->CreateCommittedResource(&prop, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&m_texture_d3d12)); FAILED(hr)) {
break;
}
D3D12RenderAPI ra{};
ra.cmdQueue = reinterpret_cast<ID3D12CommandQueue*>(rif->getResource(m_window, QSGRendererInterface::CommandQueueResource));
ra.rt = m_texture_d3d12.Get();
player->setRenderAPI(&ra, this);
return QNativeInterface::QSGD3D12Texture::fromNative(ra.rt, D3D12_RESOURCE_STATE_COMMON, m_window, size, QQuickWindow::TextureHasAlphaChannel);
}
break;
# endif
#endif // (_WIN32)
case QSGRendererInterface::MetalRhi: {
#if (__APPLE__+0)
auto dev = (__bridge id<MTLDevice>)rif->getResource(m_window, QSGRendererInterface::DeviceResource);
Q_ASSERT(dev);
MTLTextureDescriptor *desc = [[MTLTextureDescriptor alloc] init];
desc.textureType = MTLTextureType2D;
desc.pixelFormat = MTLPixelFormatRGBA8Unorm;
desc.width = size.width();
desc.height = size.height();
desc.mipmapLevelCount = 1;
desc.resourceOptions = MTLResourceStorageModePrivate;
desc.storageMode = MTLStorageModePrivate;
desc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
m_texture_mtl = [dev newTextureWithDescriptor: desc];
MetalRenderAPI ra{};
ra.texture = (__bridge void*)m_texture_mtl;
ra.device = (__bridge void*)dev;
ra.cmdQueue = rif->getResource(m_window, QSGRendererInterface::CommandQueueResource);
player->setRenderAPI(&ra, this);
# if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
nativeObj = decltype(nativeObj)(ra.texture);
# else
if (m_texture_mtl)
return QNativeInterface::QSGMetalTexture::fromNative(m_texture_mtl, m_window, size, QQuickWindow::TextureHasAlphaChannel);
# endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#endif // (__APPLE__+0)
}
break;
case QSGRendererInterface::VulkanRhi: {
#if (VK_VERSION_1_0+0) && QT_CONFIG(vulkan)
# if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
nativeLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
# endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
auto inst = reinterpret_cast<QVulkanInstance *>(rif->getResource(m_window, QSGRendererInterface::VulkanInstanceResource));
m_physDev = *static_cast<VkPhysicalDevice *>(rif->getResource(m_window, QSGRendererInterface::PhysicalDeviceResource));
auto newDev = *static_cast<VkDevice *>(rif->getResource(m_window, QSGRendererInterface::DeviceResource));
qDebug("old device: %p, newDev: %p", (void*)m_dev, (void*)newDev);
// TODO: why m_dev is 0 if device lost
freeTexture();
m_dev = newDev;
m_devFuncs = inst->deviceFunctions(m_dev);
buildTexture(size);
# if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
nativeObj = decltype(nativeObj)(m_texture_vk);
# endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
VulkanRenderAPI ra{};
ra.device =m_dev;
ra.phy_device = m_physDev;
ra.opaque = this;
ra.rt = m_texture_vk;
ra.renderTargetInfo = [](void* opaque, int* w, int* h, VkFormat* fmt, VkImageLayout* layout) {
auto node = static_cast<VideoTextureNodePub*>(opaque);
*w = node->m_size.width();
*h = node->m_size.height();
*fmt = VK_FORMAT_R8G8B8A8_UNORM;
*layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
return 1;
};
ra.currentCommandBuffer = [](void* opaque){
auto node = static_cast<VideoTextureNodePub*>(opaque);
QSGRendererInterface *rif = node->m_window->rendererInterface();
auto cmdBuf = *static_cast<VkCommandBuffer *>(rif->getResource(node->m_window, QSGRendererInterface::CommandListResource));
return cmdBuf;
};
player->setRenderAPI(&ra, this);
# if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if (m_texture_vk)
return QNativeInterface::QSGVulkanTexture::fromNative(m_texture_vk, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, m_window, size, QQuickWindow::TextureHasAlphaChannel);
# endif // (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
# else
qFatal("Rebuild with Vulkan!");
#endif // (VK_VERSION_1_0+0) && QT_CONFIG(vulkan)
}
break;
#endif //QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
default:
break;
}
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
# if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
if (nativeObj)
return m_window->createTextureFromNativeObject(QQuickWindow::NativeObjectTexture, &nativeObj, nativeLayout, size, QQuickWindow::TextureHasAlphaChannel);
# endif
#endif
return nullptr;
}
#if (VK_VERSION_1_0+0) && QT_CONFIG(vulkan)
bool VideoTextureNodePub::buildTexture(const QSize &size)
{
VkImageCreateInfo imageInfo;
memset(&imageInfo, 0, sizeof(imageInfo));
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.flags = 0;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM; // qtquick hardcoded
imageInfo.extent.width = uint32_t(size.width());
imageInfo.extent.height = uint32_t(size.height());
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
imageInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
imageInfo.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
VkImage image = VK_NULL_HANDLE;
VK_ENSURE(m_devFuncs->vkCreateImage(m_dev, &imageInfo, nullptr, &image), false);
m_texture_vk = image;
VkMemoryRequirements memReq;
m_devFuncs->vkGetImageMemoryRequirements(m_dev, image, &memReq);
quint32 memIndex = 0;
VkPhysicalDeviceMemoryProperties physDevMemProps;
m_window->vulkanInstance()->functions()->vkGetPhysicalDeviceMemoryProperties(m_physDev, &physDevMemProps);
for (uint32_t i = 0; i < physDevMemProps.memoryTypeCount; ++i) {
if (!(memReq.memoryTypeBits & (1 << i)))
continue;
memIndex = i;
}
VkMemoryAllocateInfo allocInfo = {
VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
nullptr,
memReq.size,
memIndex
};
VK_ENSURE(m_devFuncs->vkAllocateMemory(m_dev, &allocInfo, nullptr, &m_textureMemory), false);
VK_ENSURE(m_devFuncs->vkBindImageMemory(m_dev, image, m_textureMemory, 0), false);
return true;
}
void VideoTextureNodePub::freeTexture()
{
if (!m_texture_vk)
return;
VK_WARN(m_devFuncs->vkDeviceWaitIdle(m_dev));
m_devFuncs->vkFreeMemory(m_dev, m_textureMemory, nullptr);
m_textureMemory = VK_NULL_HANDLE;
m_devFuncs->vkDestroyImage(m_dev, m_texture_vk, nullptr);
m_texture_vk = VK_NULL_HANDLE;
}
#endif //(VK_VERSION_1_0+0) && QT_CONFIG(vulkan)