-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathDX11TextureObject.cpp
More file actions
188 lines (159 loc) · 5.43 KB
/
DX11TextureObject.cpp
File metadata and controls
188 lines (159 loc) · 5.43 KB
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
//========= Copyright 2015-2019, HTC Corporation. All rights reserved. ===========
#include "DX11TextureObject.h"
#include "Logger.h"
#include <thread>
DX11TextureObject::DX11TextureObject() {
mD3D11Device = NULL;
mWidthY = mHeightY = mLengthY = 0;
mWidthUV = mHeightUV = mLengthUV = 0;
for (int i = 0; i < TEXTURE_NUM; i++) {
mTextures[i] = NULL;
mShaderResourceView[i] = NULL;
}
}
DX11TextureObject::~DX11TextureObject() {
destroy();
}
void DX11TextureObject::getResourcePointers(void*& ptry, void*& ptru, void*& ptrv) {
if (mD3D11Device == NULL) {
return;
}
ptry = mShaderResourceView[0];
ptru = mShaderResourceView[1];
ptrv = mShaderResourceView[2];
}
void DX11TextureObject::create(void* handler, unsigned int width, unsigned int height) {
if (handler == NULL) {
return;
}
mD3D11Device = (ID3D11Device*) handler;
mWidthY = (unsigned int)(ceil((float) width / CPU_ALIGMENT) * CPU_ALIGMENT);
mHeightY = height;
mLengthY = mWidthY * mHeightY;
mWidthUV = mWidthY / 2;
mHeightUV = mHeightY / 2;
mLengthUV = mWidthUV * mHeightUV;
// For YUV420
// Y channel
D3D11_TEXTURE2D_DESC texDesc;
ZeroMemory(&texDesc, sizeof(D3D11_TEXTURE2D_DESC));
texDesc.Width = width;
texDesc.Height = height;
texDesc.MipLevels = texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_A8_UNORM;
texDesc.SampleDesc.Count = 1;
texDesc.Usage = D3D11_USAGE_DYNAMIC;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
texDesc.MiscFlags = 0;
HRESULT result = mD3D11Device->CreateTexture2D(&texDesc, NULL, (ID3D11Texture2D**)(&(mTextures[0])));
if (FAILED(result)) {
LOG("Create texture Y fail. Error code: %x\n", result);
}
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
shaderResourceViewDesc.Format = DXGI_FORMAT_A8_UNORM;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
result = mD3D11Device->CreateShaderResourceView((ID3D11Texture2D*)(mTextures[0]), &shaderResourceViewDesc, &(mShaderResourceView[0]));
if (FAILED(result)) {
LOG("Create shader resource view Y fail. Error code: %x\n", result);
}
// UV channel
texDesc.Width = width / 2;
texDesc.Height = height / 2;
result = mD3D11Device->CreateTexture2D(&texDesc, NULL, (ID3D11Texture2D**)(&(mTextures[1])));
if (FAILED(result)) {
LOG("Create texture U fail. Error code: %x\n", result);
}
result = mD3D11Device->CreateShaderResourceView((ID3D11Texture2D*)(mTextures[1]), &shaderResourceViewDesc, &(mShaderResourceView[1]));
if (FAILED(result)) {
LOG("Create shader resource view U fail. Error code: %x\n", result);
}
result = mD3D11Device->CreateTexture2D(&texDesc, NULL, (ID3D11Texture2D**)(&(mTextures[2])));
if (FAILED(result)) {
LOG("Create texture V fail. Error code: %x\n", result);
}
result = mD3D11Device->CreateShaderResourceView((ID3D11Texture2D*)(mTextures[2]), &shaderResourceViewDesc, &(mShaderResourceView[2]));
if (FAILED(result)) {
LOG("Create shader resource view V fail. %x\n", result);
}
}
void DX11TextureObject::upload(unsigned char* ych, unsigned char* uch, unsigned char* vch) {
if (mD3D11Device == NULL) {
return;
}
ID3D11DeviceContext* ctx = NULL;
mD3D11Device->GetImmediateContext(&ctx);
D3D11_MAPPED_SUBRESOURCE mappedResource[TEXTURE_NUM];
for (int i = 0; i < TEXTURE_NUM; i++) {
ZeroMemory(&(mappedResource[i]), sizeof(D3D11_MAPPED_SUBRESOURCE));
ctx->Map(mTextures[i], 0, D3D11_MAP_WRITE_DISCARD, 0, &(mappedResource[i]));
}
// Consider padding.
UINT rowPitchY = mappedResource[0].RowPitch;
UINT rowPitchUV = mappedResource[1].RowPitch;
uint8_t* ptrMappedY = (uint8_t*)(mappedResource[0].pData);
uint8_t* ptrMappedU = (uint8_t*)(mappedResource[1].pData);
uint8_t* ptrMappedV = (uint8_t*)(mappedResource[2].pData);
// Two thread memory copy
std::thread YThread = std::thread([&]() {
// Map region has its own row pitch which may different to texture width.
if (mWidthY == rowPitchY) {
memcpy(ptrMappedY, ych, mLengthY);
}
else {
// Handle rowpitch of mapped memory.
uint8_t* end = ych + mLengthY;
while (ych != end) {
memcpy(ptrMappedY, ych, mWidthY);
ych += mWidthY;
ptrMappedY += rowPitchY;
}
}
});
std::thread UVThread = std::thread([&]() {
if (mWidthUV == rowPitchUV) {
memcpy(ptrMappedU, uch, mLengthUV);
memcpy(ptrMappedV, vch, mLengthUV);
}
else {
// Handle rowpitch of mapped memory.
// YUV420, length U == length V
uint8_t* endU = uch + mLengthUV;
while (uch != endU) {
memcpy(ptrMappedU, uch, mWidthUV);
memcpy(ptrMappedV, vch, mWidthUV);
uch += mWidthUV;
vch += mWidthUV;
ptrMappedU += rowPitchUV;
ptrMappedV += rowPitchUV;
}
}
});
if (YThread.joinable()) {
YThread.join();
}
if (UVThread.joinable()) {
UVThread.join();
}
for (int i = 0; i < TEXTURE_NUM; i++) {
ctx->Unmap(mTextures[i], 0);
}
ctx->Release();
}
void DX11TextureObject::destroy() {
mD3D11Device = NULL;
mWidthY = mHeightY = mLengthY = 0;
mWidthUV = mHeightUV = mLengthUV = 0;
for (int i = 0; i < TEXTURE_NUM; i++) {
if (mTextures[i] != NULL) {
mTextures[i]->Release();
mTextures[i] = NULL;
}
if (mShaderResourceView[i] != NULL) {
mShaderResourceView[i]->Release();
mShaderResourceView[i] = NULL;
}
}
}