25: Proper render pass support r=grovesNL a=kvark

This PR implements the API scheme of https://github.com/gpuweb/gpuweb/pull/102 and caches both render passes and framebuffers properly in the device.

Co-authored-by: Dzmitry Malyshau <dmalyshau@mozilla.com>
This commit is contained in:
bors[bot] 2018-10-31 12:56:54 +00:00
commit c1eb437b25
13 changed files with 337 additions and 244 deletions

6
Cargo.lock generated
View File

@ -82,7 +82,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cbindgen"
version = "0.6.6"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1033,7 +1033,7 @@ dependencies = [
name = "wgpu-bindings"
version = "0.1.0"
dependencies = [
"cbindgen 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)",
"cbindgen 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1140,7 +1140,7 @@ dependencies = [
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
"checksum byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "90492c5858dd7d2e78691cfb89f90d273a2800fc11d98f60786e5d87e2f83781"
"checksum cbindgen 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "660a462ad3acdc79f83dcc84161d9e87f01cb29754b7ab5196785e1f0c11f714"
"checksum cbindgen 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "cafae60cd9e63287b58380b61a06a221cfdabc504db112dab0e32f21e5ce9014"
"checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16"
"checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3"
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"

View File

@ -1,9 +1,9 @@
#include <stdio.h>
#include "./../../wgpu-bindings/wgpu.h"
#define STAGES_LENGTH (2)
#define BLEND_STATE_LENGTH (1)
#define FORMATS_LENGTH (1)
#define STAGES_LENGTH (2)
#define BLEND_STATES_LENGTH (1)
#define ATTACHMENTS_LENGTH (1)
WGPUByteArray read_file(const char *name)
{
@ -86,7 +86,7 @@ int main()
.write_mask = 0,
};
WGPUBlendStateId blend_state_0 = wgpu_device_create_blend_state(device, &blend_state_0_desc);
WGPUBlendStateId blend_state[BLEND_STATE_LENGTH] = { blend_state_0 };
WGPUBlendStateId blend_state[BLEND_STATES_LENGTH] = { blend_state_0 };
WGPUStencilStateFaceDescriptor stencil_state_front = {
.compare = WGPUCompareFunction_Never,
@ -110,22 +110,27 @@ int main()
};
WGPUDepthStencilStateId depth_stencil_state = wgpu_device_create_depth_stencil_state(device, &depth_stencil_state_desc);
WGPUTextureFormat formats[FORMATS_LENGTH] = { WGPUTextureFormat_R8g8b8a8Unorm };
WGPUAttachmentStateDescriptor attachment_state_desc = {
.formats = formats,
.formats_length = FORMATS_LENGTH,
WGPUAttachment attachments[ATTACHMENTS_LENGTH] = {
{
.format = WGPUTextureFormat_R8g8b8a8Unorm,
.samples = 1,
},
};
WGPUAttachmentsState attachment_state = {
.color_attachments = attachments,
.color_attachments_length = ATTACHMENTS_LENGTH,
.depth_stencil_attachment = NULL,
};
WGPUAttachmentStateId attachment_state = wgpu_device_create_attachment_state(device, &attachment_state_desc);
WGPURenderPipelineDescriptor render_pipeline_desc = {
.layout = layout,
.stages = stages,
.stages_length = STAGES_LENGTH,
.primitive_topology = WGPUPrimitiveTopology_TriangleList,
.attachments_state = attachment_state,
.blend_states = blend_state,
.blend_states_length = BLEND_STATE_LENGTH,
.blend_states_length = BLEND_STATES_LENGTH,
.depth_stencil_state = depth_stencil_state,
.attachment_state = attachment_state,
};
WGPURenderPipelineId render_pipeline = wgpu_device_create_render_pipeline(device, &render_pipeline_desc);

View File

@ -37,9 +37,6 @@ fn main() {
let blend_state0 = device.create_blend_state(&wgpu::BlendStateDescriptor::REPLACE);
let depth_stencil_state = device.create_depth_stencil_state(&wgpu::DepthStencilStateDescriptor::IGNORE);
let attachment_state = device.create_attachment_state(&wgpu::AttachmentStateDescriptor {
formats: &[wgpu::TextureFormat::R8g8b8a8Unorm],
});
let _render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
@ -56,11 +53,19 @@ fn main() {
},
],
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
attachments_state: wgpu::AttachmentsState {
color_attachments: &[
wgpu::Attachment {
format: wgpu::TextureFormat::R8g8b8a8Unorm,
samples: 1,
},
],
depth_stencil_attachment: None,
},
blend_states: &[
&blend_state0,
],
depth_stencil_state: &depth_stencil_state,
attachment_state: &attachment_state,
});
let mut cmd_buf = device.create_command_buffer(&wgpu::CommandBufferDescriptor {});

View File

@ -10,4 +10,4 @@ authors = [
default = []
[dependencies]
cbindgen = "0.6.6"
cbindgen = "0.6.7"

View File

@ -8,26 +8,6 @@
#include <stdlib.h>
#include <stdbool.h>
#define WGPUColorWriteFlags_ALL 15
#define WGPUColorWriteFlags_ALPHA 8
#define WGPUColorWriteFlags_BLUE 4
#define WGPUColorWriteFlags_GREEN 2
#define WGPUColorWriteFlags_NONE 0
#define WGPUColorWriteFlags_RED 1
#define WGPUShaderStageFlags_COMPUTE 4
#define WGPUShaderStageFlags_FRAGMENT 2
#define WGPUShaderStageFlags_NONE 0
#define WGPUShaderStageFlags_VERTEX 1
typedef enum {
WGPUBindingType_UniformBuffer = 0,
WGPUBindingType_Sampler = 1,
@ -114,7 +94,16 @@ typedef enum {
WGPUTextureFormat_D32FloatS8Uint = 3,
} WGPUTextureFormat;
typedef struct WGPURenderPassDescriptor_WGPUTextureViewId WGPURenderPassDescriptor_WGPUTextureViewId;
typedef enum {
WGPUTextureViewDimension_D1,
WGPUTextureViewDimension_D2,
WGPUTextureViewDimension_D2Array,
WGPUTextureViewDimension_Cube,
WGPUTextureViewDimension_CubeArray,
WGPUTextureViewDimension_D3,
} WGPUTextureViewDimension;
typedef struct WGPURenderPassDescriptor_TextureViewId WGPURenderPassDescriptor_TextureViewId;
typedef WGPUId WGPUDeviceId;
@ -136,13 +125,6 @@ typedef WGPUId WGPURenderPassId;
typedef WGPUId WGPUInstanceId;
typedef WGPUId WGPUAttachmentStateId;
typedef struct {
const WGPUTextureFormat *formats;
uintptr_t formats_length;
} WGPUAttachmentStateDescriptor;
typedef WGPUId WGPUBindGroupLayoutId;
typedef uint32_t WGPUShaderStageFlags;
@ -214,15 +196,26 @@ typedef struct {
const char *entry_point;
} WGPUPipelineStageDescriptor;
typedef struct {
WGPUTextureFormat format;
uint32_t samples;
} WGPUAttachment;
typedef struct {
const WGPUAttachment *color_attachments;
uintptr_t color_attachments_length;
const WGPUAttachment *depth_stencil_attachment;
} WGPUAttachmentsState;
typedef struct {
WGPUPipelineLayoutId layout;
const WGPUPipelineStageDescriptor *stages;
uintptr_t stages_length;
WGPUPrimitiveTopology primitive_topology;
WGPUAttachmentsState attachments_state;
const WGPUBlendStateId *blend_states;
uintptr_t blend_states_length;
WGPUDepthStencilStateId depth_stencil_state;
WGPUAttachmentStateId attachment_state;
} WGPURenderPipelineDescriptor;
typedef struct {
@ -258,45 +251,91 @@ typedef struct {
WGPUPowerPreference power_preference;
} WGPUAdapterDescriptor;
#define WGPUBLACK (Color){ .r = 0, .g = 0, .b = 0, .a = 1 }
typedef WGPUId WGPUTextureViewId;
#define WGPUBLUE (Color){ .r = 0, .g = 0, .b = 1, .a = 1 }
typedef uint32_t WGPUTextureAspectFlags;
#define WGPUEXTEND (TrackPermit){ .bits = 1 }
typedef struct {
WGPUTextureFormat format;
WGPUTextureViewDimension dimension;
WGPUTextureAspectFlags aspect;
uint32_t base_mip_level;
uint32_t level_count;
uint32_t base_array_layer;
uint32_t array_count;
} WGPUTextureViewDescriptor;
#define WGPUGREEN (Color){ .r = 0, .g = 1, .b = 0, .a = 1 }
#define WGPUBufferUsageFlags_INDEX (BufferUsageFlags){ .bits = 16 }
#define WGPUINDEX (BufferUsageFlags){ .bits = 16 }
#define WGPUBufferUsageFlags_MAP_READ (BufferUsageFlags){ .bits = 1 }
#define WGPUMAP_READ (BufferUsageFlags){ .bits = 1 }
#define WGPUBufferUsageFlags_MAP_WRITE (BufferUsageFlags){ .bits = 2 }
#define WGPUMAP_WRITE (BufferUsageFlags){ .bits = 2 }
#define WGPUBufferUsageFlags_NONE (BufferUsageFlags){ .bits = 0 }
#define WGPUNONE (BufferUsageFlags){ .bits = 0 }
#define WGPUBufferUsageFlags_STORAGE (BufferUsageFlags){ .bits = 128 }
#define WGPUOUTPUT_ATTACHMENT (TextureUsageFlags){ .bits = 16 }
#define WGPUBufferUsageFlags_TRANSFER_DST (BufferUsageFlags){ .bits = 8 }
#define WGPUPRESENT (TextureUsageFlags){ .bits = 32 }
#define WGPUBufferUsageFlags_TRANSFER_SRC (BufferUsageFlags){ .bits = 4 }
#define WGPURED (Color){ .r = 1, .g = 0, .b = 0, .a = 1 }
#define WGPUBufferUsageFlags_UNIFORM (BufferUsageFlags){ .bits = 64 }
#define WGPUREPLACE (TrackPermit){ .bits = 2 }
#define WGPUBufferUsageFlags_VERTEX (BufferUsageFlags){ .bits = 32 }
#define WGPUSAMPLED (TextureUsageFlags){ .bits = 4 }
#define WGPUColorWriteFlags_ALL (ColorWriteFlags){ .bits = 15 }
#define WGPUSTORAGE (BufferUsageFlags){ .bits = 128 }
#define WGPUColorWriteFlags_ALPHA (ColorWriteFlags){ .bits = 8 }
#define WGPUTRANSFER_DST (BufferUsageFlags){ .bits = 8 }
#define WGPUColorWriteFlags_BLUE (ColorWriteFlags){ .bits = 4 }
#define WGPUTRANSFER_SRC (BufferUsageFlags){ .bits = 4 }
#define WGPUColorWriteFlags_COLOR (ColorWriteFlags){ .bits = 7 }
#define WGPUTRANSPARENT (Color){ .r = 0, .g = 0, .b = 0, .a = 0 }
#define WGPUColorWriteFlags_GREEN (ColorWriteFlags){ .bits = 2 }
#define WGPUUNIFORM (BufferUsageFlags){ .bits = 64 }
#define WGPUColorWriteFlags_RED (ColorWriteFlags){ .bits = 1 }
#define WGPUVERTEX (BufferUsageFlags){ .bits = 32 }
#define WGPUColor_BLACK (Color){ .r = 0, .g = 0, .b = 0, .a = 1 }
#define WGPUWHITE (Color){ .r = 1, .g = 1, .b = 1, .a = 1 }
#define WGPUColor_BLUE (Color){ .r = 0, .g = 0, .b = 1, .a = 1 }
#define WGPUColor_GREEN (Color){ .r = 0, .g = 1, .b = 0, .a = 1 }
#define WGPUColor_RED (Color){ .r = 1, .g = 0, .b = 0, .a = 1 }
#define WGPUColor_TRANSPARENT (Color){ .r = 0, .g = 0, .b = 0, .a = 0 }
#define WGPUColor_WHITE (Color){ .r = 1, .g = 1, .b = 1, .a = 1 }
#define WGPUShaderStageFlags_COMPUTE (ShaderStageFlags){ .bits = 4 }
#define WGPUShaderStageFlags_FRAGMENT (ShaderStageFlags){ .bits = 2 }
#define WGPUShaderStageFlags_VERTEX (ShaderStageFlags){ .bits = 1 }
#define WGPUTextureAspectFlags_COLOR (TextureAspectFlags){ .bits = 1 }
#define WGPUTextureAspectFlags_DEPTH (TextureAspectFlags){ .bits = 2 }
#define WGPUTextureAspectFlags_STENCIL (TextureAspectFlags){ .bits = 4 }
#define WGPUTextureUsageFlags_NONE (TextureUsageFlags){ .bits = 0 }
#define WGPUTextureUsageFlags_OUTPUT_ATTACHMENT (TextureUsageFlags){ .bits = 16 }
#define WGPUTextureUsageFlags_PRESENT (TextureUsageFlags){ .bits = 32 }
#define WGPUTextureUsageFlags_SAMPLED (TextureUsageFlags){ .bits = 4 }
#define WGPUTextureUsageFlags_STORAGE (TextureUsageFlags){ .bits = 8 }
#define WGPUTextureUsageFlags_TRANSFER_DST (TextureUsageFlags){ .bits = 2 }
#define WGPUTextureUsageFlags_TRANSFER_SRC (TextureUsageFlags){ .bits = 1 }
#define WGPUTrackPermit_EXTEND (TrackPermit){ .bits = 1 }
#define WGPUTrackPermit_REPLACE (TrackPermit){ .bits = 2 }
WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id,
const WGPUDeviceDescriptor *_desc);
@ -304,15 +343,12 @@ WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id,
WGPUComputePassId wgpu_command_buffer_begin_compute_pass(WGPUCommandBufferId command_buffer_id);
WGPURenderPassId wgpu_command_buffer_begin_render_pass(WGPUCommandBufferId command_buffer_id,
WGPURenderPassDescriptor_WGPUTextureViewId desc);
WGPURenderPassDescriptor_TextureViewId desc);
WGPUCommandBufferId wgpu_compute_pass_end_pass(WGPUComputePassId pass_id);
WGPUInstanceId wgpu_create_instance(void);
WGPUAttachmentStateId wgpu_device_create_attachment_state(WGPUDeviceId device_id,
const WGPUAttachmentStateDescriptor *desc);
WGPUBindGroupLayoutId wgpu_device_create_bind_group_layout(WGPUDeviceId device_id,
const WGPUBindGroupLayoutDescriptor *desc);
@ -346,3 +382,8 @@ void wgpu_queue_submit(WGPUQueueId queue_id,
uintptr_t command_buffer_count);
WGPUCommandBufferId wgpu_render_pass_end_pass(WGPURenderPassId pass_id);
WGPUTextureViewId wgpu_texture_create_default_texture_view(WGPUTextureId texture_id);
WGPUTextureViewId wgpu_texture_create_texture_view(WGPUTextureId texture_id,
const WGPUTextureViewDescriptor *desc);

View File

@ -2,16 +2,15 @@ use hal;
use {BindGroupLayoutId, BufferId, SamplerId, TextureViewId};
// TODO: bitflags
pub type ShaderStageFlags = u32;
#[allow(non_upper_case_globals)]
pub const ShaderStageFlags_NONE: u32 = 0;
#[allow(non_upper_case_globals)]
pub const ShaderStageFlags_VERTEX: u32 = 1;
#[allow(non_upper_case_globals)]
pub const ShaderStageFlags_FRAGMENT: u32 = 2;
#[allow(non_upper_case_globals)]
pub const ShaderStageFlags_COMPUTE: u32 = 4;
bitflags! {
#[repr(transparent)]
pub struct ShaderStageFlags: u32 {
const VERTEX = 1;
const FRAGMENT = 2;
const COMPUTE = 4;
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]

View File

@ -14,10 +14,11 @@ use {
BufferId, CommandBufferId, ComputePassId, DeviceId, RenderPassId, TextureId, TextureViewId,
};
use conv;
use device::{FramebufferKey, RenderPassKey};
use registry::{HUB, Items, Registry};
use track::{BufferTracker, TextureTracker};
use std::iter;
use std::collections::hash_map::Entry;
use std::ops::Range;
use std::thread::ThreadId;
@ -148,10 +149,10 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass(
);
let mut extent = None;
let render_pass = {
let rp_key = {
let tracker = &mut cmb.texture_tracker;
let depth_stencil_attachment = match desc.depth_stencil_attachment {
let depth_stencil_key = match desc.depth_stencil_attachment {
Some(ref at) => {
let view = view_guard.get(at.attachment);
if let Some(ex) = extent {
@ -174,7 +175,8 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass(
}
None => None,
};
let color_attachments = desc.color_attachments
let color_keys = desc.color_attachments
.iter()
.map(|at| {
let view = view_guard.get(at.attachment);
@ -193,37 +195,65 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass(
layouts: layout .. layout,
}
});
let attachments = color_attachments.chain(depth_stencil_attachment);
//TODO: retain the storage
let color_refs = (0 .. desc.color_attachments.len())
.map(|i| {
(i, hal::image::Layout::ColorAttachmentOptimal)
})
.collect::<Vec<_>>();
let ds_ref = desc.depth_stencil_attachment.as_ref().map(|_| {
(desc.color_attachments.len(), hal::image::Layout::DepthStencilAttachmentOptimal)
});
let subpass = hal::pass::SubpassDesc {
colors: &color_refs,
depth_stencil: ds_ref.as_ref(),
inputs: &[],
resolves: &[],
preserves: &[],
};
device.raw.create_render_pass(attachments, iter::once(subpass), &[])
RenderPassKey {
attachments: color_keys.chain(depth_stencil_key).collect(),
}
};
let framebuffer = {
let attachments = desc.color_attachments
let mut render_pass_cache = device.render_passes.lock().unwrap();
let render_pass = match render_pass_cache.entry(rp_key) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
let color_ids = [
(0, hal::image::Layout::ColorAttachmentOptimal),
(1, hal::image::Layout::ColorAttachmentOptimal),
(2, hal::image::Layout::ColorAttachmentOptimal),
(3, hal::image::Layout::ColorAttachmentOptimal),
];
let depth_id = (desc.color_attachments.len(), hal::image::Layout::DepthStencilAttachmentOptimal);
let subpass = hal::pass::SubpassDesc {
colors: &color_ids[.. desc.color_attachments.len()],
depth_stencil: desc.depth_stencil_attachment.as_ref().map(|_| &depth_id),
inputs: &[],
resolves: &[],
preserves: &[],
};
let pass = device.raw.create_render_pass(
&e.key().attachments,
&[subpass],
&[],
);
e.insert(pass)
}
};
let mut framebuffer_cache = device.framebuffers.lock().unwrap();
let fb_key = FramebufferKey {
attachments: desc.color_attachments
.iter()
.map(|at| at.attachment)
.chain(desc.depth_stencil_attachment.as_ref().map(|at| at.attachment))
.map(|id| &view_guard.get(id).raw);
device.raw
.create_framebuffer(&render_pass, attachments, extent.unwrap())
.unwrap()
.map(|at| Stored(at.attachment))
.chain(desc.depth_stencil_attachment.as_ref().map(|at| Stored(at.attachment)))
.collect(),
};
let framebuffer = match framebuffer_cache.entry(fb_key) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
let fb = {
let attachments = e
.key()
.attachments
.iter()
.map(|&Stored(id)| &view_guard.get(id).raw);
device.raw
.create_framebuffer(&render_pass, attachments, extent.unwrap())
.unwrap()
};
e.insert(fb)
}
};
let rect = {
@ -246,9 +276,10 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass(
let value = hal::command::ClearDepthStencil(at.clear_depth, at.clear_stencil);
hal::command::ClearValueRaw::from(hal::command::ClearValue::DepthStencil(value))
}));
current_comb.begin_render_pass(
&render_pass,
&framebuffer,
render_pass,
framebuffer,
rect,
clear_values,
hal::command::SubpassContents::Inline,

View File

@ -88,18 +88,17 @@ pub fn map_binding_type(
pub fn map_shader_stage_flags(
shader_stage_flags: binding_model::ShaderStageFlags,
) -> hal::pso::ShaderStageFlags {
use binding_model::{
ShaderStageFlags_COMPUTE, ShaderStageFlags_FRAGMENT, ShaderStageFlags_VERTEX,
};
use binding_model::ShaderStageFlags as F;
use hal::pso::ShaderStageFlags as H;
let mut value = H::empty();
if 0 != shader_stage_flags & ShaderStageFlags_VERTEX {
if shader_stage_flags.contains(F::VERTEX) {
value |= H::VERTEX;
}
if 0 != shader_stage_flags & ShaderStageFlags_FRAGMENT {
if shader_stage_flags.contains(F::FRAGMENT) {
value |= H::FRAGMENT;
}
if 0 != shader_stage_flags & ShaderStageFlags_COMPUTE {
if shader_stage_flags.contains(F::COMPUTE) {
value |= H::COMPUTE;
}
value
@ -134,22 +133,21 @@ pub fn map_blend_state_descriptor(
hal::pso::ColorBlendDesc(map_color_write_flags(color_mask), blend_state)
}
fn map_color_write_flags(flags: u32) -> hal::pso::ColorMask {
fn map_color_write_flags(flags: pipeline::ColorWriteFlags) -> hal::pso::ColorMask {
use pipeline::ColorWriteFlags as F;
use hal::pso::ColorMask as H;
use pipeline::{
ColorWriteFlags_ALPHA, ColorWriteFlags_BLUE, ColorWriteFlags_GREEN, ColorWriteFlags_RED,
};
let mut value = H::empty();
if 0 != flags & ColorWriteFlags_RED {
if flags.contains(F::RED) {
value |= H::RED;
}
if 0 != flags & ColorWriteFlags_GREEN {
if flags.contains(F::GREEN) {
value |= H::GREEN;
}
if 0 != flags & ColorWriteFlags_BLUE {
if flags.contains(F::BLUE) {
value |= H::BLUE;
}
if 0 != flags & ColorWriteFlags_ALPHA {
if flags.contains(F::ALPHA) {
value |= H::ALPHA;
}
value

View File

@ -3,7 +3,7 @@ use registry::{HUB, Items, Registry};
use track::{BufferTracker, TextureTracker};
use {
CommandBuffer, Stored, TextureUsageFlags,
AttachmentStateId, BindGroupLayoutId, BlendStateId, CommandBufferId, DepthStencilStateId,
BindGroupLayoutId, BlendStateId, CommandBufferId, DepthStencilStateId,
DeviceId, PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId,
TextureId, TextureViewId,
};
@ -14,9 +14,23 @@ use hal::{self, Device as _Device};
use rendy_memory::{allocator, Config, Heaps};
use std::{ffi, slice};
use std::collections::hash_map::{Entry, HashMap};
use std::sync::Mutex;
#[derive(Hash, PartialEq)]
pub(crate) struct RenderPassKey {
pub attachments: Vec<hal::pass::Attachment>,
}
impl Eq for RenderPassKey {}
#[derive(Hash, PartialEq)]
pub(crate) struct FramebufferKey {
pub attachments: Vec<Stored<TextureViewId>>,
}
impl Eq for FramebufferKey {}
pub struct Device<B: hal::Backend> {
pub(crate) raw: B::Device,
queue_group: hal::QueueGroup<B, hal::General>,
@ -25,6 +39,8 @@ pub struct Device<B: hal::Backend> {
buffer_tracker: Mutex<BufferTracker>,
texture_tracker: Mutex<TextureTracker>,
mem_props: hal::MemoryProperties,
pub(crate) render_passes: Mutex<HashMap<RenderPassKey, B::RenderPass>>,
pub(crate) framebuffers: Mutex<HashMap<FramebufferKey, B::Framebuffer>>,
}
impl<B: hal::Backend> Device<B> {
@ -62,6 +78,8 @@ impl<B: hal::Backend> Device<B> {
buffer_tracker: Mutex::new(BufferTracker::new()),
texture_tracker: Mutex::new(TextureTracker::new()),
mem_props,
render_passes: Mutex::new(HashMap::new()),
framebuffers: Mutex::new(HashMap::new()),
}
}
}
@ -399,59 +417,6 @@ pub extern "C" fn wgpu_queue_submit(
}
}
#[no_mangle]
pub extern "C" fn wgpu_device_create_attachment_state(
device_id: DeviceId,
desc: &pipeline::AttachmentStateDescriptor,
) -> AttachmentStateId {
let device_guard = HUB.devices.lock();
let device = &device_guard.get(device_id).raw;
let color_formats = unsafe {
slice::from_raw_parts(desc.formats, desc.formats_length)
};
let color_formats: Vec<_> = color_formats
.iter()
.cloned()
.map(conv::map_texture_format)
.collect();
let depth_stencil_format = None;
let base_pass = {
let attachments = color_formats.iter().map(|cf| hal::pass::Attachment {
format: Some(*cf),
samples: 1,
ops: hal::pass::AttachmentOps::DONT_CARE,
stencil_ops: hal::pass::AttachmentOps::DONT_CARE,
layouts: hal::image::Layout::General .. hal::image::Layout::General,
});
let subpass = hal::pass::SubpassDesc {
colors: &[(0, hal::image::Layout::ColorAttachmentOptimal)],
depth_stencil: None,
inputs: &[],
resolves: &[],
preserves: &[],
};
device.create_render_pass(
attachments,
&[subpass],
&[],
)
};
let at_state = pipeline::AttachmentState {
base_pass,
color_formats,
depth_stencil_format,
};
HUB.attachment_states
.lock()
.register(at_state)
}
#[no_mangle]
pub extern "C" fn wgpu_device_create_render_pipeline(
device_id: DeviceId,
@ -464,11 +429,78 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
};
let device_guard = HUB.devices.lock();
let device = &device_guard.get(device_id).raw;
let device = device_guard.get(device_id);
let pipeline_layout_guard = HUB.pipeline_layouts.lock();
let layout = &pipeline_layout_guard.get(desc.layout).raw;
let pipeline_stages = unsafe { slice::from_raw_parts(desc.stages, desc.stages_length) };
let shader_module_guard = HUB.shader_modules.lock();
let rp_key = {
let op_keep = hal::pass::AttachmentOps {
load: hal::pass::AttachmentLoadOp::Load,
store: hal::pass::AttachmentStoreOp::Store,
};
let color_attachments = unsafe {
slice::from_raw_parts(
desc.attachments_state.color_attachments,
desc.attachments_state.color_attachments_length,
)
};
let depth_stencil_attachment = unsafe {
desc.attachments_state.depth_stencil_attachment.as_ref()
};
let color_keys = color_attachments.iter().map(|at| hal::pass::Attachment {
format: Some(conv::map_texture_format(at.format)),
samples: at.samples as u8,
ops: op_keep,
stencil_ops: hal::pass::AttachmentOps::DONT_CARE,
layouts: hal::image::Layout::General .. hal::image::Layout::General,
});
let depth_stencil_key = depth_stencil_attachment.map(|at| hal::pass::Attachment {
format: Some(conv::map_texture_format(at.format)),
samples: at.samples as u8,
ops: op_keep,
stencil_ops: op_keep,
layouts: hal::image::Layout::General .. hal::image::Layout::General,
});
RenderPassKey {
attachments: color_keys.chain(depth_stencil_key).collect(),
}
};
let mut render_pass_cache = device.render_passes.lock().unwrap();
let main_pass = match render_pass_cache.entry(rp_key) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
let color_ids = [
(0, hal::image::Layout::ColorAttachmentOptimal),
(1, hal::image::Layout::ColorAttachmentOptimal),
(2, hal::image::Layout::ColorAttachmentOptimal),
(3, hal::image::Layout::ColorAttachmentOptimal),
];
let depth_id = (desc.attachments_state.color_attachments_length, hal::image::Layout::DepthStencilAttachmentOptimal);
let subpass = hal::pass::SubpassDesc {
colors: &color_ids[.. desc.attachments_state.color_attachments_length],
depth_stencil: if desc.attachments_state.depth_stencil_attachment.is_null() {
None
} else {
Some(&depth_id)
},
inputs: &[],
resolves: &[],
preserves: &[],
};
let pass = device.raw.create_render_pass(
&e.key().attachments,
&[subpass],
&[],
);
e.insert(pass)
}
};
let shaders = {
let mut vertex = None;
let mut fragment = None;
@ -564,18 +596,13 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
depth_bounds: None,
};
let attachment_state_guard = HUB.attachment_states.lock();
let attachment_state = attachment_state_guard.get(desc.attachment_state);
// TODO
let subpass = hal::pass::Subpass {
index: 0,
main_pass: &attachment_state.base_pass,
main_pass,
};
// TODO
let flags = hal::pso::PipelineCreationFlags::empty();
// TODO
let parent = hal::pso::BasePipeline::None;
@ -596,7 +623,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
};
// TODO: cache
let pipeline = device
let pipeline = device.raw
.create_graphics_pipeline(&pipeline_desc, None)
.unwrap();

View File

@ -123,8 +123,6 @@ type DepthStencilStateHandle = DepthStencilState;
pub type InputStateId = Id;
pub type ShaderModuleId = Id;
type ShaderModuleHandle = ShaderModule<B>;
pub type AttachmentStateId = Id;
type AttachmentStateHandle = AttachmentState<B>;
pub type RenderPipelineId = Id;
type RenderPipelineHandle = RenderPipeline<B>;
pub type ComputePipelineId = Id;

View File

@ -2,7 +2,7 @@ use hal;
use resource;
use {
AttachmentStateId, BlendStateId, ByteArray, DepthStencilStateId, PipelineLayoutId,
BlendStateId, ByteArray, DepthStencilStateId, PipelineLayoutId,
ShaderModuleId,
};
@ -34,20 +34,17 @@ pub enum BlendOperation {
Max = 4,
}
// TODO: bitflags
pub type ColorWriteFlags = u32;
#[allow(non_upper_case_globals)]
pub const ColorWriteFlags_NONE: u32 = 0;
#[allow(non_upper_case_globals)]
pub const ColorWriteFlags_RED: u32 = 1;
#[allow(non_upper_case_globals)]
pub const ColorWriteFlags_GREEN: u32 = 2;
#[allow(non_upper_case_globals)]
pub const ColorWriteFlags_BLUE: u32 = 4;
#[allow(non_upper_case_globals)]
pub const ColorWriteFlags_ALPHA: u32 = 8;
#[allow(non_upper_case_globals)]
pub const ColorWriteFlags_ALL: u32 = 15;
bitflags! {
#[repr(transparent)]
pub struct ColorWriteFlags: u32 {
const RED = 1;
const GREEN = 2;
const BLUE = 4;
const ALPHA = 8;
const COLOR = 7;
const ALL = 15;
}
}
#[repr(C)]
pub struct BlendDescriptor {
@ -77,7 +74,7 @@ impl BlendStateDescriptor {
blend_enabled: false,
alpha: BlendDescriptor::REPLACE,
color: BlendDescriptor::REPLACE,
write_mask: ColorWriteFlags_ALL,
write_mask: ColorWriteFlags::ALL,
};
}
@ -194,18 +191,6 @@ pub struct ShaderModuleDescriptor {
pub code: ByteArray,
}
#[repr(C)]
pub struct AttachmentStateDescriptor {
pub formats: *const resource::TextureFormat,
pub formats_length: usize,
}
pub(crate) struct AttachmentState<B: hal::Backend> {
pub base_pass: B::RenderPass,
pub color_formats: Vec<hal::format::Format>,
pub depth_stencil_format: Option<hal::format::Format>,
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum ShaderStage {
@ -241,16 +226,29 @@ pub enum PrimitiveTopology {
TriangleStrip = 4,
}
#[repr(C)]
pub struct Attachment {
pub format: resource::TextureFormat,
pub samples: u32,
}
#[repr(C)]
pub struct AttachmentsState {
pub color_attachments: *const Attachment,
pub color_attachments_length: usize,
pub depth_stencil_attachment: *const Attachment,
}
#[repr(C)]
pub struct RenderPipelineDescriptor {
pub layout: PipelineLayoutId,
pub stages: *const PipelineStageDescriptor,
pub stages_length: usize,
pub primitive_topology: PrimitiveTopology,
pub attachments_state: AttachmentsState,
pub blend_states: *const BlendStateId,
pub blend_states_length: usize,
pub depth_stencil_state: DepthStencilStateId,
pub attachment_state: AttachmentStateId,
}
pub(crate) struct RenderPipeline<B: hal::Backend> {

View File

@ -9,7 +9,7 @@ pub use self::local::{Id, ItemsGuard, Registry as ConcreteRegistry};
pub use self::remote::{Id, ItemsGuard, Registry as ConcreteRegistry};
use {
AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BindGroupHandle,
AdapterHandle, BindGroupLayoutHandle, BindGroupHandle,
BlendStateHandle, CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle,
RenderPassHandle, ComputePassHandle,
PipelineLayoutHandle, RenderPipelineHandle, ComputePipelineHandle, ShaderModuleHandle,
@ -39,7 +39,6 @@ pub struct Hub {
pub(crate) pipeline_layouts: ConcreteRegistry<PipelineLayoutHandle>,
pub(crate) bind_group_layouts: ConcreteRegistry<BindGroupLayoutHandle>,
pub(crate) bind_groups: ConcreteRegistry<BindGroupHandle>,
pub(crate) attachment_states: ConcreteRegistry<AttachmentStateHandle>,
pub(crate) blend_states: ConcreteRegistry<BlendStateHandle>,
pub(crate) depth_stencil_states: ConcreteRegistry<DepthStencilStateHandle>,
pub(crate) shader_modules: ConcreteRegistry<ShaderModuleHandle>,

View File

@ -4,16 +4,16 @@ extern crate arrayvec;
use arrayvec::ArrayVec;
use std::ffi::CString;
use std::ptr;
pub use wgn::{
AdapterDescriptor, Color, CommandBufferDescriptor, DeviceDescriptor, Extensions, Extent3d,
Origin3d, PowerPreference, ShaderModuleDescriptor, ShaderStage,
Origin3d, PowerPreference, ShaderModuleDescriptor, ShaderStage, ShaderStageFlags,
BindGroupLayoutBinding, BindingType, TextureDimension, TextureDescriptor, TextureFormat,
TextureUsageFlags, TextureViewDescriptor,
PrimitiveTopology, BlendStateDescriptor, ColorWriteFlags, DepthStencilStateDescriptor,
RenderPassDescriptor, RenderPassColorAttachmentDescriptor, RenderPassDepthStencilAttachmentDescriptor,
LoadOp, StoreOp,
ShaderStageFlags_NONE, ShaderStageFlags_VERTEX, ShaderStageFlags_FRAGMENT, ShaderStageFlags_COMPUTE
Attachment, LoadOp, StoreOp,
};
@ -61,10 +61,6 @@ pub struct DepthStencilState {
id: wgn::DepthStencilStateId,
}
pub struct AttachmentState {
id: wgn::AttachmentStateId,
}
pub struct RenderPipeline {
id: wgn::RenderPipelineId,
}
@ -105,17 +101,18 @@ pub struct PipelineStageDescriptor<'a> {
pub entry_point: &'a str,
}
pub struct AttachmentStateDescriptor<'a> {
pub formats: &'a [TextureFormat],
pub struct AttachmentsState<'a> {
pub color_attachments: &'a [Attachment],
pub depth_stencil_attachment: Option<Attachment>,
}
pub struct RenderPipelineDescriptor<'a> {
pub layout: &'a PipelineLayout,
pub stages: &'a [PipelineStageDescriptor<'a>],
pub primitive_topology: PrimitiveTopology,
pub attachments_state: AttachmentsState<'a>,
pub blend_states: &'a [&'a BlendState],
pub depth_stencil_state: &'a DepthStencilState,
pub attachment_state: &'a AttachmentState,
}
@ -201,15 +198,6 @@ impl Device {
}
}
pub fn create_attachment_state(&self, desc: &AttachmentStateDescriptor) -> AttachmentState {
AttachmentState {
id: wgn::wgpu_device_create_attachment_state(self.id, &wgn::AttachmentStateDescriptor {
formats: desc.formats.as_ptr(),
formats_length: desc.formats.len(),
}),
}
}
pub fn create_render_pipeline(&self, desc: &RenderPipelineDescriptor) -> RenderPipeline {
let entry_points = desc.stages
.iter()
@ -236,10 +224,14 @@ impl Device {
stages: stages.as_ptr(),
stages_length: stages.len(),
primitive_topology: desc.primitive_topology,
attachments_state: wgn::AttachmentsState {
color_attachments: desc.attachments_state.color_attachments.as_ptr(),
color_attachments_length: desc.attachments_state.color_attachments.len(),
depth_stencil_attachment: desc.attachments_state.depth_stencil_attachment.as_ref().map(|at| at as *const _).unwrap_or(ptr::null()),
},
blend_states: temp_blend_states.as_ptr(),
blend_states_length: temp_blend_states.len(),
depth_stencil_state: desc.depth_stencil_state.id,
attachment_state: desc.attachment_state.id,
}),
}
}