|
| 1 | +// dfc_continents_e2e.cpp —— continents 完整链路端到端验证(NormalNoise + shift + shifted_noise) |
| 2 | +#include <vulkan/vulkan.h> |
| 3 | +#include <cstdio> |
| 4 | +#include <cstdlib> |
| 5 | +#include <cstring> |
| 6 | +#include <vector> |
| 7 | +#include <fstream> |
| 8 | +#include <cmath> |
| 9 | +#include "noise.h" |
| 10 | +#include "xoroshiro.h" |
| 11 | +#include "md5.h" |
| 12 | + |
| 13 | +#define CHECK_VK(fn) do { VkResult _r = (fn); if (_r != VK_SUCCESS) { \ |
| 14 | + std::fprintf(stderr, "VK error %d at %s:%d (%s)\n", _r, __FILE__, __LINE__, #fn); \ |
| 15 | + std::exit(1); } } while (0) |
| 16 | + |
| 17 | +static std::vector<uint32_t> loadSpv(const char* path) { |
| 18 | + std::ifstream f(path, std::ios::binary | std::ios::ate); |
| 19 | + if (!f) { std::fprintf(stderr, "cannot open %s\n", path); std::exit(1); } |
| 20 | + std::streamsize n = f.tellg(); f.seekg(0); |
| 21 | + std::vector<uint32_t> code((size_t)n / 4); f.read((char*)code.data(), n); |
| 22 | + return code; |
| 23 | +} |
| 24 | + |
| 25 | +// 收集一个 DoublePerlinNoiseSampler 的 perm/origin(first + second) |
| 26 | +static void collectNormal(const wg::DoublePerlinNoiseSampler& dn, int octBase, |
| 27 | + std::vector<uint32_t>& perm, std::vector<double>& origin) { |
| 28 | + int n = (int)dn.firstSampler.octaveSamplers.size(); |
| 29 | + for (int i = 0; i < n; i++) { |
| 30 | + const wg::PerlinNoiseSampler* pn = dn.firstSampler.octaveSamplers[i].get(); |
| 31 | + if (pn) { |
| 32 | + for (int k = 0; k < 256; k++) perm[(octBase + i) * 256 + k] = (uint32_t)pn->permutation[k]; |
| 33 | + origin[(octBase + i) * 3 + 0] = pn->originX; |
| 34 | + origin[(octBase + i) * 3 + 1] = pn->originY; |
| 35 | + origin[(octBase + i) * 3 + 2] = pn->originZ; |
| 36 | + } |
| 37 | + pn = dn.secondSampler.octaveSamplers[i].get(); |
| 38 | + if (pn) { |
| 39 | + for (int k = 0; k < 256; k++) perm[(octBase + n + i) * 256 + k] = (uint32_t)pn->permutation[k]; |
| 40 | + origin[(octBase + n + i) * 3 + 0] = pn->originX; |
| 41 | + origin[(octBase + n + i) * 3 + 1] = pn->originY; |
| 42 | + origin[(octBase + n + i) * 3 + 2] = pn->originZ; |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +int main() { |
| 48 | + const uint64_t worldSeed = 8576294172403134396ULL; |
| 49 | + wg::XoroshiroRandom base(worldSeed); |
| 50 | + auto randomDeriver = base.nextSplitter(); |
| 51 | + |
| 52 | + // continentalness(NormalNoise,fo=-9)+ offset(fo=-3) |
| 53 | + wg::DoublePerlinNoiseSampler continentalness(randomDeriver.split("minecraft:continentalness"), |
| 54 | + wg::DoublePerlinNoiseSampler::NoiseParameters{-9, {1.0,1.0,2.0,2.0,2.0,1.0,1.0,1.0,1.0}}); |
| 55 | + wg::DoublePerlinNoiseSampler offset(randomDeriver.split("minecraft:offset"), |
| 56 | + wg::DoublePerlinNoiseSampler::NoiseParameters{-3, {1.0,1.0,1.0,0.0}}); |
| 57 | + |
| 58 | + // 收集 perm/origin(octBase 布局:continentalness 0..17,offset 18..25) |
| 59 | + const int totalOct = 18 + 8; |
| 60 | + std::vector<uint32_t> perm(totalOct * 256, 0); |
| 61 | + std::vector<double> origin(totalOct * 3, 0.0); |
| 62 | + collectNormal(continentalness, 0, perm, origin); |
| 63 | + collectNormal(offset, 18, perm, origin); |
| 64 | + |
| 65 | + // 坐标(远坐标) |
| 66 | + const uint32_t N = 1024; |
| 67 | + std::vector<int32_t> coords(3 * N); |
| 68 | + for (uint32_t i = 0; i < N; i++) { |
| 69 | + coords[3*i+0] = (int32_t)(30000000 + (i % 16)); |
| 70 | + coords[3*i+1] = (int32_t)(64 + (i / 16 % 16)); |
| 71 | + coords[3*i+2] = (int32_t)(30000000 + (i / 256)); |
| 72 | + } |
| 73 | + |
| 74 | + // ---- Vulkan 初始化(fp64,4 buffer)---- |
| 75 | + VkApplicationInfo appInfo{}; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; appInfo.apiVersion = VK_API_VERSION_1_3; |
| 76 | + VkInstanceCreateInfo instCI{}; instCI.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; instCI.pApplicationInfo = &appInfo; |
| 77 | + VkInstance instance; CHECK_VK(vkCreateInstance(&instCI, nullptr, &instance)); |
| 78 | + uint32_t devCount = 0; vkEnumeratePhysicalDevices(instance, &devCount, nullptr); |
| 79 | + std::vector<VkPhysicalDevice> phys(devCount); vkEnumeratePhysicalDevices(instance, &devCount, phys.data()); |
| 80 | + VkPhysicalDevice physDev = phys[0]; |
| 81 | + VkPhysicalDeviceProperties devProps; vkGetPhysicalDeviceProperties(physDev, &devProps); |
| 82 | + std::printf("[device] %s\n", devProps.deviceName); |
| 83 | + VkPhysicalDeviceFeatures feat{}; vkGetPhysicalDeviceFeatures(physDev, &feat); |
| 84 | + if (!feat.shaderFloat64) { std::fprintf(stderr, "fp64 not supported\n"); return 1; } |
| 85 | + uint32_t qCount = 0; vkGetPhysicalDeviceQueueFamilyProperties(physDev, &qCount, nullptr); |
| 86 | + std::vector<VkQueueFamilyProperties> qProps(qCount); vkGetPhysicalDeviceQueueFamilyProperties(physDev, &qCount, qProps.data()); |
| 87 | + uint32_t computeFamily = UINT32_MAX; |
| 88 | + for (uint32_t i = 0; i < qCount; i++) if (qProps[i].queueFlags & VK_QUEUE_COMPUTE_BIT) { computeFamily = i; break; } |
| 89 | + float qPri = 1.0f; |
| 90 | + VkDeviceQueueCreateInfo qCI{}; qCI.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; qCI.queueFamilyIndex = computeFamily; qCI.queueCount = 1; qCI.pQueuePriorities = &qPri; |
| 91 | + VkPhysicalDeviceFeatures feat2{}; feat2.shaderFloat64 = VK_TRUE; |
| 92 | + VkDeviceCreateInfo devCI{}; devCI.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; devCI.queueCreateInfoCount = 1; devCI.pQueueCreateInfos = &qCI; devCI.pEnabledFeatures = &feat2; |
| 93 | + VkDevice device; CHECK_VK(vkCreateDevice(physDev, &devCI, nullptr, &device)); |
| 94 | + VkQueue queue; vkGetDeviceQueue(device, computeFamily, 0, &queue); |
| 95 | + |
| 96 | + auto spv = loadSpv("continents.spv"); |
| 97 | + VkShaderModuleCreateInfo smCI{}; smCI.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; smCI.codeSize = spv.size()*4; smCI.pCode = spv.data(); |
| 98 | + VkShaderModule shader; CHECK_VK(vkCreateShaderModule(device, &smCI, nullptr, &shader)); |
| 99 | + VkDescriptorSetLayoutBinding bindings[4]{}; |
| 100 | + for (int b = 0; b < 4; b++) { bindings[b].binding = b; bindings[b].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; bindings[b].descriptorCount = 1; bindings[b].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; } |
| 101 | + VkDescriptorSetLayoutCreateInfo dslCI{}; dslCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; dslCI.bindingCount = 4; dslCI.pBindings = bindings; |
| 102 | + VkDescriptorSetLayout dsl; CHECK_VK(vkCreateDescriptorSetLayout(device, &dslCI, nullptr, &dsl)); |
| 103 | + VkPipelineLayoutCreateInfo plCI{}; plCI.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; plCI.setLayoutCount = 1; plCI.pSetLayouts = &dsl; |
| 104 | + VkPipelineLayout pipelineLayout; CHECK_VK(vkCreatePipelineLayout(device, &plCI, nullptr, &pipelineLayout)); |
| 105 | + VkComputePipelineCreateInfo cpCI{}; cpCI.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; |
| 106 | + cpCI.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; cpCI.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; cpCI.stage.module = shader; cpCI.stage.pName = "main"; cpCI.layout = pipelineLayout; |
| 107 | + VkPipeline pipeline; CHECK_VK(vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &cpCI, nullptr, &pipeline)); |
| 108 | + |
| 109 | + VkDeviceSize coordSize = coords.size() * sizeof(int32_t); |
| 110 | + VkDeviceSize permSize = perm.size() * sizeof(uint32_t); |
| 111 | + VkDeviceSize originSize = origin.size() * sizeof(double); |
| 112 | + VkDeviceSize outSize = N * sizeof(float); |
| 113 | + auto makeBuffer = [&](VkDeviceSize size, VkBuffer* buf, VkDeviceMemory* mem) { |
| 114 | + VkBufferCreateInfo bCI{}; bCI.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; bCI.size = size; bCI.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; bCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 115 | + CHECK_VK(vkCreateBuffer(device, &bCI, nullptr, buf)); |
| 116 | + VkMemoryRequirements req; vkGetBufferMemoryRequirements(device, *buf, &req); |
| 117 | + VkPhysicalDeviceMemoryProperties mp; vkGetPhysicalDeviceMemoryProperties(physDev, &mp); |
| 118 | + uint32_t ti = UINT32_MAX; for (uint32_t i = 0; i < mp.memoryTypeCount; i++) if ((req.memoryTypeBits & (1u<<i)) && (mp.memoryTypes[i].propertyFlags & (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT|VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))) { ti = i; break; } |
| 119 | + VkMemoryAllocateInfo aI{}; aI.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; aI.allocationSize = req.size; aI.memoryTypeIndex = ti; |
| 120 | + CHECK_VK(vkAllocateMemory(device, &aI, nullptr, mem)); CHECK_VK(vkBindBufferMemory(device, *buf, *mem, 0)); |
| 121 | + }; |
| 122 | + VkBuffer coordBuf, permBuf, originBuf, outBuf; VkDeviceMemory coordMem, permMem, originMem, outMem; |
| 123 | + makeBuffer(coordSize, &coordBuf, &coordMem); makeBuffer(permSize, &permBuf, &permMem); |
| 124 | + makeBuffer(originSize, &originBuf, &originMem); makeBuffer(outSize, &outBuf, &outMem); |
| 125 | + { void* m; CHECK_VK(vkMapMemory(device, coordMem, 0, coordSize, 0, &m)); std::memcpy(m, coords.data(), coordSize); vkUnmapMemory(device, coordMem); } |
| 126 | + { void* m; CHECK_VK(vkMapMemory(device, permMem, 0, permSize, 0, &m)); std::memcpy(m, perm.data(), permSize); vkUnmapMemory(device, permMem); } |
| 127 | + { void* m; CHECK_VK(vkMapMemory(device, originMem, 0, originSize, 0, &m)); std::memcpy(m, origin.data(), originSize); vkUnmapMemory(device, originMem); } |
| 128 | + |
| 129 | + VkDescriptorPoolSize poolSize{}; poolSize.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; poolSize.descriptorCount = 4; |
| 130 | + VkDescriptorPoolCreateInfo dpCI{}; dpCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; dpCI.maxSets = 1; dpCI.poolSizeCount = 1; dpCI.pPoolSizes = &poolSize; |
| 131 | + VkDescriptorPool dpool; CHECK_VK(vkCreateDescriptorPool(device, &dpCI, nullptr, &dpool)); |
| 132 | + VkDescriptorSetAllocateInfo dsAI{}; dsAI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; dsAI.descriptorPool = dpool; dsAI.descriptorSetCount = 1; dsAI.pSetLayouts = &dsl; |
| 133 | + VkDescriptorSet ds; CHECK_VK(vkAllocateDescriptorSets(device, &dsAI, &ds)); |
| 134 | + VkDescriptorBufferInfo dbi[4]{{coordBuf,0,coordSize},{permBuf,0,permSize},{originBuf,0,originSize},{outBuf,0,outSize}}; |
| 135 | + VkWriteDescriptorSet writes[4]{}; |
| 136 | + for (int b = 0; b < 4; b++) { writes[b].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writes[b].dstSet = ds; writes[b].dstBinding = b; writes[b].descriptorCount = 1; writes[b].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; writes[b].pBufferInfo = &dbi[b]; } |
| 137 | + vkUpdateDescriptorSets(device, 4, writes, 0, nullptr); |
| 138 | + |
| 139 | + VkCommandPoolCreateInfo cpPoolCI{}; cpPoolCI.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; cpPoolCI.queueFamilyIndex = computeFamily; |
| 140 | + VkCommandPool cmdPool; CHECK_VK(vkCreateCommandPool(device, &cpPoolCI, nullptr, &cmdPool)); |
| 141 | + VkCommandBufferAllocateInfo cbAI{}; cbAI.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; cbAI.commandPool = cmdPool; cbAI.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; cbAI.commandBufferCount = 1; |
| 142 | + VkCommandBuffer cb; CHECK_VK(vkAllocateCommandBuffers(device, &cbAI, &cb)); |
| 143 | + VkCommandBufferBeginInfo begin{}; begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 144 | + CHECK_VK(vkBeginCommandBuffer(cb, &begin)); |
| 145 | + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); |
| 146 | + vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0, 1, &ds, 0, nullptr); |
| 147 | + vkCmdDispatch(cb, (N + 255) / 256, 1, 1); |
| 148 | + CHECK_VK(vkEndCommandBuffer(cb)); |
| 149 | + VkSubmitInfo submit{}; submit.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submit.commandBufferCount = 1; submit.pCommandBuffers = &cb; |
| 150 | + VkFenceCreateInfo fenceCI{}; fenceCI.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 151 | + VkFence fence; CHECK_VK(vkCreateFence(device, &fenceCI, nullptr, &fence)); |
| 152 | + CHECK_VK(vkQueueSubmit(queue, 1, &submit, fence)); |
| 153 | + CHECK_VK(vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX)); |
| 154 | + |
| 155 | + { void* m; CHECK_VK(vkMapMemory(device, outMem, 0, outSize, 0, &m)); std::vector<float> out(N); std::memcpy(out.data(), m, outSize); vkUnmapMemory(device, outMem); |
| 156 | + double maxDiff = 0.0, sumDiff = 0.0; uint32_t maxIdx = 0; |
| 157 | + for (uint32_t i = 0; i < N; i++) { |
| 158 | + int x = coords[3*i+0], y = coords[3*i+1], z = coords[3*i+2]; |
| 159 | + // CPU 参照(对齐 vanilla ShiftDF + ShiftedNoiseDF + NoiseDF) |
| 160 | + double shiftX = offset.sample(x * 0.25, 0.0, z * 0.25) * 4.0; // SHIFT_A: y=0 |
| 161 | + double shiftZ = offset.sample(z * 0.25, x * 0.25, 0.0) * 4.0; // SHIFT_B: x=z,y=x,z=0 |
| 162 | + double d = x * 0.25 + shiftX; |
| 163 | + double e = y * 0.0 + 0.0; // shift_y = 0 |
| 164 | + double f = z * 0.25 + shiftZ; |
| 165 | + double ref = continentalness.sample(d, e, f); |
| 166 | + double diff = std::fabs((double)out[i] - ref); |
| 167 | + if (diff > maxDiff) { maxDiff = diff; maxIdx = i; } |
| 168 | + sumDiff += diff; |
| 169 | + } |
| 170 | + std::printf("[result] N=%u, continents DFC shader vs CPU double: maxDiff=%.3e avgDiff=%.3e\n", N, maxDiff, sumDiff / N); |
| 171 | + int x = coords[3*maxIdx], y = coords[3*maxIdx+1], z = coords[3*maxIdx+2]; |
| 172 | + double shiftX = offset.sample(x * 0.25, 0.0, z * 0.25) * 4.0; |
| 173 | + double shiftZ = offset.sample(z * 0.25, x * 0.25, 0.0) * 4.0; |
| 174 | + std::printf("[result] maxDiff @ (%d,%d,%d): gpu=%.9f cpu=%.9f\n", x, y, z, out[maxIdx], |
| 175 | + continentalness.sample(x*0.25+shiftX, 0.0, z*0.25+shiftZ)); |
| 176 | + } |
| 177 | + |
| 178 | + vkDestroyFence(device, fence, nullptr); vkDestroyCommandPool(device, cmdPool, nullptr); vkDestroyDescriptorPool(device, dpool, nullptr); |
| 179 | + vkFreeMemory(device, coordMem, nullptr); vkFreeMemory(device, permMem, nullptr); vkFreeMemory(device, originMem, nullptr); vkFreeMemory(device, outMem, nullptr); |
| 180 | + vkDestroyBuffer(device, coordBuf, nullptr); vkDestroyBuffer(device, permBuf, nullptr); vkDestroyBuffer(device, originBuf, nullptr); vkDestroyBuffer(device, outBuf, nullptr); |
| 181 | + vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, dsl, nullptr); vkDestroyShaderModule(device, shader, nullptr); |
| 182 | + vkDestroyDevice(device, nullptr); vkDestroyInstance(instance, nullptr); |
| 183 | + std::printf("[done]\n"); |
| 184 | + return 0; |
| 185 | +} |
0 commit comments