17#include <emscripten/val.h>
38 const bool coded = filterMode >= 0;
39 spec.generateMips = coded && (filterMode & 2) != 0;
41 spec.magFilter = spec.minFilter;
54 uintptr_t pixelsPtr,
u32 pixelsLen,
i32 format,
bool flipY,
u32 content) {
56 if (!pixels)
return 0;
60 pixelSpan, boundaryTextureFormat(format), flipY);
65 uintptr_t pixelsPtr,
u32 pixelsLen,
i32 format,
bool flipY,
66 i32 filterMode,
i32 wrapMode,
u32 content) {
67 const u8* pixels = pixelsPtr ?
boundarySpan<u8>(pixelsPtr, pixelsLen,
"rm_createTextureEx") :
nullptr;
68 if (pixelsPtr && !pixels)
return 0;
70 const TextureFormat texFormat = boundaryTextureFormat(format);
76 applySamplingCodes(spec, filterMode, wrapMode);
79 if (!handle.isValid()) {
84 if (texture && pixels) {
86 if (pixelsLen < required) {
87 ES_LOG_ERROR(
"rm_createTextureEx: pixel buffer {} < required {}; upload skipped",
90 texture->setDataRaw(pixels,
static_cast<u32>(required), flipY);
98 uintptr_t pixelsPtr,
u32 pixelsLen,
i32 format,
99 i32 filterMode,
i32 wrapMode,
u32 content) {
101 if (!pixels)
return 0;
107 applySamplingCodes(spec, filterMode, wrapMode);
119 auto* device = rm.
device();
120 if (!device)
return false;
127 i32 format, uintptr_t dataPtr,
u32 dataLen,
u32 mipLevels,
u32 content) {
134 mipLevels ? mipLevels : 1);
139 const std::string& vertSrc,
const std::string& fragSrc) {
147 glTextureId, width, height);
176 if (!out.empty()) out +=
'\n';
177 out += std::to_string(owed.handle.id());
179 out += std::to_string(
static_cast<u32>(owed.content));
189 if (!out.empty()) out +=
',';
190 out += std::to_string(handle.id());
198 if (!out.empty()) out +=
',';
199 out += std::to_string(row.handle);
201 out += row.realized ?
'1' :
'0';
209 glTextureId, width, height,
static_cast<usize>(bytes));
246emscripten::val rm_getResourceStats(resource::ResourceManager& rm) {
247 const auto st = rm.getStats();
248 auto result = emscripten::val::object();
249 result.set(
"shaderCount",
static_cast<f64>(st.shaderCount));
250 result.set(
"textureCount",
static_cast<f64>(st.textureCount));
251 result.set(
"vertexBufferCount",
static_cast<f64>(st.vertexBufferCount));
252 result.set(
"indexBufferCount",
static_cast<f64>(st.indexBufferCount));
253 result.set(
"cacheHits",
static_cast<f64>(st.cacheHits));
254 result.set(
"cacheMisses",
static_cast<f64>(st.cacheMisses));
255 result.set(
"textureBytes",
static_cast<f64>(st.textureBytes));
256 result.set(
"textureBudget",
static_cast<f64>(st.textureBudget));
257 result.set(
"textureEvictableCount",
static_cast<f64>(st.textureEvictableCount));
258 result.set(
"retainedBytes",
static_cast<f64>(st.retainedBytes));
273 return tex ? tex->
getId() : 0;
277emscripten::val rm_getTextureDimensions(resource::ResourceManager& rm,
u32 handleId) {
280 return emscripten::val::null();
282 auto result = emscripten::val::object();
283 result.set(
"width", tex->getWidth());
284 result.set(
"height", tex->getHeight());
289#ifdef ES_ENABLE_BITMAP_TEXT
291 u32 textureHandle,
u32 texWidth,
u32 texHeight) {
292 auto handle = rm.createBitmapFont(fntContent,
298 u32 texWidth,
u32 texHeight,
const std::string& chars,
299 u32 charWidth,
u32 charHeight) {
300 auto handle = rm.createLabelAtlasFont(
302 chars, charWidth, charHeight);
316 const std::string&
text,
f32 fontSize,
f32 spacing) {
319 auto result = emscripten::val::object();
320 result.set(
"width", 0);
321 result.set(
"height", 0);
324 auto metrics = font->measureText(
text, fontSize, spacing);
325 auto result = emscripten::val::object();
326 result.set(
"width", metrics.width);
327 result.set(
"height", metrics.height);
335 uintptr_t pixelsPtr,
u32 pixelsLen) {
340 const u8* pixels =
boundarySpan<u8>(pixelsPtr, pixelsLen,
"rm_updateTextureSubregion");
342 tex->updateSubRegion(x, y, width, height, pixels, pixelsLen,
false);
Always-on validation for JS-supplied pointer+length pairs at the WASM boundary.
Abstract graphics device interface.
Logging utilities for ESEngine.
#define ES_LOG_ERROR(...)
Logs an error message.
Texture metadata structures for nine-slice and other settings.
Core type definitions and utilities for ESEngine.
virtual bool supportsCompressedFormat(GfxCompressedFormat format)=0
Reports whether the backend can sample the given compressed format.
u32 getId() const
Gets the texture handle's raw value, for command payloads and the WASM boundary.
IdType id() const
Gets the packed identifier (index + generation).
Central manager for GPU resources.
Texture * getTexture(TextureHandle handle)
Gets a texture by handle.
u32 getTextureRefCount(TextureHandle handle) const
Gets the current reference count for a texture.
u32 textureNativeId(TextureHandle handle) const
The backend's own name for a texture's object, for a host that writes its pixels itself; 0 when there...
void forgoTextureContent(TextureHandle handle)
Gives up on an owed texture's content: it keeps blank storage.
const std::string & getTexturePath(TextureHandle handle) const
Gets the cached path for a texture.
void registerTextureWithPath(TextureHandle handle, const std::string &path)
Registers a texture with a path for cache lookup.
std::vector< OwedTexture > texturesAwaitingReupload() const
std::vector< MeshHandle > meshesAwaitingRematerialization() const
Replayable meshes whose geometry the device is still waiting for.
TextureHandle createTextureArray(ResourceContent content, const TextureSpecification &spec, ConstSpan< u8 > pixels)
Creates a 2D texture array from spec.layers same-size RGBA8 (or SRGB8A8) layers.
TextureHandle wrapDeviceTexture(::esengine::TextureHandle texture, u32 width, u32 height)
Names a texture the device holds for another owner (a render target's colour plane) by a resource han...
GfxDevice * device() const
void releaseShader(ShaderHandle handle)
Releases a shader (decrements ref count).
TextureHandle createCompressedTexture(ResourceContent content, u32 width, u32 height, GfxCompressedFormat format, ConstSpan< u8 > data, u32 mipLevels=1)
Creates a block-compressed texture (KTX2 → ETC2/ASTC/BC), uploading the pre-compressed blocks for mip...
ShaderHandle createShader(const std::string &vertSrc, const std::string &fragSrc, bool rewriteLoose=true, GfxShaderLanguage language=GfxShaderLanguage::GLSL_ES300, bool deferLink=false)
u32 getShaderRefCount(ShaderHandle handle) const
Gets the current reference count for a shader.
std::vector< MeshRealization > meshRealizations()
Every live mesh, for a recovery criterion to read.
void restoreTextureContent(TextureHandle handle)
Settles an owed texture whose provider wrote the pixels itself.
bool adoptTextureContent(TextureHandle target, TextureHandle source)
Moves a freshly loaded texture's pixels behind an owed handle.
bool invalidateTexturePath(const std::string &path)
Severs a path's texture-cache identity (hot reload). The next acquireTextureByPath for it misses,...
TextureHandle createTexture(ResourceContent content, const TextureSpecification &spec)
Creates a texture from specification.
void setTextureMetadata(TextureHandle handle, const TextureMetadata &metadata)
Sets metadata for a texture.
void setRetainedBudget(usize bytes)
See GfxDevice::setRetainedBudget.
void releaseTexture(TextureHandle handle)
Releases a texture (decrements ref count).
TextureHandle registerExternalTexture(ResourceContent content, u32 glTextureId, u32 width, u32 height, usize bytes=0)
Registers an externally-created GL texture.
usize trimTextureCache()
Frees every evictable cached texture now (memory pressure). Held textures and the budget are untouche...
void setTextureBudget(usize bytes)
Sets the texture pool's resident-byte budget (0 = no eviction). Over budget, refCount==0 textures are...
TextureHandle acquireTextureByPath(const std::string &path)
Reuses a cached texture by its build path: returns its handle with an added reference (reviving an ev...
ResourceContent
Who refills a resource's GPU content after a device loss.
Handle< esengine::Shader > ShaderHandle
Handle to a shader resource.
Handle< esengine::text::BitmapFont > BitmapFontHandle
Handle to a bitmap font resource.
Handle< esengine::Texture > TextureHandle
Handle to a texture resource.
Handle< esengine::Mesh > MeshHandle
Handle to GPU-resident geometry.
u32 rm_createTexture(resource::ResourceManager &rm, u32 width, u32 height, uintptr_t pixelsPtr, u32 pixelsLen, i32 format, bool flipY, u32 content)
u32 rm_registerExternalTextureSized(resource::ResourceManager &rm, u32 glTextureId, u32 width, u32 height, u32 bytes, u32 content)
std::string rm_texturesAwaitingReupload(resource::ResourceManager &rm)
The textures the device is waiting for content for, as handle|content|path lines.
u32 rm_createTextureArray(resource::ResourceManager &rm, u32 width, u32 height, u32 layers, uintptr_t pixelsPtr, u32 pixelsLen, i32 format, i32 filterMode, i32 wrapMode, u32 content)
const T * boundarySpan(uintptr_t ptr, u64 count, const char *what)
Validates a JS-supplied pointer to count elements of T and returns it typed.
std::int32_t i32
Signed 32-bit integer.
@ MirroredRepeat
Tile with mirroring.
@ Repeat
Tile the texture.
@ ClampToEdge
Clamp to edge pixels.
std::string rm_meshesAwaitingRemat(resource::ResourceManager &rm)
Replayable meshes still owed their geometry, as comma-separated handle ids. No path: the engine never...
std::uint64_t u64
Unsigned 64-bit integer.
void rm_setTextureBudget(resource::ResourceManager &rm, u32 bytes)
std::size_t usize
Size type for indexing and sizes.
bool rm_invalidateTexturePath(resource::ResourceManager &rm, const std::string &path)
void rm_releaseTexture(resource::ResourceManager &rm, u32 handleId)
u32 rm_getShaderRefCount(resource::ResourceManager &rm, u32 handleId)
void rm_setRetainedBudget(resource::ResourceManager &rm, u32 bytes)
u32 rm_registerExternalTexture(resource::ResourceManager &rm, u32 glTextureId, u32 width, u32 height, u32 content)
void rm_restoreTextureContent(resource::ResourceManager &rm, u32 handle)
@ Linear
Bilinear interpolation (smooth).
@ Nearest
No interpolation (pixelated look).
void rm_releaseShader(resource::ResourceManager &rm, u32 handleId)
TextureFormat
Texture pixel format.
@ RGB8
3 channels, 8 bits each (24 bpp)
@ SRGB8A8
sRGB-encoded RGBA8: hardware-linearized on sample (32 bpp)
@ RGBA8
4 channels, 8 bits each (32 bpp)
bool rm_supportsCompressedFormat(resource::ResourceManager &rm, i32 format)
GfxCompressedFormat
GPU-compressed texture internal formats for createCompressedTexture.
@ S3TC_DXT5_SRGB
GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT (extension).
u32 rm_getTextureRefCount(resource::ResourceManager &rm, u32 handleId)
u32 rm_getTextureGLId(resource::ResourceManager &rm, u32 handleId)
void rm_setTextureMetadata(resource::ResourceManager &rm, u32 handleId, f32 left, f32 right, f32 top, f32 bottom)
u32 rm_trimTextureCache(resource::ResourceManager &rm)
u32 rm_getTextureNativeId(resource::ResourceManager &rm, u32 handle)
void rm_registerTextureWithPath(resource::ResourceManager &rm, u32 handleId, const std::string &path)
std::span< const T > ConstSpan
void rm_forgoTextureContent(resource::ResourceManager &rm, u32 handle)
Gives up on an owed texture's content; it keeps blank storage.
std::uint32_t u32
Unsigned 32-bit integer.
u32 rm_acquireTextureByPath(resource::ResourceManager &rm, const std::string &path)
std::uint8_t u8
Unsigned 8-bit integer.
bool rm_adoptTextureContent(resource::ResourceManager &rm, u32 target, u32 source)
Moves a freshly loaded texture's pixels behind an owed handle.
u32 rm_createCompressedTexture(resource::ResourceManager &rm, u32 width, u32 height, i32 format, uintptr_t dataPtr, u32 dataLen, u32 mipLevels, u32 content)
u32 rm_createShader(resource::ResourceManager &rm, const std::string &vertSrc, const std::string &fragSrc)
double f64
64-bit floating point
void rm_updateTextureSubregion(resource::ResourceManager &rm, u32 handleId, u32 x, u32 y, u32 width, u32 height, uintptr_t pixelsPtr, u32 pixelsLen)
std::string rm_meshRealizations(resource::ResourceManager &rm)
Every live mesh as handle:realized.
u32 rm_createTextureEx(resource::ResourceManager &rm, u32 width, u32 height, uintptr_t pixelsPtr, u32 pixelsLen, i32 format, bool flipY, i32 filterMode, i32 wrapMode, u32 content)
float f32
32-bit floating point
u32 rm_wrapDeviceTexture(resource::ResourceManager &rm, u32 textureId, u32 width, u32 height)
A resource handle for a device texture another owner keeps (a render target's colour).
Texture creation parameters.
u32 height
Texture height in pixels.
TextureFormat format
Pixel format.
u32 width
Texture width in pixels.
u32 layers
Above 1, a 2D texture array of this many layers (see TextureDesc::layers).
f32 left
Left border width in pixels.
f32 top
Top border height in pixels.
f32 bottom
Bottom border height in pixels.
f32 right
Right border width in pixels.
Metadata associated with a texture resource.
SliceBorder sliceBorder
Nine-slice border configuration.