Skip to content
Open
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
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,12 @@ if(WIN32)
target_link_libraries(text_layout_test PRIVATE oc4_options oc4_render)
add_test(NAME text_layout COMMAND text_layout_test)
set_target_properties(text_layout_test PROPERTIES FOLDER "tests")

add_executable(shader_compile_test tests/shader_compile_test.cpp)
target_compile_definitions(shader_compile_test PRIVATE OC4_SOURCE_DIR="${PROJECT_SOURCE_DIR}")
target_link_libraries(shader_compile_test PRIVATE oc4_options d3dcompiler)
add_test(NAME shader_compile COMMAND shader_compile_test)
set_target_properties(shader_compile_test PROPERTIES FOLDER "tests")
endif()

set_target_properties(nifview oc4core PROPERTIES FOLDER "apps")
Expand Down
12 changes: 6 additions & 6 deletions src/render/MapView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ float fogAt(float2 p)
uint a = plotAt(cell + kCorner[k]).a;
known[k] = float2((a & 4) ? 1 : 0, (a & 8) ? 1 : 0);
}
float2 k = lerp(lerp(known[3], known[2], w.x), lerp(known[0], known[1], w.x), w.y);
return k.x * lerp(0.55, 1, k.y);
float2 v = lerp(lerp(known[3], known[2], w.x), lerp(known[0], known[1], w.x), w.y);
return v.x * lerp(0.55, 1, v.y);
}

float4 PSTerrain(PSIn i) : SV_Target
Expand Down Expand Up @@ -324,9 +324,9 @@ float4 PSTerrain(PSIn i) : SV_Target
// along its south and east edges).
int2 own = int2(floor(p));
float river = 2;
[unroll] for (int k = 0; k < 9; ++k)
[unroll] for (int r = 0; r < 9; ++r)
{
int2 q = own + int2(k % 3 - 1, k / 3 - 1);
int2 q = own + int2(r % 3 - 1, r / 3 - 1);
uint flags = plotAt(q).a;
if (flags & 16)
river = min(river, length(p - float2(clamp(p.x, q.x, q.x + 1), q.y)));
Expand All @@ -353,8 +353,8 @@ float4 PSTerrain(PSIn i) : SV_Target
// What the active team knows, blended between plot centres: black where it has not
// been, dimmed where it does not see now.
float2 w = smoothstep(0, 1, f);
float2 k = lerp(lerp(known[3], known[2], w.x), lerp(known[0], known[1], w.x), w.y);
color *= k.x * lerp(0.55, 1, k.y);
float2 v = lerp(lerp(known[3], known[2], w.x), lerp(known[0], known[1], w.x), w.y);
color *= v.x * lerp(0.55, 1, v.y);
}
return float4(color * distanceFog(i.drawn), 1);
}
Expand Down
114 changes: 114 additions & 0 deletions tests/shader_compile_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Compiles every embedded HLSL shader entry point through FXC (D3DCompile) with the same
// shader models, macros, and optimization flags as Renderer::compile(), catching Windows
// DXBC-only compile errors (such as FXC loop-variable scoping) in CI.

#include <d3dcompiler.h>
#include <wrl/client.h>

#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <sstream>
#include <string>
#include <string_view>

using Microsoft::WRL::ComPtr;

namespace
{
std::string readLiteral(const std::filesystem::path& root, const char* relPath, std::string_view name)
{
std::ifstream file(root / relPath, std::ios::binary);
if (!file)
{
std::printf("FAILED: could not open %s\n", relPath);
return {};
}
std::ostringstream ss;
ss << file.rdbuf();
const std::string text = ss.str();
const std::string marker = std::string(name) + "[] = R\"hlsl(";
constexpr std::string_view kClose = ")hlsl\"";
const size_t start = text.find(marker);
if (start == std::string::npos)
{
std::printf("FAILED: %s not found in %s\n", marker.c_str(), relPath);
return {};
}
const size_t bodyStart = start + marker.size();
const size_t end = text.find(kClose, bodyStart);
if (end == std::string::npos)
{
std::printf("FAILED: )hlsl\" not found for %.*s in %s\n", int(name.size()), name.data(), relPath);
return {};
}
return text.substr(bodyStart, end - bodyStart);
}

bool compileShader(const std::string& source, const char* name, const char* entry, bool vertex)
{
if (source.empty())
{
std::printf("FAILED: %s %s: empty shader source\n", name, entry);
return false;
}
const D3D_SHADER_MACRO defines[] = { { "UNIFORMS", vertex ? "space1" : "space3" }, { nullptr, nullptr } };
ComPtr<ID3DBlob> blob, errors;
const HRESULT hr = D3DCompile(source.data(), source.size(), name, defines, nullptr, entry,
vertex ? "vs_5_1" : "ps_5_1", D3DCOMPILE_OPTIMIZATION_LEVEL3, 0, &blob, &errors);
if (FAILED(hr))
{
const char* msg = errors ? static_cast<const char*>(errors->GetBufferPointer()) : "compile failed";
std::printf("FAILED: %s %s: %s\n", name, entry, msg);
return false;
}
return true;
}
}

int main()
{
const std::filesystem::path root(OC4_SOURCE_DIR);
const std::string frame = readLiteral(root, "src/render/Renderer.h", "kFrameConstantsHlsl");
const std::string modelBody = readLiteral(root, "src/render/Renderer.cpp", "kShaderBody");
const std::string overlay = readLiteral(root, "src/render/Renderer.cpp", "kOverlayHlsl");
const std::string terrainBody = readLiteral(root, "src/render/MapView.cpp", "kTerrainHlsl");
const std::string waterBody = readLiteral(root, "src/render/MapView.cpp", "kWaterHlsl");
if (frame.empty() || modelBody.empty() || overlay.empty() || terrainBody.empty() || waterBody.empty())
return 1;

const std::string model = frame + modelBody;
const std::string terrain = frame + terrainBody;
const std::string water = frame + waterBody;

// Keep in sync with SOURCES / ENTRIES in tools/shaders/gen_msl.py.
struct Entry
{
const std::string* source;
const char* file;
const char* entry;
bool vertex;
};
const Entry entries[] = {
{ &model, "model.hlsl", "VSMain", true },
{ &model, "model.hlsl", "PSMain", false },
{ &overlay, "overlay.hlsl", "VSOverlay", true },
{ &overlay, "overlay.hlsl", "PSOverlay", false },
{ &terrain, "terrain.hlsl", "VSTerrain", true },
{ &terrain, "terrain.hlsl", "PSTerrain", false },
{ &terrain, "terrain.hlsl", "VSBorder", true },
{ &terrain, "terrain.hlsl", "PSBorder", false },
{ &water, "water.hlsl", "VSWater", true },
{ &water, "water.hlsl", "PSWater", false },
};

int failures = 0;
for (const Entry& e : entries)
if (!compileShader(*e.source, e.file, e.entry, e.vertex))
++failures;

if (failures == 0)
std::printf("shader_compile: all %zu shaders compiled\n", std::size(entries));
return failures == 0 ? 0 : 1;
}