mirror of
https://github.com/Kbz-8/Pulse.git
synced 2026-01-11 07:23:35 +00:00
working on software backend
This commit is contained in:
1
Examples/Software/.gitignore
vendored
git.filemode.normal_file
1
Examples/Software/.gitignore
vendored
git.filemode.normal_file
@@ -0,0 +1 @@
|
||||
*.spv.h
|
||||
@@ -16,12 +16,57 @@ void DebugCallBack(PulseDebugMessageSeverity severity, const char* message)
|
||||
printf("Pulse: %s\n", message);
|
||||
}
|
||||
|
||||
#define BUFFER_SIZE (256 * sizeof(uint32_t))
|
||||
|
||||
int main(void)
|
||||
{
|
||||
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_SOFTWARE, PULSE_SHADER_FORMAT_SPIRV_BIT, PULSE_HIGH_DEBUG);
|
||||
PulseSetDebugCallback(backend, DebugCallBack);
|
||||
PulseDevice device = PulseCreateDevice(backend, NULL, 0);
|
||||
|
||||
PulseBufferCreateInfo buffer_create_info = { 0 };
|
||||
buffer_create_info.size = BUFFER_SIZE;
|
||||
buffer_create_info.usage = PULSE_BUFFER_USAGE_STORAGE_READ | PULSE_BUFFER_USAGE_STORAGE_WRITE | PULSE_BUFFER_USAGE_TRANSFER_DOWNLOAD;
|
||||
PulseBuffer buffer = PulseCreateBuffer(device, &buffer_create_info);
|
||||
|
||||
// Get result and read it on CPU
|
||||
{
|
||||
PulseBufferCreateInfo staging_buffer_create_info = { 0 };
|
||||
staging_buffer_create_info.size = BUFFER_SIZE;
|
||||
staging_buffer_create_info.usage = PULSE_BUFFER_USAGE_TRANSFER_UPLOAD | PULSE_BUFFER_USAGE_TRANSFER_DOWNLOAD;
|
||||
PulseBuffer staging_buffer = PulseCreateBuffer(device, &staging_buffer_create_info);
|
||||
|
||||
PulseFence fence = PulseCreateFence(device);
|
||||
PulseCommandList cmd = PulseRequestCommandList(device, PULSE_COMMAND_LIST_TRANSFER_ONLY);
|
||||
|
||||
PulseBufferRegion src_region = { 0 };
|
||||
src_region.buffer = buffer;
|
||||
src_region.size = BUFFER_SIZE;
|
||||
|
||||
PulseBufferRegion dst_region = { 0 };
|
||||
dst_region.buffer = staging_buffer;
|
||||
dst_region.size = BUFFER_SIZE;
|
||||
|
||||
PulseCopyBufferToBuffer(cmd, &src_region, &dst_region);
|
||||
|
||||
PulseSubmitCommandList(device, cmd, fence);
|
||||
PulseWaitForFences(device, &fence, 1, true);
|
||||
|
||||
void* ptr;
|
||||
PulseMapBuffer(staging_buffer, PULSE_MAP_READ, &ptr);
|
||||
for(uint32_t i = 0; i < BUFFER_SIZE / sizeof(uint32_t); i++)
|
||||
printf("%d, ", ((int32_t*)ptr)[i]);
|
||||
puts("");
|
||||
PulseUnmapBuffer(staging_buffer);
|
||||
|
||||
PulseDestroyBuffer(device, staging_buffer);
|
||||
|
||||
PulseReleaseCommandList(device, cmd);
|
||||
PulseDestroyFence(device, fence);
|
||||
}
|
||||
|
||||
PulseDestroyBuffer(device, buffer);
|
||||
|
||||
PulseDestroyDevice(device);
|
||||
PulseUnloadBackend(backend);
|
||||
puts("Successfully executed Pulse example using Software backend !");
|
||||
|
||||
25
Examples/Software/shader.nzsl
git.filemode.normal_file
25
Examples/Software/shader.nzsl
git.filemode.normal_file
@@ -0,0 +1,25 @@
|
||||
[nzsl_version("1.0")]
|
||||
module;
|
||||
|
||||
struct Input
|
||||
{
|
||||
[builtin(global_invocation_indices)] indices: vec3[u32]
|
||||
}
|
||||
|
||||
[layout(std430)]
|
||||
struct SSBO
|
||||
{
|
||||
data: dyn_array[i32]
|
||||
}
|
||||
|
||||
external
|
||||
{
|
||||
[set(1), binding(0)] ssbo: storage[SSBO],
|
||||
}
|
||||
|
||||
[entry(compute)]
|
||||
[workgroup(32, 32, 1)]
|
||||
fn main(input: Input)
|
||||
{
|
||||
ssbo.data[input.indices.x * input.indices.y] = i32(input.indices.x * input.indices.y);
|
||||
}
|
||||
@@ -1,7 +1,100 @@
|
||||
add_repositories("nazara-engine-repo https://github.com/NazaraEngine/xmake-repo")
|
||||
|
||||
add_requires("nzsl >=2023.12.31", { configs = { shared = false, nzslc = true } })
|
||||
|
||||
if is_cross() then
|
||||
add_requires("nzsl~host", { kind = "binary", host = true })
|
||||
end
|
||||
|
||||
-- Yoinked from NZSL xmake repo
|
||||
rule("find_nzsl")
|
||||
on_config(function(target)
|
||||
import("core.project.project")
|
||||
import("core.tool.toolchain")
|
||||
import("lib.detect.find_tool")
|
||||
|
||||
local envs
|
||||
if is_plat("windows") then
|
||||
local msvc = target:toolchain("msvc")
|
||||
if msvc and msvc:check() then
|
||||
envs = msvc:runenvs()
|
||||
end
|
||||
elseif is_plat("mingw") then
|
||||
local mingw = target:toolchain("mingw")
|
||||
if mingw and mingw:check() then
|
||||
envs = mingw:runenvs()
|
||||
end
|
||||
end
|
||||
target:data_set("nzsl_envs", envs)
|
||||
|
||||
local nzsl = project.required_package("nzsl~host") or project.required_package("nzsl")
|
||||
local nzsldir
|
||||
if nzsl then
|
||||
nzsldir = path.join(nzsl:installdir(), "bin")
|
||||
local osenvs = os.getenvs()
|
||||
envs = envs or {}
|
||||
for env, values in pairs(nzsl:get("envs")) do
|
||||
local flatval = path.joinenv(values)
|
||||
local oldenv = envs[env] or osenvs[env]
|
||||
if not oldenv or oldenv == "" then
|
||||
envs[env] = flatval
|
||||
elseif not oldenv:startswith(flatval) then
|
||||
envs[env] = flatval .. path.envsep() .. oldenv
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local nzsla = find_tool("nzsla", { version = true, paths = nzsldir, envs = envs })
|
||||
local nzslc = find_tool("nzslc", { version = true, paths = nzsldir, envs = envs })
|
||||
|
||||
target:data_set("nzsla", nzsla)
|
||||
target:data_set("nzslc", nzslc)
|
||||
target:data_set("nzsl_runenv", envs)
|
||||
end)
|
||||
rule_end()
|
||||
|
||||
rule("compile_shaders")
|
||||
set_extensions(".nzsl")
|
||||
add_deps("find_nzsl")
|
||||
|
||||
before_buildcmd_file(function(target, batchcmds, shaderfile, opt)
|
||||
local outputdir = target:data("nzsl_includedirs")
|
||||
local nzslc = target:data("nzslc")
|
||||
local runenvs = target:data("nzsl_runenv")
|
||||
assert(nzslc, "nzslc not found! please install nzsl package with nzslc enabled")
|
||||
|
||||
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.shader %s", shaderfile)
|
||||
local argv = { "--compile=spv-header", "--optimize" }
|
||||
if outputdir then
|
||||
batchcmds:mkdir(outputdir)
|
||||
table.insert(argv, "--output=" .. outputdir)
|
||||
end
|
||||
|
||||
local kind = target:data("plugin.project.kind") or ""
|
||||
if kind:match("vs") then
|
||||
table.insert(argv, "--log-format=vs")
|
||||
end
|
||||
|
||||
table.insert(argv, shaderfile)
|
||||
|
||||
batchcmds:vrunv(nzslc.program, argv, { curdir = ".", envs = runenvs })
|
||||
|
||||
local outputfile = path.join(outputdir or path.directory(shaderfile), path.basename(shaderfile) .. ".spv.h")
|
||||
|
||||
batchcmds:add_depfiles(shaderfile)
|
||||
batchcmds:add_depvalues(nzslc.version)
|
||||
batchcmds:set_depmtime(os.mtime(outputfile))
|
||||
batchcmds:set_depcache(target:dependfile(outputfile))
|
||||
end)
|
||||
rule_end()
|
||||
|
||||
|
||||
target("SoftwareExample")
|
||||
add_deps("pulse_gpu")
|
||||
if is_plat("linux") then
|
||||
set_extension(".x86_64")
|
||||
end
|
||||
add_rules("compile_shaders")
|
||||
add_files("*.c")
|
||||
add_files("*.nzsl")
|
||||
target_end()
|
||||
|
||||
Reference in New Issue
Block a user