| … | … | @@ -0,0 +1,505 @@ |
| 1 | // ============================================================================ |
| 2 | // EDIT CONTRACT ORACLE — does the MESH stage sample the same carved field the |
| 3 | // DENSITY stage does, for ANY generator body? |
| 4 | // |
| 5 | // WHAT IT EXISTS TO CATCH. |
| 6 | // |
| 7 | // There are two shader templates and they had OPPOSITE contracts: |
| 8 | // |
| 9 | // density stage dc_shader_source.h _build_custom_shader = HEADER + body + |
| 10 | // MAIN, and MAIN applies the edits (:429,:436,:452-456). |
| 11 | // => the body must NOT apply them. |
| 12 | // mesh stage shaders/edge_intersect.glsl injects the body and calls |
| 13 | // evaluate_density() directly for the binary-search midpoints |
| 14 | // (:199) and the central-difference normals (:217-219). Its |
| 15 | // only edit loop (:249) filters edit_type != 8/9 -- convex |
| 16 | // brushes for the exact-Hermite override -- and never touches |
| 17 | // the field. |
| 18 | // => the body MUST apply them. |
| 19 | // |
| 20 | // The built-in generator satisfies both ONLY because it exists twice: |
| 21 | // DENSITY_GLSL_BODY (no apply) and shaders/generators/noise_sdf.glsl (applies |
| 22 | // inline). noise_sdf even writes the fact down: "Keeping this here (and not |
| 23 | // removing it) is required: edge_intersect.glsl calls evaluate_density() |
| 24 | // directly and never applies edits separately." |
| 25 | // |
| 26 | // A CUSTOM generator has ONE body for BOTH templates and cannot satisfy both. |
| 27 | // DCTerrainGeneratorProcNodes applies edits in neither -- it contains no |
| 28 | // occurrence of `edit`, `apply_gpu_edits` or `apply_brick_delta` at all -- so on |
| 29 | // map_dc_erosion the density grid is carved while the mesh stage's crossing |
| 30 | // search and normals ride the UNCARVED field. That is the failure class |
| 31 | // edge_intersect.glsl:100-105 already documents from a previous incident: |
| 32 | // "per-chunk steps, dark seams and mis-snapped cut floors". |
| 33 | // |
| 34 | // And the tempting small fix -- emit noise_sdf's tail from the proc-nodes |
| 35 | // generator -- is WRONG on the merits: it would double-apply in the density |
| 36 | // stage, and apply_gpu_edits is ADDITIVE (`d += e.strength * t * t`, |
| 37 | // dc_shader_source.h:347), not idempotent. The carve would go twice as deep. |
| 38 | // |
| 39 | // WHAT THIS ORACLE'S SUBJECT ACTUALLY IS -- read before believing a number. |
| 40 | // |
| 41 | // THE SHADER IS THE SHIPPING ONE. shaders/edge_intersect.glsl is read off |
| 42 | // disk verbatim, including its main(), its declarations and its |
| 43 | // GENERATOR_SDF_BODY injection point. Nothing is reconstructed. |
| 44 | // |
| 45 | // THE BODY IS TRIVIAL, ON PURPOSE: `return -y`, a plane at y=0. That makes |
| 46 | // the expected answer ANALYTIC, so the oracle cannot be gated by the terrain |
| 47 | // -- the trap hyper-games #43 6 paid four rounds for. It also stands in for |
| 48 | // exactly the case that is broken: a generator body that does not apply |
| 49 | // edits, which is every custom generator. |
| 50 | // |
| 51 | // THE UPSTREAM BUFFERS ARE HAND-FED. active_edge_indices, counts, edge_scan |
| 52 | // and the two density endpoints are inputs to this stage, so supplying them |
| 53 | // directly is legitimate; it is not a substitute for the stage under test. |
| 54 | // The density endpoints are filled with the CARVED field, because that is |
| 55 | // what the density stage really produces -- the whole point is that the two |
| 56 | // stages disagree. |
| 57 | // |
| 58 | // ONE TRANSCRIPTION, and it is three lines: the sphere branch of |
| 59 | // apply_gpu_edits (dc_shader_source.h:345-349) is restated in C++ as the |
| 60 | // analytic reference. Annotated below. If someone changes the falloff, this |
| 61 | // oracle goes red for the right reason. |
| 62 | // |
| 63 | // Build (no engine, no godot-cpp, no scons): |
| 64 | // g++ -O2 -std=c++17 -o /tmp/edit_contract tests/test_edit_contract.cpp -lvulkan |
| 65 | // |
| 66 | // Run (from the dc-terrain root, so the shader path resolves): |
| 67 | // /tmp/edit_contract [--shader PATH] |
| 68 | // |
| 69 | // Exit status 0 when the mesh stage samples the carved field, 1 when it does not. |
| 70 | // ============================================================================ |
| 71 | |
| 72 | #include <vulkan/vulkan.h> |
| 73 | |
| 74 | #include <cstdio> |
| 75 | #include <cstdlib> |
| 76 | #include <cstring> |
| 77 | #include <cmath> |
| 78 | #include <string> |
| 79 | #include <vector> |
| 80 | |
| 81 | // --------------------------------------------------------------------------- |
| 82 | // The scene: a plane at y = 0 with one spherical subtract taken out of it. |
| 83 | // --------------------------------------------------------------------------- |
| 84 | static const float EDIT_POS[3] = {0.0f, 0.0f, 0.0f}; |
| 85 | static const float EDIT_RADIUS = 4.0f; |
| 86 | static const float EDIT_STRENGTH = -4.0f; // negative = subtract |
| 87 | static const uint32_t EDIT_TYPE = 0u; // 0 = sphere_sub |
| 88 | |
| 89 | // The edge under test. Chosen so the CARVED surface crosses it and the UNCARVED |
| 90 | // one does not come near it -- an uncarved plane sits at y = 0, two metres above |
| 91 | // the far endpoint -- so a stage reading the wrong field cannot land here by |
| 92 | // luck. Offset in x so the carved gradient tilts and the NORMAL discriminates |
| 93 | // too; on the axis both fields give (0,1,0) and the normal would prove nothing. |
| 94 | static const float EDGE_X = 1.5f; |
| 95 | static const float EDGE_Y0 = -2.0f; |
| 96 | static const float VOXEL_SIZE = 1.0f; |
| 97 | static const int GRID_DIMS = 32; |
| 98 | |
| 99 | // The raw generator body. Trivial by design; see the header note. |
| 100 | static const char* TRIVIAL_BODY = |
| 101 | "// Trivial analytic body: a plane at y = 0, solid below. It applies NO\n" |
| 102 | "// edits -- exactly like DCTerrainGeneratorProcNodes, and exactly like the\n" |
| 103 | "// density stage's DENSITY_GLSL_BODY. That is the contract under test.\n" |
| 104 | "float evaluate_density(float x, float y, float z) {\n" |
| 105 | " return -y;\n" |
| 106 | "}\n"; |
| 107 | |
| 108 | // --- THE ONE TRANSCRIPTION: apply_gpu_edits' sphere branch ------------------- |
| 109 | // dc_shader_source.h:341-349 |
| 110 | // float dist_sq = edx*edx + edy*edy + edz*edz; |
| 111 | // if (dist_sq > e.radius * e.radius) continue; |
| 112 | // float t = 1.0 - dist_sq / (radius*radius); |
| 113 | // d += e.strength * t * t; |
| 114 | static float carved(float x, float y, float z) { |
| 115 | float d = -y; |
| 116 | float ex = x - EDIT_POS[0], ey = y - EDIT_POS[1], ez = z - EDIT_POS[2]; |
| 117 | float dist_sq = ex * ex + ey * ey + ez * ez; |
| 118 | float r_sq = EDIT_RADIUS * EDIT_RADIUS; |
| 119 | if (dist_sq <= r_sq) { |
| 120 | float t = 1.0f - dist_sq / r_sq; |
| 121 | d += EDIT_STRENGTH * t * t; |
| 122 | } |
| 123 | return d; |
| 124 | } |
| 125 | |
| 126 | static float uncarved(float x, float y, float z) { (void)x; (void)z; return -y; } |
| 127 | |
| 128 | // Bisect a field along the test edge. Returns the y of the zero crossing, or |
| 129 | // NAN when the field does not cross it at all. |
| 130 | template <typename F> |
| 131 | static float root_on_edge(F f) { |
| 132 | float lo = EDGE_Y0, hi = EDGE_Y0 + VOXEL_SIZE; |
| 133 | float dlo = f(EDGE_X, lo, 0.0f), dhi = f(EDGE_X, hi, 0.0f); |
| 134 | if ((dlo >= 0.0f) == (dhi >= 0.0f)) return NAN; |
| 135 | for (int i = 0; i < 60; i++) { |
| 136 | float m = 0.5f * (lo + hi); |
| 137 | float dm = f(EDGE_X, m, 0.0f); |
| 138 | if ((dlo >= 0.0f) == (dm >= 0.0f)) { lo = m; dlo = dm; } else { hi = m; } |
| 139 | } |
| 140 | return 0.5f * (lo + hi); |
| 141 | } |
| 142 | |
| 143 | static void carved_normal(float x, float y, float z, float out[3]) { |
| 144 | const float h = VOXEL_SIZE * 0.01f; |
| 145 | float nx = carved(x + h, y, z) - carved(x - h, y, z); |
| 146 | float ny = carved(x, y + h, z) - carved(x, y - h, z); |
| 147 | float nz = carved(x, y, z + h) - carved(x, y, z - h); |
| 148 | float len = std::sqrt(nx * nx + ny * ny + nz * nz); |
| 149 | if (len > 1e-8f) { nx /= len; ny /= len; nz /= len; } |
| 150 | out[0] = nx; out[1] = ny; out[2] = nz; |
| 151 | } |
| 152 | |
| 153 | // --------------------------------------------------------------------------- |
| 154 | // Vulkan scaffolding. Mirrors tests/test_erosion_parity.cpp rather than |
| 155 | // factoring a shared harness out of it: refactoring the one working GPU |
| 156 | // instrument in the repo in order to land a test is how you end up with neither. |
| 157 | // --------------------------------------------------------------------------- |
| 158 | static void die(const char* what, VkResult r) { |
| 159 | fprintf(stderr, "FATAL: %s -> VkResult %d\n", what, (int)r); |
| 160 | exit(1); |
| 161 | } |
| 162 | #define VK(x) do { VkResult _r = (x); if (_r != VK_SUCCESS) die(#x, _r); } while (0) |
| 163 | |
| 164 | static std::string read_file(const char* path) { |
| 165 | FILE* f = fopen(path, "rb"); |
| 166 | if (!f) { fprintf(stderr, "FATAL: cannot open %s\n", path); exit(1); } |
| 167 | fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET); |
| 168 | std::string s(n, '\0'); |
| 169 | if (fread(&s[0], 1, n, f) != (size_t)n) { fprintf(stderr, "FATAL: short read\n"); exit(1); } |
| 170 | fclose(f); |
| 171 | return s; |
| 172 | } |
| 173 | |
| 174 | static std::vector<uint32_t> compile_glsl(const std::string& src, const char* tag) { |
| 175 | char in_path[] = "/tmp/dc_edit_contract_XXXXXX"; |
| 176 | int fd = mkstemp(in_path); |
| 177 | if (fd < 0) { perror("mkstemp"); exit(1); } |
| 178 | FILE* f = fdopen(fd, "w"); |
| 179 | fwrite(src.data(), 1, src.size(), f); |
| 180 | fclose(f); |
| 181 | std::string spv_path = std::string(in_path) + ".spv"; |
| 182 | std::string cmd = "glslc -fshader-stage=comp -O -o " + spv_path + " " + in_path; |
| 183 | if (system(cmd.c_str()) != 0) { |
| 184 | fprintf(stderr, "FATAL: glslc failed for %s (source left at %s)\n", tag, in_path); |
| 185 | exit(1); |
| 186 | } |
| 187 | FILE* sf = fopen(spv_path.c_str(), "rb"); |
| 188 | if (!sf) { fprintf(stderr, "FATAL: no SPIR-V at %s\n", spv_path.c_str()); exit(1); } |
| 189 | fseek(sf, 0, SEEK_END); long n = ftell(sf); fseek(sf, 0, SEEK_SET); |
| 190 | std::vector<uint32_t> spv(n / 4); |
| 191 | if (fread(spv.data(), 1, n, sf) != (size_t)n) { fprintf(stderr, "FATAL: short read\n"); exit(1); } |
| 192 | fclose(sf); |
| 193 | remove(in_path); remove(spv_path.c_str()); |
| 194 | return spv; |
| 195 | } |
| 196 | |
| 197 | struct Ctx { |
| 198 | VkInstance instance = VK_NULL_HANDLE; |
| 199 | VkPhysicalDevice phys = VK_NULL_HANDLE; |
| 200 | VkDevice dev = VK_NULL_HANDLE; |
| 201 | VkQueue queue = VK_NULL_HANDLE; |
| 202 | uint32_t qfam = 0; |
| 203 | VkCommandPool cmdpool = VK_NULL_HANDLE; |
| 204 | VkPhysicalDeviceMemoryProperties memprops{}; |
| 205 | char devname[256] = {0}; |
| 206 | }; |
| 207 | |
| 208 | static uint32_t find_mem(Ctx& c, uint32_t bits, VkMemoryPropertyFlags want) { |
| 209 | for (uint32_t i = 0; i < c.memprops.memoryTypeCount; i++) |
| 210 | if ((bits & (1u << i)) && (c.memprops.memoryTypes[i].propertyFlags & want) == want) |
| 211 | return i; |
| 212 | fprintf(stderr, "FATAL: no memory type with flags 0x%x\n", want); |
| 213 | exit(1); |
| 214 | } |
| 215 | |
| 216 | struct Buf { VkBuffer buf = VK_NULL_HANDLE; VkDeviceMemory mem = VK_NULL_HANDLE; |
| 217 | void* mapped = nullptr; VkDeviceSize size = 0; }; |
| 218 | |
| 219 | static Buf make_host_buf(Ctx& c, VkDeviceSize size, VkBufferUsageFlags usage) { |
| 220 | if (size == 0) size = 16; |
| 221 | Buf b; b.size = size; |
| 222 | VkBufferCreateInfo bi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; |
| 223 | bi.size = size; bi.usage = usage; bi.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 224 | VK(vkCreateBuffer(c.dev, &bi, nullptr, &b.buf)); |
| 225 | VkMemoryRequirements mr; |
| 226 | vkGetBufferMemoryRequirements(c.dev, b.buf, &mr); |
| 227 | VkMemoryAllocateInfo ai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; |
| 228 | ai.allocationSize = mr.size; |
| 229 | ai.memoryTypeIndex = find_mem(c, mr.memoryTypeBits, |
| 230 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
| 231 | VK(vkAllocateMemory(c.dev, &ai, nullptr, &b.mem)); |
| 232 | VK(vkBindBufferMemory(c.dev, b.buf, b.mem, 0)); |
| 233 | VK(vkMapMemory(c.dev, b.mem, 0, VK_WHOLE_SIZE, 0, &b.mapped)); |
| 234 | memset(b.mapped, 0, (size_t)size); |
| 235 | return b; |
| 236 | } |
| 237 | |
| 238 | static void init_vulkan(Ctx& c) { |
| 239 | VkApplicationInfo app{VK_STRUCTURE_TYPE_APPLICATION_INFO}; |
| 240 | app.pApplicationName = "dc-terrain edit contract oracle"; |
| 241 | app.apiVersion = VK_API_VERSION_1_1; |
| 242 | VkInstanceCreateInfo ii{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; |
| 243 | ii.pApplicationInfo = &app; |
| 244 | VK(vkCreateInstance(&ii, nullptr, &c.instance)); |
| 245 | uint32_t n = 0; |
| 246 | VK(vkEnumeratePhysicalDevices(c.instance, &n, nullptr)); |
| 247 | if (!n) { fprintf(stderr, "FATAL: no Vulkan device\n"); exit(1); } |
| 248 | std::vector<VkPhysicalDevice> devs(n); |
| 249 | VK(vkEnumeratePhysicalDevices(c.instance, &n, devs.data())); |
| 250 | c.phys = devs[0]; |
| 251 | for (auto d : devs) { |
| 252 | VkPhysicalDeviceProperties p; |
| 253 | vkGetPhysicalDeviceProperties(d, &p); |
| 254 | if (p.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) { c.phys = d; break; } |
| 255 | } |
| 256 | VkPhysicalDeviceProperties props; |
| 257 | vkGetPhysicalDeviceProperties(c.phys, &props); |
| 258 | snprintf(c.devname, sizeof(c.devname), "%s", props.deviceName); |
| 259 | vkGetPhysicalDeviceMemoryProperties(c.phys, &c.memprops); |
| 260 | uint32_t qn = 0; |
| 261 | vkGetPhysicalDeviceQueueFamilyProperties(c.phys, &qn, nullptr); |
| 262 | std::vector<VkQueueFamilyProperties> qs(qn); |
| 263 | vkGetPhysicalDeviceQueueFamilyProperties(c.phys, &qn, qs.data()); |
| 264 | bool found = false; |
| 265 | for (uint32_t i = 0; i < qn; i++) |
| 266 | if (qs[i].queueFlags & VK_QUEUE_COMPUTE_BIT) { c.qfam = i; found = true; break; } |
| 267 | if (!found) { fprintf(stderr, "FATAL: no compute queue\n"); exit(1); } |
| 268 | float prio = 1.0f; |
| 269 | VkDeviceQueueCreateInfo qi{VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO}; |
| 270 | qi.queueFamilyIndex = c.qfam; qi.queueCount = 1; qi.pQueuePriorities = &prio; |
| 271 | VkDeviceCreateInfo di{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; |
| 272 | di.queueCreateInfoCount = 1; di.pQueueCreateInfos = &qi; |
| 273 | VK(vkCreateDevice(c.phys, &di, nullptr, &c.dev)); |
| 274 | vkGetDeviceQueue(c.dev, c.qfam, 0, &c.queue); |
| 275 | VkCommandPoolCreateInfo ci{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; |
| 276 | ci.queueFamilyIndex = c.qfam; |
| 277 | ci.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
| 278 | VK(vkCreateCommandPool(c.dev, &ci, nullptr, &c.cmdpool)); |
| 279 | } |
| 280 | |
| 281 | // --- push constants, matching edge_intersect.glsl's block exactly ------------ |
| 282 | struct PC { |
| 283 | float cell_origin[3]; float voxel_size; |
| 284 | int32_t grid_dims[3]; uint32_t total_elements; |
| 285 | uint32_t mesh_quality_flags; uint32_t pad0, pad1, pad2; |
| 286 | }; |
| 287 | |
| 288 | // --- GpuEdit, matching the std430 struct in edge_intersect.glsl -------------- |
| 289 | struct GpuEdit { |
| 290 | uint32_t edit_type; |
| 291 | float pos_x, pos_y, pos_z; |
| 292 | float half_x, half_y, half_z; |
| 293 | float radius, strength; |
| 294 | uint32_t pad0, pad1, pad2; |
| 295 | }; |
| 296 | |
| 297 | int main(int argc, char** argv) { |
| 298 | const char* shader_path = "shaders/edge_intersect.glsl"; |
| 299 | for (int i = 1; i < argc; i++) |
| 300 | if (!strcmp(argv[i], "--shader") && i + 1 < argc) shader_path = argv[++i]; |
| 301 | |
| 302 | // ---- Build the shader: shipping template, trivial body ----------------- |
| 303 | std::string tmpl = read_file(shader_path); |
| 304 | const std::string marker = "// GENERATOR_SDF_BODY"; |
| 305 | size_t mpos = tmpl.find(marker); |
| 306 | if (mpos == std::string::npos) { |
| 307 | fprintf(stderr, "FATAL: GENERATOR_SDF_BODY marker not found in %s\n", shader_path); |
| 308 | return 1; |
| 309 | } |
| 310 | tmpl.replace(mpos, marker.length(), TRIVIAL_BODY); |
| 311 | |
| 312 | printf("edit-contract oracle\n"); |
| 313 | printf(" shader %s (%zu bytes, main() and all declarations verbatim)\n", |
| 314 | shader_path, tmpl.size()); |
| 315 | |
| 316 | // ---- The analytic answers --------------------------------------------- |
| 317 | float y_carved = root_on_edge(carved); |
| 318 | float y_uncarved = root_on_edge(uncarved); |
| 319 | if (std::isnan(y_carved)) { |
| 320 | fprintf(stderr, "FATAL: the carved field does not cross the test edge -- " |
| 321 | "the SCENE is wrong, not the shader.\n"); |
| 322 | return 1; |
| 323 | } |
| 324 | float n_ref[3]; |
| 325 | carved_normal(EDGE_X, y_carved, 0.0f, n_ref); |
| 326 | printf(" edge (%.2f, %.2f, 0) -> (%.2f, %.2f, 0)\n", |
| 327 | EDGE_X, EDGE_Y0, EDGE_X, EDGE_Y0 + VOXEL_SIZE); |
| 328 | printf(" carved crossing at y = %+.5f, normal (%.4f, %.4f, %.4f)\n", |
| 329 | y_carved, n_ref[0], n_ref[1], n_ref[2]); |
| 330 | if (std::isnan(y_uncarved)) |
| 331 | printf(" uncarved field does NOT cross this edge at all (plane is at y = 0)\n"); |
| 332 | else |
| 333 | printf(" uncarved crossing at y = %+.5f\n", y_uncarved); |
| 334 | |
| 335 | Ctx c{}; |
| 336 | init_vulkan(c); |
| 337 | printf(" device %s\n\n", c.devname); |
| 338 | |
| 339 | std::vector<uint32_t> spv = compile_glsl(tmpl, "edge_intersect + trivial body"); |
| 340 | |
| 341 | // ---- Buffers, one per declared binding --------------------------------- |
| 342 | const int density_dim = GRID_DIMS + 1; // 33 |
| 343 | const size_t density_n = (size_t)density_dim * density_dim * density_dim; |
| 344 | const size_t edge_n = (size_t)GRID_DIMS * GRID_DIMS * GRID_DIMS * 3; |
| 345 | |
| 346 | const VkBufferUsageFlags SSBO = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; |
| 347 | const VkBufferUsageFlags UBO = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
| 348 | |
| 349 | Buf b_density = make_host_buf(c, density_n * 4, SSBO); |
| 350 | Buf b_flags = make_host_buf(c, edge_n * 4, SSBO); |
| 351 | Buf b_scan = make_host_buf(c, edge_n * 4, SSBO); |
| 352 | Buf b_out = make_host_buf(c, 64 * 4, SSBO); |
| 353 | Buf b_params = make_host_buf(c, 256, UBO); |
| 354 | Buf b_voronoi = make_host_buf(c, 256, SSBO); |
| 355 | Buf b_perm = make_host_buf(c, 3072 * 4, SSBO); |
| 356 | Buf b_edits = make_host_buf(c, 16 + sizeof(GpuEdit), SSBO); |
| 357 | Buf b_active = make_host_buf(c, 64 * 4, SSBO); |
| 358 | Buf b_counts = make_host_buf(c, 64 * 4, SSBO); |
| 359 | Buf b_brick = make_host_buf(c, density_n * 4, SSBO); |
| 360 | Buf b_planes = make_host_buf(c, 64 * 16, SSBO); |
| 361 | |
| 362 | // The one active edge: voxel (0,0,0), dir 1 (the Y edge). |
| 363 | const uint32_t dir = 1u; |
| 364 | const uint32_t gid = 0u * 3u + dir; |
| 365 | ((uint32_t*)b_active.mapped)[0] = gid; |
| 366 | ((uint32_t*)b_counts.mapped)[0] = 1u; |
| 367 | ((uint32_t*)b_scan.mapped)[gid] = 0u; |
| 368 | |
| 369 | // Density endpoints = the CARVED field, because that is what the density |
| 370 | // stage really produces. The disagreement between the two stages is the |
| 371 | // whole subject. |
| 372 | const int d_idx0 = 0; |
| 373 | const int d_idx1 = d_idx0 + density_dim; // dir == 1 steps by density_dim |
| 374 | ((float*)b_density.mapped)[d_idx0] = carved(EDGE_X, EDGE_Y0, 0.0f); |
| 375 | ((float*)b_density.mapped)[d_idx1] = carved(EDGE_X, EDGE_Y0 + VOXEL_SIZE, 0.0f); |
| 376 | |
| 377 | // One spherical subtract. flags = 0, so the brick path is off and the |
| 378 | // inline/edit path is the one under test. |
| 379 | ((uint32_t*)b_edits.mapped)[0] = 1u; |
| 380 | GpuEdit* ge = (GpuEdit*)((char*)b_edits.mapped + 16); |
| 381 | memset(ge, 0, sizeof(*ge)); |
| 382 | ge->edit_type = EDIT_TYPE; |
| 383 | ge->pos_x = EDIT_POS[0]; ge->pos_y = EDIT_POS[1]; ge->pos_z = EDIT_POS[2]; |
| 384 | ge->radius = EDIT_RADIUS; |
| 385 | ge->strength = EDIT_STRENGTH; |
| 386 | |
| 387 | // ---- Descriptors ------------------------------------------------------- |
| 388 | const int NB = 12; |
| 389 | VkDescriptorSetLayoutBinding binds[NB]{}; |
| 390 | for (int i = 0; i < NB; i++) { |
| 391 | binds[i].binding = i; |
| 392 | binds[i].descriptorType = (i == 4) ? VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER |
| 393 | : VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 394 | binds[i].descriptorCount = 1; |
| 395 | binds[i].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; |
| 396 | } |
| 397 | VkDescriptorSetLayoutCreateInfo dli{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; |
| 398 | dli.bindingCount = NB; dli.pBindings = binds; |
| 399 | VkDescriptorSetLayout dsl; |
| 400 | VK(vkCreateDescriptorSetLayout(c.dev, &dli, nullptr, &dsl)); |
| 401 | |
| 402 | VkPushConstantRange pcr{VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(PC)}; |
| 403 | VkPipelineLayoutCreateInfo pli{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; |
| 404 | pli.setLayoutCount = 1; pli.pSetLayouts = &dsl; |
| 405 | pli.pushConstantRangeCount = 1; pli.pPushConstantRanges = &pcr; |
| 406 | VkPipelineLayout playout; |
| 407 | VK(vkCreatePipelineLayout(c.dev, &pli, nullptr, &playout)); |
| 408 | |
| 409 | VkShaderModuleCreateInfo smi{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; |
| 410 | smi.codeSize = spv.size() * 4; smi.pCode = spv.data(); |
| 411 | VkShaderModule mod; |
| 412 | VK(vkCreateShaderModule(c.dev, &smi, nullptr, &mod)); |
| 413 | |
| 414 | VkComputePipelineCreateInfo cpi{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; |
| 415 | cpi.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 416 | cpi.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; |
| 417 | cpi.stage.module = mod; cpi.stage.pName = "main"; |
| 418 | cpi.layout = playout; |
| 419 | VkPipeline pipe; |
| 420 | VK(vkCreateComputePipelines(c.dev, VK_NULL_HANDLE, 1, &cpi, nullptr, &pipe)); |
| 421 | |
| 422 | VkDescriptorPoolSize ps[2] = { |
| 423 | {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, NB - 1}, |
| 424 | {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1}, |
| 425 | }; |
| 426 | VkDescriptorPoolCreateInfo dpi{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; |
| 427 | dpi.maxSets = 1; dpi.poolSizeCount = 2; dpi.pPoolSizes = ps; |
| 428 | VkDescriptorPool dpool; |
| 429 | VK(vkCreateDescriptorPool(c.dev, &dpi, nullptr, &dpool)); |
| 430 | VkDescriptorSetAllocateInfo dai{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; |
| 431 | dai.descriptorPool = dpool; dai.descriptorSetCount = 1; dai.pSetLayouts = &dsl; |
| 432 | VkDescriptorSet dset; |
| 433 | VK(vkAllocateDescriptorSets(c.dev, &dai, &dset)); |
| 434 | |
| 435 | Buf* order[NB] = { &b_density, &b_flags, &b_scan, &b_out, &b_params, |
| 436 | &b_voronoi, &b_perm, &b_edits, &b_active, &b_counts, |
| 437 | &b_brick, &b_planes }; |
| 438 | VkDescriptorBufferInfo dbi[NB]; |
| 439 | VkWriteDescriptorSet w[NB]{}; |
| 440 | for (int i = 0; i < NB; i++) { |
| 441 | dbi[i] = { order[i]->buf, 0, VK_WHOLE_SIZE }; |
| 442 | w[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 443 | w[i].dstSet = dset; w[i].dstBinding = i; w[i].descriptorCount = 1; |
| 444 | w[i].descriptorType = binds[i].descriptorType; |
| 445 | w[i].pBufferInfo = &dbi[i]; |
| 446 | } |
| 447 | vkUpdateDescriptorSets(c.dev, NB, w, 0, nullptr); |
| 448 | |
| 449 | PC pc{}; |
| 450 | pc.cell_origin[0] = EDGE_X; pc.cell_origin[1] = EDGE_Y0; pc.cell_origin[2] = 0.0f; |
| 451 | pc.voxel_size = VOXEL_SIZE; |
| 452 | pc.grid_dims[0] = pc.grid_dims[1] = pc.grid_dims[2] = GRID_DIMS; |
| 453 | pc.total_elements = 1; |
| 454 | pc.mesh_quality_flags = 0; |
| 455 | |
| 456 | VkCommandBufferAllocateInfo cbi{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; |
| 457 | cbi.commandPool = c.cmdpool; cbi.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 458 | cbi.commandBufferCount = 1; |
| 459 | VkCommandBuffer cmd; |
| 460 | VK(vkAllocateCommandBuffers(c.dev, &cbi, &cmd)); |
| 461 | VkCommandBufferBeginInfo cbb{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; |
| 462 | cbb.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 463 | VK(vkBeginCommandBuffer(cmd, &cbb)); |
| 464 | vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pipe); |
| 465 | vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, playout, 0, 1, &dset, 0, nullptr); |
| 466 | vkCmdPushConstants(cmd, playout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(PC), &pc); |
| 467 | vkCmdDispatch(cmd, 1, 1, 1); |
| 468 | VK(vkEndCommandBuffer(cmd)); |
| 469 | VkSubmitInfo si{VK_STRUCTURE_TYPE_SUBMIT_INFO}; |
| 470 | si.commandBufferCount = 1; si.pCommandBuffers = &cmd; |
| 471 | VK(vkQueueSubmit(c.queue, 1, &si, VK_NULL_HANDLE)); |
| 472 | VK(vkQueueWaitIdle(c.queue)); |
| 473 | |
| 474 | const float* got = (const float*)b_out.mapped; |
| 475 | float px = got[0], py = got[1], pz = got[2]; |
| 476 | float nx = got[3], ny = got[4], nz = got[5]; |
| 477 | |
| 478 | float dy = py - y_carved; |
| 479 | float dot = nx * n_ref[0] + ny * n_ref[1] + nz * n_ref[2]; |
| 480 | if (dot > 1.0f) dot = 1.0f; |
| 481 | if (dot < -1.0f) dot = -1.0f; |
| 482 | float nerr = std::acos(dot) * 57.2957795f; |
| 483 | |
| 484 | printf("mesh stage returned\n"); |
| 485 | printf(" position (%+.5f, %+.5f, %+.5f)\n", px, py, pz); |
| 486 | printf(" normal (%.4f, %.4f, %.4f)\n", nx, ny, nz); |
| 487 | printf("\n"); |
| 488 | printf(" crossing error vs carved surface %+.5f m\n", dy); |
| 489 | printf(" normal error vs carved gradient %6.2f deg\n", nerr); |
| 490 | printf("\n"); |
| 491 | |
| 492 | bool ok = (std::fabs(dy) < 0.01f) && (nerr < 3.0f); |
| 493 | if (ok) { |
| 494 | printf("PASS the mesh stage samples the carved field for a body that " |
| 495 | "does not apply edits\n"); |
| 496 | return 0; |
| 497 | } |
| 498 | printf("FAIL the mesh stage is NOT sampling the carved field.\n"); |
| 499 | printf(" evaluate_density() returns the RAW field for this body, and\n"); |
| 500 | printf(" edge_intersect.glsl applies no edits of its own, so the\n"); |
| 501 | printf(" crossing search and the normals ride an uncarved surface\n"); |
| 502 | printf(" while the density endpoints are carved. This is what breaks\n"); |
| 503 | printf(" CSG on every custom generator (map_dc_erosion).\n"); |
| 504 | return 1; |
| 505 | } |