Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | // This is a GPU-backend specific test. It relies on static intializers to work |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkTypes.h" |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 11 | |
Brian Osman | c7ad40f | 2018-05-31 14:27:17 -0400 | [diff] [blame] | 12 | #if defined(SK_VULKAN) |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 13 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/gpu/vk/GrVkVulkan.h" |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/gpu/GrBackendSurface.h" |
| 17 | #include "include/gpu/GrTexture.h" |
| 18 | #include "src/gpu/GrContextPriv.h" |
| 19 | #include "src/gpu/vk/GrVkCopyPipeline.h" |
| 20 | #include "src/gpu/vk/GrVkGpu.h" |
| 21 | #include "src/gpu/vk/GrVkRenderTarget.h" |
| 22 | #include "src/gpu/vk/GrVkUtil.h" |
| 23 | #include "tests/Test.h" |
| 24 | #include "tools/gpu/GrContextFactory.h" |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 25 | |
| 26 | using sk_gpu_test::GrContextFactory; |
| 27 | |
| 28 | class TestVkCopyProgram { |
| 29 | public: |
| 30 | TestVkCopyProgram() |
| 31 | : fVertShaderModule(VK_NULL_HANDLE) |
| 32 | , fFragShaderModule(VK_NULL_HANDLE) |
| 33 | , fPipelineLayout(VK_NULL_HANDLE) {} |
| 34 | |
| 35 | void test(GrVkGpu* gpu, skiatest::Reporter* reporter) { |
| 36 | const char vertShaderText[] = |
| 37 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 38 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 39 | |
| 40 | "layout(set = 0, binding = 0) uniform vertexUniformBuffer {" |
| 41 | "half4 uPosXform;" |
| 42 | "half4 uTexCoordXform;" |
| 43 | "};" |
| 44 | "layout(location = 0) in float2 inPosition;" |
| 45 | "layout(location = 1) out half2 vTexCoord;" |
| 46 | |
| 47 | "// Copy Program VS\n" |
| 48 | "void main() {" |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 49 | "vTexCoord = half2(inPosition * uTexCoordXform.xy + uTexCoordXform.zw);" |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 50 | "sk_Position.xy = inPosition * uPosXform.xy + uPosXform.zw;" |
| 51 | "sk_Position.zw = half2(0, 1);" |
| 52 | "}"; |
| 53 | |
| 54 | const char fragShaderText[] = |
| 55 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 56 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 57 | |
| 58 | "layout(set = 1, binding = 0) uniform sampler2D uTextureSampler;" |
| 59 | "layout(location = 1) in half2 vTexCoord;" |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 60 | |
| 61 | "// Copy Program FS\n" |
| 62 | "void main() {" |
Greg Daniel | e464160 | 2018-03-16 08:52:32 -0400 | [diff] [blame] | 63 | "sk_FragColor = texture(uTextureSampler, vTexCoord);" |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 64 | "}"; |
| 65 | |
| 66 | SkSL::Program::Settings settings; |
Ethan Nicholas | 92e01cf | 2018-12-19 13:12:10 -0500 | [diff] [blame] | 67 | SkSL::String spirv; |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 68 | SkSL::Program::Inputs inputs; |
| 69 | if (!GrCompileVkShaderModule(gpu, vertShaderText, VK_SHADER_STAGE_VERTEX_BIT, |
Ethan Nicholas | 92e01cf | 2018-12-19 13:12:10 -0500 | [diff] [blame] | 70 | &fVertShaderModule, &fShaderStageInfo[0], settings, |
| 71 | &spirv, &inputs)) { |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 72 | this->destroyResources(gpu); |
| 73 | REPORTER_ASSERT(reporter, false); |
| 74 | return; |
| 75 | } |
| 76 | SkASSERT(inputs.isEmpty()); |
| 77 | |
| 78 | if (!GrCompileVkShaderModule(gpu, fragShaderText, VK_SHADER_STAGE_FRAGMENT_BIT, |
Ethan Nicholas | 92e01cf | 2018-12-19 13:12:10 -0500 | [diff] [blame] | 79 | &fFragShaderModule, &fShaderStageInfo[1], settings, |
| 80 | &spirv, &inputs)) { |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 81 | this->destroyResources(gpu); |
| 82 | REPORTER_ASSERT(reporter, false); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | VkDescriptorSetLayout dsLayout[2]; |
| 87 | |
| 88 | GrVkResourceProvider& resourceProvider = gpu->resourceProvider(); |
| 89 | |
| 90 | dsLayout[GrVkUniformHandler::kUniformBufferDescSet] = resourceProvider.getUniformDSLayout(); |
| 91 | |
| 92 | uint32_t samplerVisibility = kFragment_GrShaderFlag; |
| 93 | SkTArray<uint32_t> visibilityArray(&samplerVisibility, 1); |
| 94 | |
| 95 | resourceProvider.getSamplerDescriptorSetHandle(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 96 | visibilityArray, &fSamplerDSHandle); |
| 97 | dsLayout[GrVkUniformHandler::kSamplerDescSet] = |
| 98 | resourceProvider.getSamplerDSLayout(fSamplerDSHandle); |
| 99 | |
| 100 | // Create the VkPipelineLayout |
| 101 | VkPipelineLayoutCreateInfo layoutCreateInfo; |
| 102 | memset(&layoutCreateInfo, 0, sizeof(VkPipelineLayoutCreateFlags)); |
| 103 | layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 104 | layoutCreateInfo.pNext = 0; |
| 105 | layoutCreateInfo.flags = 0; |
| 106 | layoutCreateInfo.setLayoutCount = 2; |
| 107 | layoutCreateInfo.pSetLayouts = dsLayout; |
| 108 | layoutCreateInfo.pushConstantRangeCount = 0; |
| 109 | layoutCreateInfo.pPushConstantRanges = nullptr; |
| 110 | |
| 111 | VkResult err = GR_VK_CALL(gpu->vkInterface(), CreatePipelineLayout(gpu->device(), |
| 112 | &layoutCreateInfo, |
| 113 | nullptr, |
| 114 | &fPipelineLayout)); |
| 115 | if (err) { |
| 116 | this->destroyResources(gpu); |
| 117 | REPORTER_ASSERT(reporter, false); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | GrSurfaceDesc surfDesc; |
| 122 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 123 | surfDesc.fWidth = 16; |
| 124 | surfDesc.fHeight = 16; |
| 125 | surfDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 126 | surfDesc.fSampleCnt = 1; |
| 127 | sk_sp<GrTexture> tex = gpu->createTexture(surfDesc, SkBudgeted::kNo); |
| 128 | if (!tex) { |
| 129 | this->destroyResources(gpu); |
| 130 | REPORTER_ASSERT(reporter, tex.get()); |
| 131 | return; |
| 132 | |
| 133 | } |
| 134 | GrRenderTarget* rt = tex->asRenderTarget(); |
| 135 | REPORTER_ASSERT(reporter, rt); |
| 136 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt); |
| 137 | |
| 138 | GrVkCopyPipeline* copyPipeline = GrVkCopyPipeline::Create(gpu, |
| 139 | fShaderStageInfo, |
| 140 | fPipelineLayout, |
| 141 | 1, |
| 142 | *vkRT->simpleRenderPass(), |
| 143 | VK_NULL_HANDLE); |
| 144 | |
| 145 | REPORTER_ASSERT(reporter, copyPipeline); |
| 146 | if (copyPipeline) { |
| 147 | copyPipeline->unref(gpu); |
| 148 | } |
| 149 | |
| 150 | this->destroyResources(gpu); |
| 151 | } |
| 152 | |
| 153 | void destroyResources(GrVkGpu* gpu) { |
| 154 | if (VK_NULL_HANDLE != fVertShaderModule) { |
| 155 | GR_VK_CALL(gpu->vkInterface(), DestroyShaderModule(gpu->device(), fVertShaderModule, |
| 156 | nullptr)); |
| 157 | fVertShaderModule = VK_NULL_HANDLE; |
| 158 | } |
| 159 | |
| 160 | if (VK_NULL_HANDLE != fFragShaderModule) { |
| 161 | GR_VK_CALL(gpu->vkInterface(), DestroyShaderModule(gpu->device(), fFragShaderModule, |
| 162 | nullptr)); |
| 163 | fFragShaderModule = VK_NULL_HANDLE; |
| 164 | } |
| 165 | |
| 166 | if (VK_NULL_HANDLE != fPipelineLayout) { |
| 167 | GR_VK_CALL(gpu->vkInterface(), DestroyPipelineLayout(gpu->device(), fPipelineLayout, |
| 168 | nullptr)); |
| 169 | fPipelineLayout = VK_NULL_HANDLE; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | VkShaderModule fVertShaderModule; |
| 174 | VkShaderModule fFragShaderModule; |
| 175 | VkPipelineShaderStageCreateInfo fShaderStageInfo[2]; |
| 176 | |
| 177 | GrVkDescriptorSetManager::Handle fSamplerDSHandle; |
| 178 | VkPipelineLayout fPipelineLayout; |
| 179 | |
| 180 | }; |
| 181 | |
| 182 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkMakeCopyPipelineTest, reporter, ctxInfo) { |
| 183 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 184 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->priv().getGpu()); |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 185 | |
Greg Daniel | f3a4ef9 | 2018-03-01 11:34:59 -0500 | [diff] [blame] | 186 | TestVkCopyProgram copyProgram; |
| 187 | copyProgram.test(gpu, reporter); |
| 188 | } |
| 189 | |
| 190 | #endif |