【发布时间】:2021-07-30 09:19:34
【问题描述】:
我按照https://developer.android.com/ndk/guides/graphics/shader-compilers?hl=zh-cn->AOT编译->使用Android Studio中的说明进行操作。
AAsset* file = AAssetManager_open(assetManager,
"shaders/tri.vert.spv", AASSET_MODE_BUFFER);
size_t fileLength = AAsset_getLength(file);
char* fileContent = new char[fileLength];
AAsset_read(file, fileContent, fileLength);
这是我的 vert 文件和 frag 文件,它们是从 https://vulkan-tutorial.com/Drawing_a_triangle/Graphics_pipeline_basics/Shader_modules 复制的。 我把这两个文件放在src/main/shaders中
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) out vec3 fragColor;
vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
fragColor = colors[gl_VertexIndex];
}
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 fragColor;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(fragColor, 1.0);
}
但我收到了来自验证的消息:
UNASSIGNED-CoreValidation-Shader-InconsistentSpirv(ERROR / SPEC): msgNum: 0 - SPIR-V 模块无效:SPIR-V 标头无效。 对象:1 [0] 0,类型:0,名称:NULL
有人可以帮我阅读 .spv 吗?谢谢!
更新:
void createGraphicsPipeline(){
LOGD("Vulkan createGraphicsPipeline");
auto vertShaderCode = readFile("shaders/shader.frag.spv");
auto fragShaderCode = readFile("shaders/shader.frag.spv");
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
vertShaderStageInfo.sType =
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertShaderStageInfo.module = vertShaderModule;
vertShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
fragShaderStageInfo.sType =
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragShaderStageInfo.module = fragShaderModule;
fragShaderStageInfo.pName = "main";
std::vector<char> readFile(const char* filename){
if (mAssetManager == NULL) {
LOGD("Vulkan mAssetManager is null");
}
AAsset *asset = AAssetManager_open(mAssetManager, filename,
AASSET_MODE_BUFFER);
if (NULL == asset) {
LOGD("Vulkan shader is null");
throw std::runtime_error("Vulkan shader is null");
}
long size = AAsset_getLength(asset);
char* fileContent = new char[size];
AAsset_read(asset, fileContent, size);
std::vector<char> buffer;
buffer.insert(buffer.end(), fileContent, fileContent + strlen(fileContent));
AAsset_close(asset);
return buffer;
}
VkShaderModule createShaderModule(const std::vector<char>& code){
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size();
createInfo.pCode = reinterpret_cast<const uint32_t *>(code.data());
VkShaderModule shaderModule;
if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
LOGD("failed to create shader module!");
throw std::runtime_error("failed to create shader module!");
}
return shaderModule;
}
更新: 改成“fileContent + size”后报错
2021-05-10 16:59:36.310 8124-8124/com.example.vtest2 I/VALIDATION: VUID-VkPipelineShaderStageCreateInfo-pName-00707(ERROR / SPEC): msgNum: 0 - 找不到名为 main 的入口点对于阶段 VK_SHADER_STAGE_VERTEX_BIT.. Vulkan 规范指出:pName 必须是模块中具有与阶段匹配的执行模型的 OpEntryPoint 的名称 (https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#VUID-VkPipelineShaderStageCreateInfo-pName-00707)
对象:1
[0] 0,类型:0,名称:NULL
2021-05-10 16:59:36.310 8124-8124/com.example.vtest2 I/VALIDATION: UNASSIGNED-CoreValidation-Shader-InputNotProduced(ERROR / SPEC): msgNum: 0 - 片段着色器使用输入位置 0.0不是由顶点着色器编写的 对象:1 [0] 0xd,类型:15,名称:NULL
【问题讨论】:
-
您能否将您传递着色器代码的部分添加到
vkCreateShaderModule。我希望错误存在。 -
更新,非常感谢您的帮助!@SaschaWillems