togles: gamma fix, optimize texture convertation

This commit is contained in:
nillerusr 2022-03-28 00:45:37 +03:00
parent cca56d5a6f
commit 3558b0e03c
9 changed files with 82 additions and 177 deletions

View File

@ -63,10 +63,16 @@ static void *l_egl = NULL;
static void *l_gles = NULL;
typedef void *(*t_glGetProcAddress)( const char * );
t_glGetProcAddress _glGetProcAddress;
typedef EGLBoolean (*t_eglBindAPI)(EGLenum api);
typedef EGLBoolean (*t_eglInitialize)(EGLDisplay display, EGLint *major, EGLint *minor);
typedef EGLDisplay (*t_eglGetDisplay)(NativeDisplayType native_display);
typedef char const *(*t_eglQueryString)(EGLDisplay display, EGLint name);
t_eglBindAPI _eglBindAPI;
t_glGetProcAddress _glGetProcAddress;
t_eglInitialize _eglInitialize;
t_eglGetDisplay _eglGetDisplay;
t_eglQueryString _eglQueryString;
#endif
/*
@ -594,11 +600,25 @@ InitReturnVal_t CSDLMgr::Init()
l_gles = dlopen("libGLESv3.so", RTLD_LAZY);
if( l_egl )
{
_glGetProcAddress = (t_glGetProcAddress)dlsym(l_egl, "eglGetProcAddress");
}
SET_GL_ATTR(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SET_GL_ATTR(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SET_GL_ATTR(SDL_GL_CONTEXT_MINOR_VERSION, 0);
_eglInitialize = (t_eglInitialize)dlsym(l_egl, "eglInitialize");
_eglGetDisplay = (t_eglGetDisplay)dlsym(l_egl, "eglGetDisplay");
_eglQueryString = (t_eglQueryString)dlsym(l_egl, "eglQueryString");
if( _eglInitialize && _eglInitialize && _eglQueryString)
{
EGLDisplay display = _eglGetDisplay(EGL_DEFAULT_DISPLAY);
if( _eglInitialize(display, NULL, NULL) != -1
&& strstr(_eglQueryString(display, EGL_EXTENSIONS) ,"EGL_KHR_gl_colorspace") )
SET_GL_ATTR(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1)
}
#elif ANDROID
bool m_bOGL = false;

View File

@ -312,12 +312,17 @@ ImageFormat D3DFormatToImageFormat( D3DFORMAT format )
switch ( format )
{
#if !defined( _X360 )
#ifdef TOGLES
case D3DFMT_R8G8B8:
return IMAGE_FORMAT_RGB888;
case D3DFMT_A8R8G8B8:
return IMAGE_FORMAT_RGBA8888;
#else
case D3DFMT_R8G8B8:
return IMAGE_FORMAT_BGR888;
#endif
case D3DFMT_A8R8G8B8:
return IMAGE_FORMAT_BGRA8888;
#endif
case D3DFMT_X8R8G8B8:
return IMAGE_FORMAT_BGRX8888;
case D3DFMT_R5G6B5:
@ -426,6 +431,10 @@ D3DFORMAT ImageFormatToD3DFormat( ImageFormat format )
#endif
case IMAGE_FORMAT_BGRA8888:
return D3DFMT_A8R8G8B8;
case IMAGE_FORMAT_RGB888:
return D3DFMT_R8G8B8;
case IMAGE_FORMAT_RGBA8888:
return D3DFMT_A8R8G8B8;
case IMAGE_FORMAT_BGRX8888:
return D3DFMT_X8R8G8B8;
case IMAGE_FORMAT_BGR565:

View File

@ -15,7 +15,9 @@
enum NormalDecodeMode_t
{
NORMAL_DECODE_NONE = 0
NORMAL_DECODE_NONE = 0,
NORMAL_DECODE_ATI2N,
NORMAL_DECODE_ATI2N_ALPHA
};
// Forward declaration

View File

@ -99,7 +99,10 @@ void CPersistentBuffer::Init( EGLMBufferType type,uint nSize )
m_nSize = nSize;
m_nOffset = 0;
m_type = type;
if( strcmp(gGL->glGetString(GL_RENDERER), "ARM") == 0 )
g_bUsePseudoBufs = true; // works faster with Mali gpu
switch ( type )
{
case kGLMVertexBuffer: m_buffGLTarget = GL_ARRAY_BUFFER; break;

View File

@ -790,8 +790,9 @@ bool CGLMShaderPair::ValidateProgramPair()
m_locVertexParams = gGL->glGetUniformLocation( m_program, "vc" );
m_locVertexBoneParams = gGL->glGetUniformLocation( m_program, "vcbones" );
m_locVertexScreenParams = gGL->glGetUniformLocation( m_program, "vcscreen" );
m_locAlphaRef = gGL->glGetUniformLocation( m_program, "alpha_ref" );
if( !gGL->m_bHave_GL_QCOM_alpha_test )
m_locAlphaRef = gGL->glGetUniformLocation( m_program, "alpha_ref" );
m_nScreenWidthHeight = 0xFFFFFFFF;
m_locVertexInteger0 = gGL->glGetUniformLocation( m_program, "i0" );

View File

@ -1106,7 +1106,7 @@ void CGLMTex::CalcTexelDataOffsetAndStrides( int sliceIndex, int x, int y, int z
*zStrideOut = zStride;
}
extern void convert_texture( GLint &internalformat, GLsizei width, GLsizei height, GLenum &format, GLenum &type, void *data );
extern void convert_texture( GLenum &internalformat, GLsizei width, GLsizei height, GLenum &format, GLenum &type, void *data );
void CGLMTex::ReadTexels( GLMTexLockDesc *desc, bool readWholeSlice )
{
@ -1136,13 +1136,12 @@ void CGLMTex::ReadTexels( GLMTexLockDesc *desc, bool readWholeSlice )
GLMTexFormatDesc *format = m_layout->m_format;
GLenum target = m_layout->m_key.m_texGLTarget;
void *sliceAddress = m_backing + m_layout->m_slices[ desc->m_sliceIndex ].m_storageOffset; // this would change for PBO
//int sliceSize = m_layout->m_slices[ desc->m_sliceIndex ].m_storageSize;
// interestingly enough, we can use the same path for both 2D and 3D fetch
switch( target )
{
case GL_TEXTURE_CUBE_MAP:
@ -1174,25 +1173,17 @@ void CGLMTex::ReadTexels( GLMTexLockDesc *desc, bool readWholeSlice )
gGL->glGetIntegerv( GL_DRAW_FRAMEBUFFER_BINDING, &Dfbo );
gGL->glGetIntegerv( GL_READ_FRAMEBUFFER_BINDING, &Rfbo );
gGL->glGenFramebuffers(1, &fbo);
gGL->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
gGL->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_ctx->m_samplers[0].m_pBoundTex->m_texName, 0);
gGL->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, m_ctx->m_samplers[0].m_pBoundTex->m_texName, 0);
GLenum fmt = format->m_glDataFormat;
if( fmt == GL_BGR )
fmt = GL_RGB;
else if( fmt == GL_BGRA )
fmt = GL_RGBA;
gGL->glReadPixels(0, 0, m_layout->m_slices[ desc->m_sliceIndex ].m_xSize, m_layout->m_slices[ desc->m_sliceIndex ].m_ySize, fmt, format->m_glDataType == GL_UNSIGNED_INT_8_8_8_8_REV ? GL_UNSIGNED_BYTE : format->m_glDataType, sliceAddress);
GLint intformat = format->m_glDataFormat;
GLenum _format = format->m_glDataFormat;
GLenum _type = format->m_glDataType;
GLenum dataType = format->m_glDataType;
convert_texture(fmt, 0, 0, fmt, dataType, NULL);
gGL->glReadPixels(0, 0, m_layout->m_slices[ desc->m_sliceIndex ].m_xSize, m_layout->m_slices[ desc->m_sliceIndex ].m_ySize, fmt, dataType, sliceAddress);
// TODO(nillerusr): Don't convert, should change m_format to another one
convert_texture(intformat, m_layout->m_slices[ desc->m_sliceIndex ].m_xSize, m_layout->m_slices[ desc->m_sliceIndex ].m_ySize, _format, _type, sliceAddress);
gGL->glBindFramebuffer(GL_READ_FRAMEBUFFER, Rfbo);
gGL->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, Dfbo);
@ -3329,69 +3320,19 @@ static inline halffloat_t float_f2h(float f)
return ret;
}
static uint8_t srgb_table[256] = {0};
void pixel_srgb_inplace(GLvoid* pixels, GLuint size, GLuint width, GLuint height)
void convert_texture( GLenum &internalformat, GLsizei width, GLsizei height, GLenum &format, GLenum &type, void *data )
{
// return;
if(!srgb_table[255]) {
// create table
for (int i=1; i<256; ++i) {
srgb_table[i] = floorf(255.f*powf(i/255.f, 2.2f)+0.5f);
}
}
uint8_t *data = (uint8_t*)pixels;
int sz = width*height*size;
for (int i=0; i < sz; i += size)
{
data[i] = srgb_table[data[i]];
data[i+1] = srgb_table[data[i+1]];
data[i+2] = srgb_table[data[i+2]];
}
}
if( format == GL_BGRA ) format = GL_RGBA;
if( format == GL_BGR ) format = GL_RGB;
void convert_texture( GLint &internalformat, GLsizei width, GLsizei height, GLenum &format, GLenum &type, void *data )
{
// printf("internalformat=%s format=%s type=%s\n", get_enum_str(internalformat), get_enum_str(format), get_enum_str(type));
if( internalformat == GL_SRGB8 && (format == GL_RGBA || format == GL_BGRA ))
if( internalformat == GL_SRGB8 && format == GL_RGBA )
internalformat = GL_SRGB8_ALPHA8;
if( format == GL_LUMINANCE || format == GL_LUMINANCE_ALPHA )
internalformat = format;
if( internalformat == GL_SRGB8_ALPHA8 )
internalformat = GL_RGBA;
if( internalformat == GL_SRGB8 )
internalformat = GL_RGB;
if( data )
{
uint8_t *_data = (uint8_t*)data;
if( format == GL_BGR )
{
for( int i = 0; i < width*height*3; i+=3 )
{
uint8_t tmp = _data[i];
_data[i] = _data[i+2];
_data[i+2] = tmp;
}
format = GL_RGB;
}
else if( format == GL_BGRA )
{
for( int i = 0; i < width*height*4; i+=4 )
{
uint8_t tmp = _data[i];
_data[i] = _data[i+2];
_data[i+2] = tmp;
}
format = GL_RGBA;
}
if( internalformat == GL_RGBA16 && !gGL->m_bHave_GL_EXT_texture_norm16 )
{
uint16_t *_data = (uint16_t*)data;
@ -3404,57 +3345,18 @@ void convert_texture( GLint &internalformat, GLsizei width, GLsizei height, GLen
new_data[i+2] = _data[i+2] >> 8;
new_data[i+3] = _data[i+3] >> 8;
}
internalformat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
}
else if( internalformat == GL_SRGB8_ALPHA8 )
{
// pixel_srgb_inplace( data, 4, width, height );
// internalformat = GL_RGBA;
}
else if( internalformat == GL_SRGB8 )
{
// pixel_srgb_inplace( data, 3, width, height );
// internalformat = GL_RGB;
}
}
else
{
if( format == GL_BGR )
format = GL_RGB;
else if( format == GL_BGRA )
format = GL_RGBA;
if( internalformat == GL_RGBA16 && !gGL->m_bHave_GL_EXT_texture_norm16 )
{
internalformat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
}
if( internalformat == GL_RGBA16 && !gGL->m_bHave_GL_EXT_texture_norm16 )
{
internalformat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
}
if( type == GL_UNSIGNED_INT_8_8_8_8_REV )
type = GL_UNSIGNED_BYTE;
// printf("internalformat=%s format=%s type=%s\n==========================================\n", get_enum_str(internalformat), get_enum_str(format), get_enum_str(type));
}
void TexImage2D(GLenum target,
GLint level,
GLint internalformat,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const void * data)
{
convert_texture( internalformat, width, height, format, type, data );
gGL->glTexImage2D(target, level, internalformat, width, height, border, format, type, data);
}
GLboolean isDXTc(GLenum format) {
@ -3587,26 +3489,10 @@ void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
}
gGL->glTexImage2D(target, level, intformat, width, height, border, format, type, pixels);
//TexImage2D( target, level, intformat, width, height, border, format, type, pixels );
if( data != pixels )
free(pixels);
}
void TexSubImage2D( GLenum target,
GLint level,
GLint xoffset,
GLint yoffset,
GLsizei width,
GLsizei height,
GLenum format,
GLint internalformat,
GLenum type,
const void * data)
{
convert_texture( internalformat, width, height, format, type, data );
gGL->glTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, data);
}
// TexSubImage should work properly on every driver stack and GPU--enabling by default.
ConVar gl_enabletexsubimage( "gl_enabletexsubimage", "1" );
@ -3664,12 +3550,9 @@ void CGLMTex::WriteTexels( GLMTexLockDesc *desc, bool writeWholeSlice, bool noDa
// allow use of subimage if the target is texture2D and it has already been teximage'd
bool mayUseSubImage = false;
#ifdef ANDROID
if ( (target==GL_TEXTURE_2D) && (m_sliceFlags[ desc->m_sliceIndex ] & kSliceValid) )
{
mayUseSubImage = gl_enabletexsubimage.GetInt() != 0;
}
#endif
mayUseSubImage = true;
// check flavor, 2D, 3D, or cube map
// we also have the choice to use subimage if this is a tex already created. (open question as to benefit)
@ -3776,8 +3659,6 @@ void CGLMTex::WriteTexels( GLMTexLockDesc *desc, bool writeWholeSlice, bool noDa
0, // border
slice->m_storageSize, // imageSize
sliceAddress ); // data
}
else
{
@ -3789,14 +3670,15 @@ void CGLMTex::WriteTexels( GLMTexLockDesc *desc, bool writeWholeSlice, bool noDa
gGL->glPixelStorei( GL_UNPACK_SKIP_PIXELS, writeBox.xmin ); // in pixels
gGL->glPixelStorei( GL_UNPACK_SKIP_ROWS, writeBox.ymin ); // in pixels
TexSubImage2D( target,
convert_texture(intformat, writeBox.xmax - writeBox.xmin, writeBox.ymax - writeBox.ymin, glDataFormat, glDataType, sliceAddress);
gGL->glTexSubImage2D( target,
desc->m_req.m_mip, // level
writeBox.xmin, // xoffset into dest
writeBox.ymin, // yoffset into dest
writeBox.xmax - writeBox.xmin, // width (was slice->m_xSize)
writeBox.ymax - writeBox.ymin, // height (was slice->m_ySize)
glDataFormat, // format
intformat,
glDataType, // type
sliceAddress // data (will be offsetted by the SKIP_PIXELS and SKIP_ROWS - let GL do the math to find the first source texel)
);
@ -3809,7 +3691,9 @@ void CGLMTex::WriteTexels( GLMTexLockDesc *desc, bool writeWholeSlice, bool noDa
{
// uncompressed path
// http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html
TexImage2D( target, // target
convert_texture(intformat, m_layout->m_slices[ desc->m_sliceIndex ].m_xSize, m_layout->m_slices[ desc->m_sliceIndex ].m_ySize, glDataFormat, glDataType, noDataWrite ? NULL : sliceAddress);
gGL->glTexImage2D( target, // target
desc->m_req.m_mip, // level
intformat, // internalformat - don't use format->m_glIntFormat because we have the SRGB select going on above
slice->m_xSize, // width
@ -3854,8 +3738,7 @@ void CGLMTex::WriteTexels( GLMTexLockDesc *desc, bool writeWholeSlice, bool noDa
}
else
{
// uncompressed path
// http://www.opengl.org/sdk/docs/man/xhtml/glTexImage3D.xml
convert_texture(intformat, m_layout->m_slices[ desc->m_sliceIndex ].m_xSize, m_layout->m_slices[ desc->m_sliceIndex ].m_ySize, glDataFormat, glDataType, noDataWrite ? NULL : sliceAddress);
gGL->glTexImage3D( target, // target
desc->m_req.m_mip, // level
intformat, // internalformat

View File

@ -3187,9 +3187,6 @@ int D3DToGL::TranslateShader( uint32* code, CUtlBuffer *pBufDisassembledCode, bo
m_bGeneratingDebugText = (options & D3DToGL_GeneratingDebugText) != 0;
m_bGenerateSRGBWriteSuffix = (options & D3DToGL_OptionSRGBWriteSuffix) != 0;
/* if( debugLabel && (V_strstr( debugLabel ,"vertexlit_and_unlit_generic_ps") || V_strstr( debugLabel ,"vertexlit_and_unlit_generic_bump_ps") ) )
m_bGenerateSRGBWriteSuffix = true;*/
m_NumIndentTabs = 1; // start code indented one tab
m_nLoopDepth = 0;
@ -3907,8 +3904,8 @@ int D3DToGL::TranslateShader( uint32* code, CUtlBuffer *pBufDisassembledCode, bo
if( FindSubcode("_gl_FrontSecondaryColor") && !m_bFrontSecondaryColor )
StrcatToHeaderCode( "in vec4 _gl_FrontSecondaryColor;\n" );
if( m_iFragDataCount && bVertexShader )
if( !gGL->m_bHave_GL_QCOM_alpha_test && m_iFragDataCount && bVertexShader )
StrcatToHeaderCode( "\nuniform float alpha_ref;\n" );
StrcatToHeaderCode( "\nvoid main()\n{\n" );
@ -3926,12 +3923,12 @@ int D3DToGL::TranslateShader( uint32* code, CUtlBuffer *pBufDisassembledCode, bo
StrcatToALUCode( "sRGBFragData.xyz = exp( sRGBFragData.xyz );\n" );
StrcatToALUCode( "gl_FragData[0].xyz = mix( gl_FragData[0].xyz, sRGBFragData, flSRGBWrite );\n" );
}
if( m_iFragDataCount && bVertexShader )
if( !gGL->m_bHave_GL_QCOM_alpha_test && m_iFragDataCount && bVertexShader )
StrcatToALUCode( "if( gl_FragData[0].a < alpha_ref ) { discard; };\n" );
strcat_s( (char*)m_pBufALUCode->Base(), m_pBufALUCode->Size(), "}\n" );
// Put all of the strings together for final program ( pHeaderCode + pAttribCode + pParamCode + pALUCode )
StrcatToHeaderCode( (char*)m_pBufAttribCode->Base() );
StrcatToHeaderCode( (char*)m_pBufParamCode->Base() );

View File

@ -74,6 +74,9 @@ const int kGLMHighWaterUndeleted = 2048;
const int kDeletedTextureDim = 4;
const uint32 g_garbageTextureBits[ 4 * kDeletedTextureDim * kDeletedTextureDim ] = { 0 };
extern void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);
extern void convert_texture( GLenum &internalformat, GLsizei width, GLsizei height, GLenum &format, GLenum &type, void *data );
char g_nullFragmentProgramText [] =
{
"#version 300 es\n"
@ -449,20 +452,6 @@ GLMgr::~GLMgr()
{
}
extern void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
GLsizei width, GLsizei height, GLint border,
GLsizei imageSize, const GLvoid *data);
extern void TexImage2D(GLenum target,
GLint level,
GLint internalformat,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const void * data);
//===============================================================================
GLMContext *GLMgr::NewContext( IDirect3DDevice9 *pDevice, GLMDisplayParams *params )
@ -2961,11 +2950,12 @@ void GLMContext::CleanupTex( GLenum texBind, GLMTexLayout* pLayout, GLuint tex )
const int dataSize = ( chunks * chunks ) * pLayout->m_format->m_bytesPerSquareChunk;
Assert( dataSize <= ( sizeof( uint32) * ARRAYSIZE( g_garbageTextureBits ) ) );
CompressedTexImage2D( texBind, i, pLayout->m_format->m_glIntFormat, mipDim, mipDim, 0, dataSize, 0 );
CompressedTexImage2D( texBind, i, pLayout->m_format->m_glIntFormat, mipDim, mipDim, 0, dataSize, NULL );
}
else
{
TexImage2D( texBind, i, pLayout->m_format->m_glIntFormat, mipDim, mipDim, 0, pLayout->m_format->m_glDataFormat, pLayout->m_format->m_glDataType, 0 );
convert_texture( pLayout->m_format->m_glIntFormat, mipDim, mipDim, pLayout->m_format->m_glDataFormat, pLayout->m_format->m_glDataType, NULL );
gGL->glTexImage2D( texBind, i, pLayout->m_format->m_glIntFormat, mipDim, mipDim, 0, pLayout->m_format->m_glDataFormat, pLayout->m_format->m_glDataType, NULL );
}
}

View File

@ -480,7 +480,7 @@ FORCEINLINE void GLMContext::FlushDrawStates( uint nStartIndex, uint nEndIndex,
}
if( m_pBoundPair->m_locAlphaRef )
if( !gGL->m_bHave_GL_QCOM_alpha_test && m_pBoundPair->m_locAlphaRef )
{
if( !m_AlphaTestEnable.GetData().enable )
gGL->glUniform1f( m_pBoundPair->m_locAlphaRef, 0.0 );