SpiecsEngine
 
Loading...
Searching...
No Matches
Texture2D.cpp
Go to the documentation of this file.
1/**
2* @file Texture2D.cpp.
3* @brief The Texture2D Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
8#include "Texture2D.h"
9#include "Render/Vulkan/VulkanImage.h"
10#include "Render/Vulkan/VulkanRenderBackend.h"
11#include "Render/RendererResource/RendererResource.h"
12
13namespace Spices {
14
16 {
17 m_ResourcePath = "NONE";
18
19 uint32_t w = std::max(1.0f, info.width * info.sizeScale);
20 uint32_t h = std::max(1.0f, info.height * info.sizeScale);
21
22 if (!info.isDepthResource)
23 {
24 m_Resource = std::make_shared<VulkanImage>(
25 VulkanRenderBackend::GetState(),
26 info.name,
27 VK_IMAGE_TYPE_2D,
28 w,
29 h,
30 1,
31 info.description.samples,
32 info.description.format,
33 VK_IMAGE_TILING_OPTIMAL,
34 info.usage |
35 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | // Can be used for ColorAttachment.
36 VK_IMAGE_USAGE_SAMPLED_BIT | // Can be used for ShaderRead.
37 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | // Can be used for TransferSrc. /*todo: Configurable*/
38 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT | // Can be used for InputAttachment.
39 VK_IMAGE_USAGE_STORAGE_BIT, // Can be used for StorageTexture.
40 0,
41 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
42 info.mipLevel
43 );
44
45 auto resourceptr = GetResource<VulkanImage>();
46 resourceptr->CreateImageView(info.description.format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT, true);
47
48 if (info.usage & VK_IMAGE_USAGE_STORAGE_BIT)
49 {
50 resourceptr->TransitionImageLayout(
51 info.description.format,
52 VK_IMAGE_LAYOUT_UNDEFINED,
53 VK_IMAGE_LAYOUT_GENERAL
54 );
55 }
56
57 resourceptr->CreateSampler();
58 }
59 else
60 {
61 m_Resource = std::make_shared<VulkanImage>(
62 VulkanRenderBackend::GetState(),
63 info.name,
64 VK_IMAGE_TYPE_2D,
65 w,
66 h,
67 1,
68 info.description.samples,
69 info.description.format,
70 VK_IMAGE_TILING_OPTIMAL,
71 info.usage |
72 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | // Can be used for DepthAttachment.
73 VK_IMAGE_USAGE_SAMPLED_BIT | // Can be used for ShaderRead.
74 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT, // Can be used for InputAttachment.
75 0,
76 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
77 1
78 );
79
80 auto resourceptr = GetResource<VulkanImage>();
81 resourceptr->CreateImageView(info.description.format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT);
82
83 resourceptr->TransitionImageLayout(
84 info.description.format,
85 VK_IMAGE_LAYOUT_UNDEFINED,
86 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
87 );
88
89 resourceptr->CreateSampler();
90 }
91 }
92
93 Texture2D::Texture2D(const std::string& path)
94 {
95 m_ResourcePath = path;
97 }
98}
Texture2D(const RendererResourceCreateInfo &info)
Constructor Function. Used for create render resource.
Definition Texture2D.cpp:15
Texture2D(const std::string &path)
Constructor Function. Init class variable, load date immediately. Usually call it.
Definition Texture2D.cpp:93
Texture2D Class. This class defines the basic behaves of texture2D.
Definition Texture2D.h:20
static void Load(const std::string &fileName, Texture2D *outTexture)
Load image to a Texture2D object.
TextureLoader Class. This class only defines static function for load data from image file.
std::string m_ResourcePath
Texture's path in disk.
Definition Texture.h:74
uint32_t height
Texture' height..
bool isDepthResource
True if this resource is a DepthResource.
float sizeScale
Texture's size scale.
This struct defines the data used to create a texture2d. From render pass.