Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions memory/build_and_verification.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ permalink: renderer/build-and-verification

# 构建与验证

## 2026-11-13:BSP 反面与弹孔 UV 翻转的视差方向(MetaHookSv #972)

- 触发信号:作者附件 `maptest2.bsp` 的浮空立方体六面中,三面的弹孔 parallax 方向反转。
- 根因 / 约束:`SURF_PLANEBACK` 同时反转 N/T/B,但表面及弹孔 UV 仍按原始 `texinfo`
方向增长;该标记只要求校正面法线。另有独立遗漏:弹孔 H/V flip 改变 UV,却未同步 T/B。
附件 RMF/BSP 交叉核对六面均为正缩放、零旋转;恰好 -X/-Y/-Z 三面为 PLANEBACK,
不能归因于作者在编辑器中翻转纹理,也不能强行统一左右手 UV 基底。
- 正确做法:`src/gl_wsurf_tbn.inl` 集中生产 basis 构建,面切线保持 texinfo 方向,
仅校正 N;`R_UploadDecalVertexBuffer` 在 clip/no-clip 汇合后按当前 decal flags
翻转相应切线,再为该弹孔所有顶点上传同一 basis。不改 shader、UV 顺序或资源格式。
- 红灯证据:保持旧符号逻辑时,-Y 世界面 U 预期 0.394000、实际 0.406006;仅修正
PLANEBACK 后,+Y/HFLIP 弹孔 U 预期 0.406000、实际 0.393982,分别检出两类错误。
- 验证:MSVC x86 Debug/Release Renderer 与全部 18 个测试目标构建退出码 0;两种配置
CTest 各 19/19、无失败或跳过(Debug 188.19s,Release 50.46s)。真实 GL 回归新增
270 个六面世界表面、1080 个六面/四种弹孔翻转
用例,覆盖五种高度、正负/零 scale、法向/斜向视线,与独立世界光线/高度平面交点对照;
原有 24 个生产 shader 变体、432 个中点/材质对齐/lightmap 用例保留并通过。
GPU 为 Intel UHD Graphics 770 / OpenGL 4.4;clang-format 23.1.3 检查 105 个文件
无差异,`git diff --check` 退出码 0,独立只读 review 未发现阻断项。
- 适用范围 / 边界:世界 BSP 和弹孔的生产 TBN;GPU 回归使用生产 basis 和 fragment shader、
测试 vertex shader,不能等同完整游戏调用链或实机视觉验收。本次未部署游戏。
聚合树忽略目录 `build/issue972-*.log` 保存构建、红灯及完整回归日志。

## 2026-10-25:引擎粒子错误调用 TriAPI Begin,解绑粒子纹理

- 触发信号:`svencoop_2026.10.25_10.45_frame7915.rdc` 的 EID 8250 出现黑色三角片;
Expand Down
16 changes: 6 additions & 10 deletions src/gl_rsurf.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "gl_local.h"
#include "gl_wsurf_tbn.inl"
#include <algorithm>


Expand Down Expand Up @@ -959,7 +960,7 @@ void R_UploadDecalTextures(int decalIndex, texture_t* ptexture, const std::share
g_WorldSurfaceRenderer.vCachedDecals[decalIndex].matId = matId;
}

void R_UploadDecalVertexBuffer(int decalIndex, int vertCount, float* v, msurface_t* surf, CWorldSurfaceRenderMaterial* pRenderMaterial)
void R_UploadDecalVertexBuffer(int decalIndex, int vertCount, float* v, msurface_t* surf, int decalFlags, CWorldSurfaceRenderMaterial* pRenderMaterial)
{
auto worldmodel = R_FindWorldModelBySurface(surf);

Expand All @@ -985,7 +986,8 @@ void R_UploadDecalVertexBuffer(int decalIndex, int vertCount, float* v, msurface
return;
}

const auto brushface = &pWorldModel->m_vFaceBuffer[surfIndex];
const auto brushface = &pWorldModel->m_vFaceBuffer[surfIndex];
const auto tangentBasis = R_BuildDecalTangentBasis(*brushface, decalFlags);

std::vector<vertex3f_t> vPolyVertices;
std::vector<decalvertex_t> vVertexDataBuffer;
Expand All @@ -1012,15 +1014,9 @@ void R_UploadDecalVertexBuffer(int decalIndex, int vertCount, float* v, msurface
tempVertexData.lightmaptexcoord[0] = v[5];
tempVertexData.lightmaptexcoord[1] = v[6];

decalvertextbn_t tempVertexTBNData;

VectorCopy(brushface->normal, tempVertexTBNData.normal);
VectorCopy(brushface->s_tangent, tempVertexTBNData.s_tangent);
VectorCopy(brushface->t_tangent, tempVertexTBNData.t_tangent);

vPolyVertices.emplace_back(tempVertex);
vVertexDataBuffer.emplace_back(tempVertexData);
vVertexTBNDataBuffer.emplace_back(tempVertexTBNData);
vVertexTBNDataBuffer.emplace_back(tangentBasis);

v += VERTEXSIZE;
}
Expand Down Expand Up @@ -1138,7 +1134,7 @@ void R_DrawDecals(cl_entity_t* ent)
{
R_BuildCachedDecalBounds(v, vertCount, cachedDecal);
R_UploadDecalTextures(decalIndex, ptexture, pRenderMaterial);
R_UploadDecalVertexBuffer(decalIndex, vertCount, v, psurf, pRenderMaterial.get());
R_UploadDecalVertexBuffer(decalIndex, vertCount, v, psurf, plist->flags, pRenderMaterial.get());
plist->flags |= FDECAL_VBO;
}
else
Expand Down
14 changes: 2 additions & 12 deletions src/gl_wsurf.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "gl_local.h"
#include "gl_wsurf_tbn.inl"

#include <sstream>
#include <algorithm>
Expand Down Expand Up @@ -2071,21 +2072,10 @@ std::shared_ptr<CWorldSurfaceWorldModel> R_GenerateWorldSurfaceWorldModel(model_
auto poly = surf->polys;
auto pBrushFace = &pWorldModel->m_vFaceBuffer[i];

VectorCopy(surf->texinfo->vecs[0], pBrushFace->s_tangent);
VectorCopy(surf->texinfo->vecs[1], pBrushFace->t_tangent);
VectorNormalize(pBrushFace->s_tangent);
VectorNormalize(pBrushFace->t_tangent);
VectorCopy(surf->plane->normal, pBrushFace->normal);
R_BuildBrushFaceTangentBasis(surf, *pBrushFace);
pBrushFace->index = i;
pBrushFace->flags = surf->flags;

if (surf->flags & SURF_PLANEBACK)
{
VectorInverse(pBrushFace->normal);
VectorInverse(pBrushFace->s_tangent);
VectorInverse(pBrushFace->t_tangent);
}

if (surf->lightmaptexturenum + 1 > g_WorldSurfaceRenderer.iNumLightmapTextures)
g_WorldSurfaceRenderer.iNumLightmapTextures = surf->lightmaptexturenum + 1;

Expand Down
29 changes: 29 additions & 0 deletions src/gl_wsurf_tbn.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Texture axes define tangent directions independently of the BSP face side.
inline void R_BuildBrushFaceTangentBasis(const msurface_t* surf, CWorldSurfaceBrushFace& face)
{
VectorCopy(surf->texinfo->vecs[0], face.s_tangent);
VectorCopy(surf->texinfo->vecs[1], face.t_tangent);
VectorNormalize(face.s_tangent);
VectorNormalize(face.t_tangent);
VectorCopy(surf->plane->normal, face.normal);

if (surf->flags & SURF_PLANEBACK)
{
// The face normal changes side; UV gradients still follow texinfo.
VectorInverse(face.normal);
}
}

inline decalvertextbn_t R_BuildDecalTangentBasis(const CWorldSurfaceBrushFace& face, int decalFlags)
{
decalvertextbn_t basis;
VectorCopy(face.normal, basis.normal);
VectorCopy(face.s_tangent, basis.s_tangent);
VectorCopy(face.t_tangent, basis.t_tangent);
// R_DecalVertsClip mirrors these UV axes without changing the face normal.
if (decalFlags & FDECAL_HFLIP)
VectorInverse(basis.s_tangent);
if (decalFlags & FDECAL_VFLIP)
VectorInverse(basis.t_tangent);
return basis;
}
5 changes: 3 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ add_test(NAME triapi_shader_tests COMMAND triapi_shader_tests
"${PROJECT_SOURCE_DIR}/assets/svencoop/renderer/shader")
set_tests_properties(triapi_shader_tests PROPERTIES SKIP_RETURN_CODE 2)

add_executable(wsurf_parallax_shader_tests wsurf_parallax_shader_tests.cpp)
add_executable(wsurf_parallax_shader_tests wsurf_parallax_shader_tests.cpp ../src/mathlib2.cpp)
target_compile_definitions(wsurf_parallax_shader_tests PRIVATE GLEW_STATIC GLFW_INCLUDE_NONE)
target_compile_options(wsurf_parallax_shader_tests PRIVATE /UNDEBUG)
target_compile_options(wsurf_parallax_shader_tests PRIVATE /UNDEBUG /Gy)
target_link_options(wsurf_parallax_shader_tests PRIVATE /OPT:REF /INCREMENTAL:NO)
target_link_libraries(wsurf_parallax_shader_tests PRIVATE renderer_settings libglew_static glfw)
add_test(NAME wsurf_parallax_shader_tests COMMAND wsurf_parallax_shader_tests
"${PROJECT_SOURCE_DIR}/assets/svencoop/renderer/shader")
Expand Down
115 changes: 112 additions & 3 deletions tests/wsurf_parallax_shader_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Exercise the production world-surface fragment shader on a real GL context.
#include "gl_local.h"
#include "gl_shader_compile.h"
#include "gl_wsurf_tbn.inl"
#include <GLFW/glfw3.h>
#include <array>
#include <cassert>
Expand Down Expand Up @@ -87,6 +89,9 @@ GLuint Program(const std::filesystem::path& directory, const std::string& define
{
const std::string vertexText = R"(#version 430
uniform vec3 u_testView;
uniform vec3 u_testNormal = vec3(0, 0, 1);
uniform vec3 u_testTangent = vec3(1, 0, 0);
uniform vec3 u_testBitangent = vec3(0, 1, 0);
out vec3 v_worldpos, v_normal, v_tangent, v_bitangent;
out vec2 v_diffusetexcoord, v_detailtexcoord, v_normaltexcoord;
out vec2 v_parallaxtexcoord, v_speculartexcoord;
Expand All @@ -98,9 +103,9 @@ void main()
vec2 corners[3] = vec2[3](vec2(-1, -1), vec2(3, -1), vec2(-1, 3));
gl_Position = vec4(corners[gl_VertexID], 0, 1);
v_worldpos = u_testView;
v_normal = vec3(0, 0, 1);
v_tangent = vec3(1, 0, 0);
v_bitangent = vec3(0, 1, 0);
v_normal = u_testNormal;
v_tangent = u_testTangent;
v_bitangent = u_testBitangent;
v_diffusetexcoord = vec2(0.4, 0.45);
v_detailtexcoord = vec2(0.75, 1.25);
v_normaltexcoord = v_detailtexcoord;
Expand Down Expand Up @@ -251,6 +256,108 @@ void Test(const std::filesystem::path& directory, bool parallax, bool decal, boo
glDeleteBuffers(blocks.size(), blocks.data());
glDeleteProgram(program);
}

void TestSurfaceOrientations(const std::filesystem::path& directory, bool decal)
{
struct Face
{
const char* name;
vec3_t normal;
vec3_t u;
vec3_t v;
int flags;
};
// Actual texinfo axes and face sides from issue #972's maptest2.bsp cube.
// Opposite faces share UV axes, including both UV handedness conventions.
const Face faces[] = {
{"+Y", {0, 1, 0}, {1, 0, 0}, {0, 0, -1}, 0},
{"-Y", {0, -1, 0}, {1, 0, 0}, {0, 0, -1}, SURF_PLANEBACK},
{"+Z", {0, 0, 1}, {1, 0, 0}, {0, -1, 0}, 0},
{"-Z", {0, 0, -1}, {1, 0, 0}, {0, -1, 0}, SURF_PLANEBACK},
{"+X", {1, 0, 0}, {0, 1, 0}, {0, 0, -1}, 0},
{"-X", {-1, 0, 0}, {0, 1, 0}, {0, 0, -1}, SURF_PLANEBACK}};

std::string defines = "#define DIFFUSE_ENABLED\n#define GBUFFER_ENABLED\n#define FULLBRIGHT_ENABLED\n#define PARALLAXTEXTURE_ENABLED\n";
if (decal) defines += "#define DECAL_ENABLED\n";
GLuint program = Program(directory, defines);
glUseProgram(program);
std::array<GLuint, 3> blocks = {BindBlock(program, "SceneBlock", 0), BindBlock(program, "CameraBlock", 1), BindBlock(program, "EntityBlock", 3)};
std::vector<float> ramp(TextureSize * TextureSize * 4), heights(ramp.size());
for (int y = 0; y < TextureSize; ++y)
for (int x = 0; x < TextureSize; ++x)
{
size_t i = (y * TextureSize + x) * 4;
ramp[i] = (x + 0.5f) / TextureSize;
ramp[i + 1] = (y + 0.5f) / TextureSize;
ramp[i + 3] = 1;
}
const std::array<GLuint, 2> textures = {Texture(0, GL_TEXTURE_2D, ramp), Texture(3, GL_TEXTURE_2D, heights)};
int cases = 0;
for (const auto& face : faces)
{
mplane_t plane{};
mtexinfo_t texinfo{};
msurface_t surface{};
for (int k = 0; k < 3; ++k)
{
plane.normal[k] = std::abs(face.normal[k]);
// Non-unit texinfo lengths must not change the tangent direction.
texinfo.vecs[0][k] = face.u[k] * 2;
texinfo.vecs[1][k] = face.v[k] * 0.5f;
}
surface.plane = &plane;
surface.texinfo = &texinfo;
surface.flags = face.flags;
CWorldSurfaceBrushFace brushFace;
R_BuildBrushFaceTangentBasis(&surface, brushFace);
for (int flags : {0, FDECAL_HFLIP, FDECAL_VFLIP, FDECAL_HFLIP | FDECAL_VFLIP})
{
if (!decal && flags) continue;
const auto basis = R_BuildDecalTangentBasis(brushFace, flags);
for (int k = 0; k < 3; ++k)
Expect(face.normal[k], basis.normal[k], "outward face normal");
glUniform3fv(glGetUniformLocation(program, "u_testNormal"), 1, basis.normal);
glUniform3fv(glGetUniformLocation(program, "u_testTangent"), 1, basis.s_tangent);
glUniform3fv(glGetUniformLocation(program, "u_testBitangent"), 1, basis.t_tangent);
for (float height : {0.0f, 0.25f, 0.5f, 0.75f, 1.0f})
{
for (size_t i = 0; i < heights.size(); i += 4) heights[i] = height;
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TextureSize, TextureSize, GL_RGBA, GL_FLOAT, heights.data());
for (float scale : {-0.02f, 0.0f, 0.02f})
for (const auto& ray : {std::array<float, 2>{0, 0}, std::array<float, 2>{0.6f, -0.4f}, std::array<float, 2>{-0.5f, 0.3f}})
{
vec3_t direction{};
for (int k = 0; k < 3; ++k)
direction[k] = ray[0] * face.u[k] + ray[1] * face.v[k] - face.normal[k];
glUniform3fv(glGetUniformLocation(program, "u_testView"), 1, direction);
glUniform1f(glGetUniformLocation(program, "u_parallaxScale"), scale);
glDrawArrays(GL_TRIANGLES, 0, 3);
std::array<float, 4> pixel{};
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(TargetSize / 2, TargetSize / 2, 1, 1, GL_RGBA, GL_FLOAT, pixel.data());
// Intersect the world ray with a constant-height plane, then
// project that displacement onto the actual (possibly flipped) UV axes.
// This oracle does not use the production TBN to predict its own output.
const float distance = scale * (height - 0.5f) / DotProduct(direction, face.normal);
const float u = BaseUV[0] + DotProduct(direction, face.u) * distance * ((flags & FDECAL_HFLIP) ? -1 : 1);
const float v = BaseUV[1] + DotProduct(direction, face.v) * distance * ((flags & FDECAL_VFLIP) ? -1 : 1);
if (std::abs(u - pixel[0]) > Tolerance || std::abs(v - pixel[1]) > Tolerance)
std::fprintf(stderr, "face=%s decal=%d flags=%x height=%f scale=%f ray=(%f,%f)\n", face.name, decal, flags, height, scale, ray[0], ray[1]);
Expect(u, pixel[0], "oriented parallax U");
Expect(v, pixel[1], "oriented parallax V");
assert(GL_NO_ERROR == glGetError());
++cases;
}
}
}
}
std::printf("%d six-face %s parallax cases passed\n", cases, decal ? "decal/UV-flip" : "world");
glDeleteTextures(textures.size(), textures.data());
glDeleteBuffers(blocks.size(), blocks.data());
glDeleteProgram(program);
}
} // namespace

int main(int argc, char** argv)
Expand Down Expand Up @@ -299,6 +406,8 @@ int main(int argc, char** argv)
for (bool decal : {false, true})
for (bool detail : {false, true})
Test(argv[1], parallax, decal, detail);
TestSurfaceOrientations(argv[1], false);
TestSurfaceOrientations(argv[1], true);
glDeleteTextures(targets.size(), targets.data());
glDeleteFramebuffers(1, &framebuffer);
glDeleteVertexArrays(1, &vao);
Expand Down
Loading