Skip to content

Commit eb46bdb

Browse files
committed
Auto merge of #1125 - msiglreith:corell, r=kvark
Initial groundwork for a low level api ## Motivation The next gen APIs (Vulkan and D3D12) offer an low level interface of the GPU in comparison to older APIs like OpenGL or D3D11. To avoid overhead due to high level abstraction we want to investigate the implementation of a thin wrapper above the mentioned low level APIs to address #1102. ## Goals This PR creates a new low level core API `corell` and two backend implementations for the low level core. corell is heavily based on the vulkan API and shares a lot of concepts like `Instance`, `Surface`, etc., but tries to also take d3d12 into account. In future steps further abstractions could be considered to reduce the amount of setup code. Planned coverage for this PR: - [x] Instance/Context - [x] Surface - [x] Swapchain - [x] PhysicalDevice Note: Also includes some dummy types for other objects. `format` and `memory` are mainly copied from `core`. Hoping for early feedback if this is the intended way to go with regards to #1102. cc @MaikKlein for quick checking ash based vulkan implementation if you have time 😄
2 parents 827deea + 76d65f0 commit eb46bdb

File tree

14 files changed

+2136
-1
lines changed

14 files changed

+2136
-1
lines changed

Cargo.toml

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ documentation = "https://docs.rs/gfx"
1111
[features]
1212
default = []
1313
sdl = ["gfx_window_sdl"]
14-
vulkan = ["gfx_device_vulkan", "gfx_window_vulkan"]
14+
vulkan = ["gfx_device_vulkan", "gfx_device_vulkanll", "gfx_window_vulkan"]
1515
metal = ["gfx_device_metal", "gfx_window_metal"]
1616
unstable = []
1717

@@ -25,6 +25,7 @@ env_logger = "0.3"
2525
glutin = "0.7.1"
2626
winit = "0.5.1"
2727
gfx_core = { path = "src/core", version = "0.6" }
28+
gfx_corell = { path = "src/corell", version = "0.1" }
2829
gfx = { path = "src/render", version = "0.14" }
2930
gfx_device_gl = { path = "src/backend/gl", version = "0.13" }
3031
gfx_window_glutin = { path = "src/window/glutin", version = "0.14" }
@@ -34,6 +35,11 @@ path = "src/backend/vulkan"
3435
version = "0.1"
3536
optional = true
3637

38+
[dependencies.gfx_device_vulkanll]
39+
path = "src/backend/vulkanll"
40+
version = "0.1"
41+
optional = true
42+
3743
[dependencies.gfx_window_vulkan]
3844
path = "src/window/vulkan"
3945
version = "0.1"
@@ -59,6 +65,7 @@ gfx_window_glfw = { path = "src/window/glfw", version = "0.13" }
5965

6066
[target.'cfg(windows)'.dependencies]
6167
gfx_device_dx11 = { path = "src/backend/dx11", version = "0.5" }
68+
gfx_device_dx12ll = { path = "src/backend/dx12ll", version = "0.1" }
6269
gfx_window_dxgi = { path = "src/window/dxgi", version = "0.6" }
6370

6471
[[example]]
@@ -121,6 +128,11 @@ path = "examples/mipmap/main.rs"
121128
name = "particle"
122129
path = "examples/particle/main.rs"
123130

131+
[[example]]
132+
name = "trianglell"
133+
path = "examples/trianglell/main.rs"
134+
135+
124136
[dev_dependencies]
125137
cgmath = "0.7"
126138
gfx_gl = "0.3"

examples/trianglell/main.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2017 The Gfx-rs Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
extern crate env_logger;
16+
extern crate gfx_corell;
17+
#[cfg(all(target_os = "windows", not(feature = "vulkan")))]
18+
extern crate gfx_device_dx12ll as back;
19+
#[cfg(feature = "vulkan")]
20+
extern crate gfx_device_vulkanll as back;
21+
22+
extern crate winit;
23+
24+
use gfx_corell::{Instance, Adapter, Surface, SwapChain, QueueFamily};
25+
26+
pub type ColorFormat = gfx_corell::format::Rgba8;
27+
28+
fn main() {
29+
env_logger::init().unwrap();
30+
let window = winit::WindowBuilder::new()
31+
.with_dimensions(1024, 768)
32+
.with_title("triangle (Low Level)".to_string())
33+
.build()
34+
.unwrap();
35+
36+
// instantiate backend
37+
let instance = back::Instance::create();
38+
let physical_devices = instance.enumerate_adapters();
39+
let surface = instance.create_surface(&window);
40+
41+
let queue_descs = physical_devices[0].get_queue_families().map(|family| { (family, family.num_queues()) });
42+
43+
for device in &physical_devices {
44+
println!("{:?}", device.get_info());
45+
}
46+
47+
// build a new device and associated command queues
48+
let (device, queues) = physical_devices[0].open(queue_descs);
49+
50+
let mut swap_chain = surface.build_swapchain::<ColorFormat>(&queues[0]);
51+
52+
'main: loop {
53+
for event in window.poll_events() {
54+
match event {
55+
winit::Event::KeyboardInput(_, _, Some(winit::VirtualKeyCode::Escape)) |
56+
winit::Event::Closed => break 'main,
57+
_ => {},
58+
}
59+
}
60+
61+
let frame = swap_chain.acquire_frame();
62+
63+
// rendering
64+
65+
// present frame
66+
swap_chain.present();
67+
}
68+
}

src/backend/dx12ll/Cargo.toml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
# Copyright 2016 The Gfx-rs Developers.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
[package]
17+
name = "gfx_device_dx12ll"
18+
version = "0.1.0"
19+
description = "DirectX-12 (Low Level) backend for gfx-rs"
20+
homepage = "https://github.com/gfx-rs/gfx"
21+
repository = "https://github.com/gfx-rs/gfx"
22+
keywords = ["graphics", "gamedev"]
23+
license = "Apache-2.0"
24+
authors = ["The Gfx-rs Developers"]
25+
26+
[lib]
27+
name = "gfx_device_dx12ll"
28+
29+
[dependencies]
30+
log = "0.3"
31+
gfx_corell = { path = "../../corell", version = "0.1.0" }
32+
d3d12-sys = "0.2"
33+
dxgi-sys = "0.2"
34+
dxguid-sys = "0.2"
35+
comptr = { git = "https://github.com/msiglreith/comptr-rs.git" }
36+
winapi = "0.2"
37+
winit = "0.5"

src/backend/dx12ll/src/data.rs

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2017 The Gfx-rs Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use core::format::Format;
16+
use winapi::*;
17+
18+
pub fn map_format(format: Format, is_target: bool) -> Option<DXGI_FORMAT> {
19+
use core::format::SurfaceType::*;
20+
use core::format::ChannelType::*;
21+
Some(match format.0 {
22+
R4_G4 | R4_G4_B4_A4 | R5_G5_B5_A1 | R5_G6_B5 => return None,
23+
R8 => match format.1 {
24+
Int => DXGI_FORMAT_R8_SINT,
25+
Uint => DXGI_FORMAT_R8_UINT,
26+
Inorm => DXGI_FORMAT_R8_SNORM,
27+
Unorm => DXGI_FORMAT_R8_UNORM,
28+
_ => return None,
29+
},
30+
R8_G8 => match format.1 {
31+
Int => DXGI_FORMAT_R8G8_SINT,
32+
Uint => DXGI_FORMAT_R8G8_UINT,
33+
Inorm => DXGI_FORMAT_R8G8_SNORM,
34+
Unorm => DXGI_FORMAT_R8G8_UNORM,
35+
_ => return None,
36+
},
37+
R8_G8_B8_A8 => match format.1 {
38+
Int => DXGI_FORMAT_R8G8B8A8_SINT,
39+
Uint => DXGI_FORMAT_R8G8B8A8_UINT,
40+
Inorm => DXGI_FORMAT_R8G8B8A8_SNORM,
41+
Unorm => DXGI_FORMAT_R8G8B8A8_UNORM,
42+
Srgb => DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
43+
_ => return None,
44+
},
45+
R10_G10_B10_A2 => match format.1 {
46+
Uint => DXGI_FORMAT_R10G10B10A2_UINT,
47+
Unorm => DXGI_FORMAT_R10G10B10A2_UNORM,
48+
_ => return None,
49+
},
50+
R11_G11_B10 => match format.1 {
51+
Float => DXGI_FORMAT_R11G11B10_FLOAT,
52+
_ => return None,
53+
},
54+
R16 => match format.1 {
55+
Int => DXGI_FORMAT_R16_SINT,
56+
Uint => DXGI_FORMAT_R16_UINT,
57+
Inorm => DXGI_FORMAT_R16_SNORM,
58+
Unorm => DXGI_FORMAT_R16_UNORM,
59+
Float => DXGI_FORMAT_R16_FLOAT,
60+
_ => return None,
61+
},
62+
R16_G16 => match format.1 {
63+
Int => DXGI_FORMAT_R16G16_SINT,
64+
Uint => DXGI_FORMAT_R16G16_UINT,
65+
Inorm => DXGI_FORMAT_R16G16_SNORM,
66+
Unorm => DXGI_FORMAT_R16G16_UNORM,
67+
Float => DXGI_FORMAT_R16G16_FLOAT,
68+
_ => return None,
69+
},
70+
R16_G16_B16 => return None,
71+
R16_G16_B16_A16 => match format.1 {
72+
Int => DXGI_FORMAT_R16G16B16A16_SINT,
73+
Uint => DXGI_FORMAT_R16G16B16A16_UINT,
74+
Inorm => DXGI_FORMAT_R16G16B16A16_SNORM,
75+
Unorm => DXGI_FORMAT_R16G16B16A16_UNORM,
76+
Float => DXGI_FORMAT_R16G16B16A16_FLOAT,
77+
_ => return None,
78+
},
79+
R32 => match format.1 {
80+
Int => DXGI_FORMAT_R32_SINT,
81+
Uint => DXGI_FORMAT_R32_UINT,
82+
Float => DXGI_FORMAT_R32_FLOAT,
83+
_ => return None,
84+
},
85+
R32_G32 => match format.1 {
86+
Int => DXGI_FORMAT_R32G32_SINT,
87+
Uint => DXGI_FORMAT_R32G32_UINT,
88+
Float => DXGI_FORMAT_R32G32_FLOAT,
89+
_ => return None,
90+
},
91+
R32_G32_B32 => match format.1 {
92+
Int => DXGI_FORMAT_R32G32B32_SINT,
93+
Uint => DXGI_FORMAT_R32G32B32_UINT,
94+
Float => DXGI_FORMAT_R32G32B32_FLOAT,
95+
_ => return None,
96+
},
97+
R32_G32_B32_A32 => match format.1 {
98+
Int => DXGI_FORMAT_R32G32B32A32_SINT,
99+
Uint => DXGI_FORMAT_R32G32B32A32_UINT,
100+
Float => DXGI_FORMAT_R32G32B32A32_FLOAT,
101+
_ => return None,
102+
},
103+
B8_G8_R8_A8 => match format.1 {
104+
Unorm => DXGI_FORMAT_B8G8R8A8_UNORM,
105+
_ => return None,
106+
},
107+
D16 => match (is_target, format.1) {
108+
(true, _) => DXGI_FORMAT_D16_UNORM,
109+
(false, Unorm) => DXGI_FORMAT_R16_UNORM,
110+
_ => return None,
111+
},
112+
D24 => match (is_target, format.1) {
113+
(true, _) => DXGI_FORMAT_D24_UNORM_S8_UINT,
114+
(false, Unorm) => DXGI_FORMAT_R24_UNORM_X8_TYPELESS,
115+
_ => return None,
116+
},
117+
D24_S8 => match (is_target, format.1) {
118+
(true, _) => DXGI_FORMAT_D24_UNORM_S8_UINT,
119+
(false, Unorm) => DXGI_FORMAT_R24_UNORM_X8_TYPELESS,
120+
(false, Uint) => DXGI_FORMAT_X24_TYPELESS_G8_UINT,
121+
_ => return None,
122+
},
123+
D32 => match (is_target, format.1) {
124+
(true, _) => DXGI_FORMAT_D32_FLOAT,
125+
(false, Float) => DXGI_FORMAT_R32_FLOAT,
126+
_ => return None,
127+
},
128+
})
129+
}

0 commit comments

Comments
 (0)