Talvos  0.1
SPIR-V interpreter and dynamic analysis framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Image.h
Go to the documentation of this file.
1 // Copyright (c) 2018 the Talvos developers. All rights reserved.
2 //
3 // This file is distributed under a three-clause BSD license. For full license
4 // terms please see the LICENSE file distributed with this source code.
5 
8 
9 #ifndef TALVOS_IMAGE_H
10 #define TALVOS_IMAGE_H
11 
12 #include <cassert>
13 
14 #include "vulkan/vulkan_core.h"
15 
16 #include "talvos/Object.h"
17 
18 namespace talvos
19 {
20 
21 class Device;
22 
24 class Image
25 {
26 public:
28  class Texel
29  {
30  public:
32  Texel() {}
33 
37  Texel(const Object &Obj);
38 
40  Texel(const VkClearColorValue &ClearColor);
41 
44  template <typename T> T get(unsigned C) const
45  {
46  static_assert(sizeof(T) == 4);
47  assert(C < 4);
48  return ((T *)Data)[C];
49  }
50 
52  const uint8_t *getData() const { return Data; }
53 
55  template <typename T> void loadSInt(const T *Data);
56 
58  template <typename T> void loadSFloat(const T *Data);
59 
61  template <typename T> void loadSNorm(const T *Data);
62 
64  template <typename T> void loadUInt(const T *Data);
65 
67  template <typename T> void loadUNorm(const T *Data);
68 
71  template <typename T> void set(unsigned C, T Value)
72  {
73  static_assert(sizeof(T) == 4);
74  assert(C < 4);
75  ((T *)Data)[C] = Value;
76  }
77 
79  template <typename T> void storeSInt(T *Data) const;
80 
82  template <typename T> void storeSNorm(T *Data) const;
83 
85  template <typename T> void storeUInt(T *Data) const;
86 
88  template <typename T> void storeUNorm(T *Data) const;
89 
93  Object toObject(const Type *Ty) const;
94 
95  private:
97  uint8_t Data[16];
98  };
99 
100 public:
102  Image(Device &Dev, VkImageType Type, VkFormat Format, VkExtent3D Extent,
103  uint32_t NumArrayLayers = 1, uint32_t NumMipLevels = 1)
104  : Dev(Dev), Type(Type), Format(Format), Extent(Extent),
106  {
107  Address = 0;
108  }
109 
111  void bindAddress(uint64_t Address);
112 
114  uint64_t getAddress() const { return Address; }
115 
117  uint32_t getDepth(uint32_t Level = 0) const;
118 
120  uint32_t getElementSize() const;
121 
123  VkExtent3D getExtent() const { return Extent; }
124 
126  VkFormat getFormat() const { return Format; }
127 
129  uint32_t getHeight(uint32_t Level = 0) const;
130 
132  uint64_t getMipLevelOffset(uint32_t Level) const;
133 
135  uint32_t getNumArrayLayers() const { return NumArrayLayers; }
136 
138  uint32_t getNumMipLevels() const { return NumMipLevels; }
139 
141  uint64_t getTexelAddress(uint32_t X, uint32_t Y = 0, uint32_t Z = 0,
142  uint32_t Layer = 0, uint32_t MipLevel = 0) const;
143 
145  uint64_t getTotalSize() const { return getMipLevelOffset(NumMipLevels); }
146 
148  VkImageType getType() const { return Type; }
149 
151  uint32_t getWidth(uint32_t Level = 0) const;
152 
154  void read(Texel &T, uint64_t Address) const;
155 
157  void read(Texel &T, uint64_t Address, VkFormat ReadFormat) const;
158 
160  void write(const Texel &T, uint64_t Address) const;
161 
163  void write(const Texel &T, uint64_t Address, VkFormat WriteFormat) const;
164 
165 private:
167 
168  VkImageType Type;
169  VkFormat Format;
170  VkExtent3D Extent;
171  uint32_t NumArrayLayers;
172  uint32_t NumMipLevels;
173 
174  uint64_t Address = 0;
175 };
176 
179 {
180 public:
182  ImageView(const Image &Img, VkImageViewType Type, VkFormat Format,
183  VkImageSubresourceRange Range);
184 
186  uint32_t getBaseArrayLayer() const { return BaseArrayLayer; }
187 
189  uint32_t getBaseMipLevel() const { return BaseMipLevel; }
190 
192  uint32_t getDepth(uint32_t Level = 0) const;
193 
195  VkFormat getFormat() const { return Format; }
196 
198  uint32_t getHeight(uint32_t Level = 0) const;
199 
201  const Image &getImage() const { return Img; }
202 
204  uint32_t getNumArrayLayers() const { return NumArrayLayers; }
205 
207  uint32_t getNumMipLevels() const { return NumMipLevels; }
208 
210  uint64_t getTexelAddress(uint32_t X, uint32_t Y = 0, uint32_t Z = 0,
211  uint32_t Layer = 0, uint32_t MipLevel = 0) const;
212 
214  VkImageViewType getType() const { return Type; }
215 
217  uint32_t getWidth(uint32_t Level = 0) const;
218 
220  bool is1D() const;
221 
223  bool is2D() const;
224 
226  bool is3D() const;
227 
229  bool isCube() const;
230 
232  void read(Image::Texel &T, uint32_t X, uint32_t Y = 0, uint32_t Z = 0,
233  uint32_t Layer = 0, uint32_t MipLevel = 0) const;
234 
236  void write(const Image::Texel &T, uint32_t X, uint32_t Y = 0, uint32_t Z = 0,
237  uint32_t Layer = 0, uint32_t MipLevel = 0) const;
238 
239 private:
240  const Image &Img;
241 
242  VkImageViewType Type;
243  VkFormat Format;
244 
245  uint32_t BaseArrayLayer;
246  uint32_t NumArrayLayers;
247  uint32_t BaseMipLevel;
248  uint32_t NumMipLevels;
249 };
250 
252 class Sampler
253 {
254 public:
256  Sampler(const VkSamplerCreateInfo &CreateInfo) { Info = CreateInfo; }
257 
259  void sample(const ImageView *Image, Image::Texel &Texel, float S, float T = 0,
260  float R = 0, float A = 0, float Lod = 0) const;
261 
262 private:
264  VkSamplerCreateInfo Info;
265 };
266 
269 {
270  const class ImageView *Image;
271  const class Sampler *Sampler;
272 };
273 
275 uint32_t getElementSize(VkFormat Format);
276 
278 bool hasAlphaChannel(VkFormat Format);
279 
280 } // namespace talvos
281 
282 #endif
Image(Device &Dev, VkImageType Type, VkFormat Format, VkExtent3D Extent, uint32_t NumArrayLayers=1, uint32_t NumMipLevels=1)
Create an image.
Definition: Image.h:102
uint64_t Address
The memory address of the image data.
Definition: Image.h:174
VkFormat Format
The image format.
Definition: Image.h:169
uint32_t getBaseMipLevel() const
Returns the base mip level of the image view.
Definition: Image.h:189
void bindAddress(uint64_t Address)
Bind a memory address to the image (can only be called once).
Definition: Image.cpp:144
void write(const Image::Texel &T, uint32_t X, uint32_t Y=0, uint32_t Z=0, uint32_t Layer=0, uint32_t MipLevel=0) const
Write a texel to the image view at the specified coordinate.
Definition: Image.cpp:511
This class represents an image object.
Definition: Image.h:24
uint64_t getMipLevelOffset(uint32_t Level) const
Returns the offset in bytes to the beginning of the specified mip level.
Definition: Image.cpp:167
A combination of an image and a sampler used to access it.
Definition: Image.h:268
bool hasAlphaChannel(VkFormat Format)
Returns true if Format includes an alpha channel.
Definition: Image.cpp:889
uint32_t BaseArrayLayer
The base array layer.
Definition: Image.h:245
void loadUInt(const T *Data)
Load unsigned integer texel components.
Definition: Image.cpp:60
Object toObject(const Type *Ty) const
Create an object with type Ty from the texel data.
Definition: Image.cpp:135
void sample(const ImageView *Image, Image::Texel &Texel, float S, float T=0, float R=0, float A=0, float Lod=0) const
Sample a texel from an image at the specified coordinates.
Definition: Image.cpp:517
VkImageViewType Type
The type of the image view.
Definition: Image.h:242
This class represents a view into a range of image subresources.
Definition: Image.h:178
void set(unsigned C, T Value)
Set a component value in the texel.
Definition: Image.h:71
uint32_t getElementSize(VkFormat Format)
Returns the size in bytes for each element of an image with type Format.
Definition: Image.cpp:651
void write(const Texel &T, uint64_t Address) const
Write a texel to the image at the specified address.
Definition: Image.cpp:330
void storeSNorm(T *Data) const
Store normalized texel components as signed integers.
Definition: Image.cpp:89
void loadSInt(const T *Data)
Load signed integer texel components.
Definition: Image.cpp:44
uint32_t getWidth(uint32_t Level=0) const
Returns the width of the image at the specified mip level.
Definition: Image.cpp:188
uint32_t getNumArrayLayers() const
Returns the number of array layers in the image.
Definition: Image.h:135
VkExtent3D Extent
The image extent.
Definition: Image.h:170
void storeSInt(T *Data) const
Store signed integer texel components, truncating as necessary.
Definition: Image.cpp:76
VkSamplerCreateInfo Info
The sampler parameters.
Definition: Image.h:264
This class represents a single texel with four 32-bit component values.
Definition: Image.h:28
VkFormat getFormat() const
Returns the format of the image.
Definition: Image.h:195
const class ImageView * Image
Definition: Image.h:270
uint32_t getNumArrayLayers() const
Returns the number of array layers in the image view.
Definition: Image.h:204
void loadUNorm(const T *Data)
Load normalized texel components from unsigned integer data.
Definition: Image.cpp:68
const Image & getImage() const
Returns the image that the image view corresponds to.
Definition: Image.h:201
uint32_t getWidth(uint32_t Level=0) const
Get the width of the image view at the specified mip level.
Definition: Image.cpp:482
uint32_t getBaseArrayLayer() const
Returns the base array layer of the image view.
Definition: Image.h:186
VkFormat Format
The format of the image view.
Definition: Image.h:243
uint32_t getDepth(uint32_t Level=0) const
Get the depth of the image view at the specified mip level.
Definition: Image.cpp:465
uint32_t NumMipLevels
The number of mip levels.
Definition: Image.h:248
ImageView(const Image &Img, VkImageViewType Type, VkFormat Format, VkImageSubresourceRange Range)
Create an image view.
Definition: Image.cpp:450
uint32_t getHeight(uint32_t Level=0) const
Returns the height of the image at the specified mip level.
Definition: Image.cpp:161
Texel()
Create a texel with uninitialized component values.
Definition: Image.h:32
VkImageType Type
The image type.
Definition: Image.h:168
void storeUNorm(T *Data) const
Store normalized texel components as unsigned integers.
Definition: Image.cpp:118
A Device instance encapsulates properties and state for the virtual device.
Definition: Device.h:29
uint32_t getDepth(uint32_t Level=0) const
Returns the depth of the image at the specified mip level.
Definition: Image.cpp:150
This file declares the Object class.
void read(Texel &T, uint64_t Address) const
Read a texel from the image at the specified address.
Definition: Image.cpp:194
This class represents a sampler object.
Definition: Image.h:252
const uint8_t * getData() const
Returns a const pointer to the raw data backing the texel.
Definition: Image.h:52
uint32_t getHeight(uint32_t Level=0) const
Get the height of the image view at the specified mip level.
Definition: Image.cpp:470
uint32_t getElementSize() const
Returns the size in bytes of a single pixel in the image.
Definition: Image.cpp:156
uint32_t NumArrayLayers
The number of array layers.
Definition: Image.h:246
bool isCube() const
Returns true if this image view corresponds to a cube map.
Definition: Image.cpp:499
VkImageType getType() const
Returns the type of the image.
Definition: Image.h:148
VkExtent3D getExtent() const
Returns the size of a single layer of the image.
Definition: Image.h:123
uint64_t getTotalSize() const
Returns the total size of the image (including all mip levels).
Definition: Image.h:145
Device & Dev
The device this image view is created on.
Definition: Image.h:166
uint32_t NumArrayLayers
The number of array layers.
Definition: Image.h:171
This class represents a SPIR-V type.
Definition: Type.h:33
uint32_t NumMipLevels
The number of mip levels.
Definition: Image.h:172
VkFormat getFormat() const
Returns the format of the image.
Definition: Image.h:126
bool is1D() const
Returns true if this image view corresponds to a 1D image.
Definition: Image.cpp:487
bool is2D() const
Returns true if this image view corresponds to a 2D image.
Definition: Image.cpp:492
void storeUInt(T *Data) const
Store unsigned integer texel components, truncating as necessary.
Definition: Image.cpp:106
Sampler(const VkSamplerCreateInfo &CreateInfo)
Create a sampler.
Definition: Image.h:256
uint64_t getTexelAddress(uint32_t X, uint32_t Y=0, uint32_t Z=0, uint32_t Layer=0, uint32_t MipLevel=0) const
Returns the address in memory of the texel at the specified coordinate.
Definition: Image.cpp:179
VkImageViewType getType() const
Returns the type of the image view.
Definition: Image.h:214
This class represents an instruction result.
Definition: Object.h:51
void loadSNorm(const T *Data)
Load normalized texel components from signed integer data.
Definition: Image.cpp:52
uint8_t Data[16]
The data backing this texel.
Definition: Image.h:97
const class Sampler * Sampler
Definition: Image.h:271
const Image & Img
The image that the image corresponds to.
Definition: Image.h:240
uint64_t getAddress() const
Returns the memory address of the beginning of the image.
Definition: Image.h:114
uint32_t BaseMipLevel
The base mip level.
Definition: Image.h:247
void loadSFloat(const T *Data)
Load signed floating point texel components.
Definition: Image.cpp:36
bool is3D() const
Returns true if this image view corresponds to a 3D image.
Definition: Image.cpp:497
uint32_t getNumMipLevels() const
Returns the number of mip levels in the image view.
Definition: Image.h:207
void read(Image::Texel &T, uint32_t X, uint32_t Y=0, uint32_t Z=0, uint32_t Layer=0, uint32_t MipLevel=0) const
Read a texel from the image view at the specified coordinate.
Definition: Image.cpp:505
uint32_t getNumMipLevels() const
Returns the number of mip levels in the image.
Definition: Image.h:138
uint64_t getTexelAddress(uint32_t X, uint32_t Y=0, uint32_t Z=0, uint32_t Layer=0, uint32_t MipLevel=0) const
Returns the address in memory of the texel at the specified coordinate.
Definition: Image.cpp:475