Added tutorials "06 - Descriptor Sets" and "07 - Uniform Buffers". Added functions for projection matrices generation to Tools. Added function for loading images from files (with stb_image library). Updated README.md file with description of Tutorial06.

This commit is contained in:
plapins
2016-06-22 23:23:22 +02:00
parent 37d04f3be5
commit c064ec5a4a
26 changed files with 10307 additions and 6 deletions

View File

@@ -68,14 +68,15 @@ set( BASIC_SHARED_SOURCE_FILES
"Common/OperatingSystem.cpp"
"Common/VulkanFunctions.cpp" )
set( VULKAN_HEADER_FILES
set( EXTERNAL_HEADER_FILES
"Include/vk_platform.h"
"Include/vulkan.h" )
"Include/vulkan.h"
"Include/stb_image.h" )
set( ALL_BASIC_SHARED_FILES
${BASIC_SHARED_HEADER_FILES}
${BASIC_SHARED_SOURCE_FILES}
${VULKAN_HEADER_FILES} )
${EXTERNAL_HEADER_FILES} )
set( ADVANCED_SHARED_HEADER_FILES
"Common/Tools.h"
@@ -98,7 +99,7 @@ set( TUTORIAL_SOURCE_FILES REGULAR_EXPRESSION
source_group( "Header Files\\Common" FILES ${BASIC_SHARED_HEADER_FILES} )
source_group( "Source Files\\Common" FILES ${BASIC_SHARED_SOURCE_FILES} )
source_group( "Header Files\\Include" FILES ${VULKAN_HEADER_FILES} )
source_group( "Header Files\\Include" FILES ${EXTERNAL_HEADER_FILES} )
source_group( "Header Files\\Common" FILES ${ADVANCED_SHARED_HEADER_FILES} )
source_group( "Source Files\\Common" FILES ${ADVANCED_SHARED_SOURCE_FILES} )
source_group( "Header Files" FILES ${TUTORIAL_HEADER_FILES} )
@@ -134,3 +135,15 @@ add_executable( "05" "Staging_Resources"
Tutorial05/Tutorial05.h
Tutorial05/main.cpp
Tutorial05/Tutorial05.cpp )
add_executable( "06" "Descriptor_Sets"
${ALL_BASIC_AND_ADVANCED_SHARED_FILES}
Tutorial06/Tutorial06.h
Tutorial06/main.cpp
Tutorial06/Tutorial06.cpp )
add_executable( "07" "Uniform_Buffers"
${ALL_BASIC_AND_ADVANCED_SHARED_FILES}
Tutorial07/Tutorial07.h
Tutorial07/main.cpp
Tutorial07/Tutorial07.cpp )

View File

@@ -160,4 +160,20 @@ VK_DEVICE_LEVEL_FUNCTION( vkDestroyFence )
// Tutorial 05
VK_DEVICE_LEVEL_FUNCTION( vkCmdCopyBuffer )
// Tutorial 06
VK_DEVICE_LEVEL_FUNCTION( vkCreateImage )
VK_DEVICE_LEVEL_FUNCTION( vkGetImageMemoryRequirements )
VK_DEVICE_LEVEL_FUNCTION( vkBindImageMemory )
VK_DEVICE_LEVEL_FUNCTION( vkCreateSampler )
VK_DEVICE_LEVEL_FUNCTION( vkCmdCopyBufferToImage )
VK_DEVICE_LEVEL_FUNCTION( vkCreateDescriptorSetLayout )
VK_DEVICE_LEVEL_FUNCTION( vkCreateDescriptorPool )
VK_DEVICE_LEVEL_FUNCTION( vkAllocateDescriptorSets )
VK_DEVICE_LEVEL_FUNCTION( vkUpdateDescriptorSets )
VK_DEVICE_LEVEL_FUNCTION( vkCmdBindDescriptorSets )
VK_DEVICE_LEVEL_FUNCTION( vkDestroyDescriptorPool )
VK_DEVICE_LEVEL_FUNCTION( vkDestroyDescriptorSetLayout )
VK_DEVICE_LEVEL_FUNCTION( vkDestroySampler )
VK_DEVICE_LEVEL_FUNCTION( vkDestroyImage )
#undef VK_DEVICE_LEVEL_FUNCTION

View File

@@ -8,14 +8,22 @@
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#include <cmath>
#include <fstream>
#include <iostream>
#include "Tools.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
namespace ApiWithoutSecrets {
namespace Tools {
// ************************************************************ //
// GetBinaryFileContents //
// //
// Function reading binary contents of a file //
// ************************************************************ //
std::vector<char> GetBinaryFileContents( std::string const &filename ) {
std::ifstream file( filename, std::ios::binary );
@@ -37,6 +45,108 @@ namespace ApiWithoutSecrets {
return result;
}
// ************************************************************ //
// GetImageData //
// //
// Function loading image (texture) data from a specified file //
// ************************************************************ //
std::vector<char> GetImageData( std::string const &filename, int requested_components, int *width, int *height, int *components, int *data_size ) {
std::vector<char> file_data = Tools::GetBinaryFileContents( filename );
if( file_data.size() == 0 ) {
return std::vector<char>();
}
int tmp_width = 0, tmp_height = 0, tmp_components = 0;
unsigned char *image_data = stbi_load_from_memory( reinterpret_cast<unsigned char*>(&file_data[0]), static_cast<int>(file_data.size()), &tmp_width, &tmp_height, &tmp_components, requested_components );
if( (image_data == nullptr) ||
(tmp_width <= 0) ||
(tmp_height <= 0) ||
(tmp_components <= 0) ) {
std::cout << "Could not read image data!" << std::endl;
return std::vector<char>();
}
int size = (tmp_width) * (tmp_height) * (requested_components <= 0 ? tmp_components : requested_components);
if( data_size ) {
*data_size = size;
}
if( width ) {
*width = tmp_width;
}
if( height ) {
*height = tmp_height;
}
if( components ) {
*components = tmp_components;
}
std::vector<char> output(size);
memcpy( &output[0], image_data, size );
stbi_image_free( image_data );
return output;
}
// ************************************************************ //
// GetPerspectiveProjectionMatrix //
// //
// Function calculating perspective projection matrix //
// ************************************************************ //
std::array<float, 16> GetPerspectiveProjectionMatrix( float const aspect_ratio, float const field_of_view, float const near_clip, float const far_clip ) {
float f = 1.0f / std::tan( field_of_view * 0.5f * 0.01745329251994329576923690768489f );
return {
f / aspect_ratio,
0.0f,
0.0f,
0.0f,
0.0f,
f,
0.0f,
0.0f,
0.0f,
0.0f,
(near_clip + far_clip) / (near_clip - far_clip),
-1.0f,
0.0f,
0.0f,
(2.0f * near_clip * far_clip) / (near_clip - far_clip),
0.0f
};
}
// ************************************************************ //
// GetOrthographicsProjectionMatrix //
// //
// Function calculating orthographic projection matrix //
// ************************************************************ //
std::array<float, 16> GetOrthographicProjectionMatrix( float const left_plane, float const right_plane, float const top_plane, float const bottom_plane, float const near_plane, float const far_plane ) {
return {
2.0f / (right_plane - left_plane),
0.0f,
0.0f,
0.0f,
0.0f,
2.0f / (bottom_plane - top_plane),
0.0f,
0.0f,
0.0f,
0.0f,
-2.0f / (far_plane - near_plane),
0.0f,
-(right_plane + left_plane) / (right_plane - left_plane),
-(bottom_plane + top_plane) / (bottom_plane - top_plane),
-(far_plane + near_plane) / (far_plane - near_plane),
1.0f
};
}
} // namespace Tools
} // namespace ApiWithoutSecrets

View File

@@ -13,6 +13,7 @@
#include <string>
#include <vector>
#include <array>
#include "vulkan.h"
namespace ApiWithoutSecrets {
@@ -83,6 +84,27 @@ namespace ApiWithoutSecrets {
// ************************************************************ //
std::vector<char> GetBinaryFileContents( std::string const &filename );
// ************************************************************ //
// GetImageData //
// //
// Function loading image (texture) data from a specified file //
// ************************************************************ //
std::vector<char> GetImageData( std::string const &filename, int requested_components, int *width, int *height, int *components, int *data_size );
// ************************************************************ //
// GetPerspectiveProjectionMatrix //
// //
// Function calculating perspective projection matrix //
// ************************************************************ //
std::array<float, 16> GetPerspectiveProjectionMatrix( float const aspect_ratio, float const field_of_view, float const near_clip, float const far_clip );
// ************************************************************ //
// GetOrthographicsProjectionMatrix //
// //
// Function calculating orthographic projection matrix //
// ************************************************************ //
std::array<float, 16> GetOrthographicProjectionMatrix( float const left_plane, float const right_plane, float const top_plane, float const bottom_plane, float const near_plane, float const far_plane );
} // namespace Tools
} // namespace ApiWithoutSecrets

6755
Project/Include/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -0,0 +1,46 @@
Tutorial06/Data06/shader.frag
Warning, version 430 is not yet complete; most version-specific features are present, but some are missing.
Linked fragment stage:
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 20
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "main" 9 17
ExecutionMode 4 OriginLowerLeft
Source GLSL 430
Name 4 "main"
Name 9 "o_Color"
Name 13 "u_Texture"
Name 17 "v_Texcoord"
Decorate 9(o_Color) Location 0
Decorate 13(u_Texture) DescriptorSet 0
Decorate 13(u_Texture) Binding 0
Decorate 17(v_Texcoord) Location 0
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
7: TypeVector 6(float) 4
8: TypePointer Output 7(fvec4)
9(o_Color): 8(ptr) Variable Output
10: TypeImage 6(float) 2D sampled format:Unknown
11: TypeSampledImage 10
12: TypePointer UniformConstant 11
13(u_Texture): 12(ptr) Variable UniformConstant
15: TypeVector 6(float) 2
16: TypePointer Input 15(fvec2)
17(v_Texcoord): 16(ptr) Variable Input
4(main): 2 Function None 3
5: Label
14: 11 Load 13(u_Texture)
18: 15(fvec2) Load 17(v_Texcoord)
19: 7(fvec4) ImageSampleImplicitLod 14 18
Store 9(o_Color) 19
Return
FunctionEnd

View File

@@ -0,0 +1,21 @@
// Copyright 2016 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#version 430
layout(set=0, binding=0) uniform sampler2D u_Texture;
layout(location = 0) in vec2 v_Texcoord;
layout(location = 0) out vec4 o_Color;
void main() {
o_Color = texture( u_Texture, v_Texcoord );
}

View File

@@ -0,0 +1,26 @@
// Copyright 2016 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#version 450
layout(location = 0) in vec4 i_Position;
layout(location = 1) in vec2 i_Texcoord;
out gl_PerVertex
{
vec4 gl_Position;
};
layout(location = 0) out vec2 v_Texcoord;
void main() {
gl_Position = i_Position;
v_Texcoord = i_Texcoord;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 KiB

Binary file not shown.

View File

@@ -0,0 +1,54 @@
Tutorial06/Data06/shader.vert
Warning, version 450 is not yet complete; most version-specific features are present, but some are missing.
Linked vertex stage:
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 24
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Vertex 4 "main" 10 14 20 22
Source GLSL 450
Name 4 "main"
Name 8 "gl_PerVertex"
MemberName 8(gl_PerVertex) 0 "gl_Position"
Name 10 ""
Name 14 "i_Position"
Name 20 "v_Texcoord"
Name 22 "i_Texcoord"
MemberDecorate 8(gl_PerVertex) 0 BuiltIn Position
Decorate 8(gl_PerVertex) Block
Decorate 14(i_Position) Location 0
Decorate 20(v_Texcoord) Location 0
Decorate 22(i_Texcoord) Location 1
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
7: TypeVector 6(float) 4
8(gl_PerVertex): TypeStruct 7(fvec4)
9: TypePointer Output 8(gl_PerVertex)
10: 9(ptr) Variable Output
11: TypeInt 32 1
12: 11(int) Constant 0
13: TypePointer Input 7(fvec4)
14(i_Position): 13(ptr) Variable Input
16: TypePointer Output 7(fvec4)
18: TypeVector 6(float) 2
19: TypePointer Output 18(fvec2)
20(v_Texcoord): 19(ptr) Variable Output
21: TypePointer Input 18(fvec2)
22(i_Texcoord): 21(ptr) Variable Input
4(main): 2 Function None 3
5: Label
15: 7(fvec4) Load 14(i_Position)
17: 16(ptr) AccessChain 10 12
Store 17 15
23: 18(fvec2) Load 22(i_Texcoord)
Store 20(v_Texcoord) 23
Return
FunctionEnd

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,167 @@
// Copyright 2016 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#if !defined(TUTORIAL_06_HEADER)
#define TUTORIAL_06_HEADER
#include "VulkanCommon.h"
#include "Tools.h"
namespace ApiWithoutSecrets {
// ************************************************************ //
// BufferParameters //
// //
// Vulkan Buffer's parameters container class //
// ************************************************************ //
struct BufferParameters {
VkBuffer Handle;
VkDeviceMemory Memory;
uint32_t Size;
BufferParameters() :
Handle( VK_NULL_HANDLE ),
Memory( VK_NULL_HANDLE ),
Size( 0 ) {
}
};
// ************************************************************ //
// DescriptorParameters //
// //
// Container class for descriptor related resources //
// ************************************************************ //
struct DescriptorSetParameters {
VkDescriptorPool Pool;
VkDescriptorSetLayout Layout;
VkDescriptorSet Handle;
DescriptorSetParameters() :
Pool( VK_NULL_HANDLE ),
Layout( VK_NULL_HANDLE ),
Handle( VK_NULL_HANDLE ) {
}
};
// ************************************************************ //
// VertexData //
// //
// Struct describing data type and format of vertex attributes //
// ************************************************************ //
struct VertexData {
float x, y, z, w;
float u, v;
};
// ************************************************************ //
// RenderingResourcesData //
// //
// Struct containing data used during rendering process //
// ************************************************************ //
struct RenderingResourcesData {
VkFramebuffer Framebuffer;
VkCommandBuffer CommandBuffer;
VkSemaphore ImageAvailableSemaphore;
VkSemaphore FinishedRenderingSemaphore;
VkFence Fence;
RenderingResourcesData() :
Framebuffer( VK_NULL_HANDLE ),
CommandBuffer( VK_NULL_HANDLE ),
ImageAvailableSemaphore( VK_NULL_HANDLE ),
FinishedRenderingSemaphore( VK_NULL_HANDLE ),
Fence( VK_NULL_HANDLE ) {
}
};
// ************************************************************ //
// VulkanTutorial04Parameters //
// //
// Vulkan specific parameters //
// ************************************************************ //
struct VulkanTutorial06Parameters {
VkRenderPass RenderPass;
ImageParameters Image;
DescriptorSetParameters DescriptorSet;
VkPipelineLayout PipelineLayout;
VkPipeline GraphicsPipeline;
BufferParameters VertexBuffer;
BufferParameters StagingBuffer;
VkCommandPool CommandPool;
std::vector<RenderingResourcesData> RenderingResources;
static const size_t ResourcesCount = 3;
VulkanTutorial06Parameters() :
RenderPass( VK_NULL_HANDLE ),
Image(),
DescriptorSet(),
PipelineLayout(),
GraphicsPipeline( VK_NULL_HANDLE ),
VertexBuffer(),
StagingBuffer(),
CommandPool( VK_NULL_HANDLE ),
RenderingResources( ResourcesCount ) {
}
};
// ************************************************************ //
// Tutorial04 //
// //
// Class for presenting Vulkan usage topics //
// ************************************************************ //
class Tutorial06 : public VulkanCommon {
public:
Tutorial06();
~Tutorial06();
bool CreateRenderingResources();
bool CreateStagingBuffer();
bool CreateTexture();
bool CreateDescriptorSetLayout();
bool CreateDescriptorPool();
bool AllocateDescriptorSet();
bool UpdateDescriptorSet();
bool CreateRenderPass();
bool CreatePipelineLayout();
bool CreatePipeline();
bool CreateVertexBuffer();
bool Draw() override;
private:
VulkanTutorial06Parameters Vulkan;
bool CreateCommandBuffers();
bool CreateCommandPool(uint32_t queue_family_index, VkCommandPool *pool);
bool AllocateCommandBuffers(VkCommandPool pool, uint32_t count, VkCommandBuffer *command_buffers);
bool CreateSemaphores();
bool CreateFences();
bool CreateBuffer(VkBufferUsageFlags usage, VkMemoryPropertyFlagBits memoryProperty, BufferParameters &buffer);
bool AllocateBufferMemory(VkBuffer buffer, VkMemoryPropertyFlagBits property, VkDeviceMemory *memory);
bool CreateImage( uint32_t width, uint32_t height, VkImage *image );
bool AllocateImageMemory( VkImage image, VkMemoryPropertyFlagBits property, VkDeviceMemory *memory );
bool CreateImageView( ImageParameters &image_parameters );
bool CreateSampler( VkSampler *sampler );
bool CopyTextureData( char *texture_data, uint32_t data_size, uint32_t width, uint32_t height );
Tools::AutoDeleter<VkShaderModule, PFN_vkDestroyShaderModule> CreateShaderModule( const char* filename );
const std::vector<float>& GetVertexData() const;
bool CopyVertexData();
bool PrepareFrame( VkCommandBuffer command_buffer, const ImageParameters &image_parameters, VkFramebuffer &framebuffer );
bool CreateFramebuffer( VkFramebuffer &framebuffer, VkImageView image_view );
void DestroyBuffer( BufferParameters& buffer );
bool ChildOnWindowSizeChanged() override;
void ChildClear() override;
};
} // namespace ApiWithoutSecrets
#endif // TUTORIAL_06_HEADER

View File

@@ -0,0 +1,68 @@
// Copyright 2016 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#include "Tutorial06.h"
int main( int argc, char **argv ) {
ApiWithoutSecrets::OS::Window window;
ApiWithoutSecrets::Tutorial06 tutorial06;
// Window creation
if( !window.Create( "06 - Descriptor Sets" ) ) {
return -1;
}
// Vulkan preparations and initialization
if( !tutorial06.PrepareVulkan( window.GetParameters() ) ) {
return -1;
}
// Tutorial 06
if( !tutorial06.CreateRenderingResources() ) {
return -1;
}
if( !tutorial06.CreateStagingBuffer() ) {
return -1;
}
if( !tutorial06.CreateTexture() ) {
return -1;
}
if( !tutorial06.CreateDescriptorSetLayout() ) {
return -1;
}
if( !tutorial06.CreateDescriptorPool() ) {
return -1;
}
if( !tutorial06.AllocateDescriptorSet() ) {
return -1;
}
if( !tutorial06.UpdateDescriptorSet() ) {
return -1;
}
if( !tutorial06.CreateRenderPass() ) {
return -1;
}
if( !tutorial06.CreatePipelineLayout() ) {
return -1;
}
if( !tutorial06.CreatePipeline() ) {
return -1;
}
if( !tutorial06.CreateVertexBuffer() ) {
return -1;
}
// Rendering loop
if( !window.RenderingLoop( tutorial06 ) ) {
return -1;
}
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,46 @@
Tutorial07/Data07/shader.frag
Warning, version 430 is not yet complete; most version-specific features are present, but some are missing.
Linked fragment stage:
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 20
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "main" 9 17
ExecutionMode 4 OriginLowerLeft
Source GLSL 430
Name 4 "main"
Name 9 "o_Color"
Name 13 "u_Texture"
Name 17 "v_Texcoord"
Decorate 9(o_Color) Location 0
Decorate 13(u_Texture) DescriptorSet 0
Decorate 13(u_Texture) Binding 0
Decorate 17(v_Texcoord) Location 0
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
7: TypeVector 6(float) 4
8: TypePointer Output 7(fvec4)
9(o_Color): 8(ptr) Variable Output
10: TypeImage 6(float) 2D sampled format:Unknown
11: TypeSampledImage 10
12: TypePointer UniformConstant 11
13(u_Texture): 12(ptr) Variable UniformConstant
15: TypeVector 6(float) 2
16: TypePointer Input 15(fvec2)
17(v_Texcoord): 16(ptr) Variable Input
4(main): 2 Function None 3
5: Label
14: 11 Load 13(u_Texture)
18: 15(fvec2) Load 17(v_Texcoord)
19: 7(fvec4) ImageSampleImplicitLod 14 18
Store 9(o_Color) 19
Return
FunctionEnd

View File

@@ -0,0 +1,21 @@
// Copyright 2016 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#version 430
layout(set=0, binding=0) uniform sampler2D u_Texture;
layout(location = 0) in vec2 v_Texcoord;
layout(location = 0) out vec4 o_Color;
void main() {
o_Color = texture( u_Texture, v_Texcoord );
}

View File

@@ -0,0 +1,30 @@
// Copyright 2016 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#version 450
layout(set=0, binding=1) uniform u_UniformBuffer {
mat4 u_ProjectionMatrix;
};
layout(location = 0) in vec4 i_Position;
layout(location = 1) in vec2 i_Texcoord;
out gl_PerVertex
{
vec4 gl_Position;
};
layout(location = 0) out vec2 v_Texcoord;
void main() {
gl_Position = u_ProjectionMatrix * i_Position;
v_Texcoord = i_Texcoord;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 KiB

Binary file not shown.

View File

@@ -0,0 +1,71 @@
Tutorial07/Data07/shader.vert
Warning, version 450 is not yet complete; most version-specific features are present, but some are missing.
Linked vertex stage:
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 32
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Vertex 4 "main" 10 21 28 30
Source GLSL 450
Name 4 "main"
Name 8 "gl_PerVertex"
MemberName 8(gl_PerVertex) 0 "gl_Position"
Name 10 ""
Name 14 "u_UniformBuffer"
MemberName 14(u_UniformBuffer) 0 "u_ProjectionMatrix"
Name 16 ""
Name 21 "i_Position"
Name 28 "v_Texcoord"
Name 30 "i_Texcoord"
MemberDecorate 8(gl_PerVertex) 0 BuiltIn Position
Decorate 8(gl_PerVertex) Block
MemberDecorate 14(u_UniformBuffer) 0 ColMajor
MemberDecorate 14(u_UniformBuffer) 0 Offset 0
MemberDecorate 14(u_UniformBuffer) 0 MatrixStride 16
Decorate 14(u_UniformBuffer) Block
Decorate 16 DescriptorSet 0
Decorate 16 Binding 1
Decorate 21(i_Position) Location 0
Decorate 28(v_Texcoord) Location 0
Decorate 30(i_Texcoord) Location 1
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
7: TypeVector 6(float) 4
8(gl_PerVertex): TypeStruct 7(fvec4)
9: TypePointer Output 8(gl_PerVertex)
10: 9(ptr) Variable Output
11: TypeInt 32 1
12: 11(int) Constant 0
13: TypeMatrix 7(fvec4) 4
14(u_UniformBuffer): TypeStruct 13
15: TypePointer Uniform 14(u_UniformBuffer)
16: 15(ptr) Variable Uniform
17: TypePointer Uniform 13
20: TypePointer Input 7(fvec4)
21(i_Position): 20(ptr) Variable Input
24: TypePointer Output 7(fvec4)
26: TypeVector 6(float) 2
27: TypePointer Output 26(fvec2)
28(v_Texcoord): 27(ptr) Variable Output
29: TypePointer Input 26(fvec2)
30(i_Texcoord): 29(ptr) Variable Input
4(main): 2 Function None 3
5: Label
18: 17(ptr) AccessChain 16 12
19: 13 Load 18
22: 7(fvec4) Load 21(i_Position)
23: 7(fvec4) MatrixTimesVector 19 22
25: 24(ptr) AccessChain 10 12
Store 25 23
31: 26(fvec2) Load 30(i_Texcoord)
Store 28(v_Texcoord) 31
Return
FunctionEnd

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,171 @@
// Copyright 2016 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#if !defined(TUTORIAL_07_HEADER)
#define TUTORIAL_07_HEADER
#include "VulkanCommon.h"
#include "Tools.h"
namespace ApiWithoutSecrets {
// ************************************************************ //
// BufferParameters //
// //
// Vulkan Buffer's parameters container class //
// ************************************************************ //
struct BufferParameters {
VkBuffer Handle;
VkDeviceMemory Memory;
uint32_t Size;
BufferParameters() :
Handle( VK_NULL_HANDLE ),
Memory( VK_NULL_HANDLE ),
Size( 0 ) {
}
};
// ************************************************************ //
// DescriptorParameters //
// //
// Container class for descriptor related resources //
// ************************************************************ //
struct DescriptorSetParameters {
VkDescriptorPool Pool;
VkDescriptorSetLayout Layout;
VkDescriptorSet Handle;
DescriptorSetParameters() :
Pool( VK_NULL_HANDLE ),
Layout( VK_NULL_HANDLE ),
Handle( VK_NULL_HANDLE ) {
}
};
// ************************************************************ //
// VertexData //
// //
// Struct describing data type and format of vertex attributes //
// ************************************************************ //
struct VertexData {
float x, y, z, w;
float u, v;
};
// ************************************************************ //
// RenderingResourcesData //
// //
// Struct containing data used during rendering process //
// ************************************************************ //
struct RenderingResourcesData {
VkFramebuffer Framebuffer;
VkCommandBuffer CommandBuffer;
VkSemaphore ImageAvailableSemaphore;
VkSemaphore FinishedRenderingSemaphore;
VkFence Fence;
RenderingResourcesData() :
Framebuffer( VK_NULL_HANDLE ),
CommandBuffer( VK_NULL_HANDLE ),
ImageAvailableSemaphore( VK_NULL_HANDLE ),
FinishedRenderingSemaphore( VK_NULL_HANDLE ),
Fence( VK_NULL_HANDLE ) {
}
};
// ************************************************************ //
// VulkanTutorial04Parameters //
// //
// Vulkan specific parameters //
// ************************************************************ //
struct VulkanTutorial07Parameters {
VkRenderPass RenderPass;
ImageParameters Image;
BufferParameters UniformBuffer;
DescriptorSetParameters DescriptorSet;
VkPipelineLayout PipelineLayout;
VkPipeline GraphicsPipeline;
BufferParameters VertexBuffer;
BufferParameters StagingBuffer;
VkCommandPool CommandPool;
std::vector<RenderingResourcesData> RenderingResources;
static const size_t ResourcesCount = 3;
VulkanTutorial07Parameters() :
RenderPass( VK_NULL_HANDLE ),
Image(),
DescriptorSet(),
PipelineLayout(),
GraphicsPipeline( VK_NULL_HANDLE ),
VertexBuffer(),
StagingBuffer(),
CommandPool( VK_NULL_HANDLE ),
RenderingResources( ResourcesCount ) {
}
};
// ************************************************************ //
// Tutorial04 //
// //
// Class for presenting Vulkan usage topics //
// ************************************************************ //
class Tutorial07 : public VulkanCommon {
public:
Tutorial07();
~Tutorial07();
bool CreateRenderingResources();
bool CreateStagingBuffer();
bool CreateTexture();
bool CreateUniformBuffer();
bool CreateDescriptorSetLayout();
bool CreateDescriptorPool();
bool AllocateDescriptorSet();
bool UpdateDescriptorSet();
bool CreateRenderPass();
bool CreatePipelineLayout();
bool CreatePipeline();
bool CreateVertexBuffer();
bool Draw() override;
private:
VulkanTutorial07Parameters Vulkan;
bool CreateCommandBuffers();
bool CreateCommandPool(uint32_t queue_family_index, VkCommandPool *pool);
bool AllocateCommandBuffers(VkCommandPool pool, uint32_t count, VkCommandBuffer *command_buffers);
bool CreateSemaphores();
bool CreateFences();
bool CreateBuffer(VkBufferUsageFlags usage, VkMemoryPropertyFlagBits memoryProperty, BufferParameters &buffer);
bool AllocateBufferMemory(VkBuffer buffer, VkMemoryPropertyFlagBits property, VkDeviceMemory *memory);
bool CreateImage( uint32_t width, uint32_t height, VkImage *image );
bool AllocateImageMemory( VkImage image, VkMemoryPropertyFlagBits property, VkDeviceMemory *memory );
bool CreateImageView( ImageParameters &image_parameters );
bool CreateSampler( VkSampler *sampler );
bool CopyTextureData( char *texture_data, uint32_t data_size, uint32_t width, uint32_t height );
const std::array<float, 16> GetUniformBufferData() const;
bool CopyUniformBufferData();
Tools::AutoDeleter<VkShaderModule, PFN_vkDestroyShaderModule> CreateShaderModule( const char* filename );
const std::vector<float>& GetVertexData() const;
bool CopyVertexData();
bool PrepareFrame( VkCommandBuffer command_buffer, const ImageParameters &image_parameters, VkFramebuffer &framebuffer );
bool CreateFramebuffer( VkFramebuffer &framebuffer, VkImageView image_view );
void DestroyBuffer( BufferParameters& buffer );
bool ChildOnWindowSizeChanged() override;
void ChildClear() override;
};
} // namespace ApiWithoutSecrets
#endif // TUTORIAL_07_HEADER

View File

@@ -0,0 +1,71 @@
// Copyright 2016 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#include "Tutorial07.h"
int main( int argc, char **argv ) {
ApiWithoutSecrets::OS::Window window;
ApiWithoutSecrets::Tutorial07 tutorial07;
// Window creation
if( !window.Create( "07 - Uniform Buffers" ) ) {
return -1;
}
// Vulkan preparations and initialization
if( !tutorial07.PrepareVulkan( window.GetParameters() ) ) {
return -1;
}
// Tutorial 06
if( !tutorial07.CreateRenderingResources() ) {
return -1;
}
if( !tutorial07.CreateStagingBuffer() ) {
return -1;
}
if( !tutorial07.CreateTexture() ) {
return -1;
}
if( !tutorial07.CreateUniformBuffer() ) {
return -1;
}
if( !tutorial07.CreateDescriptorSetLayout() ) {
return -1;
}
if( !tutorial07.CreateDescriptorPool() ) {
return -1;
}
if( !tutorial07.AllocateDescriptorSet() ) {
return -1;
}
if( !tutorial07.UpdateDescriptorSet() ) {
return -1;
}
if( !tutorial07.CreateRenderPass() ) {
return -1;
}
if( !tutorial07.CreatePipelineLayout() ) {
return -1;
}
if( !tutorial07.CreatePipeline() ) {
return -1;
}
if( !tutorial07.CreateVertexBuffer() ) {
return -1;
}
// Rendering loop
if( !window.RenderingLoop( tutorial07 ) ) {
return -1;
}
return 0;
}

View File

@@ -58,3 +58,12 @@ This tutorial shows how to set up vertex attributes and bind buffer with a verte
#### Copying data between buffers
In this example staging resources are presented. They are used as an intermediate resources for copying data between CPU and GPU. This way, resources involved in rendering can be bound only to a device local (very fast) memory.
<hr>
### [06 - Descriptor Sets](./Project/Tutorial06/)
<img src="./Document/Images/06 - Descriptor Sets.png" height="96px" align="right">
#### Using textures in shaders
This tutorial shows what resources are needed and how they should be prepared to be able to use textures (or other shader resources) in shader programs.