shader_checker: initial plumbing
authorChris Forbes <chrisf@ijw.co.nz>
Thu, 2 Apr 2015 00:22:31 +0000 (13:22 +1300)
committerChris Forbes <chrisf@ijw.co.nz>
Thu, 16 Apr 2015 22:13:58 +0000 (10:13 +1200)
layers/CMakeLists.txt
layers/shader_checker.cpp [new file with mode: 0644]

index 7de71fc..ff746a0 100644 (file)
@@ -38,6 +38,7 @@ endif()
 include_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/../loader
+       ${CMAKE_CURRENT_SOURCE_DIR}/../../glslang
        ${CMAKE_CURRENT_BINARY_DIR}
 )
 
@@ -104,6 +105,7 @@ add_vk_layer(Multi multi.cpp)
 add_vk_layer(DrawState draw_state.cpp)
 add_vk_layer(MemTracker mem_tracker.cpp)
 #add_vk_layer(GlaveSnapshot glave_snapshot.c)
+add_vk_layer(ShaderChecker shader_checker.cpp)
 # generated
 add_vk_layer(Generic generic_layer.c)
 add_vk_layer(APIDump api_dump.cpp)
diff --git a/layers/shader_checker.cpp b/layers/shader_checker.cpp
new file mode 100644 (file)
index 0000000..8affef1
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ * Vulkan
+ *
+ * Copyright (C) 2015 LunarG, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <unordered_map>
+#include "loader_platform.h"
+#include "vk_dispatch_table_helper.h"
+#include "vkLayer.h"
+// The following is #included again to catch certain OS-specific functions
+// being used:
+#include "loader_platform.h"
+
+#include "SPIRV/spirv.h"
+
+static std::unordered_map<void *, VkLayerDispatchTable *> tableMap;
+
+static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *gpuw)
+{
+    VkLayerDispatchTable *pTable;
+
+    assert(gpuw);
+    std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) gpuw->baseObject);
+    if (it == tableMap.end())
+    {
+        pTable =  new VkLayerDispatchTable;
+        tableMap[(void *) gpuw->baseObject] = pTable;
+    } else
+    {
+        return it->second;
+    }
+
+    layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalGpu) gpuw->nextObject);
+
+    return pTable;
+}
+
+
+VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalGpu gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
+{
+    VkLayerDispatchTable* pTable = tableMap[gpu];
+    VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice);
+    // create a mapping for the device object into the dispatch table
+    tableMap.emplace(*pDevice, pTable);
+    return result;
+}
+
+
+VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalGpu gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
+{
+    if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pOutLayers[1] == NULL || pReserved == NULL)
+        return VK_ERROR_INVALID_POINTER;
+
+    if (maxLayerCount < 1)
+        return VK_ERROR_INITIALIZATION_FAILED;
+    *pOutLayerCount = 1;
+    strncpy((char *) pOutLayers[0], "ShaderChecker", maxStringSize);
+    return VK_SUCCESS;
+}
+
+
+struct extProps {
+    uint32_t version;
+    const char * const name;
+};
+#define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 1
+static const struct extProps shaderCheckerExts[SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE] = {
+    // TODO what is the version?
+    0x10, "ShaderChecker",
+};
+
+
+VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
+                                               VkExtensionInfoType infoType,
+                                               uint32_t extensionIndex,
+                                               size_t*  pDataSize,
+                                               void*    pData)
+{
+    VkResult result;
+
+    /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
+    VkExtensionProperties *ext_props;
+    uint32_t *count;
+
+    if (pDataSize == NULL)
+        return VK_ERROR_INVALID_POINTER;
+
+    switch (infoType) {
+        case VK_EXTENSION_INFO_TYPE_COUNT:
+            *pDataSize = sizeof(uint32_t);
+            if (pData == NULL)
+                return VK_SUCCESS;
+            count = (uint32_t *) pData;
+            *count = SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE;
+            break;
+        case VK_EXTENSION_INFO_TYPE_PROPERTIES:
+            *pDataSize = sizeof(VkExtensionProperties);
+            if (pData == NULL)
+                return VK_SUCCESS;
+            if (extensionIndex >= SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE)
+                return VK_ERROR_INVALID_VALUE;
+            ext_props = (VkExtensionProperties *) pData;
+            ext_props->version = shaderCheckerExts[extensionIndex].version;
+            strncpy(ext_props->extName, shaderCheckerExts[extensionIndex].name,
+                                        VK_MAX_EXTENSION_NAME);
+            ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
+            break;
+        default:
+            return VK_ERROR_INVALID_VALUE;
+    };
+
+    return VK_SUCCESS;
+}
+
+
+VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo,
+                                                   VkShader *pShader)
+{
+    VkLayerDispatchTable* pTable = tableMap[(VkBaseLayerObject *)device];
+    VkResult res = pTable->CreateShader(device, pCreateInfo, pShader);
+    return res;
+}
+
+
+VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalGpu gpu, const char* pName)
+{
+    if (gpu == NULL)
+        return NULL;
+
+    initLayerTable((const VkBaseLayerObject *) gpu);
+
+#define ADD_HOOK(fn)    \
+    if (!strncmp(#fn, pName, sizeof(#fn))) \
+        return (void *) fn
+
+    ADD_HOOK(vkGetProcAddr);
+    ADD_HOOK(vkEnumerateLayers);
+    ADD_HOOK(vkCreateDevice);
+    ADD_HOOK(vkCreateShader);
+
+    VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
+    if (gpuw->pGPA == NULL)
+        return NULL;
+    return gpuw->pGPA((VkPhysicalGpu) gpuw->nextObject, pName);
+}