forked from Averta047/imfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimfx.cpp
More file actions
174 lines (149 loc) · 5.53 KB
/
imfx.cpp
File metadata and controls
174 lines (149 loc) · 5.53 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
//================================================================//
//
// Author: Averta047 x Cluade Sonnet 4.6
// Purpose: High-level context and effect management
//
//================================================================//
#include "imfx.h"
#include "imgui_internal.h" // ImStrdup, ImFileOpen/Read/Close, IM_NEW, IM_DELETE, IM_ASSERT
//================================================================//
// Internal global context (mirrors GImGui)
//================================================================//
static ImFXContext* GImFX = nullptr;
//================================================================//
// ImFXEffect
//================================================================//
ImFXEffect::ImFXEffect()
{
SourceType = ImFXShaderSourceType_String;
ShaderLang = ImFXShaderLang_HLSL;
SourceSize = 0;
Size = ImVec2(512.0f, 512.0f);
Speed = 1.0f;
Scale = 2.0f;
Density = 0.0f;
ColorA = ImVec4(0.05f, 0.10f, 0.40f, 1.0f);
ColorB = ImVec4(0.60f, 0.10f, 0.50f, 1.0f);
_BackendFlags = ImFXEffectBackendFlags_None;
_BackendData = nullptr;
_TextureID = ImTextureID_Invalid;
BackBufferSrv = nullptr;
}
//================================================================//
// ImFXContext
//================================================================//
ImFXContext::ImFXContext()
{
DefaultEffect = nullptr;
_BackendRendererUserData = nullptr;
}
ImFXContext::~ImFXContext()
{
// Free all owned effects
for (int i = 0; i < Effects.Size; i++)
IM_DELETE(Effects[i]);
Effects.clear();
DefaultEffect = nullptr;
}
//================================================================//
// ImFX namespace
//================================================================//
namespace ImFX
{
ImFXContext* CreateContext()
{
ImFXContext* ctx = IM_NEW(ImFXContext);
if (GImFX == nullptr)
GImFX = ctx;
return ctx;
}
void DestroyContext(ImFXContext* ctx)
{
if (ctx == nullptr)
ctx = GImFX;
IM_ASSERT(ctx != nullptr && "ImFX: no context to destroy");
if (GImFX == ctx)
GImFX = nullptr;
IM_DELETE(ctx);
}
ImFXContext* GetContext()
{
return GImFX;
}
void SetContext(ImFXContext* ctx)
{
GImFX = ctx;
}
//----------------------------------------------------------------//
// Internal helpers
//----------------------------------------------------------------//
static ImFXEffect* CreateEffectBase(ImFXShaderSourceType type, ImFXShaderLang lang, ImVec2 size)
{
IM_ASSERT(GImFX != nullptr && "ImFX: call ImFX::CreateContext() before creating effects");
ImFXEffect* effect = IM_NEW(ImFXEffect);
effect->SourceType = type;
effect->ShaderLang = lang;
effect->Size = size;
GImFX->Effects.push_back(effect);
return effect;
}
static void CopySourceString(ImFXEffect* effect, const char* src)
{
int len = (int)ImStrlen(src) + 1; // include null terminator
effect->SourceBuffer.resize(len);
ImStrncpy(effect->SourceBuffer.Data, src, len);
effect->SourceSize = (size_t)len;
}
//----------------------------------------------------------------//
// CreateEffectFromString
//----------------------------------------------------------------//
ImFXEffect* CreateEffectFromString(const char* hlslSrc, ImFXShaderLang lang, ImVec2 size)
{
IM_ASSERT(hlslSrc != nullptr && "ImFX: shader source string is null");
ImFXEffect* effect = CreateEffectBase(ImFXShaderSourceType_String, lang, size);
CopySourceString(effect, hlslSrc);
return effect;
}
//----------------------------------------------------------------//
// CreateEffectFromFile
// Reads the entire file into SourceBuffer at creation time so the
// backend gets a self-contained buffer identical to the String path.
//----------------------------------------------------------------//
ImFXEffect* CreateEffectFromFile(const char* path, ImFXShaderLang lang, ImVec2 size)
{
IM_ASSERT(path != nullptr && "ImFX: shader file path is null");
ImFXEffect* effect = CreateEffectBase(ImFXShaderSourceType_File, lang, size);
// Store the path so callers can identify the source later
CopySourceString(effect, path);
// Also load the file contents into a secondary buffer for the backend.
// We re-use SourceBuffer: first we store the path for diagnostics,
// then overwrite with file data.
ImFileHandle f = ImFileOpen(path, "rb");
if (f == nullptr)
{
IM_ASSERT(false && "ImFX: failed to open shader file");
effect->_BackendFlags |= ImFXEffectBackendFlags_CompileError;
return effect;
}
ImU64 fileSize = ImFileGetSize(f);
effect->SourceBuffer.resize((int)fileSize + 1);
ImFileRead(effect->SourceBuffer.Data, 1, fileSize, f);
effect->SourceBuffer[(int)fileSize] = '\0';
effect->SourceSize = (size_t)fileSize;
ImFileClose(f);
return effect;
}
//----------------------------------------------------------------//
// CreateEffectFromBytes
// For pre-compiled CSO blobs - copied as-is, no null terminator.
//----------------------------------------------------------------//
ImFXEffect* CreateEffectFromBytes(const void* data, size_t dataSize, ImFXShaderLang lang, ImVec2 size)
{
IM_ASSERT(data != nullptr && dataSize > 0 && "ImFX: invalid byte blob");
ImFXEffect* effect = CreateEffectBase(ImFXShaderSourceType_Bytes, lang, size);
effect->SourceBuffer.resize((int)dataSize);
memcpy(effect->SourceBuffer.Data, data, dataSize);
effect->SourceSize = dataSize;
return effect;
}
} // namespace ImFX