blob: b21a0e60ceb1363e139a053e8c7ae62aa4c6ee4f [file] [log] [blame]
Greg Danielf3a4ef92018-03-01 11:34:59 -05001/*
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 Osmanc7ad40f2018-05-31 14:27:17 -040012#if defined(SK_VULKAN)
Greg Danielf3a4ef92018-03-01 11:34:59 -050013
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
24using sk_gpu_test::GrContextFactory;
25
26class TestVkCopyProgram {
27public:
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 Danielf3a4ef92018-03-01 11:34:59 -050058
59 "// Copy Program FS\n"
60 "void main() {"
Greg Daniele4641602018-03-16 08:52:32 -040061 "sk_FragColor = texture(uTextureSampler, vTexCoord);"
Greg Danielf3a4ef92018-03-01 11:34:59 -050062 "}";
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 Danielf3a4ef92018-03-01 11:34:59 -0500118 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
177DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkMakeCopyPipelineTest, reporter, ctxInfo) {
178 GrContext* context = ctxInfo.grContext();
179 GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu());
180
Greg Danielf3a4ef92018-03-01 11:34:59 -0500181 TestVkCopyProgram copyProgram;
182 copyProgram.test(gpu, reporter);
183}
184
185#endif