1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Aldaron's System Interface / Vulkan
// Copyright (c) 2017-2018 Jeron Aldaron Lau <jeron.lau@plopgrizzly.com>
// Licensed under the MIT LICENSE
//
// src/memory/mod.rs

// TODO: pub?
pub mod buffer;

use std::{ mem, ptr };
use ami::Void;

use super::VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
use super::VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
use super::Connection;
use super::types::*;

pub struct Memory<T> where T: Clone {
	pub data: T,
	pub memory: VkDeviceMemory,
	pub buffer: buffer::Buffer,
	#[allow(unused)] // TODO
	device: VkDevice,
	#[allow(unused)] // TODO
	dropfn: unsafe extern "system" fn(VkDevice, VkDeviceMemory, *const Void)
		-> ()
}

impl<T> Memory<T> where T: Clone {
	/// Allocate memory in a GPU buffer.
	#[inline(always)]
	pub fn new(c: &Connection, device: VkDevice, gpu: VkPhysicalDevice,
		data: T) -> Memory<T>
	{
		let buffer = buffer::Buffer::new(c, device, mem::size_of::<T>(),
			buffer::BufferBuilderType::Uniform);
		let mut memory = unsafe { mem::uninitialized() };
		let mem_reqs = buffer.get_reqs(c, device);
		unsafe {
			(c.mem_allocate)(
				device,
				&VkMemoryAllocateInfo {
					s_type: VkStructureType::MemoryAllocateInfo,
					next: ptr::null(),
					allocation_size: mem_reqs.size,
					memory_type_index: super::get_memory_type(
						c,
						gpu,
						mem_reqs.memory_type_bits,
						VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
						VK_MEMORY_PROPERTY_HOST_COHERENT_BIT),
				},
				ptr::null(),
				&mut memory
			).unwrap();
		}
		let dropfn = unsafe {
			super::vkd_sym(device, c.vkdsym, b"vkFreeMemory\0")
		};

		unsafe {
			(c.bind_buffer_mem)(device, buffer.buffer, memory, 0)
				.unwrap();
		}

		let memory = Memory { data, memory, buffer, device, dropfn };

		memory.update(c);

		memory
	}

	/// Update the contents of the memory.
	#[inline(always)]
	pub fn update(&self, c: &Connection) {
		let mut mapped: *mut T = unsafe { mem::uninitialized() }; // void *

		unsafe {
			(c.mapmem)(self.device, self.memory, 0, !0, 0,
				&mut mapped as *mut *mut _ as *mut *mut Void).unwrap();
		}

		if mapped.is_null() {
			panic!("Couldn't Map Buffer Memory?  Unknown cause.");
		}

		let write = self.data.clone();

		unsafe {
			*mapped = write;
		}

//		let mut map = mapped as *mut _ as *mut T;

//		unsafe {
//			*map = self.data.clone();
//		}

/*		extern "C" {
			fn memcpy(dest: *mut Void, src: *const Void, n: usize)
				-> ::ami::MemAddr<Void>;
		}*/

		unsafe {
	//		memcpy(mapped, cast!(&self.data), size);
	//		ptr::copy_nonoverlapping(data.as_ptr(), mapped, data.len());

			(c.unmap)(self.device, self.memory);

//			asi_vulkan::unmap_memory(c, self.device, self.memory);
		}
	}

	#[inline(always)]
	/// Update the contents of image memory.
	pub fn update_pitched() {
		
	}
}

impl<T> Drop for Memory<T> where T: Clone {
	#[inline(always)]
	fn drop(&mut self) {
		// TODO: Drop at correct time as for no segfault.
/*		unsafe {
			(self.dropfn)(self.device, self.memory, ptr::null());
		}
		self.buffer.drop(self.device);*/
	}
}