Skip to content

Commit

Permalink
Merge pull request #116 from kvark/plugin
Browse files Browse the repository at this point in the history
The new shader parameter system
  • Loading branch information
emberian committed Jul 23, 2014
2 parents 86d57cb + eb693b6 commit 469b17a
Show file tree
Hide file tree
Showing 10 changed files with 637 additions and 288 deletions.
24 changes: 21 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ GLFW_PLATFORM_FILE = $(SRC_DIR)/glfw_platform/lib.rs
RENDER_FILE = $(SRC_DIR)/render/lib.rs
EXAMPLE_FILES = $(SRC_DIR)/examples/*/*.rs
LIB_FILE = $(SRC_DIR)/gfx/lib.rs
MACRO_FILE = $(SRC_DIR)/macro/lib.rs

COMM_INPUT = $(SRC_DIR)/comm/*.rs
DEVICE_INPUT = $(SRC_DIR)/device/*.rs $(SRC_DIR)/device/gl/*.rs
GLFW_PLATFORM_INPUT = $(SRC_DIR)/glfw_platform/*.rs
RENDER_INPUT = $(SRC_DIR)/render/*.rs
LIB_INPUT = $(SRC_DIR)/gfx/*.rs
MACRO_INPUT = $(SRC_DIR)/macro/*.rs

DEPS_LIB_SEARCH_PATHS = $(DEPS_DIR)/gl-rs/lib $(DEPS_DIR)/glfw-rs/lib
DEPS_LIB_SEARCH_FLAGS = $(patsubst %,-L %, $(DEPS_LIB_SEARCH_PATHS))
Expand All @@ -47,13 +49,15 @@ DEVICE_OUT = $(LIB_DIR)/$(shell $(RUSTC) --print-file-name $(DEVICE_F
GLFW_PLATFORM_OUT = $(LIB_DIR)/$(shell $(RUSTC) --print-file-name $(GLFW_PLATFORM_FILE))
RENDER_OUT = $(LIB_DIR)/$(shell $(RUSTC) --print-file-name $(RENDER_FILE))
LIB_OUT = $(LIB_DIR)/$(shell $(RUSTC) --print-file-name $(LIB_FILE))
MACRO_OUT = $(LIB_DIR)/$(shell $(RUSTC) --print-file-name $(MACRO_FILE))

TEST_DIR = test
COMM_TEST_OUT = $(TEST_DIR)/$(shell $(RUSTC) --print-file-name --test $(COMM_FILE))
DEVICE_TEST_OUT = $(TEST_DIR)/$(shell $(RUSTC) --print-file-name --test $(DEVICE_FILE))
GLFW_PLATFORM_TEST_OUT= $(TEST_DIR)/$(shell $(RUSTC) --print-file-name --test $(GLFW_PLATFORM_FILE))
RENDER_TEST_OUT = $(TEST_DIR)/$(shell $(RUSTC) --print-file-name --test $(RENDER_FILE))
LIB_TEST_OUT = $(TEST_DIR)/$(shell $(RUSTC) --print-file-name --test $(LIB_FILE))
MACRO_TEST_OUT = $(TEST_DIR)/$(shell $(RUSTC) --print-file-name --test $(MACRO_FILE))

EXAMPLES_DIR = examples

Expand All @@ -63,6 +67,7 @@ DEVICE_DOC_OUT = $(DOC_DIR)/$(shell $(RUSTC) --print-crate-name $(DEVICE_
GLFW_PLATFORM_DOC_OUT = $(DOC_DIR)/$(shell $(RUSTC) --print-crate-name $(GLFW_PLATFORM_FILE))
RENDER_DOC_OUT = $(DOC_DIR)/$(shell $(RUSTC) --print-crate-name $(RENDER_FILE))
LIB_DOC_OUT = $(DOC_DIR)/$(shell $(RUSTC) --print-crate-name $(LIB_FILE))
MACRO_DOC_OUT = $(DOC_DIR)/$(shell $(RUSTC) --print-crate-name $(MACRO_FILE))

LIB_INCLUDE_FLAGS = -L $(LIB_DIR) $(DEPS_LIB_SEARCH_FLAGS)
EXAMPLE_INCLUDE_FLAGS = -L $(LIB_DIR) $(DEPS_LIB_SEARCH_FLAGS)
Expand Down Expand Up @@ -124,6 +129,10 @@ $(LIB_OUT): $(DEVICE_OUT) $(GLFW_PLATFORM_OUT) $(RENDER_OUT) $(LIB_INPUT)
mkdir -p $(LIB_DIR)
$(RUSTC) $(LIB_INCLUDE_FLAGS) --out-dir=$(LIB_DIR) $(LIB_CFG) -O $(LIB_FILE)

$(MACRO_OUT): $(MACRO_INPUT)
mkdir -p $(LIB_DIR)
$(RUSTC) --out-dir=$(LIB_DIR) -O $(MACRO_FILE)

.PHONY: lib
lib: $(LIB_OUT)

Expand Down Expand Up @@ -158,8 +167,13 @@ $(LIB_TEST_OUT): $(DEVICE_OUT) $(GLFW_PLATFORM_OUT) $(RENDER_OUT) $(LIB_INPUT)
$(RUSTC) $(LIB_INCLUDE_FLAGS) --test --out-dir=$(TEST_DIR) $(LIB_CFG) -O $(LIB_FILE)
./$(LIB_TEST_OUT)

$(MACRO_TEST_OUT): $(DEVICE_OUT) $(COMM_OUT) $(MACRO_INPUT)
mkdir -p $(TEST_DIR)
$(RUSTC) $(LIB_INCLUDE_FLAGS) --test --out-dir=$(TEST_DIR) $(LIB_CFG) -O $(MACRO_FILE)
./$(MACRO_TEST_OUT)

.PHONY: test
test: $(COMM_TEST_OUT) $(DEVICE_TEST_OUT) $(GLFW_PLATFORM_TEST_OUT) $(RENDER_TEST_OUT) $(LIB_TEST_OUT)
test: $(COMM_TEST_OUT) $(DEVICE_TEST_OUT) $(GLFW_PLATFORM_TEST_OUT) $(RENDER_TEST_OUT) $(LIB_TEST_OUT) $(MACRO_TEST_OUT)

.PHONY: clean-test
clean-test:
Expand Down Expand Up @@ -187,16 +201,20 @@ $(LIB_DOC_OUT): $(DEVICE_OUT) $(GLFW_PLATFORM_OUT) $(RENDER_OUT) $(LIB_INPUT)
mkdir -p $(DOC_DIR)
$(RUSTDOC) $(LIB_INCLUDE_FLAGS) $(LIB_CFG) -o $(DOC_DIR) $(LIB_FILE)

$(MACRO_DOC_OUT): $(MACRO_INPUT)
mkdir -p $(DOC_DIR)
$(RUSTDOC) -o $(DOC_DIR) $(MACRO_FILE)

.PHONY: doc
doc: $(COMM_DOC_OUT) $(DEVICE_DOC_OUT) $(GLFW_PLATFORM_DOC_OUT) $(RENDER_DOC_OUT) $(LIB_DOC_OUT)
doc: $(COMM_DOC_OUT) $(DEVICE_DOC_OUT) $(GLFW_PLATFORM_DOC_OUT) $(RENDER_DOC_OUT) $(LIB_DOC_OUT) $(MACRO_DOC_OUT)

.PHONY: clean-doc
clean-doc:
rm -rf $(DOC_DIR)

# Example compilation

$(EXAMPLE_FILES): lib
$(EXAMPLE_FILES): lib $(MACRO_OUT)
mkdir -p $(EXAMPLES_DIR)
$(RUSTC) $(EXAMPLE_INCLUDE_FLAGS) --out-dir=$(EXAMPLES_DIR) $@

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# gfx-rs

[![Build Status](https://travis-ci.org/bjz/gfx-rs.png?branch=master)](https://travis-ci.org/bjz/gfx-rs)
[![Gitter Chat](https://badges.gitter.im/bjz/gfx-rs.png)](https://gitter.im/bjz/gfx-rs)
[![Stories in Ready](https://badge.waffle.io/bjz/gfx-rs.png?label=S-ready&title=issues)](https://waffle.io/bjz/gfx-rs)
[![Build Status](https://travis-ci.org/gfx-rs/gfx-rs.png?branch=master)](https://travis-ci.org/gfx-rs/gfx-rs)
[![Gitter Chat](https://badges.gitter.im/gfx-rs/gfx-rs.png)](https://gitter.im/gfx-rs/gfx-rs)
[![Stories in Ready](https://badge.waffle.io/gfx-rs/gfx-rs.png?label=S-ready&title=issues)](https://waffle.io/gfx-rs/gfx-rs)

A lightweight buffer, shader and render queue manager for Rust.

Expand Down Expand Up @@ -75,5 +75,5 @@ pub type Matrix4x3<T> = [[T,..3],..4];
gfx-rs is still in the early stages of development. Help is most appreciated.

If you are interested in helping out, you can contact the developers on
[Gitter](https://gitter.im/bjz/gfx-rs). They are also often reachable on
[Gitter](https://gitter.im/gfx-rs/gfx-rs). They are also often reachable on
`irc.mozilla.org #rust-gamedev`.
23 changes: 16 additions & 7 deletions src/examples/triangle/main.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
#![feature(phase)]
#![crate_name = "triangle"]

#[phase(link, plugin)]
#[phase(plugin)]
extern crate gfx_macro;
extern crate gfx;
extern crate glfw;

#[shader_param]
struct Params {
blue: f32,
}

static VERTEX_SRC: gfx::ShaderSource = shaders! {
GLSL_120: b"
#version 120
attribute vec2 a_Pos;
varying vec4 v_Color;
uniform float blue;
void main() {
v_Color = vec4(a_Pos+0.5, 0.0, 1.0);
v_Color = vec4(a_Pos+0.5, blue, 1.0);
gl_Position = vec4(a_Pos, 0.0, 1.0);
}
"
GLSL_150: b"
#version 150 core
in vec2 a_Pos;
out vec4 v_Color;
uniform float blue;
void main() {
v_Color = vec4(a_Pos+0.5, 0.0, 1.0);
v_Color = vec4(a_Pos+0.5, blue, 1.0);
gl_Position = vec4(a_Pos, 0.0, 1.0);
}
"
Expand Down Expand Up @@ -74,9 +82,6 @@ fn main() {
VERTEX_SRC.clone(),
FRAGMENT_SRC.clone());
let frame = gfx::Frame::new();
let mut env = gfx::Environment::new();
env.add_uniform("color", gfx::ValueF32Vec([0.1, 0.1, 0.1, 0.1]));
let env = renderer.create_environment(env);
let state = gfx::DrawState::new();
let mesh = {
let data = vec![-0.5f32, -0.5, 0.5, -0.5, 0.0, 0.5];
Expand All @@ -85,14 +90,18 @@ fn main() {
.add("a_Pos", 2, gfx::mesh::F32)
.complete(3)
};
let data = Params {
blue: 0.3,
};
let bundle = renderer.bundle_program(program, data).unwrap();
while !renderer.should_finish() {
let cdata = gfx::ClearData {
color: Some(gfx::Color([0.3, 0.3, 0.3, 1.0])),
depth: None,
stencil: None,
};
renderer.clear(cdata, frame);
renderer.draw(&mesh, gfx::mesh::VertexSlice(0, 3), frame, program, env, state).unwrap();
renderer.draw(&mesh, gfx::mesh::VertexSlice(0, 3), frame, &bundle, state).unwrap();
renderer.end_frame();
for err in renderer.iter_errors() {
println!("Renderer error: {}", err);
Expand Down
28 changes: 5 additions & 23 deletions src/gfx/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![license = "ASL2"]
#![crate_type = "lib"]

#![feature(macro_rules, phase)]
#![feature(phase)]

#[phase(plugin, link)] extern crate log;
extern crate libc;
Expand All @@ -30,10 +30,11 @@ extern crate render;
// public re-exports
pub use render::{BufferHandle, SurfaceHandle, TextureHandle, SamplerHandle, ProgramHandle, EnvirHandle};
pub use render::Renderer;
pub use Environment = render::envir::Storage;
pub use render::envir::{BlockVar, UniformVar, TextureVar};
pub use render::mesh;
pub use render::rast::{DrawState, BlendAdditive, BlendAlpha};
pub use render::shade::{ParameterSink, ToUniform, ShaderParam,
ParameterLinkError, ParameterError, ErrorInternal, ErrorUniform, ErrorBlock, ErrorTexture,
FnUniform, FnBlock, FnTexture, VarUniform, VarBlock, VarTexture};
pub use render::target::Frame;
pub use device::attrib;
pub use device::target::{Color, ClearData, Plane, TextureLayer, TextureLevel};
Expand All @@ -44,30 +45,11 @@ pub use device::shade::{ShaderSource, StaticBytes, NOT_PROVIDED};
#[cfg(glfw)]
pub use glfw = glfw_platform;


#[allow(visible_private_types)]
pub fn start<C: GraphicsContext<GlBackEnd>, P: GlProvider>(graphics_context: C, provider: P, queue_size: QueueSize)
-> Result<(Renderer, Device<render::Token, GlBackEnd, C>), InitError> {
device::init(graphics_context, provider, queue_size).map(|(tx, rx, server, ack, should_finish)| {
(Renderer::new(tx, rx, ack, should_finish), server)
})
}

// This should live in `device`, but macro reexporting does not work yet.
#[macro_export]
macro_rules! shaders {
(GLSL_120: $v:expr $($t:tt)*) => {
::gfx::ShaderSource {
glsl_120: ::gfx::StaticBytes($v),
..shaders!($($t)*)
}
};
(GLSL_150: $v:expr $($t:tt)*) => {
::gfx::ShaderSource {
glsl_150: ::gfx::StaticBytes($v),
..shaders!($($t)*)
}
};
() => {
::gfx::NOT_PROVIDED
}
}
44 changes: 44 additions & 0 deletions src/macro/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2014 The Gfx-rs Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![crate_name = "gfx_macro"]
#![comment = "Helper macros for gfx-rs"]
#![license = "ASL2"]
#![crate_type = "dylib"]

#![feature(macro_rules, plugin_registrar)]

//! Macro extensions crate.
//! Implements `shaders!` macro as well as `shader_param` attribute

pub mod shade;

#[macro_export]
macro_rules! shaders {
(GLSL_120: $v:expr $($t:tt)*) => {
::gfx::ShaderSource {
glsl_120: ::gfx::StaticBytes($v),
..shaders!($($t)*)
}
};
(GLSL_150: $v:expr $($t:tt)*) => {
::gfx::ShaderSource {
glsl_150: ::gfx::StaticBytes($v),
..shaders!($($t)*)
}
};
() => {
::gfx::NOT_PROVIDED
}
}
Loading

0 comments on commit 469b17a

Please sign in to comment.