mirror of
https://github.com/melonDS-emu/melonDS.git
synced 2025-07-25 15:19:53 -06:00
implement texture blending/shading modes
This commit is contained in:
@ -131,16 +131,18 @@ void main()
|
|||||||
|
|
||||||
const char* kRenderVSCommon = R"(
|
const char* kRenderVSCommon = R"(
|
||||||
|
|
||||||
|
layout(std140, binding=0) uniform uConfig
|
||||||
|
{
|
||||||
|
vec2 uScreenSize;
|
||||||
|
uint uDispCnt;
|
||||||
|
vec4 uToonColors[32];
|
||||||
|
};
|
||||||
|
|
||||||
layout(location=0) in uvec4 vPosition;
|
layout(location=0) in uvec4 vPosition;
|
||||||
layout(location=1) in uvec4 vColor;
|
layout(location=1) in uvec4 vColor;
|
||||||
layout(location=2) in ivec2 vTexcoord;
|
layout(location=2) in ivec2 vTexcoord;
|
||||||
layout(location=3) in uvec3 vPolygonAttr;
|
layout(location=3) in uvec3 vPolygonAttr;
|
||||||
|
|
||||||
layout(std140, binding=0) uniform uConfig
|
|
||||||
{
|
|
||||||
vec2 uScreenSize;
|
|
||||||
};
|
|
||||||
|
|
||||||
smooth out vec4 fColor;
|
smooth out vec4 fColor;
|
||||||
smooth out vec2 fTexcoord;
|
smooth out vec2 fTexcoord;
|
||||||
flat out uvec3 fPolygonAttr;
|
flat out uvec3 fPolygonAttr;
|
||||||
@ -151,6 +153,13 @@ const char* kRenderFSCommon = R"(
|
|||||||
layout(binding=0) uniform usampler2D TexMem;
|
layout(binding=0) uniform usampler2D TexMem;
|
||||||
layout(binding=1) uniform sampler2D TexPalMem;
|
layout(binding=1) uniform sampler2D TexPalMem;
|
||||||
|
|
||||||
|
layout(std140, binding=0) uniform uConfig
|
||||||
|
{
|
||||||
|
vec2 uScreenSize;
|
||||||
|
uint uDispCnt;
|
||||||
|
vec4 uToonColors[32];
|
||||||
|
};
|
||||||
|
|
||||||
smooth in vec4 fColor;
|
smooth in vec4 fColor;
|
||||||
smooth in vec2 fTexcoord;
|
smooth in vec2 fTexcoord;
|
||||||
flat in uvec3 fPolygonAttr;
|
flat in uvec3 fPolygonAttr;
|
||||||
@ -360,9 +369,24 @@ vec4 FinalColor()
|
|||||||
{
|
{
|
||||||
vec4 col;
|
vec4 col;
|
||||||
vec4 vcol = fColor;
|
vec4 vcol = fColor;
|
||||||
|
uint blendmode = (fPolygonAttr.x >> 4) & 0x3;
|
||||||
|
|
||||||
// TODO: also check DISPCNT
|
if (blendmode == 2)
|
||||||
if (((fPolygonAttr.y >> 26) & 0x7) == 0)
|
{
|
||||||
|
if ((uDispCnt & (1<<1)) == 0)
|
||||||
|
{
|
||||||
|
// toon
|
||||||
|
vec3 tooncolor = uToonColors[int(vcol.r * 31)].rgb;
|
||||||
|
vcol.rgb = tooncolor;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// highlight
|
||||||
|
vcol.rgb = vcol.rrr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((((fPolygonAttr.y >> 26) & 0x7) == 0) || ((uDispCnt & (1<<0)) == 0))
|
||||||
{
|
{
|
||||||
// no texture
|
// no texture
|
||||||
col = vcol;
|
col = vcol;
|
||||||
@ -371,8 +395,27 @@ vec4 FinalColor()
|
|||||||
{
|
{
|
||||||
vec4 tcol = TextureLookup();
|
vec4 tcol = TextureLookup();
|
||||||
|
|
||||||
|
if ((blendmode & 1) != 0)
|
||||||
|
{
|
||||||
|
// decal
|
||||||
|
col.rgb = (tcol.rgb * tcol.a) + (vcol.rgb * (1.0-tcol.a));
|
||||||
|
col.a = vcol.a;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// modulate
|
||||||
col = vcol * tcol;
|
col = vcol * tcol;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (blendmode == 2)
|
||||||
|
{
|
||||||
|
if ((uDispCnt & (1<<1)) != 0)
|
||||||
|
{
|
||||||
|
vec3 tooncolor = uToonColors[int(vcol.r * 31)].rgb;
|
||||||
|
col.rgb = min(col.rgb + tooncolor, 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return col.bgra;
|
return col.bgra;
|
||||||
}
|
}
|
||||||
@ -555,6 +598,9 @@ GLuint RenderShader[16][3];
|
|||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
float uScreenSize[2];
|
float uScreenSize[2];
|
||||||
|
u32 uDispCnt;
|
||||||
|
u32 __pad0;
|
||||||
|
float uToonColors[32][4];
|
||||||
|
|
||||||
} ShaderConfig;
|
} ShaderConfig;
|
||||||
|
|
||||||
@ -709,7 +755,7 @@ bool BuildShaderProgram(const char* vs, const char* fs, GLuint* ids, const char*
|
|||||||
char* log = new char[res+1];
|
char* log = new char[res+1];
|
||||||
glGetShaderInfoLog(ids[1], res+1, NULL, log);
|
glGetShaderInfoLog(ids[1], res+1, NULL, log);
|
||||||
printf("OpenGL: failed to compile fragment shader %s: %s\n", name, log);
|
printf("OpenGL: failed to compile fragment shader %s: %s\n", name, log);
|
||||||
printf("shader source:\n--\n%s\n--\n", fs);
|
//printf("shader source:\n--\n%s\n--\n", fs);
|
||||||
delete[] log;
|
delete[] log;
|
||||||
|
|
||||||
glDeleteShader(ids[0]);
|
glDeleteShader(ids[0]);
|
||||||
@ -1294,6 +1340,25 @@ void VCount144()
|
|||||||
|
|
||||||
void RenderFrame()
|
void RenderFrame()
|
||||||
{
|
{
|
||||||
|
ShaderConfig.uDispCnt = RenderDispCnt;
|
||||||
|
|
||||||
|
for (int i = 0; i < 32; i++)
|
||||||
|
{
|
||||||
|
u16 c = RenderToonTable[i];
|
||||||
|
u32 r = c & 0x1F;
|
||||||
|
u32 g = (c >> 5) & 0x1F;
|
||||||
|
u32 b = (c >> 10) & 0x1F;
|
||||||
|
|
||||||
|
ShaderConfig.uToonColors[i][0] = (float)r / 31.0;
|
||||||
|
ShaderConfig.uToonColors[i][1] = (float)g / 31.0;
|
||||||
|
ShaderConfig.uToonColors[i][2] = (float)b / 31.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
glBindBuffer(GL_UNIFORM_BUFFER, ShaderConfigUBO);
|
||||||
|
void* unibuf = glMapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY);
|
||||||
|
if (unibuf) memcpy(unibuf, &ShaderConfig, sizeof(ShaderConfig));
|
||||||
|
glUnmapBuffer(GL_UNIFORM_BUFFER);
|
||||||
|
|
||||||
// SUCKY!!!!!!!!!!!!!!!!!!
|
// SUCKY!!!!!!!!!!!!!!!!!!
|
||||||
// TODO: detect when VRAM blocks are modified!
|
// TODO: detect when VRAM blocks are modified!
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
Reference in New Issue
Block a user