mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 22:07:55 +01:00
48b7c679a0
A player's simulator (Vulkan, GeForce RTX 4060, NVIDIA 610.88, through Steam) crashed at every start as the cab loaded: an access violation (read of address 8) inside nvgpucomp64.dll while vkCreateComputePipelines compiled water_snowlayer.comp. The crash came on $baltyk_skm1.scn and $td.scn alike, at the same address in the driver. The snow shaders (shaders/vk14 and shaders/ogl46, the same in both) no longer use what the compute shader alone had: a vector component written at an index known only while running through an inout parameter (snRef), constant arrays of structs and of ints indexed at run time (sn_grids, sn_lump_tiles, the neighbours of snSpan) and a local vec4 indexed by the blade. They are worked out by small functions and explicit components instead (snRefPut, snRefWord, snGrid, snLumpTile, snNear), and the blade is read from the storage as water_snow.comp, which the same driver compiled, does. The results are the same. glslc compiles both variants and spirv-val passes; whether 610.88 compiles them now can't be checked without that driver. So that a driver crashing on a program the renderer can go without doesn't take the simulator down, the programs of the windscreen water are made under gfx::DriverCrashGuard (rendering/common/drivercrashguard.*): - with msvc the fault of the driver is caught (SEH) and the session goes on without the program: the glasses without the snow, or without the simulation of the water; - the program is written down in the cache of the renderer (driver_crash_attempt.txt) before the driver gets it and crossed out after. a program a session ended on is suspect (driver_crash_suspects.txt); when a second session ends on it, or when a fault was caught, it is recorded (driver_crashes.txt) and not made again with that driver. a new driver or a changed program is tried again; - Vulkan makes these pipelines without the VkPipelineCache, whose lock a faulting driver might leave taken (the drivers keep their own cache on the disk). Tested: unit tests (1211/1211, with a caught access violation), Vulkan and OpenGL 4.6 in the cab of 303E on $td.scn (RTX 4070, 621.14), and on Vulkan with a real access violation injected into the pipeline of water_snowlayer.comp: the session went on without the snow, the next one skipped it. No change of the game data.
159 lines
7.0 KiB
C++
159 lines
7.0 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
// the compute passes of the vulkan renderer (the stages P of the plan): a kernel is a compute shader of shaders/vk14 with its
|
|
// pipeline. its layout is the pass set of the scene (set 0, the persistent one, for the shaders which read the lights, the
|
|
// clusters, the shadow maps or the weather of the pass) and a pushed set of its own (set 1), whose bindings are read from the
|
|
// SPIR-V of the shader, with push constants for the parameters the opengl renderer gives its compute programs as loose
|
|
// uniforms. the data of the gpu read back by the cpu goes through a readback ring, a buffer seen by the cpu for each frame in
|
|
// flight, taken once the gpu is done with the frame
|
|
|
|
#pragma once
|
|
|
|
#include "rendering/vulkan14/vulkan14common.h"
|
|
#include "rendering/vulkan14/vulkan14descriptors.h"
|
|
#include "rendering/vulkan14/vulkan14memory.h"
|
|
#include "rendering/vulkan14/vulkan14shaders.h"
|
|
|
|
#include <optional>
|
|
|
|
namespace vk14
|
|
{
|
|
|
|
// what a template of a pushed set reads for each of its bindings: an image or a buffer, laid out alike
|
|
union descriptor
|
|
{
|
|
VkDescriptorImageInfo image;
|
|
VkDescriptorBufferInfo buffer;
|
|
};
|
|
static_assert(sizeof(VkDescriptorImageInfo) == sizeof(VkDescriptorBufferInfo), "the images and the buffers of the templates are laid out by one stride");
|
|
|
|
inline descriptor image_descriptor(VkSampler Sampler, VkImageView View, VkImageLayout Layout)
|
|
{
|
|
descriptor result;
|
|
result.image = VkDescriptorImageInfo{Sampler, View, Layout};
|
|
return result;
|
|
}
|
|
inline descriptor storage_image(VkImageView View)
|
|
{
|
|
descriptor result;
|
|
result.image = VkDescriptorImageInfo{VK_NULL_HANDLE, View, VK_IMAGE_LAYOUT_GENERAL};
|
|
return result;
|
|
}
|
|
inline descriptor buffer_descriptor(VkBuffer Buffer, VkDeviceSize Offset, VkDeviceSize Range)
|
|
{
|
|
descriptor result;
|
|
result.buffer = VkDescriptorBufferInfo{Buffer, Offset, Range};
|
|
return result;
|
|
}
|
|
|
|
// the bindings of the descriptor set Set the shader uses, from the decorations of its variables, in the order of the bindings
|
|
std::vector<VkDescriptorSetLayoutBinding> reflect_bindings(std::vector<std::uint32_t> const &Spirv, std::uint32_t Set, VkShaderStageFlags Stages);
|
|
|
|
// the set of the compute kernels pushed with their dispatches
|
|
constexpr std::uint32_t COMPUTE_PUSH_SET = 1;
|
|
// the push constants of the kernels at most, vulkan 1.4 guarantees them
|
|
constexpr std::uint32_t COMPUTE_PUSH_CONSTANTS = 256;
|
|
|
|
class compute_kernel
|
|
{
|
|
public:
|
|
compute_kernel() = default;
|
|
compute_kernel(compute_kernel const &) = delete;
|
|
compute_kernel &operator=(compute_kernel const &) = delete;
|
|
|
|
// the kernel of the shader: Passlayout is the layout of the pass set of the scene, set 0 of every kernel; the bindings of
|
|
// set 1 are those the shader declares. Specialization: the values of the constants 0, 1, 2 ...; Guard: how the pipeline is made
|
|
bool create(VkDevice Device, pipeline_library &Pipelines, std::shared_ptr<shader_code const> const &Shader, VkDescriptorSetLayout Passlayout, std::vector<std::uint32_t> const &Specialization, std::string const &Name,
|
|
PipelineGuard Guard = PipelineGuard::none);
|
|
void destroy(VkDevice Device);
|
|
explicit operator bool() const
|
|
{
|
|
return m_pipeline != VK_NULL_HANDLE;
|
|
}
|
|
VkPipelineLayout layout() const
|
|
{
|
|
return m_layout;
|
|
}
|
|
std::size_t bindings() const
|
|
{
|
|
return m_count;
|
|
}
|
|
// the pipeline bound, the set pushed (Descriptors in the order of the bindings of the shader), the constants pushed (Size
|
|
// bytes) and the groups dispatched
|
|
void dispatch(command_stream *Commands, descriptor const *Descriptors, void const *Constants, std::uint32_t Size, std::uint32_t X, std::uint32_t Y = 1, std::uint32_t Z = 1) const;
|
|
template <typename Constants> void dispatch(command_stream *Commands, std::vector<descriptor> const &Descriptors, Constants const &Data, std::uint32_t X, std::uint32_t Y = 1, std::uint32_t Z = 1) const
|
|
{
|
|
static_assert(sizeof(Constants) <= COMPUTE_PUSH_CONSTANTS, "too many push constants");
|
|
dispatch(Commands, Descriptors.data(), &Data, static_cast<std::uint32_t>(sizeof(Constants)), X, Y, Z);
|
|
}
|
|
// the same without push constants
|
|
void run(command_stream *Commands, std::vector<descriptor> const &Descriptors, std::uint32_t X, std::uint32_t Y = 1, std::uint32_t Z = 1) const
|
|
{
|
|
dispatch(Commands, Descriptors.data(), nullptr, 0, X, Y, Z);
|
|
}
|
|
// the same with the groups taken from a buffer
|
|
void dispatch_indirect(command_stream *Commands, descriptor const *Descriptors, void const *Constants, std::uint32_t Size, VkBuffer Buffer, VkDeviceSize Offset) const;
|
|
|
|
private:
|
|
void bind(command_stream *Commands, descriptor const *Descriptors, void const *Constants, std::uint32_t Size) const;
|
|
|
|
push_set m_set;
|
|
VkPipelineLayout m_layout{VK_NULL_HANDLE};
|
|
VkPipeline m_pipeline{VK_NULL_HANDLE};
|
|
std::size_t m_count{0};
|
|
std::string m_name;
|
|
};
|
|
|
|
// a global memory barrier, between the passes which write and read buffers (the images have their own, vk14::barriers())
|
|
void memory_barrier(command_stream *Commands, VkPipelineStageFlags2 SrcStage, VkAccessFlags2 SrcAccess, VkPipelineStageFlags2 DstStage, VkAccessFlags2 DstAccess);
|
|
// the writes of the compute passes made seen by what follows: the compute passes, the draws, the indirect commands, the copies
|
|
void compute_barrier(command_stream *Commands);
|
|
// Size bytes of Data (a multiple of 4, at most 64 KiB) written to the buffer from Offset with the commands of the frame, between
|
|
// its reads by the shaders before and after. outside a rendering
|
|
void update_buffer(command_stream *Commands, buffer const &Target, void const *Data, VkDeviceSize Size, VkDeviceSize Offset);
|
|
|
|
// data copied from the gpu for the cpu: a buffer per frame in flight, the copy of a frame read once the gpu is done with it
|
|
class readback_ring
|
|
{
|
|
public:
|
|
// Shared: copied to by the queues of the families the allocator shares between (the async compute of M6)
|
|
bool create(allocator const &Allocator, VkDeviceSize Size, std::uint32_t Frames, std::string const &Name, bool Shared = false);
|
|
void destroy();
|
|
// the copy of Size bytes (at most the size of the ring) of Source from Offset, in the frame of the slot, done by the gpu once
|
|
// it reaches Value of the timeline
|
|
void push(command_stream *Commands, std::uint32_t Slot, std::uint64_t Value, VkBuffer Source, VkDeviceSize Offset, VkDeviceSize Size);
|
|
// the newest copy the gpu is done with (Completed: the value of the timeline it reached), copied to Data; false when there's
|
|
// none newer than the one taken last
|
|
bool pop(std::uint64_t Completed, void *Data, VkDeviceSize Size);
|
|
// the frame of the copy taken last, for the data which tell what they were made for
|
|
std::uint64_t popped_value() const
|
|
{
|
|
return m_popped;
|
|
}
|
|
std::uint32_t popped_slot() const
|
|
{
|
|
return m_poppedslot;
|
|
}
|
|
|
|
private:
|
|
struct slot
|
|
{
|
|
buffer data;
|
|
std::uint64_t value{0};
|
|
bool pending{false};
|
|
};
|
|
std::vector<slot> m_slots;
|
|
VkDeviceSize m_size{0};
|
|
std::uint64_t m_popped{0};
|
|
std::uint32_t m_poppedslot{0};
|
|
};
|
|
|
|
} // namespace vk14
|