support 3D texture/2D texture arrays

This commit is contained in:
Nick Fisher
2025-03-10 18:05:42 +08:00
parent 7d87d229d0
commit e91d1ca0aa
16 changed files with 451 additions and 97 deletions

View File

@@ -1,6 +1,9 @@
import 'dart:typed_data';
import 'package:thermion_dart/src/viewer/src/ffi/src/callbacks.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_texture.dart';
import 'package:thermion_dart/thermion_dart.dart';
import 'package:vector_math/vector_math_64.dart';
class FFIMaterial extends Material {
final Pointer<TEngine> engine;
@@ -51,10 +54,9 @@ class FFIMaterialInstance extends MaterialInstance {
}
@override
Future setParameterFloat4(
String name, double x, double y, double z, double w) async {
MaterialInstance_setParameterFloat4(
pointer, name.toNativeUtf8().cast<Char>(), x, y, z, w);
Future setParameterFloat(String name, double value) async {
MaterialInstance_setParameterFloat(
pointer, name.toNativeUtf8().cast<Char>(), value);
}
@override
@@ -64,9 +66,32 @@ class FFIMaterialInstance extends MaterialInstance {
}
@override
Future setParameterFloat(String name, double value) async {
MaterialInstance_setParameterFloat(
pointer, name.toNativeUtf8().cast<Char>(), value);
Future setParameterFloat3(String name, double x, double y, double z) async {
MaterialInstance_setParameterFloat3(
pointer, name.toNativeUtf8().cast<Char>(), x, y, z);
}
@override
Future setParameterFloat3Array(String name, List<Vector3> array) async {
final ptr = name.toNativeUtf8(allocator: calloc).cast<Char>();
final data = Float64List(array.length * 3);
int i = 0;
for (final item in array) {
data[i] = item.x;
data[i + 1] = item.y;
data[i + 2] = item.z;
i += 3;
}
MaterialInstance_setParameterFloat3Array(
pointer, ptr, data.address, array.length * 3);
calloc.free(ptr);
}
@override
Future setParameterFloat4(
String name, double x, double y, double z, double w) async {
MaterialInstance_setParameterFloat4(
pointer, name.toNativeUtf8().cast<Char>(), x, y, z, w);
}
@override

View File

@@ -112,12 +112,33 @@ class FFITexture extends Texture {
int zOffset,
int width,
int height,
int channels,
int depth,
Uint8List buffer,
PixelDataFormat format,
PixelDataType type) {
// TODO: implement setImage3D
throw UnimplementedError();
PixelDataType type) async {
final success = await withBoolCallback((cb) {
Texture_setImageWithDepthRenderThread(
_engine,
pointer,
level,
buffer.address,
buffer.lengthInBytes,
0,
0,
zOffset,
width,
height,
channels,
depth,
format.index,
type.index,
cb);
});
if (!success) {
throw Exception("Failed to set image");
}
}
@override

View File

@@ -64,14 +64,11 @@ external void MaterialInstance_setDepthCulling(
@ffi.Native<
ffi.Void Function(ffi.Pointer<TMaterialInstance>, ffi.Pointer<ffi.Char>,
ffi.Double, ffi.Double, ffi.Double, ffi.Double)>(isLeaf: true)
external void MaterialInstance_setParameterFloat4(
ffi.Double)>(isLeaf: true)
external void MaterialInstance_setParameterFloat(
ffi.Pointer<TMaterialInstance> materialInstance,
ffi.Pointer<ffi.Char> propertyName,
double x,
double y,
double w,
double z,
double value,
);
@ffi.Native<
@@ -86,11 +83,35 @@ external void MaterialInstance_setParameterFloat2(
@ffi.Native<
ffi.Void Function(ffi.Pointer<TMaterialInstance>, ffi.Pointer<ffi.Char>,
ffi.Double)>(isLeaf: true)
external void MaterialInstance_setParameterFloat(
ffi.Double, ffi.Double, ffi.Double)>(isLeaf: true)
external void MaterialInstance_setParameterFloat3(
ffi.Pointer<TMaterialInstance> materialInstance,
ffi.Pointer<ffi.Char> propertyName,
double value,
double x,
double y,
double z,
);
@ffi.Native<
ffi.Void Function(ffi.Pointer<TMaterialInstance>, ffi.Pointer<ffi.Char>,
ffi.Pointer<ffi.Double>, ffi.Uint32)>(isLeaf: true)
external void MaterialInstance_setParameterFloat3Array(
ffi.Pointer<TMaterialInstance> tMaterialInstance,
ffi.Pointer<ffi.Char> propertyName,
ffi.Pointer<ffi.Double> raw,
int length,
);
@ffi.Native<
ffi.Void Function(ffi.Pointer<TMaterialInstance>, ffi.Pointer<ffi.Char>,
ffi.Double, ffi.Double, ffi.Double, ffi.Double)>(isLeaf: true)
external void MaterialInstance_setParameterFloat4(
ffi.Pointer<TMaterialInstance> materialInstance,
ffi.Pointer<ffi.Char> propertyName,
double x,
double y,
double w,
double z,
);
@ffi.Native<
@@ -912,6 +933,39 @@ external bool Texture_setImage(
int pixelDataType,
);
@ffi.Native<
ffi.Bool Function(
ffi.Pointer<TEngine>,
ffi.Pointer<TTexture>,
ffi.Uint32,
ffi.Pointer<ffi.Uint8>,
ffi.Size,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32)>(isLeaf: true)
external bool Texture_setImageWithDepth(
ffi.Pointer<TEngine> tEngine,
ffi.Pointer<TTexture> tTexture,
int level,
ffi.Pointer<ffi.Uint8> data,
int size,
int x_offset,
int y_offset,
int z_offset,
int width,
int height,
int channels,
int depth,
int bufferFormat,
int pixelDataType,
);
@ffi.Native<
ffi.Pointer<TLinearImage> Function(
ffi.Uint32, ffi.Uint32, ffi.Uint32)>(isLeaf: true)
@@ -1634,6 +1688,7 @@ external void Engine_destroyMaterialRenderThread(
ffi.Pointer<TEngine>,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint8,
ffi.UnsignedInt,
ffi.UnsignedInt,
@@ -1644,6 +1699,7 @@ external void _Engine_buildTextureRenderThread(
ffi.Pointer<TEngine> engine,
int width,
int height,
int depth,
int levels,
int sampler,
int format,
@@ -1655,6 +1711,7 @@ void Engine_buildTextureRenderThread(
ffi.Pointer<TEngine> engine,
int width,
int height,
int depth,
int levels,
TTextureSamplerType sampler,
TTextureFormat format,
@@ -1665,6 +1722,7 @@ void Engine_buildTextureRenderThread(
engine,
width,
height,
depth,
levels,
sampler.value,
format.value,
@@ -2267,6 +2325,42 @@ external void Texture_setImageRenderThread(
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Bool)>> onComplete,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<TEngine>,
ffi.Pointer<TTexture>,
ffi.Uint32,
ffi.Pointer<ffi.Uint8>,
ffi.Size,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Bool)>>)>(
isLeaf: true)
external void Texture_setImageWithDepthRenderThread(
ffi.Pointer<TEngine> tEngine,
ffi.Pointer<TTexture> tTexture,
int level,
ffi.Pointer<ffi.Uint8> data,
int size,
int x_offset,
int y_offset,
int z_offset,
int width,
int height,
int channels,
int depth,
int bufferFormat,
int pixelDataType,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Bool)>> onComplete,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<TRenderTarget>,
@@ -3007,6 +3101,7 @@ external ffi.Pointer<TEntityManager> Engine_getEntityManager(
ffi.Pointer<TEngine>,
ffi.Uint32,
ffi.Uint32,
ffi.Uint32,
ffi.Uint8,
ffi.UnsignedInt,
ffi.UnsignedInt)>(symbol: "Engine_buildTexture", isLeaf: true)
@@ -3014,6 +3109,7 @@ external ffi.Pointer<TTexture> _Engine_buildTexture(
ffi.Pointer<TEngine> engine,
int width,
int height,
int depth,
int levels,
int sampler,
int format,
@@ -3023,6 +3119,7 @@ ffi.Pointer<TTexture> Engine_buildTexture(
ffi.Pointer<TEngine> engine,
int width,
int height,
int depth,
int levels,
TTextureSamplerType sampler,
TTextureFormat format,
@@ -3031,6 +3128,7 @@ ffi.Pointer<TTexture> Engine_buildTexture(
engine,
width,
height,
depth,
levels,
sampler.value,
format.value,
@@ -3808,20 +3906,22 @@ typedef DartPickCallbackFunction = void Function(
enum TTextureSamplerType {
SAMPLER_2D(0),
SAMPLER_CUBEMAP(1),
SAMPLER_EXTERNAL(2),
SAMPLER_3D(3),
SAMPLER_2D_ARRAY(4);
SAMPLER_2D_ARRAY(1),
SAMPLER_CUBEMAP(2),
SAMPLER_EXTERNAL(3),
SAMPLER_3D(4),
SAMPLER_CUBEMAP_ARRAY(5);
final int value;
const TTextureSamplerType(this.value);
static TTextureSamplerType fromValue(int value) => switch (value) {
0 => SAMPLER_2D,
1 => SAMPLER_CUBEMAP,
2 => SAMPLER_EXTERNAL,
3 => SAMPLER_3D,
4 => SAMPLER_2D_ARRAY,
1 => SAMPLER_2D_ARRAY,
2 => SAMPLER_CUBEMAP,
3 => SAMPLER_EXTERNAL,
4 => SAMPLER_3D,
5 => SAMPLER_CUBEMAP_ARRAY,
_ =>
throw ArgumentError("Unknown value for TTextureSamplerType: $value"),
};

View File

@@ -1830,7 +1830,7 @@ class ThermionViewerFFI extends ThermionViewer {
///
///
Future<Texture> createTexture(int width, int height,
{int levels = 1,
{int depth = 1, int levels = 1,
TextureSamplerType textureSamplerType = TextureSamplerType.SAMPLER_2D,
TextureFormat textureFormat = TextureFormat.RGBA16F}) async {
final texturePtr = await withPointerCallback<TTexture>((cb) {
@@ -1838,6 +1838,7 @@ class ThermionViewerFFI extends ThermionViewer {
_engine!,
width,
height,
depth,
levels,
TTextureSamplerType.values[textureSamplerType.index],
TTextureFormat.values[textureFormat.index],

View File

@@ -18,7 +18,7 @@ class Geometry {
}) : indices = Uint16List.fromList(indices),
normals = normals ?? Float32List(0),
uvs = uvs ?? Float32List(0) {
assert(this.uvs.length == 0 || this.uvs.length == (vertices.length ~/ 3) * 2);
assert(this.uvs.length == 0 || this.uvs.length == (vertices.length ~/ 3 * 2), "Expected either zero or ${indices.length * 2} UVs, got ${this.uvs.length}");
}
void scale(double factor) {

View File

@@ -1,4 +1,7 @@
import 'dart:typed_data';
import 'package:thermion_dart/thermion_dart.dart';
import 'package:vector_math/vector_math_64.dart';
enum SamplerCompareFunction {
/// !< Less or equal
@@ -102,10 +105,13 @@ abstract class MaterialInstance {
Future setDepthWriteEnabled(bool enabled);
Future setDepthFunc(SamplerCompareFunction depthFunc);
Future setDepthCullingEnabled(bool enabled);
Future setParameterFloat(String name, double x);
Future setParameterFloat2(String name, double x, double y);
Future setParameterFloat3(String name, double x, double y, double z);
Future setParameterFloat3Array(String name, List<Vector3> data);
Future setParameterFloat4(
String name, double x, double y, double z, double w);
Future setParameterFloat2(String name, double x, double y);
Future setParameterFloat(String name, double x);
Future setParameterInt(String name, int value);
Future setParameterBool(String name, bool value);
Future setParameterTexture(

View File

@@ -1,24 +1,13 @@
import 'dart:typed_data';
import 'package:thermion_dart/src/viewer/src/ffi/src/callbacks.dart';
import 'package:thermion_dart/thermion_dart.dart';
/// Defines the type of sampler to use with a texture
enum TextureSamplerType {
/// 2D texture
SAMPLER_2D,
/// Cubemap texture
SAMPLER_2D_ARRAY,
SAMPLER_CUBEMAP,
/// External texture (video/camera)
SAMPLER_EXTERNAL,
/// 3D texture
SAMPLER_3D,
/// 2D array texture
SAMPLER_2D_ARRAY
SAMPLER_CUBEMAP_ARRAY
}
/// Defines internal texture formats
@@ -306,7 +295,7 @@ abstract class Texture {
covariant LinearImage image, PixelDataFormat format, PixelDataType type);
/// Sets the image data for a 2D texture or a texture level
Future setImage(int level, Uint8List buffer, int width, int height,
Future setImage(int level, Uint8List buffer, int width, int height,
int channels, PixelDataFormat format, PixelDataType type);
/// Sets the image data for a region of a 2D texture
@@ -321,6 +310,7 @@ abstract class Texture {
int zOffset,
int width,
int height,
int channels,
int depth,
Uint8List buffer,
PixelDataFormat format,

View File

@@ -776,7 +776,7 @@ abstract class ThermionViewer {
///
///
Future<Texture> createTexture(int width, int height,
{TextureSamplerType textureSamplerType = TextureSamplerType.SAMPLER_2D,
{int depth = 1, int levels = 1, TextureSamplerType textureSamplerType = TextureSamplerType.SAMPLER_2D,
TextureFormat textureFormat = TextureFormat.RGBA32F});
///

View File

@@ -23,6 +23,7 @@ EMSCRIPTEN_KEEPALIVE TEntityManager *Engine_getEntityManager(TEngine *engine);
EMSCRIPTEN_KEEPALIVE TTexture *Engine_buildTexture(TEngine *engine,
uint32_t width,
uint32_t height,
uint32_t depth,
uint8_t levels,
TTextureSamplerType sampler,
TTextureFormat format);

View File

@@ -75,9 +75,11 @@ extern "C"
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setDepthWrite(TMaterialInstance *materialInstance, bool enabled);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setDepthCulling(TMaterialInstance *materialInstance, bool enabled);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat4(TMaterialInstance *materialInstance, const char *propertyName, double x, double y, double w, double z);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat2(TMaterialInstance *materialInstance, const char *propertyName, double x, double y);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat(TMaterialInstance *materialInstance, const char *propertyName, double value);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat2(TMaterialInstance *materialInstance, const char *propertyName, double x, double y);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat3(TMaterialInstance *materialInstance, const char *propertyName, double x, double y, double z);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat3Array(TMaterialInstance *tMaterialInstance, const char *propertyName, double *raw, uint32_t length);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat4(TMaterialInstance *materialInstance, const char *propertyName, double x, double y, double w, double z);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterInt(TMaterialInstance *materialInstance, const char *propertyName, int value);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterBool(TMaterialInstance *materialInstance, const char *propertyName, bool value);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterTexture(TMaterialInstance *materialInstance, const char *propertyName, TTexture *texture, TTextureSampler *sampler);

View File

@@ -15,11 +15,12 @@ extern "C"
enum TTextureSamplerType
{
SAMPLER_2D = 0, // 2D texture
SAMPLER_CUBEMAP, // Cubemap texture
SAMPLER_EXTERNAL, // External texture (video/camera)
SAMPLER_3D, // 3D texture
SAMPLER_2D_ARRAY // 2D array texture
SAMPLER_2D = 0,
SAMPLER_2D_ARRAY = 1,
SAMPLER_CUBEMAP=2,
SAMPLER_EXTERNAL=3,
SAMPLER_3D=4,
SAMPLER_CUBEMAP_ARRAY=5
};
enum TTextureFormat
@@ -204,6 +205,22 @@ EMSCRIPTEN_KEEPALIVE bool Texture_setImage(
uint32_t bufferFormat,
uint32_t pixelDataType
);
EMSCRIPTEN_KEEPALIVE bool Texture_setImageWithDepth(
TEngine *tEngine,
TTexture *tTexture,
uint32_t level,
uint8_t *data,
size_t size,
uint32_t x_offset,
uint32_t y_offset,
uint32_t z_offset,
uint32_t width,
uint32_t height,
uint32_t channels,
uint32_t depth,
uint32_t bufferFormat,
uint32_t pixelDataType
);
EMSCRIPTEN_KEEPALIVE TLinearImage *Image_createEmpty(uint32_t width,uint32_t height,uint32_t channel);
EMSCRIPTEN_KEEPALIVE TLinearImage *Image_decode(uint8_t* data, size_t length, const char* name = "image");
EMSCRIPTEN_KEEPALIVE float *Image_getBytes(TLinearImage *tLinearImage);

View File

@@ -53,6 +53,7 @@ namespace thermion
EMSCRIPTEN_KEEPALIVE void Engine_buildTextureRenderThread(TEngine *engine,
uint32_t width,
uint32_t height,
uint32_t depth,
uint8_t levels,
TTextureSamplerType sampler,
TTextureFormat format,
@@ -169,6 +170,23 @@ namespace thermion
uint32_t pixelDataType,
void (*onComplete)(bool)
);
EMSCRIPTEN_KEEPALIVE void Texture_setImageWithDepthRenderThread(
TEngine *tEngine,
TTexture *tTexture,
uint32_t level,
uint8_t *data,
size_t size,
uint32_t x_offset,
uint32_t y_offset,
uint32_t z_offset,
uint32_t width,
uint32_t height,
uint32_t channels,
uint32_t depth,
uint32_t bufferFormat,
uint32_t pixelDataType,
void (*onComplete)(bool)
);
EMSCRIPTEN_KEEPALIVE void RenderTarget_getColorTextureRenderThread(TRenderTarget *tRenderTarget, void (*onComplete)(TTexture *));
// TextureSampler methods

View File

@@ -226,21 +226,29 @@ namespace thermion
EMSCRIPTEN_KEEPALIVE TTexture *Engine_buildTexture(TEngine *tEngine,
uint32_t width,
uint32_t height,
uint32_t depth,
uint8_t levels,
TTextureSamplerType tSamplerType,
TTextureFormat tFormat)
{
TRACE("Creating texture %dx%d (depth %d), sampler type %d, format %d", width, height, depth, static_cast<int>(tSamplerType), static_cast<int>(tFormat));
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto format = convertToFilamentFormat(tFormat);
auto samplerType = static_cast<::filament::Texture::Sampler>(static_cast<int>(tSamplerType));
auto *texture = Texture::Builder()
.width(width)
.height(height)
.depth(depth)
.levels(levels)
.sampler(samplerType)
.format(format)
.build(*engine);
Log("Created texture %d x %d, format %d", texture->getWidth(), texture->getHeight(), texture->getFormat());
if(texture) {
TRACE("Texture successfully created");
} else {
Log("Error: failed to created texture");
}
return reinterpret_cast<TTexture *>(texture);
}

View File

@@ -1,3 +1,5 @@
#include <vector>
#include <filament/MaterialInstance.h>
#include <filament/Material.h>
#include <math/mat4.h>
@@ -41,11 +43,11 @@ namespace thermion
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setDepthCulling(enabled);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat4(TMaterialInstance *tMaterialInstance, const char *propertyName, double x, double y, double z, double w)
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat(TMaterialInstance *tMaterialInstance, const char *propertyName, double value)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
filament::math::float4 data{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w)};
materialInstance->setParameter(propertyName, data);
auto fValue = static_cast<float>(value);
materialInstance->setParameter(propertyName, fValue);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat2(TMaterialInstance *materialInstance, const char *propertyName, double x, double y)
@@ -54,16 +56,36 @@ namespace thermion
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setParameter(propertyName, data);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat(TMaterialInstance *tMaterialInstance, const char *propertyName, double value)
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat3(TMaterialInstance *materialInstance, const char *propertyName, double x, double y, double z)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
auto fValue = static_cast<float>(value);
materialInstance->setParameter(propertyName, fValue);
filament::math::float3 data{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) };
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setParameter(propertyName, data);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterInt(TMaterialInstance *materialInstance, const char *propertyName, int value)
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat3Array(TMaterialInstance *tMaterialInstance, const char *propertyName, double* raw, uint32_t length) {
TRACE("Setting property %s to array from raw length %d", propertyName, length);
std::vector<filament::math::float3> data;
for(int i = 0; i < length; i+=3) {
// TRACE("Vector %f %f %f", static_cast<float>(raw[i]), static_cast<float>(raw[i+1]), static_cast<float>(raw[i+2]));
data.push_back(filament::math::float3 { static_cast<float>(raw[i]), static_cast<float>(raw[i+1]), static_cast<float>(raw[i+2]) });
}
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
materialInstance->setParameter(propertyName, data.data(), data.size());
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat4(TMaterialInstance *tMaterialInstance, const char *propertyName, double x, double y, double z, double w)
{
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setParameter(propertyName, value);
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
filament::math::float4 data{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w)};
materialInstance->setParameter(propertyName, data);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterInt(TMaterialInstance *tMaterialInstance, const char *propertyName, int value)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
materialInstance->setParameter(propertyName, value);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterBool(TMaterialInstance *materialInstance, const char *propertyName, bool value)

View File

@@ -93,7 +93,7 @@ namespace thermion
return false;
}
Log("Dimensions %d x %d, channels %d, size %d, buffer format %d and pixel data type %d", w, h, channels, size, bufferFormat, pixelDataType);
TRACE("Loading image from dimensions %d x %d, channels %d, size %d, buffer format %d and pixel data type %d", w, h, channels, size, bufferFormat, pixelDataType);
filament::Texture::PixelBufferDescriptor buffer(
image->getPixelRef(),
@@ -115,51 +115,151 @@ namespace thermion
uint32_t height,
uint32_t channels,
uint32_t tBufferFormat,
uint32_t tPixelDataType
) {
uint32_t tPixelDataType)
{
auto engine = reinterpret_cast<filament::Engine *>(tEngine);
auto texture = reinterpret_cast<filament::Texture *>(tTexture);
auto bufferFormat = static_cast<PixelBufferDescriptor::PixelDataFormat>(tBufferFormat);
auto pixelDataType = static_cast<PixelBufferDescriptor::PixelDataType>(tPixelDataType);
switch (bufferFormat)
{
case PixelBufferDescriptor::PixelDataFormat::RGB:
case PixelBufferDescriptor::PixelDataFormat::RGBA:
if(size != width * height * channels * sizeof(float)) {
Log("Size mismatch");
return false;
}
break;
case PixelBufferDescriptor::PixelDataFormat::RGB_INTEGER:
case PixelBufferDescriptor::PixelDataFormat::RGBA_INTEGER:
if(size != width * height * channels * sizeof(uint8_t)) {
Log("Size mismatch");
// return false;
}
break;
default:
Log("Unsupported buffer format type : %d", bufferFormat);
case PixelBufferDescriptor::PixelDataFormat::RGB:
case PixelBufferDescriptor::PixelDataFormat::RGBA:
if (size != width * height * channels * sizeof(float))
{
Log("Size mismatch");
return false;
}
break;
case PixelBufferDescriptor::PixelDataFormat::RGB_INTEGER:
case PixelBufferDescriptor::PixelDataFormat::RGBA_INTEGER:
if (size != width * height * channels * sizeof(uint8_t))
{
Log("Size mismatch");
// return false;
}
break;
default:
Log("Unsupported buffer format type : %d", bufferFormat);
return false;
}
filament::Texture::PixelBufferDescriptor buffer(
data,
// the texture upload is async, so we need to copy the buffer
auto *buffer = new std::vector<uint8_t>(size);
std::copy(data, data + size, buffer->begin());
filament::Texture::PixelBufferDescriptor::Callback freeCallback = [](void *buf, size_t,
void *data)
{
delete reinterpret_cast<std::vector<uint8_t> *>(data);
};
filament::Texture::PixelBufferDescriptor pbd(
buffer->data(),
size,
bufferFormat,
pixelDataType);
texture->setImage(*engine, level, std::move(buffer));
pixelDataType,
1, // alignment
0, // left
0, // top
0, // stride
freeCallback,
buffer);
texture->setImage(*engine, level, std::move(pbd));
return true;
}
EMSCRIPTEN_KEEPALIVE bool Texture_setImageWithDepth(
TEngine *tEngine,
TTexture *tTexture,
uint32_t level,
uint8_t *data,
size_t size,
uint32_t x_offset,
uint32_t y_offset,
uint32_t z_offset,
uint32_t width,
uint32_t height,
uint32_t channels,
uint32_t depth,
uint32_t tBufferFormat,
uint32_t tPixelDataType)
{
auto engine = reinterpret_cast<filament::Engine *>(tEngine);
EMSCRIPTEN_KEEPALIVE TLinearImage *Image_createEmpty(uint32_t width,uint32_t height,uint32_t channel) {
auto *image = new ::image::LinearImage(width, height, channel);
return reinterpret_cast<TLinearImage*>(image);
auto texture = reinterpret_cast<filament::Texture *>(tTexture);
auto bufferFormat = static_cast<PixelBufferDescriptor::PixelDataFormat>(tBufferFormat);
auto pixelDataType = static_cast<PixelBufferDescriptor::PixelDataType>(tPixelDataType);
TRACE("Setting texture image (depth %d, %dx%dx%d (%d bytes, z_offset %d)", depth, width, height, channels, size, z_offset);
switch (bufferFormat)
{
case PixelBufferDescriptor::PixelDataFormat::RGB:
case PixelBufferDescriptor::PixelDataFormat::RGBA:
if (size != width * height * channels * sizeof(float))
{
Log("Size mismatch");
return false;
}
break;
case PixelBufferDescriptor::PixelDataFormat::RGB_INTEGER:
case PixelBufferDescriptor::PixelDataFormat::RGBA_INTEGER:
if (size != width * height * channels * sizeof(uint8_t))
{
Log("Size mismatch");
// return false;
}
break;
default:
Log("Unsupported buffer format type : %d", bufferFormat);
return false;
}
// the texture upload is async, so we need to copy the buffer
auto *buffer = new std::vector<uint8_t>(size);
std::copy(data, data + size, buffer->begin());
filament::Texture::PixelBufferDescriptor::Callback freeCallback = [](void *buf, size_t,
void *data)
{
delete reinterpret_cast<std::vector<uint8_t> *>(data);
};
filament::Texture::PixelBufferDescriptor pbd(
buffer->data(),
size,
bufferFormat,
pixelDataType,
1, // alignment
0, // left
0, // top
0, // stride
freeCallback,
buffer);
texture->setImage(
*engine,
level,
x_offset,
y_offset,
z_offset,
width,
height,
depth,
std::move(pbd));
return true;
}
EMSCRIPTEN_KEEPALIVE TLinearImage *Image_createEmpty(uint32_t width, uint32_t height, uint32_t channel)
{
auto *image = new ::image::LinearImage(width, height, channel);
return reinterpret_cast<TLinearImage *>(image);
}
EMSCRIPTEN_KEEPALIVE TTextureSampler *TextureSampler_create()
{
@@ -288,11 +388,12 @@ namespace thermion
delete textureSampler;
}
}
EMSCRIPTEN_KEEPALIVE TTexture *RenderTarget_getColorTexture(TRenderTarget *tRenderTarget) {
auto renderTarget = reinterpret_cast<filament::RenderTarget*>(tRenderTarget);
EMSCRIPTEN_KEEPALIVE TTexture *RenderTarget_getColorTexture(TRenderTarget *tRenderTarget)
{
auto renderTarget = reinterpret_cast<filament::RenderTarget *>(tRenderTarget);
auto texture = renderTarget->getTexture(filament::RenderTarget::AttachmentPoint::COLOR0);
return reinterpret_cast<TTexture*>(texture);
return reinterpret_cast<TTexture *>(texture);
}
#ifdef __cplusplus

View File

@@ -332,6 +332,7 @@ extern "C"
EMSCRIPTEN_KEEPALIVE void Engine_buildTextureRenderThread(TEngine *engine,
uint32_t width,
uint32_t height,
uint32_t depth,
uint8_t levels,
TTextureSamplerType sampler,
TTextureFormat format,
@@ -340,7 +341,7 @@ extern "C"
std::packaged_task<void()> lambda(
[=]() mutable
{
auto texture = Engine_buildTexture(engine, width, height, levels, sampler, format);
auto texture = Engine_buildTexture(engine, width, height, depth, levels, sampler, format);
onComplete(texture);
});
auto fut = _rl->add_task(lambda);
@@ -909,6 +910,47 @@ extern "C"
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void Texture_setImageWithDepthRenderThread(
TEngine *tEngine,
TTexture *tTexture,
uint32_t level,
uint8_t *data,
size_t size,
uint32_t x_offset,
uint32_t y_offset,
uint32_t z_offset,
uint32_t width,
uint32_t height,
uint32_t channels,
uint32_t depth,
uint32_t bufferFormat,
uint32_t pixelDataType,
void (*onComplete)(bool)
) {
std::packaged_task<void()> lambda(
[=]() mutable
{
bool result = Texture_setImageWithDepth(
tEngine,
tTexture,
level,
data,
size,
x_offset,
y_offset,
z_offset,
width,
height,
channels,
depth,
bufferFormat,
pixelDataType
);
onComplete(result);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void RenderTarget_getColorTextureRenderThread(TRenderTarget *tRenderTarget, void (*onComplete)(TTexture *))
{
std::packaged_task<void()> lambda(