-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathRenderingPlugin.cpp
More file actions
378 lines (303 loc) · 11.1 KB
/
RenderingPlugin.cpp
File metadata and controls
378 lines (303 loc) · 11.1 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
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
// Example low level rendering Unity plugin
#include "PlatformBase.h"
#include "RenderAPI.h"
#include <assert.h>
#include <math.h>
#include <vector>
// --------------------------------------------------------------------------
// SetTimeFromUnity, an example function we export which is called by one of the scripts.
static float g_Time;
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetTimeFromUnity (float t) { g_Time = t; }
// --------------------------------------------------------------------------
// SetTextureFromUnity, an example function we export which is called by one of the scripts.
static void* g_TextureHandle = NULL;
static int g_TextureWidth = 0;
static int g_TextureHeight = 0;
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;
}
// --------------------------------------------------------------------------
// SetMeshBuffersFromUnity, an example function we export which is called by one of the scripts.
static void* g_VertexBufferHandle = NULL;
static int g_VertexBufferVertexCount;
struct MeshVertex
{
float pos[3];
float normal[3];
float color[4];
float uv[2];
};
static std::vector<MeshVertex> g_VertexSource;
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetMeshBuffersFromUnity(void* vertexBufferHandle, int vertexCount, float* sourceVertices, float* sourceNormals, float* sourceUV)
{
// A script calls this at initialization time; just remember the pointer here.
// Will update buffer data each frame from the plugin rendering event (buffer update
// needs to happen on the rendering thread).
g_VertexBufferHandle = vertexBufferHandle;
g_VertexBufferVertexCount = vertexCount;
// The script also passes original source mesh data. The reason is that the vertex buffer we'll be modifying
// will be marked as "dynamic", and on many platforms this means we can only write into it, but not read its previous
// contents. In this example we're not creating meshes from scratch, but are just altering original mesh data --
// so remember it. The script just passes pointers to regular C# array contents.
g_VertexSource.resize(vertexCount);
for (int i = 0; i < vertexCount; ++i)
{
MeshVertex& v = g_VertexSource[i];
v.pos[0] = sourceVertices[0];
v.pos[1] = sourceVertices[1];
v.pos[2] = sourceVertices[2];
v.normal[0] = sourceNormals[0];
v.normal[1] = sourceNormals[1];
v.normal[2] = sourceNormals[2];
v.uv[0] = sourceUV[0];
v.uv[1] = sourceUV[1];
sourceVertices += 3;
sourceNormals += 3;
sourceUV += 2;
}
}
// --------------------------------------------------------------------------
// UnitySetInterfaces
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);
static IUnityInterfaces* s_UnityInterfaces = NULL;
static IUnityGraphics* s_Graphics = NULL;
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);
#if SUPPORT_VULKAN
if (s_Graphics->GetRenderer() == kUnityGfxRendererNull)
{
extern void RenderAPI_Vulkan_OnPluginLoad(IUnityInterfaces*);
RenderAPI_Vulkan_OnPluginLoad(unityInterfaces);
}
#endif // SUPPORT_VULKAN
// Run OnGraphicsDeviceEvent(initialize) manually on plugin load
#ifndef DEBUG //postponed to OnRenderEvent in debugMode, to have a valid GL_Context when dynamically loading the dll. Please make sure DEBUG preprocessor macro is defined in Debug configuration.
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
#endif
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload()
{
s_Graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);
}
#if UNITY_WEBGL
typedef void (UNITY_INTERFACE_API * PluginLoadFunc)(IUnityInterfaces* unityInterfaces);
typedef void (UNITY_INTERFACE_API * PluginUnloadFunc)();
extern "C" void UnityRegisterRenderingPlugin(PluginLoadFunc loadPlugin, PluginUnloadFunc unloadPlugin);
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API RegisterPlugin()
{
UnityRegisterRenderingPlugin(UnityPluginLoad, UnityPluginUnload);
}
#endif
// --------------------------------------------------------------------------
// GraphicsDeviceEvent
static RenderAPI* s_CurrentAPI = NULL;
static UnityGfxRenderer s_DeviceType = kUnityGfxRendererNull;
static bool s_GraphicsInitialized = false;
static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType)
{
// Create graphics API implementation upon initialization
if (eventType == kUnityGfxDeviceEventInitialize)
{
assert(s_CurrentAPI == NULL);
s_DeviceType = s_Graphics->GetRenderer();
s_CurrentAPI = CreateRenderAPI(s_DeviceType);
s_GraphicsInitialized = true;
}
// Let the implementation process the device related events
if (s_CurrentAPI)
{
s_CurrentAPI->ProcessDeviceEvent(eventType, s_UnityInterfaces);
}
// Cleanup graphics API implementation upon shutdown
if (eventType == kUnityGfxDeviceEventShutdown)
{
delete s_CurrentAPI;
s_CurrentAPI = NULL;
s_DeviceType = kUnityGfxRendererNull;
}
}
// --------------------------------------------------------------------------
// OnRenderEvent
// This will be called for GL.IssuePluginEvent script calls; eventID will
// be the integer passed to IssuePluginEvent. In this example, we just ignore
// that value.
static void DrawColoredTriangle()
{
// Draw a colored triangle. Note that colors will come out differently
// in D3D and OpenGL, for example, since they expect color bytes
// in different ordering.
struct MyVertex
{
float x, y, z;
unsigned int color;
};
MyVertex verts[3] =
{
{ -0.5f, -0.25f, 0, 0xFFff0000 },
{ 0.5f, -0.25f, 0, 0xFF00ff00 },
{ 0, 0.5f , 0, 0xFF0000ff },
};
// Transformation matrix: rotate around Z axis based on time.
float phi = g_Time * 2.0f; // time set externally from Unity script
float cosPhi = cosf(phi);
float sinPhi = sinf(phi);
float depth = 0.7f;
float finalDepth = s_CurrentAPI->GetUsesReverseZ() ? 1.0f - depth : depth;
float worldMatrix[16] = {
cosPhi,-sinPhi,0,0,
sinPhi,cosPhi,0,0,
0,0,1,0,
0,0,finalDepth,1,
};
s_CurrentAPI->DrawSimpleTriangles(worldMatrix, 1, verts);
}
static void ModifyTexturePixels()
{
void* textureHandle = g_TextureHandle;
int width = g_TextureWidth;
int height = g_TextureHeight;
if (!textureHandle)
return;
int textureRowPitch;
void* textureDataPtr = s_CurrentAPI->BeginModifyTexture(textureHandle, width, height, &textureRowPitch);
if (!textureDataPtr)
return;
const float t = g_Time * 4.0f;
unsigned char* dst = (unsigned char*)textureDataPtr;
for (int y = 0; y < height; ++y)
{
unsigned char* ptr = dst;
for (int x = 0; x < width; ++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;
// Write the texture pixel
ptr[0] = vv;
ptr[1] = vv;
ptr[2] = vv;
ptr[3] = vv;
// To next pixel (our pixels are 4 bpp)
ptr += 4;
}
// To next image row
dst += textureRowPitch;
}
s_CurrentAPI->EndModifyTexture(textureHandle, width, height, textureRowPitch, textureDataPtr);
}
static void ModifyVertexBuffer()
{
void* bufferHandle = g_VertexBufferHandle;
int vertexCount = g_VertexBufferVertexCount;
if (!bufferHandle)
return;
size_t bufferSize;
void* bufferDataPtr = s_CurrentAPI->BeginModifyVertexBuffer(bufferHandle, &bufferSize);
if (!bufferDataPtr)
return;
int vertexStride = int(bufferSize / vertexCount);
// Unity should return us a buffer that is the size of `vertexCount * sizeof(MeshVertex)`
// If that's not the case then we should quit to avoid unexpected results.
// This can happen if https://docs.unity3d.com/ScriptReference/Mesh.GetNativeVertexBufferPtr.html returns
// a pointer to a buffer with an unexpected layout.
if (static_cast<unsigned int>(vertexStride) != sizeof(MeshVertex))
return;
const float t = g_Time * 3.0f;
char* bufferPtr = (char*)bufferDataPtr;
// modify vertex Y position with several scrolling sine waves,
// copy the rest of the source data unmodified
for (int i = 0; i < vertexCount; ++i)
{
const MeshVertex& src = g_VertexSource[i];
MeshVertex& dst = *(MeshVertex*)bufferPtr;
dst.pos[0] = src.pos[0];
dst.pos[1] = src.pos[1] + sinf(src.pos[0] * 1.1f + t) * 0.4f + sinf(src.pos[2] * 0.9f - t) * 0.3f;
dst.pos[2] = src.pos[2];
dst.normal[0] = src.normal[0];
dst.normal[1] = src.normal[1];
dst.normal[2] = src.normal[2];
dst.uv[0] = src.uv[0];
dst.uv[1] = src.uv[1];
bufferPtr += vertexStride;
}
s_CurrentAPI->EndModifyVertexBuffer(bufferHandle);
}
static void drawToPluginTexture()
{
s_CurrentAPI->drawToPluginTexture();
}
static void drawToRenderTexture()
{
s_CurrentAPI->drawToRenderTexture();
}
static void UNITY_INTERFACE_API OnRenderEvent(int eventID)
{
#ifdef DEBUG //edit to make it possible to dynamically load the dll.
if (!s_GraphicsInitialized)
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
#endif
// Unknown / unsupported graphics device type? Do nothing
if (s_CurrentAPI == NULL)
return;
if (eventID == 1)
{
drawToRenderTexture();
DrawColoredTriangle();
ModifyTexturePixels();
ModifyVertexBuffer();
}
if (eventID == 2)
{
drawToPluginTexture();
}
}
// --------------------------------------------------------------------------
// GetRenderEventFunc, an example function we export which is used to get a rendering event callback function.
extern "C" UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventFunc()
{
return OnRenderEvent;
}
// --------------------------------------------------------------------------
// DX12 plugin specific
// --------------------------------------------------------------------------
extern "C" UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API GetRenderTexture()
{
return s_CurrentAPI->getRenderTexture();
}
extern "C" UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetRenderTexture(UnityRenderBuffer rb)
{
s_CurrentAPI->setRenderTextureResource(rb);
}
extern "C" UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsSwapChainAvailable()
{
return s_CurrentAPI->isSwapChainAvailable();
}
extern "C" UNITY_INTERFACE_EXPORT unsigned int UNITY_INTERFACE_API GetPresentFlags()
{
return s_CurrentAPI->getPresentFlags();
}
extern "C" UNITY_INTERFACE_EXPORT unsigned int UNITY_INTERFACE_API GetSyncInterval()
{
return s_CurrentAPI->getSyncInterval();
}
extern "C" UNITY_INTERFACE_EXPORT unsigned int UNITY_INTERFACE_API GetBackBufferWidth()
{
return s_CurrentAPI->getBackbufferHeight();
}
extern "C" UNITY_INTERFACE_EXPORT unsigned int UNITY_INTERFACE_API GetBackBufferHeight()
{
return s_CurrentAPI->getBackbufferWidth();
}