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