Skip to content

Commit a178dfa

Browse files
committed
Linux stuff
1 parent 00395a3 commit a178dfa

File tree

7 files changed

+347
-22
lines changed

7 files changed

+347
-22
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
.vs
22
copy.bat
3-
vsproject64
3+
vsproject64
4+
CMakeCache.txt
5+
CMakeFiles/
6+
Makefile
7+
bin/
8+
cmake_install.cmake

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ set_target_properties(PluginRust PROPERTIES PREFIX "")
2828
target_include_directories(PluginRust PRIVATE libs inc)
2929

3030
if (NOT MSVC)
31-
target_compile_options(GIFCapture PRIVATE -Wno-narrowing)
32-
endif()
31+
target_compile_options(PluginRust PRIVATE -Wno-narrowing)
32+
endif()

dllmain.cpp

+38-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#include <Windows.h>
1+
#define WIN32_LEAN_AND_MEAN
2+
#ifdef _WIN32
3+
#include <windows.h>
4+
#endif
25
#include <PluginAPI/Plugin.h>
36
#include <imgui/imgui.h>
47

@@ -27,8 +30,13 @@ namespace rs
2730
virtual bool Init(bool isWeb, int sedVersion) {
2831
m_buildLangDefinition();
2932
m_spv = nullptr;
30-
m_codegenPath = "rustc_codegen_spirv.dll";
3133

34+
#ifdef _WIN32
35+
m_codegenPath = "rustc_codegen_spirv.dll";
36+
#else
37+
m_codegenPath = "librustc_codegen_spirv.so";
38+
#endif
39+
3240
if (sedVersion == 1003005)
3341
m_hostVersion = 1;
3442
else
@@ -180,7 +188,7 @@ namespace rs
180188
if (strcmp(key, "codegen") == 0) {
181189
m_codegenPath = std::string(val);
182190
if (!std::filesystem::exists(m_codegenPath)) {
183-
printf("[RSHADERS] codegen .dll doesn't exists.\n");
191+
printf("%s\n[RSHADERS] codegen .dll doesn't exists.\n", m_codegenPath.c_str());
184192
}
185193
}
186194
}
@@ -219,7 +227,9 @@ namespace rs
219227
AddMessage(Messages, ed::plugin::MessageType::Error, nullptr, "Cargo not properly set up", -1);
220228
return m_spv;
221229
}
222-
230+
231+
printf("starting to parse!\n");
232+
223233
// TODO: split by \n then parse...
224234
bool compileSuccess = true;
225235
std::stringstream outputParser(output);
@@ -277,9 +287,15 @@ namespace rs
277287
return nullptr;
278288
}
279289

290+
printf("starting to read spv!\n");
291+
280292

281293
// read from .spv file
282294
FILE* reader = fopen("rust_crates/shader/target/spirv-unknown-unknown/release/rust_shader.spv", "rb");
295+
if (reader == nullptr) {
296+
*compiled = false;
297+
return nullptr;
298+
}
283299
fseek(reader, 0, SEEK_END);
284300
long fsize = ftell(reader);
285301
fseek(reader, 0, SEEK_SET);
@@ -475,7 +491,8 @@ namespace rs
475491
std::string result;
476492
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
477493
if (!pipe) {
478-
throw std::runtime_error("popen() failed!");
494+
printf("Failed to run exec()");
495+
return "";
479496
}
480497
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
481498
result += buffer.data();
@@ -507,18 +524,20 @@ extern "C" {
507524
}
508525
}
509526

510-
BOOL APIENTRY DllMain(HMODULE hModule,
511-
DWORD ul_reason_for_call,
512-
LPVOID lpReserved
513-
)
527+
#ifdef _WIN32
528+
BOOL APIENTRY DllMain( HMODULE hModule,
529+
DWORD ul_reason_for_call,
530+
LPVOID lpReserved
531+
)
514532
{
515-
switch (ul_reason_for_call)
516-
{
517-
case DLL_PROCESS_ATTACH:
518-
case DLL_THREAD_ATTACH:
519-
case DLL_THREAD_DETACH:
520-
case DLL_PROCESS_DETACH:
521-
break;
522-
}
523-
return TRUE;
524-
}
533+
switch (ul_reason_for_call)
534+
{
535+
case DLL_PROCESS_ATTACH:
536+
case DLL_THREAD_ATTACH:
537+
case DLL_THREAD_DETACH:
538+
case DLL_PROCESS_DETACH:
539+
break;
540+
}
541+
return TRUE;
542+
}
543+
#endif

rust_crates/shader/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "rust_shader"
3+
version = "0.1.0"
4+
authors = ["Embark <[email protected]>"]
5+
edition = "2018"
6+
license = "MIT OR Apache-2.0"
7+
publish = false
8+
9+
[lib]
10+
crate-type = ["dylib"]
11+
12+
[dependencies]
13+
spirv-std = { path = "../spirv-std" }

rust_crates/spirv-std/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "spirv-std"
3+
version = "0.1.0"
4+
authors = ["Embark <[email protected]>"]
5+
edition = "2018"
6+
license = "MIT OR Apache-2.0"
7+
repository = "https://github.com/EmbarkStudios/rust-gpu"
8+
9+
[dependencies]
10+
# https://github.com/bitshifter/glam-rs/pull/85
11+
glam = { git = "https://github.com/EmbarkStudios/glam-rs", rev = "c9561e4dfd55fa5a9d6838cae3c9e90c8edafaf9" }

rust_crates/spirv-std/src/lib.rs

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#![no_std]
2+
#![feature(register_attr, repr_simd, core_intrinsics)]
3+
#![cfg_attr(target_arch = "spirv", feature(asm))]
4+
#![register_attr(spirv)]
5+
// Our standard Clippy lints that we use in Embark projects, we opt out of a few that are not appropriate for the specific crate (yet)
6+
#![warn(
7+
clippy::all,
8+
clippy::doc_markdown,
9+
clippy::dbg_macro,
10+
clippy::todo,
11+
clippy::empty_enum,
12+
clippy::enum_glob_use,
13+
clippy::pub_enum_variant_names,
14+
clippy::mem_forget,
15+
clippy::filter_map_next,
16+
clippy::needless_continue,
17+
clippy::needless_borrow,
18+
clippy::match_wildcard_for_single_variants,
19+
clippy::if_let_mutex,
20+
clippy::mismatched_target_os,
21+
clippy::await_holding_lock,
22+
clippy::match_on_vec_items,
23+
clippy::imprecise_flops,
24+
//clippy::suboptimal_flops,
25+
clippy::lossy_float_literal,
26+
clippy::rest_pat_in_fully_bound_structs,
27+
clippy::fn_params_excessive_bools,
28+
clippy::exit,
29+
clippy::inefficient_to_string,
30+
clippy::linkedlist,
31+
clippy::macro_use_imports,
32+
clippy::option_option,
33+
clippy::verbose_file_reads,
34+
clippy::unnested_or_patterns,
35+
rust_2018_idioms,
36+
future_incompatible,
37+
nonstandard_style
38+
)]
39+
40+
mod math_ext;
41+
pub use math_ext::MathExt;
42+
43+
pub use glam;
44+
45+
macro_rules! pointer_addrspace_write {
46+
(false) => {};
47+
(true) => {
48+
#[inline]
49+
#[allow(unused_attributes)]
50+
#[spirv(really_unsafe_ignore_bitcasts)]
51+
pub fn store(&mut self, v: T) {
52+
*self.x = v
53+
}
54+
};
55+
}
56+
57+
macro_rules! pointer_addrspace {
58+
($storage_class:ident, $type_name:ident, $writeable:tt) => {
59+
#[allow(unused_attributes)]
60+
#[spirv($storage_class)]
61+
pub struct $type_name<'a, T> {
62+
x: &'a mut T,
63+
}
64+
65+
impl<'a, T: Copy> $type_name<'a, T> {
66+
#[inline]
67+
#[allow(unused_attributes)]
68+
#[spirv(really_unsafe_ignore_bitcasts)]
69+
pub fn load(&self) -> T {
70+
*self.x
71+
}
72+
73+
pointer_addrspace_write!($writeable);
74+
}
75+
};
76+
}
77+
78+
// Make sure these strings stay synced with symbols.rs
79+
// Note the type names don't have to match anything, they can be renamed (only the string must match)
80+
pointer_addrspace!(uniform_constant, UniformConstant, false);
81+
pointer_addrspace!(input, Input, false);
82+
pointer_addrspace!(uniform, Uniform, true);
83+
pointer_addrspace!(output, Output, true);
84+
pointer_addrspace!(workgroup, Workgroup, true);
85+
pointer_addrspace!(cross_workgroup, CrossWorkgroup, true);
86+
pointer_addrspace!(private, Private, true);
87+
pointer_addrspace!(function, Function, true);
88+
pointer_addrspace!(generic, Generic, true);
89+
pointer_addrspace!(push_constant, PushConstant, false);
90+
pointer_addrspace!(atomic_counter, AtomicCounter, true);
91+
pointer_addrspace!(image, Image, true);
92+
pointer_addrspace!(storage_buffer, StorageBuffer, true);
93+
pointer_addrspace!(callable_data_khr, CallableDataKHR, true);
94+
pointer_addrspace!(incoming_callable_data_khr, IncomingCallableDataKHR, true);
95+
pointer_addrspace!(ray_payload_khr, RayPayloadKHR, true);
96+
pointer_addrspace!(hit_attribute_khr, HitAttributeKHR, true);
97+
pointer_addrspace!(incoming_ray_payload_khr, IncomingRayPayloadKHR, true);
98+
pointer_addrspace!(shader_record_buffer_khr, ShaderRecordBufferKHR, true);
99+
pointer_addrspace!(physical_storage_buffer, PhysicalStorageBuffer, true);
100+
101+
pub trait Derivative {
102+
fn ddx(self) -> Self;
103+
fn ddx_fine(self) -> Self;
104+
fn ddx_coarse(self) -> Self;
105+
fn ddy(self) -> Self;
106+
fn ddy_fine(self) -> Self;
107+
fn ddy_coarse(self) -> Self;
108+
fn fwidth(self) -> Self;
109+
fn fwidth_fine(self) -> Self;
110+
fn fwidth_coarse(self) -> Self;
111+
}
112+
113+
#[cfg(target_arch = "spirv")]
114+
macro_rules! deriv_caps {
115+
(true) => {
116+
asm!("OpCapability DerivativeControl")
117+
};
118+
(false) => {};
119+
}
120+
121+
macro_rules! deriv_fn {
122+
($name:ident, $inst:ident, $needs_caps:tt) => {
123+
fn $name(self) -> Self {
124+
#[cfg(not(target_arch = "spirv"))]
125+
panic!(concat!(stringify!($name), " is not supported on the CPU"));
126+
#[cfg(target_arch = "spirv")]
127+
unsafe {
128+
let o;
129+
deriv_caps!($needs_caps);
130+
asm!(
131+
concat!("{1} = ", stringify!($inst), " typeof{0} {0}"),
132+
in(reg) self,
133+
out(reg) o,
134+
);
135+
o
136+
}
137+
}
138+
};
139+
}
140+
macro_rules! deriv_impl {
141+
($ty:ty) => {
142+
impl Derivative for $ty {
143+
deriv_fn!(ddx, OpDPdx, false);
144+
deriv_fn!(ddx_fine, OpDPdxFine, true);
145+
deriv_fn!(ddx_coarse, OpDPdxCoarse, true);
146+
deriv_fn!(ddy, OpDPdy, false);
147+
deriv_fn!(ddy_fine, OpDPdyFine, true);
148+
deriv_fn!(ddy_coarse, OpDPdyCoarse, true);
149+
deriv_fn!(fwidth, OpFwidth, false);
150+
deriv_fn!(fwidth_fine, OpFwidthFine, true);
151+
deriv_fn!(fwidth_coarse, OpFwidthCoarse, true);
152+
}
153+
};
154+
}
155+
156+
// "must be a scalar or vector of floating-point type. The component width must be 32 bits."
157+
deriv_impl!(f32);
158+
// TODO: Fix rustc to support these
159+
// deriv_impl!(glam::Vec2);
160+
// deriv_impl!(glam::Vec3);
161+
// deriv_impl!(glam::Vec4);
162+
163+
/// libcore requires a few external symbols to be defined:
164+
/// <https://github.com/rust-lang/rust/blob/c2bc344eb23d8c1d18e803b3f1e631cf99926fbb/library/core/src/lib.rs#L23-L27>
165+
/// TODO: This is copied from `compiler_builtins/mem.rs`. Can we use that one instead? The note in the above link says
166+
/// "[the symbols] can also be provided by the compiler-builtins crate". The memcpy in `compiler_builtins` is behind a
167+
/// "mem" feature flag - can we enable that somehow?
168+
/// <https://github.com/rust-lang/compiler-builtins/blob/eff506cd49b637f1ab5931625a33cef7e91fbbf6/src/mem.rs#L12-L13>
169+
#[allow(clippy::missing_safety_doc)]
170+
#[no_mangle]
171+
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
172+
let mut i = 0;
173+
while i < n {
174+
let a = *s1.add(i);
175+
let b = *s2.add(i);
176+
if a != b {
177+
return a as i32 - b as i32;
178+
}
179+
i += 1;
180+
}
181+
0
182+
}

0 commit comments

Comments
 (0)