456 lines
15 KiB
C++
456 lines
15 KiB
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#include "wiGraphicsDevice_Vulkan.h"
|
|
#include "wiHelper.h"
|
|
#include "ResourceMapping.h"
|
|
|
|
#include <sstream>
|
|
#include <vector>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#ifdef WICKEDENGINE_BUILD_VULKAN
|
|
#pragma comment(lib,"vulkan-1.lib")
|
|
|
|
namespace wiGraphicsTypes
|
|
{
|
|
|
|
// Validation layer helpers:
|
|
const std::vector<const char*> validationLayers = {
|
|
"VK_LAYER_LUNARG_standard_validation"
|
|
};
|
|
#ifdef NDEBUG
|
|
const bool enableValidationLayers = false;
|
|
#else
|
|
const bool enableValidationLayers = true;
|
|
#endif
|
|
bool checkValidationLayerSupport() {
|
|
uint32_t layerCount;
|
|
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
|
|
|
std::vector<VkLayerProperties> availableLayers(layerCount);
|
|
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
|
|
|
for (const char* layerName : validationLayers) {
|
|
bool layerFound = false;
|
|
|
|
for (const auto& layerProperties : availableLayers) {
|
|
if (strcmp(layerName, layerProperties.layerName) == 0) {
|
|
layerFound = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!layerFound) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
|
VkDebugReportFlagsEXT flags,
|
|
VkDebugReportObjectTypeEXT objType,
|
|
uint64_t obj,
|
|
size_t location,
|
|
int32_t code,
|
|
const char* layerPrefix,
|
|
const char* msg,
|
|
void* userData) {
|
|
|
|
std::stringstream ss("");
|
|
ss << "validation layer: " << msg << std::endl;
|
|
|
|
std::cerr << ss.str();
|
|
OutputDebugStringA(ss.str().c_str());
|
|
|
|
return VK_FALSE;
|
|
}
|
|
VkResult CreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugReportCallbackEXT* pCallback) {
|
|
auto func = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT");
|
|
if (func != nullptr) {
|
|
return func(instance, pCreateInfo, pAllocator, pCallback);
|
|
}
|
|
else {
|
|
return VK_ERROR_EXTENSION_NOT_PRESENT;
|
|
}
|
|
}
|
|
void DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback, const VkAllocationCallbacks* pAllocator) {
|
|
auto func = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugReportCallbackEXT");
|
|
if (func != nullptr) {
|
|
func(instance, callback, pAllocator);
|
|
}
|
|
}
|
|
|
|
|
|
// Engine functions
|
|
GraphicsDevice_Vulkan::GraphicsDevice_Vulkan(wiWindowRegistration::window_type window, bool fullscreen) : GraphicsDevice()
|
|
{
|
|
FULLSCREEN = fullscreen;
|
|
|
|
RECT rect = RECT();
|
|
GetClientRect(window, &rect);
|
|
SCREENWIDTH = rect.right - rect.left;
|
|
SCREENHEIGHT = rect.bottom - rect.top;
|
|
|
|
|
|
|
|
// Fill out application info:
|
|
VkApplicationInfo appInfo = {};
|
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
|
appInfo.pApplicationName = "Wicked Engine Application";
|
|
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
appInfo.pEngineName = "Wicked Engine";
|
|
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
appInfo.apiVersion = VK_API_VERSION_1_0;
|
|
|
|
// Enumerate available extensions:
|
|
uint32_t extensionCount = 0;
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
|
|
std::vector<VkExtensionProperties> extensions(extensionCount);
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
|
|
std::vector<const char*> extensionNames;
|
|
for (auto& x : extensions)
|
|
{
|
|
extensionNames.push_back(x.extensionName);
|
|
}
|
|
|
|
if (enableValidationLayers && !checkValidationLayerSupport()) {
|
|
throw std::runtime_error("validation layers requested, but not available!");
|
|
}
|
|
|
|
// Create instance:
|
|
VkInstanceCreateInfo createInfo = {};
|
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
createInfo.pApplicationInfo = &appInfo;
|
|
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
|
createInfo.ppEnabledExtensionNames = extensionNames.data();
|
|
createInfo.enabledLayerCount = 0;
|
|
if (enableValidationLayers)
|
|
{
|
|
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
|
createInfo.ppEnabledLayerNames = validationLayers.data();
|
|
}
|
|
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
|
|
throw std::runtime_error("failed to create instance!");
|
|
}
|
|
|
|
// Register validation layer callback:
|
|
if (enableValidationLayers)
|
|
{
|
|
VkDebugReportCallbackCreateInfoEXT createInfo = {};
|
|
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
|
|
createInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
|
|
createInfo.pfnCallback = debugCallback;
|
|
if (CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, &callback) != VK_SUCCESS) {
|
|
throw std::runtime_error("failed to set up debug callback!");
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
GraphicsDevice_Vulkan::~GraphicsDevice_Vulkan()
|
|
{
|
|
DestroyDebugReportCallbackEXT(instance, callback, nullptr);
|
|
vkDestroyInstance(instance, nullptr);
|
|
}
|
|
|
|
void GraphicsDevice_Vulkan::SetResolution(int width, int height)
|
|
{
|
|
if (width != SCREENWIDTH || height != SCREENHEIGHT)
|
|
{
|
|
SCREENWIDTH = width;
|
|
SCREENHEIGHT = height;
|
|
//swapChain->ResizeBuffers(2, width, height, _ConvertFormat(GetBackBufferFormat()), 0);
|
|
RESOLUTIONCHANGED = true;
|
|
}
|
|
}
|
|
|
|
Texture2D GraphicsDevice_Vulkan::GetBackBuffer()
|
|
{
|
|
return Texture2D();
|
|
}
|
|
|
|
HRESULT GraphicsDevice_Vulkan::CreateBuffer(const GPUBufferDesc *pDesc, const SubresourceData* pInitialData, GPUBuffer *ppBuffer)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateTexture1D(const Texture1DDesc* pDesc, const SubresourceData *pInitialData, Texture1D **ppTexture1D)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateTexture2D(const Texture2DDesc* pDesc, const SubresourceData *pInitialData, Texture2D **ppTexture2D)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateTexture3D(const Texture3DDesc* pDesc, const SubresourceData *pInitialData, Texture3D **ppTexture3D)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateInputLayout(const VertexLayoutDesc *pInputElementDescs, UINT NumElements,
|
|
const void *pShaderBytecodeWithInputSignature, SIZE_T BytecodeLength, VertexLayout *pInputLayout)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateVertexShader(const void *pShaderBytecode, SIZE_T BytecodeLength, VertexShader *pVertexShader)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreatePixelShader(const void *pShaderBytecode, SIZE_T BytecodeLength, PixelShader *pPixelShader)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateGeometryShader(const void *pShaderBytecode, SIZE_T BytecodeLength, GeometryShader *pGeometryShader)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateHullShader(const void *pShaderBytecode, SIZE_T BytecodeLength, HullShader *pHullShader)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateDomainShader(const void *pShaderBytecode, SIZE_T BytecodeLength, DomainShader *pDomainShader)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateComputeShader(const void *pShaderBytecode, SIZE_T BytecodeLength, ComputeShader *pComputeShader)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateBlendState(const BlendStateDesc *pBlendStateDesc, BlendState *pBlendState)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateDepthStencilState(const DepthStencilStateDesc *pDepthStencilStateDesc, DepthStencilState *pDepthStencilState)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateRasterizerState(const RasterizerStateDesc *pRasterizerStateDesc, RasterizerState *pRasterizerState)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateSamplerState(const SamplerDesc *pSamplerDesc, Sampler *pSamplerState)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateQuery(const GPUQueryDesc *pDesc, GPUQuery *pQuery)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateGraphicsPSO(const GraphicsPSODesc* pDesc, GraphicsPSO* pso)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::CreateComputePSO(const ComputePSODesc* pDesc, ComputePSO* pso)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
|
|
|
|
void GraphicsDevice_Vulkan::PresentBegin()
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::PresentEnd()
|
|
{
|
|
}
|
|
|
|
void GraphicsDevice_Vulkan::ExecuteDeferredContexts()
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::FinishCommandList(GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
|
|
|
|
void GraphicsDevice_Vulkan::BindViewports(UINT NumViewports, const ViewPort *pViewports, GRAPHICSTHREAD threadID)
|
|
{
|
|
//assert(NumViewports <= 6);
|
|
//VkViewport viewports[6];
|
|
//for (UINT i = 0; i < NumViewports; ++i)
|
|
//{
|
|
// viewports[i].x = pViewports[i].TopLeftX;
|
|
// viewports[i].y = pViewports[i].TopLeftY;
|
|
// viewports[i].width = pViewports[i].Width;
|
|
// viewports[i].height = pViewports[i].Height;
|
|
// viewports[i].minDepth = pViewports[i].MinDepth;
|
|
// viewports[i].maxDepth = pViewports[i].MaxDepth;
|
|
//}
|
|
//vkCmdSetViewport(commandBuffers[threadID], 0, NumViewports, viewports);
|
|
}
|
|
void GraphicsDevice_Vulkan::BindRenderTargetsUAVs(UINT NumViews, Texture* const *ppRenderTargets, Texture2D* depthStencilTexture, GPUResource* const *ppUAVs, int slotUAV, int countUAV,
|
|
GRAPHICSTHREAD threadID, int arrayIndex)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindRenderTargets(UINT NumViews, Texture* const *ppRenderTargets, Texture2D* depthStencilTexture, GRAPHICSTHREAD threadID, int arrayIndex)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::ClearRenderTarget(Texture* pTexture, const FLOAT ColorRGBA[4], GRAPHICSTHREAD threadID, int arrayIndex)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::ClearDepthStencil(Texture2D* pTexture, UINT ClearFlags, FLOAT Depth, UINT8 Stencil, GRAPHICSTHREAD threadID, int arrayIndex)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindResource(SHADERSTAGE stage, GPUResource* resource, int slot, GRAPHICSTHREAD threadID, int arrayIndex)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindResources(SHADERSTAGE stage, GPUResource *const* resources, int slot, int count, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindUnorderedAccessResource(SHADERSTAGE stage, GPUResource* resource, int slot, GRAPHICSTHREAD threadID, int arrayIndex)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindUnorderedAccessResources(SHADERSTAGE stage, GPUResource *const* resources, int slot, int count, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindUnorderedAccessResourceCS(GPUResource* resource, int slot, GRAPHICSTHREAD threadID, int arrayIndex)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindUnorderedAccessResourcesCS(GPUResource *const* resources, int slot, int count, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::UnBindResources(int slot, int num, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::UnBindUnorderedAccessResources(int slot, int num, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindSampler(SHADERSTAGE stage, Sampler* sampler, int slot, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindConstantBuffer(SHADERSTAGE stage, GPUBuffer* buffer, int slot, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindVertexBuffers(GPUBuffer* const *vertexBuffers, int slot, int count, const UINT* strides, const UINT* offsets, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindIndexBuffer(GPUBuffer* indexBuffer, const INDEXBUFFER_FORMAT format, UINT offset, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindPrimitiveTopology(PRIMITIVETOPOLOGY type, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindStencilRef(UINT value, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindBlendFactor(XMFLOAT4 value, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindGraphicsPSO(GraphicsPSO* pso, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::BindComputePSO(ComputePSO* pso, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::Draw(int vertexCount, UINT startVertexLocation, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::DrawIndexed(int indexCount, UINT startIndexLocation, UINT baseVertexLocation, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::DrawInstanced(int vertexCount, int instanceCount, UINT startVertexLocation, UINT startInstanceLocation, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::DrawIndexedInstanced(int indexCount, int instanceCount, UINT startIndexLocation, UINT baseVertexLocation, UINT startInstanceLocation, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::DrawInstancedIndirect(GPUBuffer* args, UINT args_offset, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::DrawIndexedInstancedIndirect(GPUBuffer* args, UINT args_offset, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::Dispatch(UINT threadGroupCountX, UINT threadGroupCountY, UINT threadGroupCountZ, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::DispatchIndirect(GPUBuffer* args, UINT args_offset, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::GenerateMips(Texture* texture, GRAPHICSTHREAD threadID, int arrayIndex)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::CopyTexture2D(Texture2D* pDst, Texture2D* pSrc, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::CopyTexture2D_Region(Texture2D* pDst, UINT dstMip, UINT dstX, UINT dstY, Texture2D* pSrc, UINT srcMip, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::MSAAResolve(Texture2D* pDst, Texture2D* pSrc, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::UpdateBuffer(GPUBuffer* buffer, const void* data, GRAPHICSTHREAD threadID, int dataSize)
|
|
{
|
|
}
|
|
void* GraphicsDevice_Vulkan::AllocateFromRingBuffer(GPURingBuffer* buffer, size_t dataSize, UINT& offsetIntoBuffer, GRAPHICSTHREAD threadID)
|
|
{
|
|
offsetIntoBuffer = 0;
|
|
return nullptr;
|
|
}
|
|
void GraphicsDevice_Vulkan::InvalidateBufferAccess(GPUBuffer* buffer, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
bool GraphicsDevice_Vulkan::DownloadBuffer(GPUBuffer* bufferToDownload, GPUBuffer* bufferDest, void* dataDest, GRAPHICSTHREAD threadID)
|
|
{
|
|
return false;
|
|
}
|
|
void GraphicsDevice_Vulkan::SetScissorRects(UINT numRects, const Rect* rects, GRAPHICSTHREAD threadID)
|
|
{
|
|
//assert(rects != nullptr);
|
|
//assert(numRects <= 8);
|
|
//VkRect2D scissors[8];
|
|
//for (UINT i = 0; i < numRects; ++i)
|
|
//{
|
|
// scissors[i].extent.width = abs(rects[i].right - rects[i].left);
|
|
// scissors[i].extent.height = abs(rects[i].bottom - rects[i].top);
|
|
// scissors[i].offset.x = rects[i].left;
|
|
// scissors[i].offset.y = rects[i].bottom;
|
|
//}
|
|
//vkCmdSetScissor(commandBuffers[threadID], 0, numRects, scissors);
|
|
}
|
|
|
|
void GraphicsDevice_Vulkan::QueryBegin(GPUQuery *query, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::QueryEnd(GPUQuery *query, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
bool GraphicsDevice_Vulkan::QueryRead(GPUQuery *query, GRAPHICSTHREAD threadID)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
void GraphicsDevice_Vulkan::UAVBarrier(GPUResource *const* uavs, UINT NumBarriers, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::TransitionBarrier(GPUResource *const* resources, UINT NumBarriers, RESOURCE_STATES stateBefore, RESOURCE_STATES stateAfter, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
|
|
|
|
HRESULT GraphicsDevice_Vulkan::CreateTextureFromFile(const std::string& fileName, Texture2D **ppTexture, bool mipMaps, GRAPHICSTHREAD threadID)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::SaveTexturePNG(const std::string& fileName, Texture2D *pTexture, GRAPHICSTHREAD threadID)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
HRESULT GraphicsDevice_Vulkan::SaveTextureDDS(const std::string& fileName, Texture *pTexture, GRAPHICSTHREAD threadID)
|
|
{
|
|
return E_FAIL;
|
|
}
|
|
|
|
void GraphicsDevice_Vulkan::EventBegin(const std::string& name, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::EventEnd(GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
void GraphicsDevice_Vulkan::SetMarker(const std::string& name, GRAPHICSTHREAD threadID)
|
|
{
|
|
}
|
|
|
|
}
|
|
|
|
|
|
#endif // WICKEDENGINE_BUILD_VULKAN
|