-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureSubPlugin.cpp
More file actions
142 lines (123 loc) · 4.04 KB
/
TextureSubPlugin.cpp
File metadata and controls
142 lines (123 loc) · 4.04 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
#include <assert.h>
#include <math.h>
#include "IUnityLog.h"
#include "TextureSubPluginAPI.hpp"
enum Event {
TextureSubImage2D = 0,
TextureSubImage3D = 1,
CreateTexture3D = 2,
DestroyTexture3D = 3
};
struct TextureSubImage2DParams {
void* texture_handle;
int32_t xoffset;
int32_t yoffset;
int32_t width;
int32_t height;
void* data_ptr;
int32_t level;
Format format;
};
struct TextureSubImage3DParams {
void* texture_handle;
int32_t xoffset;
int32_t yoffset;
int32_t zoffset;
int32_t width;
int32_t height;
int32_t depth;
void* data_ptr;
int32_t level;
Format format;
};
struct CreateTexture3DParams {
uint32_t texture_id;
uint32_t width;
uint32_t height;
uint32_t depth;
Format format;
};
struct DestroyTexture3DParams {
uint32_t texture_id;
};
// global state
static TextureSubPluginAPI* s_CurrentAPI = NULL;
static UnityGfxRenderer s_DeviceType = kUnityGfxRendererNull;
static void UNITY_INTERFACE_API
OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType);
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
UnityPluginLoad(IUnityInterfaces* unityInterfaces) {
g_UnityInterfaces = unityInterfaces;
g_Graphics = g_UnityInterfaces->Get<IUnityGraphics>();
g_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent);
g_Log = g_UnityInterfaces->Get<IUnityLog>();
// Run OnGraphicsDeviceEvent(initialize) manually on plugin load
OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);
}
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload() {
g_Graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);
}
static void UNITY_INTERFACE_API
OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType) {
// Create graphics API implementation upon initialization
if (eventType == kUnityGfxDeviceEventInitialize) {
assert(s_CurrentAPI == NULL);
s_DeviceType = g_Graphics->GetRenderer();
s_CurrentAPI = CreateTextureSubPluginAPI(s_DeviceType);
}
// Let the implementation process the device related events
if (s_CurrentAPI) {
s_CurrentAPI->ProcessDeviceEvent(eventType, g_UnityInterfaces);
}
// Cleanup graphics API implementation upon shutdown
if (eventType == kUnityGfxDeviceEventShutdown) {
delete s_CurrentAPI;
s_CurrentAPI = NULL;
s_DeviceType = kUnityGfxRendererNull;
}
}
static void UNITY_INTERFACE_API OnRenderEvent(int eventID, void* data) {
// Unknown / unsupported graphics device type? Do nothing
if (s_CurrentAPI == NULL) return;
switch ((Event)eventID) {
case Event::TextureSubImage2D: {
auto args = static_cast<TextureSubImage2DParams*>(data);
s_CurrentAPI->TextureSubImage2D(
args->texture_handle, args->xoffset, args->yoffset, args->width,
args->height, args->data_ptr, args->level, args->format);
break;
}
case Event::TextureSubImage3D: {
auto args = static_cast<TextureSubImage3DParams*>(data);
s_CurrentAPI->TextureSubImage3D(args->texture_handle, args->xoffset,
args->yoffset, args->zoffset, args->width,
args->height, args->depth, args->data_ptr,
args->level, args->format);
break;
}
case Event::CreateTexture3D: {
auto args = static_cast<CreateTexture3DParams*>(data);
s_CurrentAPI->CreateTexture3D(args->texture_id, args->width, args->height,
args->depth, args->format);
break;
}
case Event::DestroyTexture3D: {
auto args = static_cast<DestroyTexture3DParams*>(data);
s_CurrentAPI->DestroyTexture3D(args->texture_id);
break;
}
default: {
UNITY_LOG_ERROR(g_Log, "unknown event ID!");
return;
}
}
}
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
GetRenderEventFunc() {
return OnRenderEvent;
}
extern "C" UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API
RetrieveCreatedTexture3D(uint32_t texture_id) {
if (s_CurrentAPI == NULL) return nullptr;
return s_CurrentAPI->RetrieveCreatedTexture3D(texture_id);
}