1
0
mirror of https://github.com/blawar/GLideN64.git synced 2024-07-04 10:03:36 +00:00

Optional fragment based writing of depth

This commit is contained in:
Francisco Zurita 2016-08-12 17:54:36 -04:00 committed by Sergey Lipskiy
parent bd04064b6b
commit 74b21d712c
7 changed files with 64 additions and 54 deletions

View File

@ -37,6 +37,7 @@ void Config::resetToDefaults()
generalEmulation.correctTexrectCoords = tcDisable;
generalEmulation.enableNativeResTexrects = 0;
generalEmulation.enableLegacyBlending = 0;
generalEmulation.enableFragmentDepthWrite = 1;
generalEmulation.hacks = 0;
#ifdef ANDROID
generalEmulation.forcePolygonOffset = 0;

View File

@ -4,7 +4,7 @@
#include <string>
#include "Types.h"
#define CONFIG_VERSION_CURRENT 12U
#define CONFIG_VERSION_CURRENT 13U
#define BILINEAR_3POINT 0
#define BILINEAR_STANDARD 1
@ -50,6 +50,7 @@ struct Config
u32 correctTexrectCoords;
u32 enableNativeResTexrects;
u32 enableLegacyBlending;
u32 enableFragmentDepthWrite;
u32 hacks;
#ifdef ANDROID
u32 forcePolygonOffset;

View File

@ -25,6 +25,7 @@ static GLuint g_vertex_shader_object_notex;
static GLuint g_calc_light_shader_object;
static GLuint g_calc_mipmap_shader_object;
static GLuint g_calc_noise_shader_object;
static GLuint g_write_depth_shader_object;
static GLuint g_calc_depth_shader_object;
static GLuint g_readtex_shader_object;
static GLuint g_readtex_ms_shader_object;
@ -215,6 +216,7 @@ void InitShaderCombiner()
g_calc_light_shader_object = _createShader(GL_FRAGMENT_SHADER, fragment_shader_calc_light);
g_calc_mipmap_shader_object = _createShader(GL_FRAGMENT_SHADER, fragment_shader_mipmap);
g_calc_noise_shader_object = _createShader(GL_FRAGMENT_SHADER, fragment_shader_noise);
g_write_depth_shader_object = _createShader(GL_FRAGMENT_SHADER, fragment_shader_depth);
g_readtex_shader_object = _createShader(GL_FRAGMENT_SHADER, config.texture.bilinearMode == BILINEAR_3POINT ? fragment_shader_readtex_3point : fragment_shader_readtex);
g_readtex_ms_shader_object = _createShader(GL_FRAGMENT_SHADER, fragment_shader_readtex_ms);
g_dither_shader_object = _createShader(GL_FRAGMENT_SHADER, fragment_shader_dither);
@ -226,7 +228,8 @@ void InitShaderCombiner()
const int texLoc = glGetUniformLocation(g_monochrome_image_program, "uColorImage");
glUniform1i(texLoc, 0);
if ((config.generalEmulation.hacks&hack_LoadDepthTextures) != 0) {
if (config.generalEmulation.enableFragmentDepthWrite != 0 &&
(config.generalEmulation.hacks&hack_LoadDepthTextures) != 0) {
GLuint depth_texture_shader_object = _createShader(GL_FRAGMENT_SHADER, depth_texture_fragment_shader);
g_depth_texture_program = glCreateProgram();
glBindAttribLocation(g_depth_texture_program, SC_POSITION, "aPosition");
@ -273,6 +276,8 @@ void DestroyShaderCombiner() {
g_readtex_ms_shader_object = 0;
glDeleteShader(g_calc_noise_shader_object);
g_calc_noise_shader_object = 0;
glDeleteShader(g_write_depth_shader_object);
g_write_depth_shader_object = 0;
glDeleteShader(g_dither_shader_object);
g_dither_shader_object = 0;
glDeleteShader(g_calc_depth_shader_object);
@ -325,7 +330,7 @@ ShaderCombiner::ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCo
strFragmentShader.append(fragment_shader_header_noise);
strFragmentShader.append(fragment_shader_header_noise_dither);
strFragmentShader.append(fragment_shader_header_write_depth);
if (bUseLod)
strFragmentShader.append(fragment_shader_header_mipmap);
else {
@ -346,6 +351,7 @@ ShaderCombiner::ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCo
strFragmentShader.append(fragment_shader_header_common_variables_blend_mux_2cycle);
strFragmentShader.append(fragment_shader_header_noise);
strFragmentShader.append(fragment_shader_header_noise_dither);
strFragmentShader.append(fragment_shader_header_write_depth);
#ifdef GL_IMAGE_TEXTURES_SUPPORT
if (video().getRender().isImageTexturesSupported() && config.frameBufferEmulation.N64DepthCompare != 0)
@ -397,27 +403,26 @@ ShaderCombiner::ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCo
if (video().getRender().isImageTexturesSupported() && config.frameBufferEmulation.N64DepthCompare != 0)
strFragmentShader.append(" if (!depth_compare()) discard; \n");
strFragmentShader.append(
#ifdef GLESX
"#ifdef GL_NV_fragdepth \n"
#endif
" if (uRenderTarget != 0) { \n"
" if (uRenderTarget > 1) { \n"
" ivec2 coord = ivec2(gl_FragCoord.xy); \n"
" if (gl_FragDepth >= texelFetch(uDepthTex, coord, 0).r) discard; \n"
" } \n"
" gl_FragDepth = fragColor.r; \n"
" } \n"
#ifdef GLESX
"#endif \n"
#endif
);
if (config.generalEmulation.enableFragmentDepthWrite != 0){
strFragmentShader.append(
" if (uRenderTarget != 0) { \n"
" if (uRenderTarget > 1) { \n"
" ivec2 coord = ivec2(gl_FragCoord.xy); \n"
" if (gl_FragDepth >= texelFetch(uDepthTex, coord, 0).r) discard; \n"
" } \n"
" gl_FragDepth = fragColor.r; \n"
" } \n"
);
}
strFragmentShader.append(fragment_shader_end);
if (config.generalEmulation.enableNoise == 0)
strFragmentShader.append(fragment_shader_dummy_noise);
if (config.generalEmulation.enableFragmentDepthWrite == 0)
strFragmentShader.append(fragment_shader_dummy_depth);
if (bUseLod && config.generalEmulation.enableLOD == 0)
strFragmentShader.append(fragment_shader_fake_mipmap);
@ -442,7 +447,10 @@ ShaderCombiner::ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCo
strFragmentShader.append(fragment_shader_noise);
strFragmentShader.append(fragment_shader_dither);
}
#endif
if (config.generalEmulation.enableFragmentDepthWrite != 0) {
strFragmentShader.append(fragment_shader_depth);
}
#endif // GLESX
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar * strShaderData = strFragmentShader.data();
@ -475,6 +483,10 @@ ShaderCombiner::ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCo
glAttachShader(m_program, g_calc_noise_shader_object);
glAttachShader(m_program, g_dither_shader_object);
}
if (config.generalEmulation.enableFragmentDepthWrite != 0) {
glAttachShader(m_program, g_write_depth_shader_object);
}
#endif
if (CombinerInfo::get().isShaderCacheSupported())
glProgramParameteri(m_program, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
@ -857,6 +869,7 @@ void ShaderCombiner::getShaderCombinerOptionsSet(std::vector<u32> & _vecOptions)
_vecOptions.push_back(config.generalEmulation.enableLOD);
_vecOptions.push_back(config.frameBufferEmulation.N64DepthCompare);
_vecOptions.push_back(config.generalEmulation.enableLegacyBlending);
_vecOptions.push_back(config.generalEmulation.enableFragmentDepthWrite);
}
#ifdef GL_IMAGE_TEXTURES_SUPPORT

View File

@ -264,6 +264,8 @@ static const char* fragment_shader_header_common_variables_blend_mux_2cycle =
static const char* fragment_shader_header_noise =
"lowp float snoise();\n";
static const char* fragment_shader_header_write_depth =
"void writeDepth();\n";
static const char* fragment_shader_header_calc_light =
"void calc_light(in lowp float fLights, in lowp vec3 input_color, out lowp vec3 output_color);\n";
static const char* fragment_shader_header_mipmap =
@ -317,13 +319,7 @@ static const char* fragment_shader_header_main =
" \n"
"void main() \n"
"{ \n"
#ifdef GLESX
"#ifdef GL_NV_fragdepth \n"
" gl_FragDepth = clamp((gl_FragCoord.z * 2.0 - 1.0) * uDepthScale.s + uDepthScale.t, 0.0, 1.0); \n"
"#endif \n"
#else
" gl_FragDepth = clamp((gl_FragCoord.z * 2.0 - 1.0) * uDepthScale.s + uDepthScale.t, 0.0, 1.0); \n"
#endif
" writeDepth(); \n"
" lowp mat4 muxPM = mat4(vec4(0.0), vec4(0.0), uBlendColor, uFogColor); \n"
" lowp vec4 muxA = vec4(0.0, uFogColor.a, vShadeColor.a, 0.0); \n"
" lowp vec4 muxB = vec4(0.0, 1.0, 1.0, 0.0); \n"
@ -551,6 +547,23 @@ static const char* fragment_shader_dummy_noise =
"} \n"
;
static const char* fragment_shader_depth =
AUXILIARY_SHADER_VERSION
#ifndef GLESX
"uniform mediump vec2 uDepthScale; \n"
#endif
"void writeDepth() \n"
"{ \n"
" gl_FragDepth = clamp((gl_FragCoord.z * 2.0 - 1.0) * uDepthScale.s + uDepthScale.t, 0.0, 1.0); \n"
"} \n"
;
static const char* fragment_shader_dummy_depth =
"void writeDepth() \n"
"{ \n"
"} \n"
;
#ifdef GL_IMAGE_TEXTURES_SUPPORT
static const char* depth_compare_shader_float =
#ifndef GLESX
@ -698,13 +711,7 @@ MAIN_SHADER_VERSION
"in mediump vec2 vTexCoord0; \n"
"void main() \n"
"{ \n"
#ifdef GLESX
"#ifdef GL_NV_fragdepth \n"
" gl_FragDepth = texture(uTex0, vTexCoord0).r; \n"
"#endif \n"
#else
" gl_FragDepth = texture(uTex0, vTexCoord0).r; \n"
#endif
"} \n"
;
@ -779,19 +786,11 @@ MAIN_SHADER_VERSION
const char * strTexrectDrawerFragmentShaderTex =
"uniform sampler2D uTex0; \n"
"uniform lowp int uEnableAlphaTest; \n"
"uniform mediump vec2 uDepthScale; \n"
"lowp vec4 uTestColor = vec4(4.0/255.0, 2.0/255.0, 1.0/255.0, 0.0); \n"
"lowp vec4 uTestColor = vec4(4.0/255.0, 2.0/255.0, 1.0/255.0, 0.0); \n"
"in mediump vec2 vTexCoord0; \n"
"out lowp vec4 fragColor; \n"
"void main() \n"
"{ \n"
#ifdef GLESX
"#ifdef GL_NV_fragdepth \n"
" gl_FragDepth = clamp((gl_FragCoord.z * 2.0 - 1.0) * uDepthScale.s + uDepthScale.t, 0.0, 1.0); \n"
"#endif \n"
#else
" gl_FragDepth = clamp((gl_FragCoord.z * 2.0 - 1.0) * uDepthScale.s + uDepthScale.t, 0.0, 1.0); \n"
#endif
" fragColor = texFilter(uTex0, vTexCoord0); \n"
" if (fragColor == uTestColor) discard; \n"
" if (uEnableAlphaTest == 1 && !(fragColor.a > 0.0)) discard; \n"

View File

@ -398,10 +398,7 @@ void OGLRender::TexrectDrawer::init()
assert(m_textureBoundsLoc >= 0);
m_enableAlphaTestLoc = glGetUniformLocation(m_programTex, "uEnableAlphaTest");
assert(m_enableAlphaTestLoc >= 0);
#ifndef GLES2
m_depthScaleLoc = glGetUniformLocation(m_programTex, "uDepthScale");
assert(m_depthScaleLoc >= 0);
#endif
glUseProgram(0);
m_vecRectCoords.reserve(256);
@ -570,10 +567,7 @@ bool OGLRender::TexrectDrawer::draw()
glUniform1i(m_enableAlphaTestLoc, enableAlphaTest);
float texBounds[4] = { s0, t0, s1, t1 };
glUniform4fv(m_textureBoundsLoc, 1, texBounds);
if (RSP.bLLE)
glUniform2f(m_depthScaleLoc, 0.5f, 0.5f);
else
glUniform2f(m_depthScaleLoc, gSP.viewport.vscale[2], gSP.viewport.vtrans[2]);
glEnableVertexAttribArray(SC_TEXCOORD0);
rect[0].x = m_ulx;

View File

@ -235,7 +235,6 @@ private:
GLuint m_programClean;
GLint m_enableAlphaTestLoc;
GLint m_textureBoundsLoc;
GLint m_depthScaleLoc;
gDPScissor m_scissor;
CachedTexture * m_pTexture;
FrameBuffer * m_pBuffer;

View File

@ -72,9 +72,11 @@ bool Config_SetDefault()
assert(res == M64ERR_SUCCESS);
res = ConfigSetDefaultInt(g_configVideoGliden64, "CorrectTexrectCoords", config.generalEmulation.correctTexrectCoords, "Make texrect coordinates continuous to avoid black lines between them. (0=Off, 1=Auto, 2=Force)");
assert(res == M64ERR_SUCCESS);
res = ConfigSetDefaultBool(g_configVideoGliden64, "enableNativeResTexrects", config.generalEmulation.enableNativeResTexrects, "Render 2D texrects in native resolution to fix misalignment between parts of 2D image.");
res = ConfigSetDefaultBool(g_configVideoGliden64, "EnableNativeResTexrects", config.generalEmulation.enableNativeResTexrects, "Render 2D texrects in native resolution to fix misalignment between parts of 2D image.");
assert(res == M64ERR_SUCCESS);
res = ConfigSetDefaultBool(g_configVideoGliden64, "enableLegacyBlending", config.generalEmulation.enableLegacyBlending, "Do not use shaders to emulate N64 blending modes. Works faster on slow GPU. Can cause glitches.");
res = ConfigSetDefaultBool(g_configVideoGliden64, "EnableLegacyBlending", config.generalEmulation.enableLegacyBlending, "Do not use shaders to emulate N64 blending modes. Works faster on slow GPU. Can cause glitches.");
assert(res == M64ERR_SUCCESS);
res = ConfigSetDefaultBool(g_configVideoGliden64, "EnableFragmentDepthWrite", config.generalEmulation.enableFragmentDepthWrite, "Enable writing of fragment depth. Some mobile GPUs do not support it, thus it made optional. Leave enabled.");
assert(res == M64ERR_SUCCESS);
#ifdef ANDROID
res = ConfigSetDefaultBool(g_configVideoGliden64, "ForcePolygonOffset", config.generalEmulation.forcePolygonOffset, "If true, use polygon offset values specified below");
@ -206,8 +208,9 @@ void Config_LoadConfig()
config.generalEmulation.enableHWLighting = ConfigGetParamBool(g_configVideoGliden64, "EnableHWLighting");
config.generalEmulation.enableShadersStorage = ConfigGetParamBool(g_configVideoGliden64, "EnableShadersStorage");
config.generalEmulation.correctTexrectCoords = ConfigGetParamInt(g_configVideoGliden64, "CorrectTexrectCoords");
config.generalEmulation.enableNativeResTexrects = ConfigGetParamBool(g_configVideoGliden64, "enableNativeResTexrects");
config.generalEmulation.enableLegacyBlending = ConfigGetParamBool(g_configVideoGliden64, "enableLegacyBlending");
config.generalEmulation.enableNativeResTexrects = ConfigGetParamBool(g_configVideoGliden64, "EnableNativeResTexrects");
config.generalEmulation.enableLegacyBlending = ConfigGetParamBool(g_configVideoGliden64, "EnableLegacyBlending");
config.generalEmulation.enableFragmentDepthWrite = ConfigGetParamBool(g_configVideoGliden64, "EnableFragmentDepthWrite");
#ifdef ANDROID
config.generalEmulation.forcePolygonOffset = ConfigGetParamBool(g_configVideoGliden64, "ForcePolygonOffset");
config.generalEmulation.polygonOffsetFactor = ConfigGetParamFloat(g_configVideoGliden64, "PolygonOffsetFactor");