-
Notifications
You must be signed in to change notification settings - Fork 24
/
TexturesUWP.cpp
393 lines (329 loc) · 9.43 KB
/
TexturesUWP.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
//
// Ritchie Lozada (rlozada@microsoft.com)
//
// Plugin Handlers - Based on UNITY LOW LEVEL PLUGIN EXAMPLE
// https://docs.unity3d.com/Manual/NativePluginInterface.html
// https://bitbucket.org/Unity-Technologies/graphicsdemos
#include "pch.h"
#include "TexturesUWP.h"
#include <cmath>
#include <d3d11.h>
#include <assert.h>
#include "UnityPluginAPI/IUnityGraphics.h"
#include "UnityPluginAPI/IUnityInterface.h"
#include "UnityPluginAPI/IUnityGraphicsD3D11.h"
#include "VideoDecoder.h"
#include "libyuv/convert_argb.h"
#include <thread>
// Plugin Mode
int PluginMode = 0;
// Decoder Namespace
using namespace WebRTCDirectXClientComponent;
int const textureWidth = 1280;
int const textureHeight = 720;
static void* g_TextureHandle = NULL;
static int g_TextureWidth = textureWidth;
static int g_TextureHeight = textureHeight;
static float g_Time = 0;
static VideoDecoder* videoDecoder = NULL;
static IUnityInterfaces* s_UnityInterfaces = NULL;
static IUnityGraphics* s_Graphics = NULL;
static UnityGfxRenderer s_DeviceType = kUnityGfxRendererNull;
static ID3D11Device* m_Device;
void* textureDataPtr;
bool isARGBFrameReady = false;
byte* yuvDataBuf = NULL;
byte* yDataBuf = NULL;
byte* uDataBuf = NULL;
byte* vDataBuf = NULL;
int yuvDataBufLen = 0;
unsigned int wRawFrame = textureWidth;
unsigned int hRawFrame = textureHeight;
unsigned int pixelSize = 4;
unsigned int yStrideBuf = 0;
unsigned int uStrideBuf = 0;
unsigned int vStrideBuf = 0;
byte* h264DataBuf = NULL;
unsigned int h264DataBufLen = 0;
unsigned int wH264Frame = textureWidth;
unsigned int hH264Frame = textureHeight;
byte* argbDataBuf = NULL;
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);
void InitializeVideoProcessing()
{
// Setup Video Decoder
videoDecoder = new VideoDecoder();
videoDecoder->Initialize(hH264Frame, wH264Frame);
// Pre-Allocate Buffers instead of dynamic allocation and release
int bufSize = g_TextureWidth * g_TextureHeight * pixelSize;
yuvDataBuf = new byte[bufSize];
memset(yuvDataBuf, 0, bufSize);
h264DataBuf = new byte[bufSize];
memset(h264DataBuf, 0, bufSize);
argbDataBuf = new byte[bufSize];
memset(argbDataBuf, 0, bufSize);
}
void ShutdownVideoProcessing()
{
// Release Byte Buffers
if (yuvDataBuf != NULL)
delete[] yuvDataBuf;
if (h264DataBuf != NULL)
delete[] h264DataBuf;
if (argbDataBuf != NULL)
delete[] argbDataBuf;
// Cleanup Video Decoder
if (videoDecoder != NULL)
videoDecoder->Deinitialize();
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
{
s_UnityInterfaces = unityInterfaces;
s_Graphics = s_UnityInterfaces->Get<IUnityGraphics>();
s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
// Run OnGraphicsDeviceEvent(initialize) manually on plugin load
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
InitializeVideoProcessing();
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
{
s_Graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);
ShutdownVideoProcessing();
}
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType)
{
switch (eventType)
{
case kUnityGfxDeviceEventInitialize:
{
//TODO: user initialization code
s_DeviceType = s_Graphics->GetRenderer();
IUnityGraphicsD3D11* d3d = s_UnityInterfaces->Get<IUnityGraphicsD3D11>();
m_Device = d3d->GetDevice();
break;
}
case kUnityGfxDeviceEventShutdown:
{
//TODO: user shutdown code
s_DeviceType = kUnityGfxRendererNull;
break;
}
case kUnityGfxDeviceEventBeforeReset:
{
break;
}
case kUnityGfxDeviceEventAfterReset:
{
break;
}
};
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetTextureFromUnity(void* textureHandle, int w, int h)
{
// A script calls this at initialization time; just remember the texture pointer here.
// Will update texture pixels each frame from the plugin rendering event (texture update
// needs to happen on the rendering thread).
g_TextureHandle = textureHandle;
g_TextureWidth = w;
g_TextureHeight = h;
}
void ProcessTestFrameDataThread()
{
while (true)
{
if (!g_TextureHandle)
{
Sleep(500);
continue;;
}
//int bufSize = g_TextureWidth * g_TextureHeight * pixelSize;
const float t = g_Time * 4.0f;
g_Time += 0.03f;
// TODO: Setup Mutex/Lock for isARGBFrameReady/argbDataBuf
// Quick solution to coordinate render status
isARGBFrameReady = false;
byte* dst = argbDataBuf;
for (int y = 0; y < g_TextureHeight; ++y)
{
byte* ptr = dst;
for (int x = 0; x < g_TextureWidth; ++x)
{
// Simple "plasma effect": several combined sine waves
int vv = int(
(127.0f + (127.0f * sinf(x / 7.0f + t))) +
(127.0f + (127.0f * sinf(y / 5.0f - t))) +
(127.0f + (127.0f * sinf((x + y) / 6.0f - t))) +
(127.0f + (127.0f * sinf(sqrtf(float(x*x + y*y)) / 4.0f - t)))
) / 4;
ptr[0] = vv / 2;
ptr[1] = vv;
ptr[2] = vv;
ptr[3] = 255;
// To next pixel (our pixels are 4 bpp)
ptr += 4;
}
// To next image row
dst += pixelSize * g_TextureWidth;
}
isARGBFrameReady = true;
Sleep(15);
}
// Texture Update is executed when Unity triggers the render event to avoid collision
// between the plugin and Unity's graphics update. Data can be manipulated but graphics
// needs to follow Unity render event.
//
//UpdateUnityTexture(g_TextureHandle, g_TextureWidth * pixelSize, argbDataBuf);
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetPluginMode(int mode)
{
PluginMode = mode;
if(PluginMode == 2)
{
// Starts the display update thread and detach to run continuously
std::thread t(ProcessTestFrameDataThread);
t.detach();
}
}
void UpdateUnityTexture(void* textureHandle, int rowPitch, void* dataPtr)
{
ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)textureHandle;
assert(d3dtex);
ID3D11DeviceContext* ctx = NULL;
m_Device->GetImmediateContext(&ctx);
ctx->UpdateSubresource(d3dtex, 0, NULL, dataPtr, rowPitch, 0);
ctx->Release();
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API ProcessRawFrame(unsigned int w, unsigned int h, byte* yPlane, unsigned int yStride, byte* uPlane, unsigned int uStride, byte* vPlane, unsigned int vStride)
{
if ((yPlane == NULL) || (vPlane == NULL) || (uPlane == NULL))
return;
wRawFrame = w;
hRawFrame = h;
// TODO: Test VP8 RawFrame Event Setup
// Process YUV to ARGB
int half_width = (wRawFrame + 1) / 2;
int half_height = (hRawFrame + 1) / 2;
int bufSize2 = half_width * half_height;
isARGBFrameReady = !libyuv::I420ToARGB(
yPlane,
yStride,
uPlane,
uStride,
vPlane,
vStride,
argbDataBuf,
wRawFrame * pixelSize,
wRawFrame,
hRawFrame);
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API ProcessH264Frame(unsigned int w, unsigned int h, byte* data, unsigned int bufSize)
{
wRawFrame = w;
hRawFrame = h;
wH264Frame = w;
hH264Frame = h;
isARGBFrameReady = false;
if (!videoDecoder->DecodeH264VideoFrame(
data,
bufSize,
wH264Frame,
hH264Frame,
&yuvDataBuf,
&yuvDataBufLen))
{
int half_width = (wH264Frame + 1) / 2;
int half_height = (hH264Frame + 1) / 2;
yStrideBuf = wH264Frame;
uStrideBuf = half_width;
vStrideBuf = half_width;
// Note: Decode Information is in YVU ordering instead YUV
yDataBuf = yuvDataBuf;
vDataBuf = yuvDataBuf + yStrideBuf * hH264Frame;
uDataBuf = vDataBuf + vStrideBuf * half_height;
isARGBFrameReady = !libyuv::I420ToARGB(
yDataBuf,
yStrideBuf,
uDataBuf,
uStrideBuf,
vDataBuf,
vStrideBuf,
argbDataBuf,
wH264Frame * pixelSize,
wH264Frame,
hH264Frame);
// YUY2 Output Processing
// isARGBFrameReady = !libyuv::YUY2ToARGB(
// yuvDataBuf,
// wH264Frame * 2,
// argbDataBuf,
// wH264Frame * pixelSize,
// wH264Frame,
// hH264Frame);
}
}
static void ProcessARGBFrameData()
{
if (!g_TextureHandle)
return;
if (!isARGBFrameReady)
return;
UpdateUnityTexture(g_TextureHandle, g_TextureWidth * pixelSize, argbDataBuf);
isARGBFrameReady = false;
}
static void ProcessTestFrameData()
{
if (!g_TextureHandle)
return;
//int bufSize = g_TextureWidth * g_TextureHeight * pixelSize;
const float t = g_Time * 4.0f;
g_Time += 0.03f;
byte* dst = argbDataBuf;
for (int y = 0; y < g_TextureHeight; ++y)
{
byte* ptr = dst;
for (int x = 0; x < g_TextureWidth; ++x)
{
// Simple "plasma effect": several combined sine waves
int vv = int(
(127.0f + (127.0f * sinf(x / 7.0f + t))) +
(127.0f + (127.0f * sinf(y / 5.0f - t))) +
(127.0f + (127.0f * sinf((x + y) / 6.0f - t))) +
(127.0f + (127.0f * sinf(sqrtf(float(x*x + y*y)) / 4.0f - t)))
) / 4;
ptr[0] = vv / 2;
ptr[1] = vv;
ptr[2] = vv;
ptr[3] = 255;
// To next pixel (our pixels are 4 bpp)
ptr += 4;
}
// To next image row
dst += pixelSize * g_TextureWidth;
}
UpdateUnityTexture(g_TextureHandle, g_TextureWidth * pixelSize, argbDataBuf);
}
static void UNITY_INTERFACE_API OnRenderEvent(int eventID)
{
switch (PluginMode)
{
case 0: // WebRTC Image Frame Rendering
ProcessARGBFrameData();
break;
case 1: // Invoke
ProcessTestFrameData();
break;
case 2: // Invoke
if(isARGBFrameReady)
{
UpdateUnityTexture(g_TextureHandle, g_TextureWidth * pixelSize, argbDataBuf);
isARGBFrameReady = false;
}
break;
}
}
extern "C" UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventFunc()
{
return OnRenderEvent;
}