Skip to content

Commit 16db27a

Browse files
author
tuxalin
committed
-fix for cube and array textures -fix for correct image type -expose pipeline bind
1 parent 2cc7e82 commit 16db27a

File tree

6 files changed

+64
-25
lines changed

6 files changed

+64
-25
lines changed

examples/common/Camera.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class Camera
5353
float sensitivity;
5454
float zoom;
5555

56-
// Constructor with vectors
5756
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
5857
float yaw = cYaw, float pitch = cPitch)
59-
: front(glm::vec3(0.0f, 0.0f, -1.0f))
58+
: center(glm::vec3(0.0f))
59+
, front(glm::vec3(0.0f, 0.0f, -1.0f))
6060
, speed(cSpeed)
6161
, sensitivity(cSensitivity)
6262
, zoom(cZoom)
@@ -67,9 +67,10 @@ class Camera
6767
this->pitch = pitch;
6868
updateCameraVectors();
6969
}
70-
// Constructor with scalar values
70+
7171
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch)
72-
: front(glm::vec3(0.0f, 0.0f, -1.0f))
72+
: center(glm::vec3(0.0f))
73+
, front(glm::vec3(0.0f, 0.0f, -1.0f))
7374
, speed(cSpeed)
7475
, sensitivity(cSensitivity)
7576
, zoom(cZoom)

include/ri/DeviceContext.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ inline TextureProperties DeviceContext::textureProperties(ColorFormat format, Te
106106
uint32_t flags) const
107107
{
108108
VkImageFormatProperties props;
109-
vkGetPhysicalDeviceImageFormatProperties(m_physicalDevice, (VkFormat)format, (VkImageType)type,
110-
(VkImageTiling)tiling, flags, 0, &props);
109+
VkImageType imageType = detail::getImageType(type.get());
110+
vkGetPhysicalDeviceImageFormatProperties(m_physicalDevice, (VkFormat)format, imageType, (VkImageTiling)tiling,
111+
flags, 0, &props);
111112
return props;
112113
}
113114

include/ri/RenderPipeline.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ class RenderPipeline : util::noncopyable, public RenderObject<VkPipeline>
161161
/// @note To use it you Must create the pipeline with specific dynamic states.
162162
DynamicState& dynamicState();
163163

164-
/// @note Also binds the pipeline.
164+
void bind(const CommandBuffer& buffer) const;
165+
/// @note Also binds the pipeline and the default render pass.
165166
void begin(const CommandBuffer& buffer, const RenderTarget& target) const;
167+
/// Ends the default render pass
166168
void end(const CommandBuffer& buffer) const;
167169

168170
void pushConstants(const void* src, ShaderStage stages, size_t offset, size_t size, CommandBuffer& buffer);
@@ -227,7 +229,6 @@ class RenderPipeline : util::noncopyable, public RenderObject<VkPipeline>
227229
, m_scissor(scissor)
228230
{
229231
}
230-
void bind(const CommandBuffer& buffer) const;
231232

232233
static VkPipelineLayout createLayout(const VkDevice device, const CreateParams& params,
233234
const std::vector<VkDescriptorSetLayout>& descriptorLayouts);

include/ri/Texture.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ class Texture : util::noncopyable, public RenderObject<VkImage>
8787
int32_t offsetX = 0, offsetY = 0, offsetZ = 0;
8888
/// @note If zero then will use texture size.
8989
Sizei size;
90-
uint32_t depth = 1;
90+
uint32_t depth = 1;
91+
uint32_t baseArrayLayer = 0;
92+
uint32_t mipLevel = 0;
93+
size_t bufferOffset = 0;
9194

9295
CopyParams()
9396
: oldLayout(TextureLayoutType::eUndefined)
@@ -144,6 +147,7 @@ class Texture : util::noncopyable, public RenderObject<VkImage>
144147
ColorFormat m_format;
145148
Sizei m_size;
146149
uint32_t m_mipLevels;
150+
uint32_t m_arrayLevels;
147151

148152
friend const Texture* detail::createReferenceTexture(VkImage handle, int type, int format, const Sizei& size);
149153
friend detail::TextureDescriptorInfo detail::getTextureDescriptorInfo(const Texture& texture);

include/ri/internal/ri_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace detail
4848
const Texture* createReferenceTexture(VkImage handle, int type, int format, const Sizei& size);
4949
TextureDescriptorInfo getTextureDescriptorInfo(const Texture& texture);
5050
VkImageAspectFlags getImageAspectFlags(VkFormat format);
51+
VkImageType getImageType(int type);
5152

5253
VkPhysicalDevice getDevicePhysicalHandle(const ri::DeviceContext& device);
5354
VkQueue getDeviceQueue(const ri::DeviceContext& device, int deviceOperation);

src/ri/Texture.cpp

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <ri/Texture.h>
33

4+
#include <ri/Buffer.h>
45
#include <ri/CommandBuffer.h>
56
#include <ri/DeviceContext.h>
67

@@ -36,6 +37,25 @@ namespace detail
3637

3738
return flags;
3839
}
40+
41+
VkImageType getImageType(int type)
42+
{
43+
switch (type)
44+
{
45+
case ri::TextureType::e1D:
46+
case ri::TextureType::eArray1D:
47+
return VK_IMAGE_TYPE_1D;
48+
case ri::TextureType::e2D:
49+
case ri::TextureType::eArray2D:
50+
case ri::TextureType::eCube:
51+
return VK_IMAGE_TYPE_2D;
52+
case ri::TextureType::e3D:
53+
return VK_IMAGE_TYPE_3D;
54+
default:
55+
assert(false);
56+
return VK_IMAGE_TYPE_2D;
57+
}
58+
}
3959
}
4060

4161
namespace
@@ -60,9 +80,10 @@ Texture::Texture(const DeviceContext& device, const TextureParams& params)
6080
, m_size(params.size)
6181
, m_mipLevels(params.mipLevels ? params.mipLevels
6282
: (uint32_t)floor(log2(std::max(params.size.width, params.size.height))) + 1)
83+
, m_arrayLevels(m_type == TextureType::eCube ? 6 : params.arrayLevels)
6384
{
6485
#ifndef NDEBUG
65-
TextureProperties props =
86+
const TextureProperties props =
6687
device.textureProperties(params.format, params.type, TextureTiling::eOptimal, params.flags);
6788
assert(props.sampleCounts >= params.samples);
6889
assert(props.maxExtent.width >= params.size.width);
@@ -90,6 +111,7 @@ Texture::Texture(VkImage handle, TextureType type, ColorFormat format, const Siz
90111
, m_type(type)
91112
, m_format(format)
92113
, m_size(size)
114+
, m_arrayLevels(0)
93115
{
94116
}
95117

@@ -113,22 +135,27 @@ void Texture::copy(const Buffer& src, const CopyParams& params, CommandBuffer& c
113135
transitionImageLayout(params.oldLayout, params.transferLayout, commandBuffer);
114136

115137
// copy buffer to image
116-
VkBufferImageCopy region = {};
117-
// TODO: expose these
118-
region.bufferOffset = 0;
138+
139+
assert((m_arrayLevels * m_size.pixelCount() * sizeof(uint32_t)) < src.bytes());
140+
141+
std::vector<VkBufferImageCopy> bufferCopyRegions;
142+
VkBufferImageCopy region = {};
143+
region.bufferOffset = params.bufferOffset;
119144
region.bufferRowLength = 0;
120145
region.bufferImageHeight = 0;
121146
region.imageSubresource.aspectMask = detail::getImageAspectFlags((VkFormat)m_format);
122-
region.imageSubresource.mipLevel = 0;
123-
region.imageSubresource.baseArrayLayer = 0;
124-
region.imageSubresource.layerCount = 1;
147+
region.imageSubresource.mipLevel = params.mipLevel;
148+
region.imageSubresource.baseArrayLayer = params.baseArrayLayer;
149+
region.imageSubresource.layerCount = m_arrayLevels;
150+
const Sizei size = params.size.width == 0 || params.size.height == 0 ? m_size : params.size;
151+
region.imageOffset = {params.offsetX, params.offsetY, params.offsetZ};
152+
region.imageExtent = {size.width, size.height, params.depth};
125153

126-
const Sizei size = params.size.width == 0 || params.size.height == 0 ? m_size : params.size;
127-
region.imageOffset = {params.offsetX, params.offsetY, params.offsetZ};
128-
region.imageExtent = {size.width, size.height, params.depth};
154+
bufferCopyRegions.push_back(region);
129155

130156
vkCmdCopyBufferToImage(detail::getVkHandle(commandBuffer), detail::getVkHandle(src), m_handle,
131-
(VkImageLayout)TextureLayoutType::eTransferDstOptimal, 1, &region);
157+
(VkImageLayout)TextureLayoutType::eTransferDstOptimal, bufferCopyRegions.size(),
158+
bufferCopyRegions.data());
132159

133160
if (params.transferLayout != params.finalLayout)
134161
transitionImageLayout(params.transferLayout, params.finalLayout, false, commandBuffer);
@@ -139,6 +166,8 @@ void Texture::generateMipMaps(CommandBuffer& commandBuffer)
139166
assert(m_format != ColorFormat::eDepth32 && m_format != ColorFormat::eDepth24Stencil8 &&
140167
m_format != ColorFormat::eDepth32Stencil8);
141168

169+
// TODO: handle 2D arrays
170+
142171
// Copy down mips from n-1 to n
143172
for (uint32_t i = 1; i < m_mipLevels; i++)
144173
{
@@ -200,12 +229,12 @@ inline void Texture::createImage(const TextureParams& params)
200229
{
201230
VkImageCreateInfo imageInfo = {};
202231
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
203-
imageInfo.imageType = (VkImageType)params.type;
232+
imageInfo.imageType = detail::getImageType(params.type.get());
204233
imageInfo.extent.width = static_cast<uint32_t>(params.size.width);
205234
imageInfo.extent.height = static_cast<uint32_t>(params.size.height);
206235
imageInfo.extent.depth = params.depth;
207236
imageInfo.mipLevels = m_mipLevels;
208-
imageInfo.arrayLayers = params.arrayLevels;
237+
imageInfo.arrayLayers = m_type == TextureType::eCube ? 6 : params.arrayLevels;
209238
imageInfo.format = (VkFormat)params.format;
210239
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
211240
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -215,6 +244,8 @@ inline void Texture::createImage(const TextureParams& params)
215244
assert(math::isPowerOfTwo(params.samples));
216245
imageInfo.samples = (VkSampleCountFlagBits)params.samples;
217246
imageInfo.flags = 0;
247+
if (m_type == TextureType::eCube)
248+
imageInfo.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
218249

219250
RI_CHECK_RESULT_MSG("failed to create image") = vkCreateImage(m_device, &imageInfo, nullptr, &m_handle);
220251
}
@@ -233,7 +264,7 @@ void Texture::createImageView(const TextureParams& params, VkImageAspectFlags as
233264
viewInfo.subresourceRange.baseMipLevel = 0;
234265
viewInfo.subresourceRange.levelCount = m_mipLevels;
235266
viewInfo.subresourceRange.baseArrayLayer = 0;
236-
viewInfo.subresourceRange.layerCount = 1;
267+
viewInfo.subresourceRange.layerCount = m_type == TextureType::eCube ? 6 : 1;
237268

238269
RI_CHECK_RESULT_MSG("failed to create image view") = vkCreateImageView(m_device, &viewInfo, nullptr, &m_view);
239270
}
@@ -406,9 +437,9 @@ void Texture::transitionImageLayout(TextureLayoutType oldLayout, TextureLayoutTy
406437
VkImageSubresourceRange subresourceRange = {};
407438
subresourceRange.aspectMask = detail::getImageAspectFlags((VkFormat)m_format);
408439
subresourceRange.baseMipLevel = 0;
409-
subresourceRange.levelCount = 1;
440+
subresourceRange.levelCount = m_mipLevels;
410441
subresourceRange.baseArrayLayer = 0;
411-
subresourceRange.layerCount = 1;
442+
subresourceRange.layerCount = m_arrayLevels;
412443

413444
const PipelineBarrierSettings settings =
414445
getPipelineBarrierSettings(oldLayout, newLayout, readAccess, subresourceRange);

0 commit comments

Comments
 (0)