add setImage method to set texture image directly from buffer

This commit is contained in:
Nick Fisher
2025-03-04 18:15:54 +08:00
parent f7fa02180a
commit 9abb192148
3 changed files with 77 additions and 4 deletions

View File

@@ -76,10 +76,22 @@ class FFITexture extends Texture {
}
@override
Future setImage(
int level, Uint8List buffer, PixelDataFormat format, PixelDataType type) {
// TODO: implement setImage
throw UnimplementedError();
Future setImage(int level, Uint8List buffer, int width, int height,
int channels, PixelDataFormat format, PixelDataType type) async {
final success = Texture_setImage(
_engine,
pointer,
level,
buffer.address,
buffer.lengthInBytes,
width,
height,
channels,
format.index,
type.index);
if (!success) {
throw Exception("Failed to set image");
}
}
@override

View File

@@ -192,6 +192,18 @@ enum TPixelDataType {
};
EMSCRIPTEN_KEEPALIVE bool Texture_loadImage(TEngine *tEngine, TTexture *tTexture, TLinearImage *tImage, TPixelDataFormat bufferFormat, TPixelDataType pixelDataType);
EMSCRIPTEN_KEEPALIVE bool Texture_setImage(
TEngine *tEngine,
TTexture *tTexture,
uint32_t level,
uint8_t *data,
size_t size,
uint32_t width,
uint32_t height,
uint32_t channels,
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

@@ -105,6 +105,55 @@ namespace thermion
return true;
}
EMSCRIPTEN_KEEPALIVE bool Texture_setImage(
TEngine *tEngine,
TTexture *tTexture,
uint32_t level,
uint8_t *data,
size_t size,
uint32_t width,
uint32_t height,
uint32_t channels,
uint32_t tBufferFormat,
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);
return false;
}
filament::Texture::PixelBufferDescriptor buffer(
data,
size,
bufferFormat,
pixelDataType);
texture->setImage(*engine, level, std::move(buffer));
return true;
}
EMSCRIPTEN_KEEPALIVE TLinearImage *Image_createEmpty(uint32_t width,uint32_t height,uint32_t channel) {
auto *image = new ::image::LinearImage(width, height, channel);