@@ -249,6 +249,14 @@ class VKRenderer : public GraphicsAPIRenderer
249
249
}
250
250
public:
251
251
VkSampler m_sampler;
252
+ const VulkanApi* m_api;
253
+ SamplerStateImpl (const VulkanApi* api)
254
+ : m_api(api)
255
+ {}
256
+ ~SamplerStateImpl ()
257
+ {
258
+ m_api->vkDestroySampler (m_api->m_device , m_sampler, nullptr );
259
+ }
252
260
};
253
261
254
262
class ResourceViewImpl : public IResourceView , public RefObject
@@ -268,17 +276,26 @@ class VKRenderer : public GraphicsAPIRenderer
268
276
TexelBuffer,
269
277
PlainBuffer,
270
278
};
279
+ public:
280
+ ResourceViewImpl (ViewType viewType, const VulkanApi* api)
281
+ : m_type(viewType), m_api(api)
282
+ {
283
+ }
271
284
ViewType m_type;
285
+ const VulkanApi* m_api;
272
286
};
273
287
274
288
class TextureResourceViewImpl : public ResourceViewImpl
275
289
{
276
290
public:
277
- TextureResourceViewImpl ()
291
+ TextureResourceViewImpl (const VulkanApi* api)
292
+ : ResourceViewImpl(ViewType::Texture, api)
278
293
{
279
- m_type = ViewType::Texture;
280
294
}
281
-
295
+ ~TextureResourceViewImpl ()
296
+ {
297
+ m_api->vkDestroyImageView (m_api->m_device , m_view, nullptr );
298
+ }
282
299
RefPtr<TextureResourceImpl> m_texture;
283
300
VkImageView m_view;
284
301
VkImageLayout m_layout;
@@ -287,23 +304,25 @@ class VKRenderer : public GraphicsAPIRenderer
287
304
class TexelBufferResourceViewImpl : public ResourceViewImpl
288
305
{
289
306
public:
290
- TexelBufferResourceViewImpl ()
307
+ TexelBufferResourceViewImpl (const VulkanApi* api)
308
+ : ResourceViewImpl(ViewType::TexelBuffer, api)
291
309
{
292
- m_type = ViewType::TexelBuffer;
293
310
}
294
-
311
+ ~TexelBufferResourceViewImpl ()
312
+ {
313
+ m_api->vkDestroyBufferView (m_api->m_device , m_view, nullptr );
314
+ }
295
315
RefPtr<BufferResourceImpl> m_buffer;
296
316
VkBufferView m_view;
297
317
};
298
318
299
319
class PlainBufferResourceViewImpl : public ResourceViewImpl
300
320
{
301
321
public:
302
- PlainBufferResourceViewImpl ()
322
+ PlainBufferResourceViewImpl (const VulkanApi* api)
323
+ : ResourceViewImpl(ViewType::PlainBuffer, api)
303
324
{
304
- m_type = ViewType::PlainBuffer;
305
325
}
306
-
307
326
RefPtr<BufferResourceImpl> m_buffer;
308
327
VkDeviceSize offset;
309
328
VkDeviceSize size;
@@ -1989,16 +2008,68 @@ Result VKRenderer::createSamplerState(ISamplerState::Desc const& desc, ISamplerS
1989
2008
VkSampler sampler;
1990
2009
SLANG_VK_RETURN_ON_FAIL (m_api.vkCreateSampler (m_device, &samplerInfo, nullptr , &sampler));
1991
2010
1992
- RefPtr<SamplerStateImpl> samplerImpl = new SamplerStateImpl ();
2011
+ RefPtr<SamplerStateImpl> samplerImpl = new SamplerStateImpl (&m_api );
1993
2012
samplerImpl->m_sampler = sampler;
1994
2013
*outSampler = samplerImpl.detach ();
1995
2014
return SLANG_OK;
1996
2015
}
1997
2016
1998
2017
Result VKRenderer::createTextureView (ITextureResource* texture, IResourceView::Desc const & desc, IResourceView** outView)
1999
2018
{
2000
- assert (!" unimplemented" );
2001
- return SLANG_FAIL;
2019
+ auto resourceImpl = static_cast <TextureResourceImpl*>(texture);
2020
+ RefPtr<TextureResourceViewImpl> view = new TextureResourceViewImpl (&m_api);
2021
+ view->m_texture = resourceImpl;
2022
+ VkImageViewCreateInfo createInfo = {};
2023
+ createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
2024
+ createInfo.flags = 0 ;
2025
+ createInfo.format = VulkanUtil::getVkFormat (desc.format );
2026
+ createInfo.image = resourceImpl->m_image ;
2027
+ createInfo.components = VkComponentMapping{ VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,VK_COMPONENT_SWIZZLE_B,VK_COMPONENT_SWIZZLE_A };
2028
+ switch (resourceImpl->getType ())
2029
+ {
2030
+ case IResource::Type::Texture1D:
2031
+ createInfo.viewType = VK_IMAGE_VIEW_TYPE_1D;
2032
+ break ;
2033
+ case IResource::Type::Texture2D:
2034
+ createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
2035
+ break ;
2036
+ case IResource::Type::Texture3D:
2037
+ createInfo.viewType = VK_IMAGE_VIEW_TYPE_3D;
2038
+ break ;
2039
+ case IResource::Type::TextureCube:
2040
+ createInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
2041
+ break ;
2042
+ default :
2043
+ SLANG_UNIMPLEMENTED_X (" Unknown Texture type." );
2044
+ break ;
2045
+ }
2046
+ createInfo.subresourceRange .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
2047
+ createInfo.subresourceRange .baseArrayLayer = 0 ;
2048
+ createInfo.subresourceRange .baseMipLevel = 0 ;
2049
+ createInfo.subresourceRange .layerCount = VK_REMAINING_ARRAY_LAYERS;
2050
+ createInfo.subresourceRange .levelCount = VK_REMAINING_MIP_LEVELS;
2051
+ switch (desc.type )
2052
+ {
2053
+ case IResourceView::Type::DepthStencil:
2054
+ view->m_layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
2055
+ createInfo.subresourceRange .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
2056
+ break ;
2057
+ case IResourceView::Type::RenderTarget:
2058
+ view->m_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
2059
+ break ;
2060
+ case IResourceView::Type::ShaderResource:
2061
+ view->m_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
2062
+ break ;
2063
+ case IResourceView::Type::UnorderedAccess:
2064
+ view->m_layout = VK_IMAGE_LAYOUT_GENERAL;
2065
+ break ;
2066
+ default :
2067
+ SLANG_UNIMPLEMENTED_X (" Unknown TextureViewDesc type." );
2068
+ break ;
2069
+ }
2070
+ m_api.vkCreateImageView (m_device, &createInfo, nullptr , &view->m_view );
2071
+ *outView = view.detach ();
2072
+ return SLANG_OK;
2002
2073
}
2003
2074
2004
2075
Result VKRenderer::createBufferView (IBufferResource* buffer, IResourceView::Desc const & desc, IResourceView** outView)
@@ -2041,7 +2112,7 @@ Result VKRenderer::createBufferView(IBufferResource* buffer, IResourceView::Desc
2041
2112
{
2042
2113
// Buffer usage that doesn't involve formatting doesn't
2043
2114
// require a view in Vulkan.
2044
- RefPtr<PlainBufferResourceViewImpl> viewImpl = new PlainBufferResourceViewImpl ();
2115
+ RefPtr<PlainBufferResourceViewImpl> viewImpl = new PlainBufferResourceViewImpl (&m_api );
2045
2116
viewImpl->m_buffer = resourceImpl;
2046
2117
viewImpl->offset = 0 ;
2047
2118
viewImpl->size = size;
@@ -2065,7 +2136,7 @@ Result VKRenderer::createBufferView(IBufferResource* buffer, IResourceView::Desc
2065
2136
VkBufferView view;
2066
2137
SLANG_VK_RETURN_ON_FAIL (m_api.vkCreateBufferView (m_device, &info, nullptr , &view));
2067
2138
2068
- RefPtr<TexelBufferResourceViewImpl> viewImpl = new TexelBufferResourceViewImpl ();
2139
+ RefPtr<TexelBufferResourceViewImpl> viewImpl = new TexelBufferResourceViewImpl (&m_api );
2069
2140
viewImpl->m_buffer = resourceImpl;
2070
2141
viewImpl->m_view = view;
2071
2142
*outView = viewImpl.detach ();
@@ -2698,7 +2769,6 @@ void VKRenderer::DescriptorSetImpl::setResource(UInt range, UInt index, IResourc
2698
2769
VkDescriptorImageInfo imageInfo = {};
2699
2770
imageInfo.imageView = textureViewImpl->m_view ;
2700
2771
imageInfo.imageLayout = textureViewImpl->m_layout ;
2701
- // imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
2702
2772
2703
2773
VkWriteDescriptorSet writeInfo = { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET };
2704
2774
writeInfo.dstSet = m_descriptorSet;
@@ -2762,7 +2832,16 @@ void VKRenderer::DescriptorSetImpl::setSampler(UInt range, UInt index, ISamplerS
2762
2832
auto boundObjectIndex = rangeInfo.arrayIndex + index ;
2763
2833
auto descriptorType = rangeInfo.vkDescriptorType ;
2764
2834
2765
- // TODO: Actually bind it!
2835
+ VkWriteDescriptorSet writeInfo = { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET };
2836
+ writeInfo.dstSet = m_descriptorSet;
2837
+ writeInfo.dstBinding = uint32_t (bindingIndex);
2838
+ writeInfo.dstArrayElement = uint32_t (index );
2839
+ writeInfo.descriptorCount = 1 ;
2840
+ writeInfo.descriptorType = descriptorType;
2841
+ VkDescriptorImageInfo imageInfo = {};
2842
+ imageInfo.sampler = static_cast <SamplerStateImpl*>(sampler)->m_sampler ;
2843
+ writeInfo.pImageInfo = &imageInfo;
2844
+ m_renderer->m_api .vkUpdateDescriptorSets (m_renderer->m_device , 1 , &writeInfo, 0 , nullptr );
2766
2845
2767
2846
m_boundObjects[boundObjectIndex] = dynamic_cast <RefObject*>(sampler);
2768
2847
}
@@ -2885,9 +2964,6 @@ Result VKRenderer::createGraphicsPipelineState(const GraphicsPipelineStateDesc&
2885
2964
auto pipelineLayoutImpl = (PipelineLayoutImpl*) desc.pipelineLayout ;
2886
2965
auto inputLayoutImpl = (InputLayoutImpl*) desc.inputLayout ;
2887
2966
2888
- const int width = int (desc.framebufferWidth );
2889
- const int height = int (desc.framebufferHeight );
2890
-
2891
2967
// Shader Stages
2892
2968
//
2893
2969
// Currently only handles vertex/fragment.
@@ -2932,14 +3008,16 @@ Result VKRenderer::createGraphicsPipelineState(const GraphicsPipelineStateDesc&
2932
3008
VkViewport viewport = {};
2933
3009
viewport.x = 0 .0f ;
2934
3010
viewport.y = 0 .0f ;
2935
- viewport.width = (float )width;
2936
- viewport.height = (float )height;
3011
+ // We are using dynamic viewport and scissor state.
3012
+ // Here we specify an arbitrary size, actual viewport will be set at `beginRenderPass` time.
3013
+ viewport.width = 16 .0f ;
3014
+ viewport.height = 16 .0f ;
2937
3015
viewport.minDepth = 0 .0f ;
2938
3016
viewport.maxDepth = 1 .0f ;
2939
3017
2940
3018
VkRect2D scissor = {};
2941
3019
scissor.offset = { 0 , 0 };
2942
- scissor.extent = { uint32_t (width ), uint32_t (height ) };
3020
+ scissor.extent = { uint32_t (16 ), uint32_t (16 ) };
2943
3021
2944
3022
VkPipelineViewportStateCreateInfo viewportState = {};
2945
3023
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
@@ -2978,6 +3056,12 @@ Result VKRenderer::createGraphicsPipelineState(const GraphicsPipelineStateDesc&
2978
3056
colorBlending.blendConstants [2 ] = 0 .0f ;
2979
3057
colorBlending.blendConstants [3 ] = 0 .0f ;
2980
3058
3059
+ VkPipelineDynamicStateCreateInfo dynamicStateInfo = {};
3060
+ dynamicStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
3061
+ dynamicStateInfo.dynamicStateCount = 2 ;
3062
+ VkDynamicState dynamicStates[] = { VK_DYNAMIC_STATE_VIEWPORT , VK_DYNAMIC_STATE_SCISSOR};
3063
+ dynamicStateInfo.pDynamicStates = dynamicStates;
3064
+
2981
3065
VkGraphicsPipelineCreateInfo pipelineInfo = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
2982
3066
2983
3067
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
@@ -2993,6 +3077,7 @@ Result VKRenderer::createGraphicsPipelineState(const GraphicsPipelineStateDesc&
2993
3077
pipelineInfo.renderPass = m_renderPass;
2994
3078
pipelineInfo.subpass = 0 ;
2995
3079
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
3080
+ pipelineInfo.pDynamicState = &dynamicStateInfo;
2996
3081
2997
3082
VkPipeline pipeline = VK_NULL_HANDLE;
2998
3083
SLANG_VK_CHECK (m_api.vkCreateGraphicsPipelines (m_device, pipelineCache, 1 , &pipelineInfo, nullptr , &pipeline));
0 commit comments