From 7ab0fd15a0b405115be208d2bee830bdad19da4f Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 16 Jul 2026 17:56:48 +0200 Subject: [PATCH] change how water refraction is handled split it into its own pass we will draw "refraction planes" before drawing anything translucent this way we wont cause any draw order issues or anything like that this is also a bit faster and more sane than the other approach only caveat: translucent stuff behind water planes is unaffected by refraction but better than it was before, and sure better than trying to copy the screen per plane or shit like that lol like this, this should be mostly feasible to actually use, the skybox water stuff kinda was a non issue since i noticed even just normal water does not draw over the map geometry (must have misremembered that part since software does do that), but still might be worth looking into at some point --- src/hardware/hw_main.cpp | 297 ++++++++++++++++++++----------- src/hardware/hw_shaders.h | 62 +------ src/hardware/r_opengl/r_opengl.c | 21 ++- src/hardware/r_opengl/r_opengl.h | 1 + 4 files changed, 216 insertions(+), 165 deletions(-) diff --git a/src/hardware/hw_main.cpp b/src/hardware/hw_main.cpp index 5f6931a73..5cfc9ad26 100644 --- a/src/hardware/hw_main.cpp +++ b/src/hardware/hw_main.cpp @@ -728,15 +728,6 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool else if (cv_ripplewater.value && (PolyFlags & PF_Ripple)) { shader = SHADER_WATER; - - // handle water refraction - // this is not needed on opague water at all - // FIXME: this is ugly - if (alpha < 255) - { - shader = SHADER_WATERREFRACT; - GL_DrawWaterPolygon(&Surf, planeVerts, nrPlaneVerts, PolyFlags|PF_ColorMapped); - } } else shader = SHADER_FLOOR; @@ -744,8 +735,7 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool PolyFlags |= PF_ColorMapped; } - if (shader != SHADER_WATERREFRACT) - HWR_ProcessPolygon(&Surf, planeVerts, nrPlaneVerts, PolyFlags, shader, false); + HWR_ProcessPolygon(&Surf, planeVerts, nrPlaneVerts, PolyFlags, shader, false); if (gl_maphashorizonlines && subsector && cv_glhorizonlines.value) { @@ -826,10 +816,165 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool horizonpts[4].y = gl_viewz; // Draw - if (shader == SHADER_WATERREFRACT) - GL_DrawWaterPolygon(&Surf, horizonpts, 6, PolyFlags); - else - HWR_ProcessPolygon(&Surf, horizonpts, 6, PolyFlags, shader, true); + HWR_ProcessPolygon(&Surf, horizonpts, 6, PolyFlags, shader, true); + } + } + } +#undef SETUP3DVERT +} + +// cut down version of HWR_RenderPlane +// which renders only the refracted scene in place of planes +// FIXME: we probably could just use stencil stuff for this +// but i hate messing with that +static void HWR_RenderRefractionPlane(subsector_t *subsector, extrasubsector_t *xsub, boolean isceiling, fixed_t fixedheight, sector_t *FOFsector) +{ + FSurfaceInfo Surf = {}; + FOutVector *v3d; + polyvertex_t *pv; + pslope_t *slope = NULL; + + size_t nrPlaneVerts; + INT32 i; + + float height; // constant y for all points on the convex flat polygon + + static FOutVector *planeVerts = NULL; + static UINT16 numAllocedPlaneVerts = 0; + + poly_t *planepoly = xsub->planepoly; + + // no convex poly were generated for this subsector + if (!planepoly) + return; + + nrPlaneVerts = planepoly->numpts; + + if (nrPlaneVerts < 3) // not even a triangle ? + return; + + const sector_t *sec = FOFsector; + + // Get the slope pointer to simplify future code + if (!isceiling && sec->f_slope) + slope = sec->f_slope; + else if (isceiling && sec->c_slope) + slope = sec->c_slope; + + height = FixedToFloat(fixedheight); + + // Allocate plane-vertex buffer if we need to + if (!planeVerts || nrPlaneVerts > numAllocedPlaneVerts) + { + numAllocedPlaneVerts = (UINT16)nrPlaneVerts; + Z_Free(planeVerts); + Z_Malloc(numAllocedPlaneVerts * sizeof (FOutVector), PU_LEVEL, &planeVerts); + } + + pv = planepoly->pts; + +#define SETUP3DVERT(vert, vx, vy) {\ + vert->s = 0.0f;\ + vert->t = 0.0f;\ +\ + if (slope)\ + {\ + fixedheight = P_GetSlopeZAt(slope, FloatToFixed((vx)), FloatToFixed((vy)));\ + height = FixedToFloat(fixedheight);\ + }\ +\ + vert->x = (vx);\ + vert->y = height;\ + vert->z = (vy);\ +\ +} + for (i = 0, v3d = planeVerts; i < (INT32)nrPlaneVerts; i++, v3d++, pv++) + SETUP3DVERT(v3d, pv->x, pv->y); + + // this does not need PF_Occlude or smth, treat it as if it wasnt there at all lol + GL_DrawWaterPolygon(&Surf, planeVerts, nrPlaneVerts, PF_NoTexture|PF_Ripple); // this does not need a texture + + // not sure if horizonlines even work with this lol + if (gl_maphashorizonlines && subsector && cv_glhorizonlines.value) + { + // Horizon lines + FOutVector horizonpts[6]; + float dist, vx, vy; + float x1, y1, xd, yd; + UINT8 numplanes, j; + vertex_t v; // For determining the closest distance from the line to the camera, to split render planes for minimum distortion; + + static constexpr float renderdist = 27000.0f; // How far out to properly render the plane + static constexpr float farrenderdist = 32768.0f; // From here, raise plane to horizon level to fill in the line with some texture distortion + + const seg_t *line = &segs[subsector->firstline]; + + for (i = 0; i < subsector->numlines; i++, line++) + { + if (line->polyseg) + continue; + + line_t* ld = line->linedef; + + // this check sucks and is a hotspot lel + if (LIKELY(ld->special != HORIZONSPECIAL)) + continue; + + if (R_PointOnSegSide(viewx, viewy, line) != 0) + continue; + + P_ClosestPointOnLine(viewx, viewy, ld, &v); + dist = FixedToFloat(R_PointToDist(v.x, v.y)); + + x1 = (line->fv1.x); + y1 = (line->fv1.y); + + xd = (line->fv2.x) - x1; + yd = (line->fv2.y) - y1; + + // Based on the seg length and the distance from the line, split horizon into multiple poly sets to reduce distortion + dist = sqrtf((xd*xd) + (yd*yd)) / dist / 16.0f; + + if (dist > 100.0f) + numplanes = 100; + else + numplanes = (UINT8)dist + 1; + + for (j = 0; j < numplanes; j++) + { + // Left side + vx = x1 + xd * j / numplanes; + vy = y1 + yd * j / numplanes; + SETUP3DVERT((&horizonpts[1]), vx, vy); + + dist = sqrtf(powf(vx - gl_viewx, 2) + powf(vy - gl_viewy, 2)); + vx = (vx - gl_viewx) * renderdist / dist + gl_viewx; + vy = (vy - gl_viewy) * renderdist / dist + gl_viewy; + SETUP3DVERT((&horizonpts[0]), vx, vy); + + // Right side + vx = x1 + xd * (j+1) / numplanes; + vy = y1 + yd * (j+1) / numplanes; + SETUP3DVERT((&horizonpts[2]), vx, vy); + + dist = sqrtf(powf(vx - gl_viewx, 2) + powf(vy - gl_viewy, 2)); + vx = (vx - gl_viewx) * renderdist / dist + gl_viewx; + vy = (vy - gl_viewy) * renderdist / dist + gl_viewy; + SETUP3DVERT((&horizonpts[3]), vx, vy); + + // Horizon fills + vx = (horizonpts[0].x - gl_viewx) * farrenderdist / renderdist + gl_viewx; + vy = (horizonpts[0].z - gl_viewy) * farrenderdist / renderdist + gl_viewy; + SETUP3DVERT((&horizonpts[5]), vx, vy); + horizonpts[5].y = gl_viewz; + + vx = (horizonpts[3].x - gl_viewx) * farrenderdist / renderdist + gl_viewx; + vy = (horizonpts[3].z - gl_viewy) * farrenderdist / renderdist + gl_viewy; + SETUP3DVERT((&horizonpts[4]), vx, vy); + horizonpts[4].y = gl_viewz; + + // Draw + GL_DrawWaterPolygon(&Surf, horizonpts, 6, PF_NoTexture|PF_Ripple); } } } @@ -4254,7 +4399,9 @@ static void HWR_RenderDrawNodes(void) // Reversed order for (i = 0; i < numdrawnodes; i++) + { sortindex[i] = numdrawnodes - i - 1; + } // The order is correct apart from planes in the same subsector. // So scan the list and sort out these cases. @@ -4308,14 +4455,6 @@ static void HWR_RenderDrawNodes(void) { planeinfo_t *plane = &drawnode->u.plane; - // if its a water plane - // we draw them seperately before... - // this has to be done so both ceiling and floor can be included in the refraction part - if (cv_ripplewater.value && plane->alpha < 255 && plane->blend & PF_Ripple) - { - break; - } - // We aren't traversing the BSP tree, so make gl_frontsector null to avoid crashes. gl_frontsector = NULL; @@ -4338,7 +4477,6 @@ static void HWR_RenderDrawNodes(void) HWR_RenderPolyObjectPlane(polyplane->polysector, polyplane->isceiling, polyplane->fixedheight, polyplane->blend, polyplane->lightlevel, polyplane->lumpnum, polyplane->FOFSector, polyplane->alpha, polyplane->planecolormap); - } break; case DRAWNODE_WALL: @@ -4364,22 +4502,14 @@ static void HWR_RenderDrawNodes(void) static void HWR_RenderRefractionWater(void) { - // TODO: defer water plane render to happen after EVERYTHING - // otherwise skybox water cant capture the map geometry properly - // so the map may draw over the water plane in some cases - // see hadal trench - // didnt notice any other map besides this, yet..... - - // all this bullshit - // just so either ceiling or floor planes can be captured in the opposite plane........ - - // caveat with all of this is - // some translucent things behind water planes may not get the refraction... - // i have no clue how to ever fix that (maybe another pass with more depth compares? kinda wonder if we even need the whole drawnode thing in gl anyways) - // but it seems like software also suffers from this in some cases - - // other idea: copy only screen parts that water covers similar to software but per plane - // maybe that will be fast enough? + // this basically just draws a refracted screen copy in the place of water planes + // best approach i can think of to handle translucent things being drawn behind water planes + // this means other translucent shit will appear "correctly" and does not cause any sorting issues + // caveat: anything translucent behind water planes will be unaffected by the refraction effect + // there just is no better way to do this unless we wanna trash performance to hell by doing a screen copy per water plane + // it "might" be possible to only partially copy the screen like the software renderer + // but that will be rather tricky to implement since we dont really have "screen bounds" we can use for this + // plus i also immensely suck at maths static std::vector waterdrawnodes; @@ -4405,11 +4535,16 @@ static void HWR_RenderRefractionWater(void) planeinfo_t *plane = &drawnode->u.plane; + // uh dont think we actually need this lol + // but better be safe for now + if (plane->alpha >= 255) + continue; + // this only applies to translucent water with ripple effect! - if (plane->alpha < 255 && plane->blend & PF_Ripple) - { - waterdrawnodes.push_back(drawnodes[w]); - } + if (!(plane->blend & PF_Ripple)) + continue; + + waterdrawnodes.push_back(drawnodes[w]); } const size_t numwaterdrawnodes = waterdrawnodes.size(); @@ -4418,67 +4553,29 @@ static void HWR_RenderRefractionWater(void) if (!numwaterdrawnodes) return; - // split floor and ceiling planes of the water fofs - // otherwise the floor might not be visible to the ceiling plane or the other way around - auto waterisfloor = [](const gl_drawnode_t &node) FUNCINLINE + // this is probably a bit wasteful + // since we only copy the screen once + // but you might be able to see both planes not overlap if you look from a wonky angle + auto wotercompare = [](const void *p1, const void *p2) { - return !node.u.plane.isceiling; + const gl_drawnode_t *n1 = (const gl_drawnode_t *)p1; + const gl_drawnode_t *n2 = (const gl_drawnode_t *)p2; + + return abs(n2->u.plane.fixedheight - viewz) - + abs(n1->u.plane.fixedheight - viewz); }; - // stable_partition will move everything the predicate returns true for to the beginning preserving relative order (phew....) - // this is kinda lazy but i dont give a fuck honestly - auto watersplitend = std::stable_partition(waterdrawnodes.begin(), waterdrawnodes.end(), waterisfloor); - // we "could" capture the screen for each plane instead (that would also fix the dumbass sky probably), but man :chaobaba: that even kills my 3080 lmao + qs22j(waterdrawnodes.data(), waterdrawnodes.size(), sizeof(gl_drawnode_t), wotercompare); - // not sure if we *really* need to sort those? - // then again this sorts them relative to the viewheight - // this should mostly mean ceiling above floor - // but this might not always be true? - // tho this shouldnt cost much really - // the below is much worse - qs22j(waterdrawnodes.data(), numwaterdrawnodes, sizeof(gl_drawnode_t), CompareDrawNodePlanes); + GL_CopyMainFramebufferTexture(); + GL_CopyMainFramebufferDepth(); - // dont want copying screen contents twice - // that shit is kinda costly - boolean floorwater = (watersplitend != waterdrawnodes.begin()); - boolean ceilingwater = (watersplitend != waterdrawnodes.end()); - - // FIXME: i am not sure if we should draw the floor or ceiling planes first - // i am also not sure if that even matters? - - // finally draw the wöter - - if (floorwater) + // now finally render the refraction planes + for (auto &drawnode : waterdrawnodes) { - // gladly we do not need fbos for this... - GL_CopyMainFramebufferTexture(); - - // floor water - for (auto it = waterdrawnodes.begin(); it != watersplitend; ++it) - { - planeinfo_t *plane = &it->u.plane; - gl_frontsector = NULL; - if (!(plane->blend & PF_NoTexture)) - HWR_GetFlat(plane->lumpnum, R_NoEncore(plane->FOFSector, plane->isceiling)); - HWR_RenderPlane(NULL, plane->xsub, plane->isceiling, plane->fixedheight, plane->blend, plane->lightlevel, - plane->lumpnum, plane->FOFSector, plane->alpha, plane->planecolormap); - } - } - - if (ceilingwater) - { - GL_CopyMainFramebufferTexture(); - - // ceiling water - for (auto it = watersplitend; it != waterdrawnodes.end(); ++it) - { - planeinfo_t *plane = &it->u.plane; - gl_frontsector = NULL; - if (!(plane->blend & PF_NoTexture)) - HWR_GetFlat(plane->lumpnum, R_NoEncore(plane->FOFSector, plane->isceiling)); - HWR_RenderPlane(NULL, plane->xsub, plane->isceiling, plane->fixedheight, plane->blend, plane->lightlevel, - plane->lumpnum, plane->FOFSector, plane->alpha, plane->planecolormap); - } + planeinfo_t *plane = &drawnode.u.plane; + gl_frontsector = NULL; + HWR_RenderRefractionPlane(NULL, plane->xsub, plane->isceiling, plane->fixedheight, plane->FOFSector); } waterdrawnodes.clear(); @@ -5772,7 +5869,7 @@ static void HWR_RenderViewpoint(gl_portal_t *rootportal, int stencil_level, bool ps_hw_waterdrawtime.value.p = 0; // thisll be a good mess making this toggable - if (cv_ripplewater.value) + if (cv_ripplewater.value && HWR_UseShader()) // dont need this without shaders do we { // should we not do this during portal render? // wonder how bad the perf will be.... diff --git a/src/hardware/hw_shaders.h b/src/hardware/hw_shaders.h index 3481e5a38..7be55fde7 100644 --- a/src/hardware/hw_shaders.h +++ b/src/hardware/hw_shaders.h @@ -340,37 +340,13 @@ "vec2 uv = gl_FragCoord.xy / scr_resolution;\n" \ "vec2 scene_uv = uv + vec2(sdistort, cdistort) * 0.05;\n" \ "float scenedepth = texture2D(scene_depth_tex, scene_uv).r;\n" \ - "vec4 watersurface = texture2D(tex, vec2(gl_TexCoord[0].s - sdistort, gl_TexCoord[0].t - cdistort));\n" \ - "vec4 scene;\n" \ "vec4 texel;\n" \ - "float refractalpha = 0.0;\n" \ - "bool pixelinfront = scenedepth < gl_FragCoord.z;\n" \ - "if (pixelinfront) {\n" \ - "scene = watersurface;\n" \ - "texel = watersurface;\n" \ + "if (scenedepth < gl_FragCoord.z) {\n" \ + "discard;\n" \ "} else {\n" \ - "refractalpha = (watersurface.a * poly_color.a);\n" \ - "scene = texture2D(scene_tex, scene_uv);\n" \ - "texel = mix(scene, watersurface, refractalpha);\n" \ + "texel = texture2D(scene_tex, scene_uv);\n" \ "}\n" -// copy paste but needs to modify this for palrender stuff -// the refracted mix shouldnt have the actual alpha applied -// as we do this during the mix phase in GLSL_WATERREFRACT_TEXEL -#define GLSL_PALETTEWATER_RENDERING \ - "float tex_pal_idx = texture3D(palette_lookup_tex, vec3((watersurface * 63.0 + 0.5) / 64.0))[0] * 255.0;\n" \ - "float z = gl_FragCoord.z / gl_FragCoord.w;\n" \ - "float light_y = clamp(floor(R_DoomColormap(lighting, z, gl_FragCoord.xy)), 0.0, 31.0);\n" \ - "vec2 lighttable_coord = vec2((tex_pal_idx + 0.5) / 256.0, (light_y + 0.5) / 32.0);\n" \ - "vec4 final_color = texture2D(lighttable_tex, lighttable_coord);\n" \ - "if (pixelinfront) {\n" \ - "final_color.a = (texel.a * poly_color.a);\n" \ - "} else {\n" \ - "final_color = mix(scene, final_color, refractalpha);\n" \ - "final_color.a = texel.a;\n" \ - "}\n" \ - "gl_FragColor = final_color;\n" \ - // adjusted values to match software a little better // original values //"const float freq = 0.025;\n" @@ -390,43 +366,15 @@ "const float amp = 0.018;\n" \ "const float speed = 2.0;\n" \ "const float pi = 3.14159;\n" \ - "#ifdef SRB2_PALETTE_RENDERING\n" \ - "uniform sampler2D tex;\n" \ "uniform sampler2D scene_tex;\n" \ "uniform sampler2D scene_depth_tex;\n" \ - "uniform sampler3D palette_lookup_tex;\n" \ - "uniform sampler2D lighttable_tex;\n" \ - "uniform vec4 poly_color;\n" \ - "uniform float lighting;\n" \ - "uniform float leveltime;\n" \ - GLSL_DOOM_COLORMAP \ - "void main(void) {\n" \ - GLSL_WATERREFRACT_TEXEL \ - GLSL_PALETTEWATER_RENDERING \ - "}\n" \ - "#else\n" \ - "uniform sampler2D tex;\n" \ - "uniform sampler2D scene_tex;\n" \ - "uniform sampler2D scene_depth_tex;\n" \ - "uniform vec4 poly_color;\n" \ - "uniform vec4 tint_color;\n" \ - "uniform vec4 fade_color;\n" \ - "uniform float lighting;\n" \ - "uniform float fade_start;\n" \ - "uniform float fade_end;\n" \ "uniform float leveltime;\n" \ GLSL_DOOM_COLORMAP \ GLSL_DOOM_LIGHT_EQUATION \ "void main(void) {\n" \ GLSL_WATERREFRACT_TEXEL \ - "vec4 base_color = texel * poly_color;\n" \ - "vec4 final_color = base_color;\n" \ - GLSL_SOFTWARE_TINT_EQUATION \ - GLSL_SOFTWARE_FADE_EQUATION \ - "final_color.a = pixelinfront ? (texel.a * poly_color.a) : texel.a;\n" \ - "gl_FragColor = final_color;\n" \ - "}\n" \ - "#endif\0" + "gl_FragColor = texel;\n" \ + "}\0" // // Fog block shader diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 2b13c10cb..9a0bd067d 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -3453,6 +3453,19 @@ void GL_CopyMainFramebufferTexture(void) pglBindTexture(GL_TEXTURE_2D, 0); } + // now copy the whole scene + pglBindTexture(GL_TEXTURE_2D, sceneTextures[SCENE_TEX]); + pglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, screen_width, screen_height); + + // not sure why, but this is needed + // otherwise bad things happen + tex_downloaded = sceneTextures[SCENE_TEX]; + + pglBindTexture(GL_TEXTURE_2D, 0); +} + +void GL_CopyMainFramebufferDepth(void) +{ if (!sceneTextures[SCENE_DEPTHTEX]) { pglGenTextures(1, &sceneTextures[SCENE_DEPTHTEX]); @@ -3465,14 +3478,6 @@ void GL_CopyMainFramebufferTexture(void) pglBindTexture(GL_TEXTURE_2D, 0); } - // now copy the whole scene - pglBindTexture(GL_TEXTURE_2D, sceneTextures[SCENE_TEX]); - pglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, screen_width, screen_height); - - // not sure why, but this is needed - // otherwise bad things happen - tex_downloaded = sceneTextures[SCENE_TEX]; - // and copy the depth from the scene // we need this for depth testing within the shader // so we can discard stuff in front of the water diff --git a/src/hardware/r_opengl/r_opengl.h b/src/hardware/r_opengl/r_opengl.h index 9be01c55d..f9722d3ef 100644 --- a/src/hardware/r_opengl/r_opengl.h +++ b/src/hardware/r_opengl/r_opengl.h @@ -105,6 +105,7 @@ extern int supportFBO; #endif void GL_CopyMainFramebufferTexture(void); +void GL_CopyMainFramebufferDepth(void); // ========================================================================== // GLOBAL