Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Vulkan/VulkanUtil.cpp
3186 views
1
// Copyright (c) 2016- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <string>
19
#include "Common/Log.h"
20
#include "Common/StringUtils.h"
21
#include "Common/GPU/Vulkan/VulkanContext.h"
22
#include "GPU/Vulkan/VulkanUtil.h"
23
24
using namespace PPSSPP_VK;
25
26
const VkComponentMapping VULKAN_4444_SWIZZLE = { VK_COMPONENT_SWIZZLE_A, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B };
27
const VkComponentMapping VULKAN_1555_SWIZZLE = { VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_A };
28
const VkComponentMapping VULKAN_565_SWIZZLE = { VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_IDENTITY };
29
const VkComponentMapping VULKAN_8888_SWIZZLE = { VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY };
30
31
VkShaderModule CompileShaderModule(VulkanContext *vulkan, VkShaderStageFlagBits stage, const char *code, std::string *error) {
32
std::vector<uint32_t> spirv;
33
bool success = GLSLtoSPV(stage, code, GLSLVariant::VULKAN, spirv, error);
34
if (!error->empty()) {
35
if (success) {
36
ERROR_LOG(Log::G3D, "Warnings in shader compilation!");
37
} else {
38
ERROR_LOG(Log::G3D, "Error in shader compilation!");
39
}
40
ERROR_LOG(Log::G3D, "Messages: %s", error->c_str());
41
ERROR_LOG(Log::G3D, "Shader source:\n%s", LineNumberString(code).c_str());
42
OutputDebugStringUTF8("Messages:\n");
43
OutputDebugStringUTF8(error->c_str());
44
OutputDebugStringUTF8(LineNumberString(code).c_str());
45
return VK_NULL_HANDLE;
46
} else {
47
VkShaderModule module;
48
if (vulkan->CreateShaderModule(spirv, &module, stage == VK_SHADER_STAGE_VERTEX_BIT ? "system_vs" : "system_fs")) {
49
return module;
50
} else {
51
return VK_NULL_HANDLE;
52
}
53
}
54
}
55
56
VulkanComputeShaderManager::VulkanComputeShaderManager(VulkanContext *vulkan) : vulkan_(vulkan), pipelines_(8) {}
57
VulkanComputeShaderManager::~VulkanComputeShaderManager() {}
58
59
void VulkanComputeShaderManager::InitDeviceObjects(Draw::DrawContext *draw) {
60
VkPipelineCacheCreateInfo pc{ VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO };
61
VkResult res = vkCreatePipelineCache(vulkan_->GetDevice(), &pc, nullptr, &pipelineCache_);
62
_assert_(VK_SUCCESS == res);
63
64
static const BindingType bindingTypes[3] = {
65
BindingType::STORAGE_IMAGE_COMPUTE,
66
BindingType::STORAGE_BUFFER_COMPUTE,
67
BindingType::STORAGE_BUFFER_COMPUTE,
68
};
69
70
VkDescriptorSetLayoutBinding bindings[3] = {};
71
bindings[0].descriptorCount = 1;
72
bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
73
bindings[0].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
74
bindings[0].binding = 0;
75
bindings[1].descriptorCount = 1;
76
bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
77
bindings[1].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
78
bindings[1].binding = 1;
79
bindings[2].descriptorCount = 1;
80
bindings[2].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
81
bindings[2].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
82
bindings[2].binding = 2;
83
84
VkDevice device = vulkan_->GetDevice();
85
86
VkDescriptorSetLayoutCreateInfo dsl = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO };
87
dsl.bindingCount = ARRAY_SIZE(bindings);
88
dsl.pBindings = bindings;
89
res = vkCreateDescriptorSetLayout(device, &dsl, nullptr, &descriptorSetLayout_);
90
_assert_(VK_SUCCESS == res);
91
92
for (int i = 0; i < ARRAY_SIZE(frameData_); i++) {
93
frameData_[i].descPool.Create(vulkan_, bindingTypes, ARRAY_SIZE(bindingTypes), 4096);
94
frameData_[i].descPoolUsed = false;
95
}
96
97
VkPushConstantRange push = {};
98
push.offset = 0;
99
push.size = 16;
100
push.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
101
102
VkPipelineLayoutCreateInfo pl = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO };
103
pl.pPushConstantRanges = &push;
104
pl.pushConstantRangeCount = 1;
105
VkDescriptorSetLayout setLayouts[1] = { descriptorSetLayout_ };
106
pl.setLayoutCount = ARRAY_SIZE(setLayouts);
107
pl.pSetLayouts = setLayouts;
108
pl.flags = 0;
109
res = vkCreatePipelineLayout(device, &pl, nullptr, &pipelineLayout_);
110
_assert_(VK_SUCCESS == res);
111
}
112
113
void VulkanComputeShaderManager::DestroyDeviceObjects() {
114
for (int i = 0; i < ARRAY_SIZE(frameData_); i++) {
115
frameData_[i].descPool.Destroy();
116
}
117
if (descriptorSetLayout_) {
118
vulkan_->Delete().QueueDeleteDescriptorSetLayout(descriptorSetLayout_);
119
}
120
pipelines_.Iterate([&](const PipelineKey &key, VkPipeline pipeline) {
121
vulkan_->Delete().QueueDeletePipeline(pipeline);
122
});
123
pipelines_.Clear();
124
125
if (pipelineLayout_) {
126
vulkan_->Delete().QueueDeletePipelineLayout(pipelineLayout_);
127
}
128
if (pipelineCache_ != VK_NULL_HANDLE) {
129
vulkan_->Delete().QueueDeletePipelineCache(pipelineCache_);
130
}
131
}
132
133
VkDescriptorSet VulkanComputeShaderManager::GetDescriptorSet(VkImageView image, VkBuffer buffer, VkDeviceSize offset, VkDeviceSize range, VkBuffer buffer2, VkDeviceSize offset2, VkDeviceSize range2) {
134
int curFrame = vulkan_->GetCurFrame();
135
FrameData &frameData = frameData_[curFrame];
136
frameData.descPoolUsed = true;
137
VkDescriptorSet desc;
138
frameData.descPool.Allocate(&desc, 1, &descriptorSetLayout_);
139
_assert_(desc != VK_NULL_HANDLE);
140
141
VkWriteDescriptorSet writes[3]{};
142
int n = 0;
143
VkDescriptorImageInfo imageInfo = {};
144
VkDescriptorBufferInfo bufferInfo[2] = {};
145
if (image) {
146
imageInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
147
imageInfo.imageView = image;
148
imageInfo.sampler = VK_NULL_HANDLE;
149
writes[n].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
150
writes[n].dstBinding = 0;
151
writes[n].pImageInfo = &imageInfo;
152
writes[n].descriptorCount = 1;
153
writes[n].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
154
writes[n].dstSet = desc;
155
n++;
156
}
157
bufferInfo[0].buffer = buffer;
158
bufferInfo[0].offset = offset;
159
bufferInfo[0].range = range;
160
writes[n].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
161
writes[n].dstBinding = 1;
162
writes[n].pBufferInfo = &bufferInfo[0];
163
writes[n].descriptorCount = 1;
164
writes[n].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
165
writes[n].dstSet = desc;
166
n++;
167
if (buffer2) {
168
bufferInfo[1].buffer = buffer2;
169
bufferInfo[1].offset = offset2;
170
bufferInfo[1].range = range2;
171
writes[n].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
172
writes[n].dstBinding = 2;
173
writes[n].pBufferInfo = &bufferInfo[1];
174
writes[n].descriptorCount = 1;
175
writes[n].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
176
writes[n].dstSet = desc;
177
n++;
178
}
179
vkUpdateDescriptorSets(vulkan_->GetDevice(), n, writes, 0, nullptr);
180
return desc;
181
}
182
183
VkPipeline VulkanComputeShaderManager::GetPipeline(VkShaderModule cs) {
184
PipelineKey key{ cs };
185
VkPipeline pipeline;
186
if (pipelines_.Get(key, &pipeline)) {
187
return pipeline;
188
}
189
190
VkComputePipelineCreateInfo pci{ VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO };
191
pci.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
192
pci.stage.module = cs;
193
pci.stage.pName = "main";
194
pci.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
195
pci.layout = pipelineLayout_;
196
pci.flags = 0;
197
198
VkResult res = vkCreateComputePipelines(vulkan_->GetDevice(), pipelineCache_, 1, &pci, nullptr, &pipeline);
199
_assert_(res == VK_SUCCESS);
200
201
pipelines_.Insert(key, pipeline);
202
return pipeline;
203
}
204
205
void VulkanComputeShaderManager::BeginFrame() {
206
int curFrame = vulkan_->GetCurFrame();
207
FrameData &frame = frameData_[curFrame];
208
if (frame.descPoolUsed) {
209
frame.descPool.Reset();
210
frame.descPoolUsed = false;
211
}
212
}
213
214