Skip to content

Commit

Permalink
GLES: Implement a custom modulo function for PowerVR. Fixes blockines…
Browse files Browse the repository at this point in the history
…s, see #7153 and #7150
  • Loading branch information
hrydgard committed Dec 14, 2014
1 parent e2d2950 commit f424bf7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
19 changes: 14 additions & 5 deletions GPU/GLES/FragmentShaderGenerator.cpp
Expand Up @@ -487,6 +487,7 @@ void GenerateFragmentShader(char *buffer) {
const char *texture = "texture2D";
const char *texelFetch = NULL;
bool highpFog = false;
bool highpTexcoord = false;
bool bitwiseOps = false;

#if defined(USING_GLES2)
Expand All @@ -511,7 +512,8 @@ void GenerateFragmentShader(char *buffer) {
// PowerVR needs highp to do the fog in MHU correctly.
// Others don't, and some can't handle highp in the fragment shader.
highpFog = gl_extensions.gpuVendor == GPU_VENDOR_POWERVR;

highpTexcoord = highpFog;

// GL_NV_shader_framebuffer_fetch available on mobile platform and ES 2.0 only but not desktop
if (gl_extensions.NV_shader_framebuffer_fetch) {
WRITE(p, "#extension GL_NV_shader_framebuffer_fetch : require\n");
Expand Down Expand Up @@ -635,9 +637,9 @@ void GenerateFragmentShader(char *buffer) {
}
if (doTexture) {
if (doTextureProjection)
WRITE(p, "%s mediump vec3 v_texcoord;\n", varying);
WRITE(p, "%s %s vec3 v_texcoord;\n", varying, highpTexcoord ? "highp" : "mediump");
else
WRITE(p, "%s mediump vec2 v_texcoord;\n", varying);
WRITE(p, "%s %s vec2 v_texcoord;\n", varying, highpTexcoord ? "highp" : "mediump");
}

if (!g_Config.bFragmentTestCache) {
Expand Down Expand Up @@ -668,6 +670,11 @@ void GenerateFragmentShader(char *buffer) {
WRITE(p, "out vec4 fragColor0;\n");
}

// PowerVR needs a custom modulo function. For some reason, this has far higher precision than the builtin one.
if (gl_extensions.gpuVendor == GPU_VENDOR_POWERVR && gstate_c.needShaderTexClamp) {
WRITE(p, "float mymod(float a, float b) { return a - b * floor(a / b); }\n");
}

WRITE(p, "void main() {\n");

if (gstate.isModeClear()) {
Expand Down Expand Up @@ -700,15 +707,17 @@ void GenerateFragmentShader(char *buffer) {
vcoord = "1.0 - " + vcoord;
}

std::string modulo = gl_extensions.gpuVendor == GPU_VENDOR_POWERVR ? "mymod" : "mod";

if (gstate.isTexCoordClampedS()) {
ucoord = "clamp(" + ucoord + ", u_texclamp.z, u_texclamp.x - u_texclamp.z)";
} else {
ucoord = "mod(" + ucoord + ", u_texclamp.x)";
ucoord = modulo + "(" + ucoord + ", u_texclamp.x)";
}
if (gstate.isTexCoordClampedT()) {
vcoord = "clamp(" + vcoord + ", u_texclamp.w, u_texclamp.y - u_texclamp.w)";
} else {
vcoord = "mod(" + vcoord + ", u_texclamp.y)";
vcoord = modulo + "(" + vcoord + ", u_texclamp.y)";
}
if (textureAtOffset) {
ucoord = "(" + ucoord + " + u_texclampoff.x)";
Expand Down
7 changes: 5 additions & 2 deletions GPU/GLES/VertexShaderGenerator.cpp
Expand Up @@ -163,6 +163,7 @@ void GenerateVertexShader(int prim, u32 vertType, char *buffer, bool useHWTransf
const char *attribute = "attribute";
const char * const * boneWeightDecl = boneWeightAttrDecl;
bool highpFog = false;
bool highpTexcoord = false;

#if defined(USING_GLES2)
// Let's wait until we have a real use for this.
Expand All @@ -178,6 +179,8 @@ void GenerateVertexShader(int prim, u32 vertType, char *buffer, bool useHWTransf
// PowerVR needs highp to do the fog in MHU correctly.
// Others don't, and some can't handle highp in the fragment shader.
highpFog = gl_extensions.gpuVendor == GPU_VENDOR_POWERVR;
highpTexcoord = gl_extensions.gpuVendor == GPU_VENDOR_POWERVR;

#elif !defined(FORCE_OPENGL_2_0)
if (gl_extensions.VersionGEThan(3, 3, 0)) {
glslES30 = true;
Expand Down Expand Up @@ -339,9 +342,9 @@ void GenerateVertexShader(int prim, u32 vertType, char *buffer, bool useHWTransf
}
if (doTexture) {
if (doTextureProjection)
WRITE(p, "%s mediump vec3 v_texcoord;\n", varying);
WRITE(p, "%s %s vec3 v_texcoord;\n", varying, highpTexcoord ? "highp" : "mediump");
else
WRITE(p, "%s mediump vec2 v_texcoord;\n", varying);
WRITE(p, "%s %s vec2 v_texcoord;\n", varying, highpTexcoord ? "highp" : "mediump");
}


Expand Down

0 comments on commit f424bf7

Please sign in to comment.