File size: 4,584 Bytes
6b0231d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4 expandtab: */

#ifndef VULKAN_LOAD_TEST_SAMPLE_H
#define VULKAN_LOAD_TEST_SAMPLE_H

/*
 * Copyright 2017-2020 Mark Callow.
 * SPDX-License-Identifier: Apache-2.0
 */

#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include "LoadTestSample.h"
#include "VulkanAppSDL.h"
#include "VulkanContext.h"
// MeshLoader needs vulkantools.h to be already included. It is included by
// vulkantextoverlay.hpp via VulkanAppSDL.h.
#include "utils/VulkanMeshLoader.hpp"

#include <ktxvulkan.h>

namespace VMA_CALLBACKS
{
    void InitVMA(VkPhysicalDevice& physicalDevice, VkDevice& device, VkInstance& instance, 
                 VkPhysicalDeviceMemoryProperties& devMemProps);
    void DestroyVMA();
    uint64_t AllocMemCWrapper(VkMemoryAllocateInfo* allocInfo, VkMemoryRequirements* memReq, uint64_t* numPages);
    VkResult BindBufferMemoryCWrapper(VkBuffer buffer, uint64_t allocId);
    VkResult BindImageMemoryCWrapper(VkImage image, uint64_t allocId);
    VkResult MapMemoryCWrapper(uint64_t allocId, uint64_t, VkDeviceSize* mapLength, void** dataPtr);
    void UnmapMemoryCWrapper(uint64_t allocId, uint64_t);
    void FreeMemCWrapper(uint64_t allocId);
}

#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))

extern "C" const char* vkFormatString(VkFormat format);

class VulkanLoadTestSample : public LoadTestSample {
  public:
    typedef uint64_t ticks_t;
    VulkanLoadTestSample(VulkanContext& vkctx,
                     uint32_t width, uint32_t height,
                     /* const char* const szArgs, */
                     const std::string sBasePath, int32_t yflip = -1)
           : LoadTestSample(width, height, /*szArgs,*/ sBasePath, yflip),
             vkctx(vkctx),
             defaultClearColor(std::array<float,4>({0.025f, 0.025f, 0.025f, 1.0f}))
    {
    }

    virtual ~VulkanLoadTestSample();
    virtual int doEvent(SDL_Event* event) { return LoadTestSample::doEvent(event); };
    virtual void resize(uint32_t width, uint32_t height) = 0;
    virtual void run(uint32_t msTicks) = 0;

    virtual void getOverlayText(VulkanTextOverlay* /*textOverlay*/,
                                float /*yoffset*/) { }
    virtual const char* customizeTitle(const char* const title) {
        return title;
    }

    typedef VulkanLoadTestSample* (*PFN_create)(VulkanContext&,
                                    uint32_t width, uint32_t height,
                                    const char* const szArgs,
                                    const std::string sBasePath);

  protected:

    ktxVulkanTexture_subAllocatorCallbacks subAllocatorCallbacks = {
        VMA_CALLBACKS::AllocMemCWrapper,
        VMA_CALLBACKS::BindBufferMemoryCWrapper,
        VMA_CALLBACKS::BindImageMemoryCWrapper,
        VMA_CALLBACKS::MapMemoryCWrapper,
        VMA_CALLBACKS::UnmapMemoryCWrapper,
        VMA_CALLBACKS::FreeMemCWrapper
    };

    virtual void keyPressed(uint32_t /*keyCode*/) { }
    virtual void viewChanged() { }
    bool gpuSupportsSwizzle() {
        return vkctx.gpuSupportsSwizzle();
    }

    std::string ktxfilename;
    int externalFile = 0;

    vk::PipelineShaderStageCreateInfo
    loadShader(std::string filename,
               vk::ShaderStageFlagBits stage,
               const char* modname = "main");
    void loadMesh(std::string filename,
                  vkMeshLoader::MeshBuffer* meshBuffer,
                  std::vector<vkMeshLoader::VertexLayout> vertexLayout,
                  float scale);

    struct MeshBufferInfo
    {
        vk::Buffer buf;
        vk::DeviceMemory mem;
        vk::DeviceSize size = 0;

        void freeResources(vk::Device& device) {
            if (buf) device.destroyBuffer(buf);
            if (mem) device.freeMemory(mem);
        }
    };

    struct MeshBuffer
    {
        MeshBufferInfo vertices;
        MeshBufferInfo indices;
        uint32_t indexCount;
        glm::vec3 dim;

        void freeResources(vk::Device& device) {
            vertices.freeResources(device);
            indices.freeResources(device);
        }
    };

    struct UniformData
    {
        vk::Buffer buffer;
        vk::DeviceMemory memory;
        vk::DescriptorBufferInfo descriptor;
        uint32_t allocSize;
        void* mapped = nullptr;

        void freeResources(vk::Device& device) {
            device.destroyBuffer(buffer);
            device.freeMemory(memory);
        }
    };

    VulkanContext& vkctx;

    // Saved for clean-up
    std::vector<VkShaderModule> shaderModules;

    const vk::ClearColorValue defaultClearColor;
};

#endif /* VULKAN_LOAD_TEST_SAMPLE_H */