0xC0000005: Access violation when reading at position 0xDDDDDDDD. C++ [duplicate] - c++

This question already has answers here:
When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?
(9 answers)
What are the debug memory fill patterns in Visual Studio C++ and Windows?
(3 answers)
Closed 1 year ago.
Header
#include <iostream>
#define GLFW_INCLUDE_VULKAN
#include "GLFW\glfw3.h"
#include "PhysicalDeviceVK.h"
#define CATCH(value) if(value != VK_SUCCESS){ __debugbreak;}
class DeviceVK
{
private:
/**
* #return VkPhysicalDevice Get the best physical device.
*/
VkPhysicalDevice getPhysicalDevice(VkInstance instance);
/**
* #return uint32_t Get the index of the best queue family.
*/
uint32_t getQueueFamily(std::vector<VkQueueFamilyProperties> queueFamilies);
/**
* #return VkDeviceCreateInfo create the device create info.
*/
VkDeviceCreateInfo createDeviceCreateInfo();
public:
VkDevice device = nullptr;
VkQueue queue = nullptr;
PhysicalDeviceVK physicalDevice;
uint32_t queueIndex = NULL;
DeviceVK(VkInstance instance, VkSurfaceKHR surface);
DeviceVK() {}
};
C++ File
#include "DeviceVK.h"
DeviceVK::DeviceVK(VkInstance instance, VkSurfaceKHR surface)
{
VkPhysicalDevice bestGPU = getPhysicalDevice(instance);
this->physicalDevice = PhysicalDeviceVK(bestGPU);
VkDeviceCreateInfo deviceCreateInfo = createDeviceCreateInfo();
CATCH(vkCreateDevice(bestGPU, &deviceCreateInfo, nullptr, &this->device));
vkGetDeviceQueue(this->device, this->queueIndex, 1, &this->queue);
this->physicalDevice.debug(surface);
}
VkPhysicalDevice DeviceVK::getPhysicalDevice(VkInstance instance)
{
uint32_t amountOfGraphicCards = 0;
vkEnumeratePhysicalDevices(instance, &amountOfGraphicCards, nullptr);
VkPhysicalDevice* physicalDevices = new VkPhysicalDevice[amountOfGraphicCards];
CATCH(vkEnumeratePhysicalDevices(instance, &amountOfGraphicCards, physicalDevices));
int bestGPU = 0, maxHeap = 0;
for (int i = 0; i < amountOfGraphicCards; i++)
{
PhysicalDeviceVK physicalDevice(physicalDevices[i]);
VkPhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getPhysicalDeviceMemoryProperties();
auto heapsPointer = memoryProperties.memoryHeaps;
auto heaps = std::vector<VkMemoryHeap>(heapsPointer, heapsPointer + memoryProperties.memoryHeapCount);
for (const auto& heap : heaps)
{
if (heap.flags & VkMemoryHeapFlagBits::VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
{
if (maxHeap < heap.size)
{
maxHeap = heap.size;
bestGPU = i;
}
}
}
}
VkPhysicalDevice bestDevice = physicalDevices[bestGPU];
return bestDevice;
}
uint32_t DeviceVK::getQueueFamily(std::vector<VkQueueFamilyProperties> queueFamilies)
{
return ((uint32_t)0);
}
VkDeviceCreateInfo DeviceVK::createDeviceCreateInfo()
{
std::vector<VkQueueFamilyProperties> queueFamilies = this->physicalDevice.getQueueFamilyProperties();
this->queueIndex = getQueueFamily(queueFamilies);
VkDeviceQueueCreateInfo deviceQueueCreateInfo;
deviceQueueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
deviceQueueCreateInfo.pNext = nullptr;
deviceQueueCreateInfo.flags = 0;
deviceQueueCreateInfo.queueFamilyIndex = this->queueIndex;
deviceQueueCreateInfo.queueCount = queueFamilies.size();
const float queuePriorityValue = 1.0f;
deviceQueueCreateInfo.pQueuePriorities = &queuePriorityValue;
queueFamilies.clear();
VkPhysicalDeviceFeatures deviceFeatures = {};
const std::vector<const char*> deviceExtensions =
{
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
VkDeviceCreateInfo deviceCreateInfo;
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceCreateInfo.pNext = nullptr;
deviceCreateInfo.flags = 0;
deviceCreateInfo.queueCreateInfoCount = 1;
deviceCreateInfo.pQueueCreateInfos = &deviceQueueCreateInfo;
deviceCreateInfo.enabledLayerCount = 0;
deviceCreateInfo.ppEnabledLayerNames = nullptr;
deviceCreateInfo.enabledExtensionCount = deviceExtensions.size();
deviceCreateInfo.ppEnabledExtensionNames = deviceExtensions.data();
deviceCreateInfo.pEnabledFeatures = &deviceFeatures;
return deviceCreateInfo;
}
Here I call the class
void VulkanGraphic::createDevice()
{
this->deviceVK = DeviceVK(this->instance, this->surface);
}
Every time in run this program, it crashes because of a
0xC0000005: Access violation when reading at position 0xDDDDDDDD.
I also tried deleting the last two lines, where the error appears. But it actually says, that the program crashes at the End of the constructor, where a empty line is.
I'm using Vulkan and GLFW
The error happens
Also when I'm removing the lines...

Related

Fail to inport malloc memory as VkDeviceMemory

I'm trying to import a host memory as a staging buffer in Vulkan.
In the future, the plan is to use a shared memory as staging vertex buffer (in the host). But I can't manage to successfully import a previously allocated memory into vulkan.
The return of vkAllocateMemory is VK_ERROR_OUT_OF_DEVICE_MEMORY.
Here is my code:
void createMallocedBuffer(VkPhysicalDevice const& physicalDevice,
VkDevice const& device,
void * data,
VkBuffer & buffer,
VkDeviceMemory& bufferMemory )
{
VkPhysicalDeviceExternalMemoryHostPropertiesEXT externalMemHostProp;
externalMemHostProp.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT;
externalMemHostProp.pNext = nullptr;
VkPhysicalDeviceProperties physicalDeviceProps;
VkPhysicalDeviceProperties2 physicalDeviceProps2;
physicalDeviceProps2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
physicalDeviceProps2.pNext = &externalMemHostProp;
physicalDeviceProps2.properties = physicalDeviceProps;
vkGetPhysicalDeviceProperties2(physicalDevice, &physicalDeviceProps2);
data = malloc(externalMemHostProp.minImportedHostPointerAlignment);
VkExternalMemoryHandleTypeFlagBits externalMemoryFlagBits {
VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
};
VkMemoryHostPointerPropertiesEXT pointersProps {
.sType = VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT,
.pNext = nullptr,
};
vkGetMemoryHostPointerPropertiesEXT(device,
externalMemoryFlagBits,
data,
&pointersProps);
VkExternalMemoryBufferCreateInfo externalMemCreateInfo {
.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
.pNext = nullptr,
.handleTypes = externalMemoryFlagBits,
};
VkBufferUsageFlags bufferUsage {
VK_BUFFER_USAGE_TRANSFER_SRC_BIT
};
VkBufferCreateInfo bufferInfo {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = &externalMemCreateInfo,
.flags = 0,
.size = externalMemHostProp.minImportedHostPointerAlignment,
.usage = bufferUsage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS)
{
throw std::runtime_error("failed to create buffer!");
}
VkMemoryHostPointerPropertiesEXT memoryHostPointerProperties {
.sType = VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT,
};
if (auto result = vkGetMemoryHostPointerPropertiesEXT(device, externalMemoryFlagBits, data, &memoryHostPointerProperties);
result != VK_SUCCESS)
{
// printError(result);
}
VkMemoryPropertyFlags memProperties {
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
};
VkImportMemoryHostPointerInfoEXT importHostPointerInfo {
.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT,
.pNext = nullptr,
.handleType = externalMemoryFlagBits,
.pHostPointer = data,
};
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
auto memTypeIndex = findMemoryType(physicalDevice, memRequirements.memoryTypeBits, memProperties);
VkMemoryAllocateInfo allocInfo {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.pNext = &importHostPointerInfo,
.allocationSize = externalMemHostProp.minImportedHostPointerAlignment,
.memoryTypeIndex = memTypeIndex,
};
if (auto result = vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory);
result != VK_SUCCESS)
{
// printError(result);
throw std::runtime_error("failed to allocate buffer memory!");
}
vkBindBufferMemory(device, buffer, bufferMemory, 0);
}
uint32_t findMemoryType(VkPhysicalDevice const& physicalDevice,
uint32_t const typeFilter,
VkMemoryPropertyFlags const& properties )
{
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
for (uint32_t i = 0; i < memProperties.memoryTypeCount; ++i)
{
auto const typeSelect = (typeFilter & (1 << i));
auto const flagSelect = (memProperties.memoryTypes[i].propertyFlags & properties);
if (typeSelect && (flagSelect == properties))
{
return i;
}
}
throw std::runtime_error("failed to find suitable memory type!");
}

Can't create vulkan device only in debug

I'm making a vulkan renderer and when I try to create a logical device it throws the following exception on vkCreateDevice only in debug mode:
Exception thrown at 0x00007FFBA327A34B (vulkan-1.dll) in Game.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Here's the code (By the way this was made mostly following the vulkan-tutorial.com tutorial):
bool FindQueueFamilies(VkPhysicalDevice device, uint32_t* indices)
{
bool found = false;
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
VkQueueFamilyProperties queueFamilies[512];
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies);
for (uint32_t i = 0; i < queueFamilyCount; i++)
{
const auto& queueFamily = queueFamilies[i];
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
if (indices != nullptr)
{
*indices = i;
}
found = true;
}
}
if (!found)
{
Loggers::Error("Didn't find queue family!");
}
return found;
}
...
void* RenderDevice::CreateVulkan(void* physicalDevice)
{
//Logical device
uint32_t indices = 0;
FindQueueFamilies(*((VkPhysicalDevice*)physicalDevice), &indices);
float queuePriority = 1.0f;
VkDeviceQueueCreateInfo queueCreateInfo{};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.pNext = nullptr;
queueCreateInfo.flags = 0;
queueCreateInfo.queueFamilyIndex = indices;
queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = &queuePriority;
VkPhysicalDeviceFeatures deviceFeatures{};
VkDeviceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.pNext = nullptr;
createInfo.flags = 0;
createInfo.queueCreateInfoCount = 1;
createInfo.pQueueCreateInfos = &queueCreateInfo;
createInfo.enabledExtensionCount = deviceExtensionsCount;
createInfo.ppEnabledExtensionNames = deviceExtensions;
createInfo.pEnabledFeatures = &deviceFeatures;
VkDevice device;
if (vkCreateDevice(*((VkPhysicalDevice*)physicalDevice), &createInfo, nullptr, &device) != VK_SUCCESS)
{
Loggers::Error("failed to create logical device!");
}
VkQueue graphicsQueue;
vkGetDeviceQueue(device, indices, 0, &graphicsQueue);
return &device;
}
To me it looks like you meant physicalDevice instead of *((VkPhysicalDevice*)physicalDevice), since that's what vulkan-turorial.com uses and that's also what I use in my rendered. Was that your problem? Also keep in mind that access violations can be caused by bad pointers.

libe57 read file 65536 points

I compiled the libe57 in order to use it in my project. I did it and now I'm trying to use it. (I'm on windows 10 x64)
Basing on this tutorial, I did this
int
main(int argc, char** argv)
{
char sFile[] = "PTX/file.e57";
_bstr_t bsFile = sFile; //converts Unicode to UTF-8
e57::Reader eReader((char*)bsFile);
e57::E57Root rootHeader;
eReader.GetE57Root(rootHeader);
const char* fileGuid = rootHeader.guid.c_str();
e57::DateTime fileGPSTime = rootHeader.creationDateTime;
int data3DCount = eReader.GetData3DCount();
int scanIndex = 0;
e57::Data3D scanHeader;
eReader.ReadData3D(scanIndex, scanHeader);
_bstr_t bstrName = scanHeader.name.c_str();
_bstr_t bstrGuid = scanHeader.guid.c_str();
_bstr_t bstrDesc = scanHeader.description.c_str();
int64_t nColumn = 0;
int64_t nRow = 0;
int64_t nPointsSize = 0; //Number of points
int64_t nGroupsSize = 0; //Number of groups
int64_t nCountSize = 0; //Number of points per group
bool bColumnIndex = false; //indicates that idElementName is "columnIndex"
eReader.GetData3DSizes(scanIndex, nRow, nColumn, nPointsSize, nGroupsSize, nCountSize, bColumnIndex);
int64_t nSize = nRow;
if (nSize == 0) nSize = 1024; // choose a chunk size
int8_t * isInvalidData = NULL;
if (scanHeader.pointFields.cartesianInvalidStateField)
isInvalidData = new int8_t[nSize];
double * xData = NULL;
if (scanHeader.pointFields.cartesianXField)
xData = new double[nSize];
double * yData = NULL;
if (scanHeader.pointFields.cartesianYField)
yData = new double[nSize];
double * zData = NULL;
if (scanHeader.pointFields.cartesianZField)
zData = new double[nSize];
double * intData = NULL;
bool bIntensity = false;
double intRange = 0;
double intOffset = 0;
if (scanHeader.pointFields.intensityField)
{
bIntensity = true;
intData = new double[nSize];
intRange = scanHeader.intensityLimits.intensityMaximum - scanHeader.intensityLimits.intensityMinimum;
intOffset = scanHeader.intensityLimits.intensityMinimum;
}
uint16_t * redData = NULL;
uint16_t * greenData = NULL;
uint16_t * blueData = NULL;
bool bColor = false;
int32_t colorRedRange = 1;
int32_t colorRedOffset = 0;
int32_t colorGreenRange = 1;
int32_t colorGreenOffset = 0;
int32_t colorBlueRange = 1;
int32_t colorBlueOffset = 0;
if (scanHeader.pointFields.colorRedField)
{
bColor = true;
redData = new uint16_t[nSize];
greenData = new uint16_t[nSize];
blueData = new uint16_t[nSize];
colorRedRange = scanHeader.colorLimits.colorRedMaximum - scanHeader.colorLimits.colorRedMinimum;
colorRedOffset = scanHeader.colorLimits.colorRedMinimum;
colorGreenRange = scanHeader.colorLimits.colorGreenMaximum - scanHeader.colorLimits.colorGreenMinimum;
colorGreenOffset = scanHeader.colorLimits.colorGreenMinimum;
colorBlueRange = scanHeader.colorLimits.colorBlueMaximum - scanHeader.colorLimits.colorBlueMinimum;
colorBlueOffset = scanHeader.colorLimits.colorBlueMinimum;
}
int64_t * idElementValue = NULL;
int64_t * startPointIndex = NULL;
int64_t * pointCount = NULL;
if (nGroupsSize > 0)
{
idElementValue = new int64_t[nGroupsSize];
startPointIndex = new int64_t[nGroupsSize];
pointCount = new int64_t[nGroupsSize];
if (!eReader.ReadData3DGroupsData(scanIndex, nGroupsSize, idElementValue,
startPointIndex, pointCount))
nGroupsSize = 0;
}
int32_t * rowIndex = NULL;
int32_t * columnIndex = NULL;
if (scanHeader.pointFields.rowIndexField)
rowIndex = new int32_t[nSize];
if (scanHeader.pointFields.columnIndexField)
columnIndex = new int32_t[nRow];
e57::CompressedVectorReader dataReader = eReader.SetUpData3DPointsData(
scanIndex, //!< data block index given by the NewData3D
nRow, //!< size of each of the buffers given
xData, //!< pointer to a buffer with the x data
yData, //!< pointer to a buffer with the y data
zData, //!< pointer to a buffer with the z data
isInvalidData, //!< pointer to a buffer with the valid indication
intData, //!< pointer to a buffer with the lidar return intesity
NULL,
redData, //!< pointer to a buffer with the color red data
greenData, //!< pointer to a buffer with the color green data
blueData, //!< pointer to a buffer with the color blue data
NULL,
NULL,
NULL,
NULL,
NULL,
rowIndex, //!< pointer to a buffer with the rowIndex
columnIndex //!< pointer to a buffer with the columnIndex
);
int64_t count = 0;
unsigned size = 0;
int col = 0;
int row = 0;
int cpt = 0;
while (size = dataReader.read())
{
cpt++;
}
std::cout << cpt << std::endl;
dataReader.close();
if (isInvalidData) delete isInvalidData;
if (xData) delete xData;
if (yData) delete yData;
if (zData) delete zData;
if (intData) delete intData;
if (redData) delete redData;
if (greenData) delete greenData;
if (blueData) delete blueData;
}
The problem is that everytime, the reader says that there are 65536 rows and 1 cols, meaning that my pointcloud contain always 65536 points. I tried with several files that have more than 200k points each, but the result are always the same, it writes in my XYZ file 65536 points, and no more.
EDIT 2: shorter example : the problem is still the same
#include "E57Foundation.h"
#include "E57Simple.h"
#include "comutil.h"
#include <iostream>
#include <vector>
int
main(int argc, char** argv)
{
e57::Reader eReader("PTX/file.e57");
int scanIndex = 0; //picking the first scan
e57::Data3D scanHeader; //read scan's header information
eReader.ReadData3D(scanIndex, scanHeader);
_bstr_t scanGuid = scanHeader.guid.c_str(); //get guid
int64_t nColumn = 0; //Number of Columns in a structure scan (from "indexBounds" if structure data)
int64_t nRow = 0; //Number of Rows in a structure scan
int64_t nPointsSize = 0; //Number of points
int64_t nGroupsSize = 0; //Number of groups (from "groupingByLine" if present)
int64_t nCountsSize = 0; //Number of points per group
bool bColumnIndex = false;
eReader.GetData3DSizes(scanIndex, nRow, nColumn, nPointsSize, nGroupsSize, nCountsSize, bColumnIndex);
int64_t nSize = (nRow > 0) ? nRow : 1024; //Pick a size for buffers
double *xData = new double[nSize];
double *yData = new double[nSize];
double *zData = new double[nSize];
e57::CompressedVectorReader dataReader = eReader.SetUpData3DPointsData(
scanIndex, //!< scan data index
nSize, //!< size of each of the buffers given
xData, //!< pointer to a buffer with the x data
yData, //!< pointer to a buffer with the y data
zData); //!< pointer to a buffer with the z data
//still a size of 65536
dataReader.close();
delete xData;
delete yData;
delete zData;
}

Vulkan Crash Program

I'm starting a game engine with Vulkan and SDL2. My program is crashing in a very specific part of my code. I'm really new in the Vulkan API and i have tried several modifications with no success. If someone could help me, here I give the full code and the part that I think it is crashing.
// GE_Base.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vulkan/vulkan.h>
#include <SDL.h>
#include <glm.hpp>
#include <SDL_vulkan.h>
#include "GE_code_class.h"
#define WIDTH 1280
#define HEIGHT 720
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_Window * window = SDL_CreateWindow("GE_Base", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIDTH, HEIGHT, SDL_WINDOW_VULKAN);
////////////////////////////////
//VULKAN SETUP
const char* instanceExtensionNames[] = {
VK_KHR_SURFACE_EXTENSION_NAME,
};
VkApplicationInfo ai = {};
ai.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
ai.pNext = nullptr;
ai.pApplicationName = "SDL Vulkan";
ai.applicationVersion = 1;
ai.pEngineName = "SDLV";
ai.engineVersion = 1;
ai.apiVersion = VK_MAKE_VERSION(1, 0, 3);
VkInstanceCreateInfo ici = {};
ici.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
ici.pNext = nullptr;
ici.flags = 0;
ici.pApplicationInfo = &ai;
ici.enabledLayerCount = 0;
ici.ppEnabledLayerNames = nullptr;
ici.enabledExtensionCount = 0;
ici.ppEnabledExtensionNames = nullptr;
VkInstance instance;
vkCreateInstance(&ici, nullptr, &instance);
//CHECK PHYSICAL DEVICE (GRAFIC CARD)
VK_DEFINE_HANDLE(VkPhysicalDevice);
uint32_t deviceCount = 1;
VkPhysicalDevice gpu;
//VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)malloc(sizeof(deviceCount));
VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)malloc(sizeof(deviceCount));
vkEnumeratePhysicalDevices(instance, &deviceCount, physical_devices);
gpu = physical_devices[0];
//CHECK QUEUE FAMILIES
uint32_t graphics_queue_node_index;
/*if (queue_props->queueCount <= 0) queue_props->queueCount = 1;*/
uint32_t amountOfFamilies = 0;
vkGetPhysicalDeviceQueueFamilyProperties(gpu, &amountOfFamilies, NULL);
VkQueueFamilyProperties *queue_props = new VkQueueFamilyProperties[amountOfFamilies];
vkGetPhysicalDeviceQueueFamilyProperties(gpu, &amountOfFamilies, queue_props);
assert(queue_props->queueCount >= 1);
VkPhysicalDeviceFeatures features;
vkGetPhysicalDeviceFeatures(gpu, &features);
uint32_t graphicsQueueNodeIndex = UINT32_MAX;
unsigned int i;
for (i = 0; i < queue_props->queueCount; i++) {
if ((queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
if (graphicsQueueNodeIndex == UINT32_MAX)
graphicsQueueNodeIndex = i;
}
}
graphics_queue_node_index = graphicsQueueNodeIndex;
//LOGICAL DEVICE
VkDevice device;
float queue_priorities[1] = { 0.0 };
VkDeviceQueueCreateInfo q;
q.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
q.pNext = nullptr,
q.queueFamilyIndex = graphics_queue_node_index,
q.queueCount = 1,
q.pQueuePriorities = queue_priorities;
graphicsQueueNodeIndex = 2000;
graphics_queue_node_index = graphicsQueueNodeIndex;
//CREATE & INSTANTIATE DEVICE
VkDeviceCreateInfo d;
d.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
d.pNext = nullptr,
d.queueCreateInfoCount = 1,
d.pQueueCreateInfos = &q,
d.enabledLayerCount = 1,
d.ppEnabledLayerNames = nullptr,
d.enabledExtensionCount = 1,
d.ppEnabledExtensionNames = (const char *const *) instanceExtensionNames,
d.pEnabledFeatures = nullptr;
vkCreateDevice(gpu, &d, nullptr, &device);
//SURFACE WINDOW
VkSurfaceKHR surface;
SDL_Vulkan_CreateSurface(window, (SDL_vulkanInstance)&ici, (SDL_vulkanSurface*)&surface);
//PRESENTATION QUEU
VkBool32 *supportsPresent = new VkBool32[queue_props->queueCount];
vkGetPhysicalDeviceSurfaceSupportKHR(gpu, queue_props->queueCount, surface, &supportsPresent[i]);
graphics_queue_node_index = graphicsQueueNodeIndex;
//QUEUE
VK_DEFINE_HANDLE(VkQueue)
VkQueue queu_;
vkGetDeviceQueue(device, graphics_queue_node_index, queue_props->queueCount, &queu_);
////////////////////////////////
GE_class *engine;
engine = new GE_class();
engine->Init(1280, 720);
bool quit_app = false;
while (!quit_app) {
SDL_Event eve;
while (SDL_PollEvent(&eve)) {
switch (eve.type) {
case SDL_WINDOWEVENT:
if (eve.window.event == SDL_WINDOWEVENT_RESIZED) {
}
break;
case SDL_QUIT:
quit_app = true;
break;
}
}
//double currentTime = (double)SDL_GetTicks() / 1000.0;
//GLrender(currentTime);
//double currentTime = (double) SDL_GetTicks() / 1000.0;
//myRenderCode(currentTime);
//SDL_GL_SwapWindow(window);
/*vkQueuePresentKHR()*/
}
//SDL_DestroyWindow(window);
//SDL_Quit();
return 0;
}
VkQueue queu_;
vkGetDeviceQueue(device, graphics_queue_node_index, queue_props-queueCount, &queu_);
These are the lines that I think it's crashing. I guess in the previous lines I did
something wrong or I'm not initializing some variable. I'm not really sure.
The value of graphics_queue_node_index is 2000 when you call vkGetDeviceQueue. That's most likely not a valid queue index.
VkDeviceQueueCreateInfo q;
// ...
q.queueFamilyIndex = graphics_queue_node_index,
// ...
graphicsQueueNodeIndex = 2000;
graphics_queue_node_index = graphicsQueueNodeIndex;
// ...
vkGetPhysicalDeviceSurfaceSupportKHR(gpu, queue_props->queueCount, surface, &supportsPresent[i]);
graphics_queue_node_index = graphicsQueueNodeIndex; // redundant?
// ...
vkGetDeviceQueue(device, graphics_queue_node_index, queue_props->queueCount, &queu_);

CoreAudio Stuff From C++ to Swift

How to convert the code below to swift? Someone help.
I want to convert this cpp code to swift, from project FreeStreamer.
But some C++ struct and some C callback drive me crazy.
Help.
Here is the code from audio_stream.h and audio_stream.cpp
// var
queued_packet_t *m_queuedHead;
queued_packet_t *m_queuedTail;
queued_packet_t *m_playPacket;
std::list <queued_packet_t*> m_processedPackets;
//struct
typedef struct queued_packet {
UInt64 identifier;
AudioStreamPacketDescription desc;
struct queued_packet *next;
char data[];
} queued_packet_t;
//function one
OSStatus Audio_Stream::encoderDataCallback(AudioConverterRef inAudioConverter, UInt32 *ioNumberDataPackets, AudioBufferList *ioData, AudioStreamPacketDescription **outDataPacketDescription, void *inUserData) {
Audio_Stream *THIS = (Audio_Stream *)inUserData;
pthread_mutex_trylock(&THIS->m_packetQueueMutex);
// Dequeue one packet per time for the decoder
queued_packet_t *front = THIS->m_playPacket;
if (!front) {
/* Don't deadlock */
AS_LOCK_TRACE("encoderDataCallback 2: unlock\n");
pthread_mutex_unlock(&THIS->m_packetQueueMutex);
pthread_mutex_trylock(&THIS->m_streamStateMutex);
THIS->m_converterRunOutOfData = true;
pthread_mutex_unlock(&THIS->m_streamStateMutex);
*ioNumberDataPackets = 0;
ioData->mBuffers[0].mDataByteSize = 0;
return noErr;
}
*ioNumberDataPackets = 1;
ioData->mBuffers[0].mData = front->data;
ioData->mBuffers[0].mDataByteSize = front->desc.mDataByteSize;
ioData->mBuffers[0].mNumberChannels = THIS->m_srcFormat.mChannelsPerFrame;
if (outDataPacketDescription) {
*outDataPacketDescription = &front->desc;
}
THIS->m_playPacket = front->next;
THIS->m_processedPackets.push_front(front);
AS_LOCK_TRACE("encoderDataCallback 5: unlock\n");
pthread_mutex_unlock(&THIS->m_packetQueueMutex);
return noErr;
}
//function two
void Audio_Stream::streamDataCallback(void *inClientData, UInt32 inNumberBytes, UInt32 inNumberPackets, const void *inInputData, AudioStreamPacketDescription *inPacketDescriptions) {
AS_TRACE("%s: inNumberBytes %u, inNumberPackets %u\n", __FUNCTION__, (unsigned int)inNumberBytes, (unsigned int)inNumberPackets);
Audio_Stream *THIS = static_cast<Audio_Stream*>(inClientData);
if (!THIS->m_audioStreamParserRunning) {
AS_TRACE("%s: stray callback detected!\n", __PRETTY_FUNCTION__);
return;
}
for (int i = 0; i < inNumberPackets; i++) {
/* Allocate the packet */
UInt32 size = inPacketDescriptions[i].mDataByteSize;
queued_packet_t *packet = (queued_packet_t *)malloc(sizeof(queued_packet_t) + size);
packet->identifier = THIS->m_packetIdentifier;
// If the stream didn't provide bitRate (m_bitRate == 0), then let's calculate it
if (THIS->m_bitRate == 0 && THIS->m_bitrateBufferIndex < kAudioStreamBitrateBufferSize) {
// Only keep sampling for one buffer cycle; this is to keep the counters (for instance) duration
// stable.
THIS->m_bitrateBuffer[THIS->m_bitrateBufferIndex++] = 8 * inPacketDescriptions[i].mDataByteSize / THIS->m_packetDuration;
if (THIS->m_bitrateBufferIndex == kAudioStreamBitrateBufferSize) {
if (THIS->m_delegate) {
THIS->m_delegate->bitrateAvailable();
}
}
}
AS_LOCK_TRACE("streamDataCallback: lock\n");
pthread_mutex_trylock(&THIS->m_packetQueueMutex);
/* Prepare the packet */
packet->next = NULL;
packet->desc = inPacketDescriptions[i];
packet->desc.mStartOffset = 0;
memcpy(packet->data, (const char *)inInputData + inPacketDescriptions[i].mStartOffset,
size);
if (THIS->m_queuedHead == NULL) {
THIS->m_queuedHead = THIS->m_queuedTail = THIS->m_playPacket = packet;
} else {
THIS->m_queuedTail->next = packet;
THIS->m_queuedTail = packet;
}
THIS->m_cachedDataSize += size;
THIS->m_packetIdentifier++;
AS_LOCK_TRACE("streamDataCallback: unlock\n");
pthread_mutex_unlock(&THIS->m_packetQueueMutex);
}
THIS->determineBufferingLimits();
}
All the FreeStreamer project has been rewrite wieth Swift 3.0 in here FreePlayer
Answer can be found here AudioStream