From 6bb3ea86ce3c96ac0f982e068cc2445147712e6c Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 30 Dec 2019 17:28:51 +0800 Subject: [PATCH] Float texture (#2032) * convert internal format for float texture * define some gl enum for windows and macOS * make android/windows support float texture --- native/cocos/base/CCConfiguration.cpp | 9 -------- native/cocos/platform/mac/CCGL-mac.h | 6 +++++ native/cocos/platform/win32/CCGL-win32.h | 6 +++++ .../js-bindings/manual/jsb_opengl_manual.cpp | 23 +++++++++++++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/native/cocos/base/CCConfiguration.cpp b/native/cocos/base/CCConfiguration.cpp index 47ea19fbf7..44666555fb 100644 --- a/native/cocos/base/CCConfiguration.cpp +++ b/native/cocos/base/CCConfiguration.cpp @@ -176,20 +176,11 @@ void Configuration::gatherGPUInfo() _valueDict["gl.supports_OES_packed_depth_stencil"] = Value(_supportsOESPackedDepthStencil); if (_isOpenglES3) { -#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS - _supportsFloatTexture = false; // throws invalid operation when calling glTexSubImage2D -#else _supportsFloatTexture = true; -#endif _supportsShareableVAO = true; } else { - // implementation on these platforms will clamp the data to [0, 1], not suitable for our usage -#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 - _supportsFloatTexture = false; -#else _supportsFloatTexture = checkForGLExtension("texture_float"); -#endif _valueDict["gl.supports_float_texture"] = Value(_supportsFloatTexture); _supportsShareableVAO = checkForGLExtension("vertex_array_object"); diff --git a/native/cocos/platform/mac/CCGL-mac.h b/native/cocos/platform/mac/CCGL-mac.h index f75f82151b..0df8ce9238 100644 --- a/native/cocos/platform/mac/CCGL-mac.h +++ b/native/cocos/platform/mac/CCGL-mac.h @@ -44,6 +44,12 @@ THE SOFTWARE. #define glDepthRangef glDepthRange #define glReleaseShaderCompiler(xxx) +#define GL_RGBA32F_EXT 0x8814 +#define GL_RGB32F_EXT 0x8815 +#define GL_RGBA16F_EXT 0x881A +#define GL_RGB16F_EXT 0x881B +#define GL_HALF_FLOAT_OES 0x8D61 + #endif // __PLATFORM_MAC_CCGL_H__ diff --git a/native/cocos/platform/win32/CCGL-win32.h b/native/cocos/platform/win32/CCGL-win32.h index 0e78bec8c4..255d88ed49 100644 --- a/native/cocos/platform/win32/CCGL-win32.h +++ b/native/cocos/platform/win32/CCGL-win32.h @@ -34,6 +34,12 @@ THE SOFTWARE. #define CC_GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8 +#define GL_RGBA32F_EXT 0x8814 +#define GL_RGB32F_EXT 0x8815 +#define GL_RGBA16F_EXT 0x881A +#define GL_RGB16F_EXT 0x881B +#define GL_HALF_FLOAT_OES 0x8D61 + #endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 #endif // __CCGL_H__ diff --git a/native/cocos/scripting/js-bindings/manual/jsb_opengl_manual.cpp b/native/cocos/scripting/js-bindings/manual/jsb_opengl_manual.cpp index 87e7833a09..50b82d4f29 100644 --- a/native/cocos/scripting/js-bindings/manual/jsb_opengl_manual.cpp +++ b/native/cocos/scripting/js-bindings/manual/jsb_opengl_manual.cpp @@ -2117,6 +2117,28 @@ static void setUnpackAlignmentByWidthAndFormat(uint32_t width, GLenum format) ccPixelStorei(GL_UNPACK_ALIGNMENT, alignment); } +static GLenum convertFloatTextureInternalFormat(GLenum format, GLenum type) +{ + if (format == GL_RGBA) + { + if (type == GL_FLOAT) + return GL_RGBA32F_EXT; + else if (type == GL_HALF_FLOAT_OES) + return GL_RGBA16F_EXT; + } + else if (format == GL_RGB) + { + if (type == GL_FLOAT) + return GL_RGB32F_EXT; + else if (type == GL_HALF_FLOAT_OES) + return GL_RGB16F_EXT; + } + + //FIXME: support other types, such as GL_ALPHA32F_EXT, GL_LUMINANCE32F_EXT? + + return format; +} + // Arguments: GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, ArrayBufferView // Ret value: void static bool JSB_glTexImage2D(se::State& s) { @@ -2172,6 +2194,7 @@ static bool JSB_glTexImage2D(se::State& s) { else setUnpackAlignmentByWidthAndFormat(width, format); + internalformat = convertFloatTextureInternalFormat(format, type); JSB_GL_CHECK(glTexImage2D((GLenum)target , (GLint)level , (GLint)internalformat , (GLsizei)width , (GLsizei)height , (GLint)border , (GLenum)format , (GLenum)type , (GLvoid*)pixels)); return true;