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});
///