From c29e3e77cd49af26f8c93e9558313173f25dc41d Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 1 Mar 2019 19:23:21 +0100 Subject: [PATCH] Add function TextureUtils::copyToTexture which allows to copy a given ColorBuffer to a GX2Texture --- src/myutils/texture_utils.cpp | 47 +++++++++++++++++++++++++++++++++++ src/myutils/texture_utils.h | 1 + 2 files changed, 48 insertions(+) diff --git a/src/myutils/texture_utils.cpp b/src/myutils/texture_utils.cpp index 7a638ba..efd59b7 100644 --- a/src/myutils/texture_utils.cpp +++ b/src/myutils/texture_utils.cpp @@ -64,6 +64,53 @@ void TextureUtils::drawTexture(GX2Texture * texture, GX2Sampler* sampler, float Texture2DShader::instance()->draw(); } + +void TextureUtils::copyToTexture(GX2ColorBuffer* sourceBuffer, GX2Texture * target) { + if(sourceBuffer == NULL || target == NULL) { + return; + } + if (sourceBuffer->surface.aa == GX2_AA_MODE_1X) { + // If AA is disabled, we can simply use GX2CopySurface. + GX2CopySurface(&sourceBuffer->surface, + sourceBuffer->view_mip, + sourceBuffer->view_first_slice, + &target->surface, 0, 0); + GX2DrawDone(); + } else { + // If AA is enabled, we need to resolve the AA buffer. + + // Allocate surface to resolve buffer onto + GX2Surface tempSurface; + tempSurface = sourceBuffer->surface; + tempSurface.aa = GX2_AA_MODE_1X; + GX2CalcSurfaceSizeAndAlignment(&tempSurface); + + tempSurface.image_data = MemoryUtils::alloc( + tempSurface.image_size, + tempSurface.align + ); + if(tempSurface.image_data == NULL) { + DEBUG_FUNCTION_LINE("VideoSquoosher: failed to allocate AA surface\n"); + if(target->surface.image_data != NULL) { + MemoryUtils::free(target->surface.image_data); + target->surface.image_data = NULL; + } + return; + } + + // Resolve, then copy result to target + GX2ResolveAAColorBuffer(sourceBuffer,&tempSurface, 0, 0); + GX2CopySurface(&tempSurface, 0, 0,&target->surface, 0, 0); + + if(tempSurface.image_data != NULL) { + MemoryUtils::free(tempSurface.image_data); + tempSurface.image_data = NULL; + } + GX2DrawDone(); + GX2Invalidate(GX2_INVALIDATE_CPU, target->surface.image_data, target->surface.image_size); + } +} + bool TextureUtils::convertImageToTexture(const uint8_t *img, int32_t imgSize, void * _texture){ if(!img || (imgSize < 8) || _texture == NULL){ return false; diff --git a/src/myutils/texture_utils.h b/src/myutils/texture_utils.h index 68d23c5..fc369d3 100644 --- a/src/myutils/texture_utils.h +++ b/src/myutils/texture_utils.h @@ -24,6 +24,7 @@ class TextureUtils { public: static bool convertImageToTexture(const uint8_t *img, int32_t imgSize, void * texture); static void drawTexture(GX2Texture * texture, GX2Sampler* sampler, float x, float y, int32_t width, int32_t height, float alpha); + static void copyToTexture(GX2ColorBuffer* sourceBuffer, GX2Texture * target); private: TextureUtils() {} ~TextureUtils() {}