xshade-lang / xshade Goto Github PK
View Code? Open in Web Editor NEW"cross shade" meta shading language and compiler
License: MIT License
"cross shade" meta shading language and compiler
License: MIT License
Product types to model tuples and units.
fn main() {
// implementation
}
fn main() -> (f32, i32) {
return (0.0, 1);
}
this example creates a product type (f32, i32, Foo)
.
let my_product_type_value = (0.0, 1, Foo::new());
both values of the tuple are available as local foo
and bar
fn get_foo_bar() -> (Foo, Bar) {
return (Foo::new(), Bar::new());
}
fn main() {
let (foo, bar) = get_foo_bar();
}
only foo
is available since the second value was discarded with the _
operator
fn get_foo_bar() -> (Foo, Bar) {
return (Foo::new(), Bar::new());
}
fn main() {
let (foo, _) = get_foo_bar();
}
let my_product_type_value = (0.0, 1, Foo::new());
let foo = my_product_type_value.2;
passes/ast/type_checking
parse_function
Add a short summary.
Each BlockDeclaration has a Span attached to it, which gives the offset, length, line and column of the respective "first character" of the block.
It is default initialized and returned from parsing.
Give some examples.
//... ],
block: BlockDeclaration {
span: Span::new(0, 0, 1, 1),
statements: [
Return(
Give some references about research or otherwise relevant information.
We need to write up a whitepaper describing our goals and non-goals with xshade.
custom constant variants like bindable constants, samplers etc should not be it's own AST node, they are specialized variants of const nodes and the AST should reflect this. Also i'm too lazy to implement independent type checking for all of them!
passes/ast/type_checking
We currently use the parser combinator library nom, which doesn't give us spans (text locations) by default. There is one other open source project which uses nom for lexing/parsing: monkey-rust. We should look at how they solve this. We need the spans to give useful compile errors to the user.
The old TypeEnvironment was encapsulated inside our SymbolTable for convenience, this lead to an unclear API. We first make the TypeEnvironment shared so old code doesn't break:
TypeEnvironment
shared like SymbolTable
: https://github.com/xshade-lang/xshade/blob/master/src/type_system/symbol_table.rs#L10SymbolTable
use a shared reference to the TypeEnvironment: https://github.com/xshade-lang/xshade/blob/master/src/type_system/symbol_table.rs#L71Old code: https://github.com/Vengarioth/xshade/blob/master/src/type_system/type_check.rs#L242-L369
passes/ast/type_checking
Automatically inverts a one-parameter function. This feature can be refined over time, for the start it would be sufficient to only invert simple operations like sin
, cos
, exp
, log
or sqrt
.
A conversion function and its inverted variant:
fn main() {
let to_u = (x) => x * 2.0 - 1.0;
let from_u = invert to_u;
}
TODO
Old code: https://github.com/Vengarioth/xshade/blob/master/src/type_system/type_check.rs#L57-L80
passes/ast/type_checking
As talked before, we need a simple string interner. Currently the compiler copies strings all across the place.
Old code: https://github.com/Vengarioth/xshade/blob/master/src/type_system/type_check.rs#L94-L124
passes/ast/type_checking
Currently the parser returns the smallest valid program, it should provide an error when it's not able to parse the whole input.
Recursion is dangerous on the gpu as it quickly leads to diverging executions. It is forbidden in GLSL and HLSL. We will forbid it too and have to make sure the compiler detects it correctly.
Refactor FunctionArgumentDeclaration
etc into a CallableSignature
featuring both arguments and return type.
We need syntax to make things from other modules accessible. This proposal shows a possible solution.
import
and export
are top-level constructs and cannot exist inside of blocks, however they can be placed at any position within a file. There are no syntax for default exports or imports as this leads to issues when IDEs try to refactor code.
Allowed exports and imports are:
import { Foo, Bar } from other_module;
struct Foobar {
x: Foo;
y: Bar;
}
struct Foo {
x: f32,
y: f32,
}
struct Bar {
x: f32,
y: f32,
}
fn doFoo(in: Input) -> Output {
// ...
}
export { Foo, Bar, doFoo };
export Foo;
export { Foo, Bar };
import Foo from bar;
import { Foo, Bar } from bar;
import * from bar;
import * from foo/bar;
File resolution is a tricky thing for module systems. How do we resolve the standard library? How do we resolve libraries from a potential package manager?
Old code: https://github.com/Vengarioth/xshade/blob/master/src/type_system/type_check.rs#L126-L218
passes/ast/type_checking
Also known as generics
or templates
.
A struct type which takes in a type parameter:
struct MyStruct<T> {
foo: T,
}
Hi!
Are you familiar with other projects targeting GPU graphics using functional programming approach?
i.e.
How is your system different?
We're missing a basic (re-) assignment expression.
fn test() -> f32 {
let x = 1.0;
x = 2.0;
return x;
}
Refactor operator in parser and ast, currently two implementations, one for operator declaration for types and one in expression trees.
Old code: https://github.com/Vengarioth/xshade/blob/master/src/type_system/type_check.rs#L82-L92
passes/ast/type_checking
On GPUs loops can quickly cause performance issues by branching or diverging kernels. To avoid such issues xshade limits the power of loop constructs. One of those limitations is the number of iterations needs to be known at compile time.
for i in 0..16 {
samples[i] = sample(i);
}
Often times while developing shaders we want to be sure that our functions return values of a certain range when given input of a certain range, especially when converting from/to uv space into data (we encountered this while implementing procedural sky rendering).
let samples = from_distribution(PlanetHeight, AtmosphereHeight);
let results = samples.map(convert_from_height);
assert_range(results, 0.0, PI);
Where from_distribution
returns a list of values within the range of its two parameters, map
is your typical mapping function, assert_range
asserts that a list of values is within the given range of parameters.
One idea is to build an interpreter for xshade, running xshade code on the CPU.
Another aproach can be to translate xshade code into another CPU language to run the tests.
It could also be possible to use compute shaders to test xshade code.
After all AST nodes have spans we should add nice error messages.
An example error message.
error: type `vec3` is unknown or not included.
D:\Workspace\xshade\examples\phong.xs:2:15
1 | struct VertexInput {
2 | position: vec3,
| ^^^^ unknown type `vec3`
3 | }
implementations for `vec3` are found in:
* std::vec3 (type alias for `Vector<f32; 3>`)
Types with terms applied to them.
A Vector
type with type parameter T
and term parameter N
. N
is used to determine the number of components this Vector has. A Vector<f32; 4>
for instance is a Vector with 4 components (x, y, z, w), each of type f32
.
struct Vector<T; N> {
values: [T; N],
}
A more extensive example with implementation blocks and limits to types and type terms.
struct Vector<T, N>
where T: Numeric,
N <= 4
{
values: [T; N],
}
impl operator * for Vector<T, 4> {
#[native_impl] // <- compiler tries to substitute a native implementation instead
fn mul(self, scalar: T) -> Self {
// optional implementation
}
#[native_impl]
fn mul(self, other: Self) -> Self {
// optional implementation
}
#[native_impl]
fn mul(self, other: Matrix<T; 4, 4>) -> Self {
// optional implementation
}
}
Old code: https://github.com/Vengarioth/xshade/blob/master/src/type_system/type_check.rs#L220-L240
passes/ast/type_checking
A type representing multiple possible values, like union structs in C, enums in rust. This concept requires pattern matching support from the language to be useful.
This feature should be discussed, as it requires branching which is a bad thing on GPU programs.
An Option<T>
representing the posibility of a value not being present.
enum Option<T> {
Some(T),
None,
}
Matching on an Option<T>
.
match query_some_api() {
Some(my_val) => { //* ... *// },
None => { //* ... *// },
}
passes/ast/type_checking
In the domain of shaders, a program is considered a collection of functions for different shader stages. As a shading language we should implement this concept as a first class citizen.
The parser should allow for any shader stages, later a program checker (similar to a type checker) should validate that the stage is one of vertex
or fragment
for now. The syntax is stage <stage name>(<arguments>) { <body> }
.
A shader program with vertex and fragment stages.
program VertexColored {
stage vertex(in: VertexInput) -> VertexOutput {
return VertexOutput {
position: in.position * mvp,
color: in.color,
}
}
stage fragment(in: VertexOutput) -> vec4 {
return in.color;
}
}
passes/ast/type_checking
Old code: https://github.com/Vengarioth/xshade/blob/master/src/type_system/type_check.rs#L371-L410
passes/ast/type_checking
There is currently no precedence for operations, the parser will parse expressions from right to left (to avoid left recursion). We first need to define precedence for all expressions, then implement it in the compiler.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.