Skip to content

Commit

Permalink
Merge #178
Browse files Browse the repository at this point in the history
178: gfx update, copyless handles, signposts r=msiglreith a=kvark



Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
  • Loading branch information
bors[bot] and kvark committed Mar 19, 2019
2 parents c5125e1 + 5d8488b commit 5779641
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 115 deletions.
191 changes: 106 additions & 85 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ FULL_LIBRARY_PATH=$(CURDIR)/target/debug
LIBRARY=target/debug/libportability.$(LIB_EXTENSION)
LIBRARY_FAST=target/release/libportability.$(LIB_EXTENSION)

.PHONY: all rebuild debug release version-debug version-release binding run-native cts clean cherry dota-debug dota-release dota-orig dota-bench-gfx dota-bench-orig dota-bench-gl package
.PHONY: all rebuild debug release version-debug version-release binding run-native cts clean cherry dota-debug dota-release dota-orig dota-bench-gfx dota-bench-orig dota-bench-gl package memcpy-report

all: $(NATIVE_TARGET)

Expand Down Expand Up @@ -172,3 +172,7 @@ target/debug/libvulkan.$(LIB_EXTENSION):

cherry: $(LIBRARY) target/debug/libvulkan.$(LIB_EXTENSION)
cd $(CHERRY_DIR) && rm -f Cherry.db && RUST_LOG=warn LD_LIBRARY_PATH=$(FULL_LIBRARY_PATH) go run server.go

memcpy-report:
RUSTFLAGS='-g --emit=llvm-ir' cd libportability && cargo build --release --features $(BACKEND)
../memcpy-find/memcpy-find target/release/deps/portability.ll | rustfilt >etc/portability-memcpy.txt
1 change: 1 addition & 0 deletions libportability-gfx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ nightly = []
metal-capture = ["gfx-backend-metal/auto-capture"]

[dependencies]
copyless = "0.1.1"
lazy_static = "1.0"
log = { version = "0.4", features = ["release_max_level_error"] }

Expand Down
26 changes: 13 additions & 13 deletions libportability-gfx/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,25 @@ use super::*;

pub fn limits_from_hal(limits: Limits) -> VkPhysicalDeviceLimits {
VkPhysicalDeviceLimits {
maxImageDimension1D: limits.max_texture_size as _,
maxImageDimension2D: limits.max_texture_size as _,
maxImageDimension3D: limits.max_texture_size as _,
maxImageDimensionCube: limits.max_texture_size as _,
maxFramebufferWidth: limits.max_texture_size as _, //TODO
maxFramebufferHeight: limits.max_texture_size as _, //TODO
maxImageDimension1D: limits.max_image_1d_size,
maxImageDimension2D: limits.max_image_2d_size,
maxImageDimension3D: limits.max_image_3d_size,
maxImageDimensionCube: limits.max_image_cube_size,
maxFramebufferWidth: limits.max_framebuffer_extent.width,
maxFramebufferHeight: limits.max_framebuffer_extent.height,
maxTexelBufferElements: limits.max_texel_elements as _,
maxTessellationPatchSize: limits.max_patch_size as _,
maxPushConstantsSize: 0x80, //TODO
maxPushConstantsSize: limits.max_push_constants_size as _,
maxViewports: limits.max_viewports as _,
maxViewportDimensions: [limits.max_texture_size as u32; 2],
maxViewportDimensions: limits.max_viewport_dimensions,
maxVertexInputAttributes: limits.max_vertex_input_attributes as _,
maxVertexInputBindings: limits.max_vertex_input_bindings as _,
maxVertexInputAttributeOffset: limits.max_vertex_input_attribute_offset as _,
maxVertexInputBindingStride: limits.max_vertex_input_binding_stride as _,
maxVertexOutputComponents: limits.max_vertex_output_components as _,
maxComputeWorkGroupCount: limits.max_compute_group_count,
maxComputeWorkGroupSize: limits.max_compute_group_size,
bufferImageGranularity: 1, //TODO
optimalBufferCopyOffsetAlignment: limits.min_buffer_copy_offset_alignment,
optimalBufferCopyRowPitchAlignment: limits.min_buffer_copy_pitch_alignment,
maxComputeWorkGroupCount: limits.max_compute_work_group_count,
maxComputeWorkGroupSize: limits.max_compute_work_group_size,
bufferImageGranularity: limits.buffer_image_granularity,
minTexelBufferOffsetAlignment: limits.min_texel_buffer_offset_alignment,
minUniformBufferOffsetAlignment: limits.min_uniform_buffer_offset_alignment,
minStorageBufferOffsetAlignment: limits.min_storage_buffer_offset_alignment,
Expand All @@ -38,6 +36,8 @@ pub fn limits_from_hal(limits: Limits) -> VkPhysicalDeviceLimits {
maxColorAttachments: limits.max_color_attachments as _,
nonCoherentAtomSize: limits.non_coherent_atom_size as _,
maxSamplerAnisotropy: limits.max_sampler_anisotropy,
optimalBufferCopyOffsetAlignment: limits.optimal_buffer_copy_offset_alignment,
optimalBufferCopyRowPitchAlignment: limits.optimal_buffer_copy_pitch_alignment,
.. unsafe { mem::zeroed() } //TODO
}
}
Expand Down
41 changes: 36 additions & 5 deletions libportability-gfx/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::sync::{Arc, Mutex};
#[cfg(feature = "nightly")]
use hal::backend::FastHashMap;

use copyless::{BoxAllocation, BoxHelper};


#[cfg(feature = "nightly")]
lazy_static! {
static ref REGISTRY: Arc<Mutex<FastHashMap<usize, &'static str>>> = Arc::new(Mutex::new(FastHashMap::default()));
Expand All @@ -25,9 +28,12 @@ impl Handle<()> {
}
}

impl<T: 'static> Handle<T> {
pub fn new(value: T) -> Self {
let ptr = Box::into_raw(Box::new(value));
pub struct HandleAllocation<T>(BoxAllocation<T>);

impl<T> HandleAllocation<T> {
#[inline(always)]
pub fn init(self, value: T) -> Handle<T> {
let ptr = Box::into_raw(self.0.init(value));
#[cfg(feature = "nightly")]
{
use std::intrinsics::type_name;
Expand All @@ -36,6 +42,17 @@ impl<T: 'static> Handle<T> {
}
Handle(ptr)
}
}

impl<T: 'static> Handle<T> {
pub fn alloc() -> HandleAllocation<T> {
HandleAllocation(Box::alloc())
}

// Note: ideally this constructor isn't used
pub fn new(value: T) -> Self {
Self::alloc().init(value)
}

pub fn null() -> Self {
Handle(VK_NULL_HANDLE as *mut _)
Expand Down Expand Up @@ -125,17 +142,31 @@ pub type DispatchHandle<T> = Handle<T>;
#[cfg(feature = "dispatch")]
mod dispatch {
use VK_NULL_HANDLE;
use copyless::{BoxAllocation, BoxHelper};
use std::{borrow, cmp, fmt, ops};

const ICD_LOADER_MAGIC: u64 = 0x01CDC0DE;

#[repr(C)]
pub struct DispatchHandle<T>(*mut (u64, T));

pub struct DisplatchHandleAllocation<T>(BoxAllocation<(u64, T)>);

impl<T> DisplatchHandleAllocation<T> {
#[inline(always)]
pub fn init(self, value: T) -> DispatchHandle<T> {
let ptr = Box::into_raw(self.0.init((ICD_LOADER_MAGIC, value)));
DispatchHandle(ptr)
}
}

impl<T> DispatchHandle<T> {
pub fn alloc() -> DisplatchHandleAllocation<T> {
DisplatchHandleAllocation(Box::alloc())
}

pub fn new(value: T) -> Self {
let ptr = Box::into_raw(Box::new((ICD_LOADER_MAGIC, value)));
DispatchHandle(ptr)
Self::alloc().init(value)
}

pub fn null() -> Self {
Expand Down
30 changes: 19 additions & 11 deletions libportability-gfx/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use hal::device::WaitFor;
use hal::pool::RawCommandPool;
use hal::queue::RawCommandQueue;

#[cfg(feature = "gfx-backend-metal")]
use std::env;
use std::ffi::{CStr, CString};
use std::os::raw::c_int;
#[cfg(feature = "renderdoc")]
Expand Down Expand Up @@ -759,7 +761,6 @@ pub extern "C" fn gfxCreateDevice(
#[cfg(feature = "gfx-backend-metal")]
{
use back::OnlineRecording;
use std::env;

if let Ok(value) = env::var("GFX_METAL_RECORDING") {
gpu.device.online_recording = match value.to_lowercase().as_str() {
Expand Down Expand Up @@ -1033,8 +1034,6 @@ pub extern "C" fn gfxGetDeviceQueue(

#[cfg(feature = "gfx-backend-metal")]
{
use std::env;

if let Ok(value) = env::var("GFX_METAL_STITCHING") {
let mut q = queue;
q.stitch_deferred = match value.to_lowercase().as_str() {
Expand Down Expand Up @@ -3166,11 +3165,11 @@ pub extern "C" fn gfxAllocateCommandBuffers(
level => panic!("Unexpected command buffer lvel: {:?}", level),
};

let count = info.commandBufferCount as usize;
let cmd_bufs = info.commandPool.pool.allocate_vec(count, level);

let output = unsafe { slice::from_raw_parts_mut(pCommandBuffers, count) };
for (out, cmd_buf) in output.iter_mut().zip(cmd_bufs) {
let output = unsafe {
slice::from_raw_parts_mut(pCommandBuffers, info.commandBufferCount as usize)
};
for out in output.iter_mut() {
let cmd_buf = info.commandPool.pool.allocate_one(level);
*out = DispatchHandle::new(cmd_buf);
}
info.commandPool.buffers.extend_from_slice(output);
Expand Down Expand Up @@ -4283,7 +4282,6 @@ pub extern "C" fn gfxCreateSwapchainKHR(
#[cfg(feature = "gfx-backend-metal")]
{
use back::AcquireMode;
use std::env;

if let Ok(value) = env::var("GFX_METAL_ACQUIRING") {
swapchain.acquire_mode = match value.to_lowercase().as_str() {
Expand Down Expand Up @@ -4644,9 +4642,13 @@ pub extern "C" fn gfxCreateMetalSurfaceEXT(
let info = unsafe { &*pCreateInfo };
#[cfg(feature = "gfx-backend-metal")]
unsafe {
let enable_signposts = env::var("GFX_METAL_SIGNPOSTS").is_ok();
if enable_signposts {
println!("GFX: enabled signposts");
}
assert_eq!(info.flags, 0);
*pSurface = Handle::new(
instance.backend.create_surface_from_layer(info.pLayer as *mut _),
instance.backend.create_surface_from_layer(info.pLayer as *mut _, enable_signposts),
);
VkResult::VK_SUCCESS
}
Expand All @@ -4668,9 +4670,15 @@ pub extern "C" fn gfxCreateMacOSSurfaceMVK(
let info = unsafe { &*pCreateInfo };
#[cfg(target_os="macos")]
unsafe {
use std::env;

let enable_signposts = env::var("GFX_METAL_SIGNPOSTS").is_ok();
if enable_signposts {
println!("GFX: enabled signposts");
}
assert_eq!(info.flags, 0);
*pSurface = Handle::new(
instance.backend.create_surface_from_nsview(info.pView),
instance.backend.create_surface_from_nsview(info.pView, enable_signposts),
);
VkResult::VK_SUCCESS
}
Expand Down
1 change: 1 addition & 0 deletions libportability-gfx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern crate gfx_backend_metal as back;
#[cfg(feature = "gfx-backend-vulkan")]
extern crate gfx_backend_vulkan as back;

extern crate copyless;
#[macro_use]
extern crate lazy_static;
#[macro_use]
Expand Down

0 comments on commit 5779641

Please sign in to comment.