Skip to content

Commit 4a2c18c

Browse files
removed tons of duplicate code!
1 parent f626b7d commit 4a2c18c

File tree

6 files changed

+85
-187
lines changed

6 files changed

+85
-187
lines changed

include/nbl/builtin/hlsl/surface_transform.h

Lines changed: 71 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// For conditions of distribution and use, see copyright notice in nabla.h
44
#ifndef _NBL_BUILTIN_HLSL_SURFACE_TRANSFORM_INCLUDED_
55
#define _NBL_BUILTIN_HLSL_SURFACE_TRANSFORM_INCLUDED_
6+
#include <nbl/builtin/hlsl/limits.hlsl>
67

78
namespace nbl
89
{
@@ -24,6 +25,70 @@ enum class FLAG_BITS : uint16_t
2425
HORIZONTAL_MIRROR_ROTATE_270_BIT = 0x0080,
2526
INHERIT_BIT = 0x0100,
2627
ALL_BITS = 0x01FF
28+
};
29+
30+
// define everything else in terms of this
31+
float32_t2x2 transformMatrix(const FLAG_BITS transform)
32+
{
33+
switch (transform)
34+
{
35+
case FLAG_BITS::IDENTITY_BIT:
36+
return float32_t2x2( 1.f, 0.f,
37+
0.f, 1.f);
38+
case FLAG_BITS::ROTATE_90_BIT:
39+
return float32_t2x2( 0.f, 1.f,
40+
-1.f, 0.f);
41+
case FLAG_BITS::ROTATE_180_BIT:
42+
return float32_t2x2(-1.f, 0.f,
43+
0.f,-1.f);
44+
case FLAG_BITS::ROTATE_270_BIT:
45+
return float32_t2x2( 0.f,-1.f,
46+
1.f, 0.f);
47+
case FLAG_BITS::HORIZONTAL_MIRROR_BIT:
48+
return float32_t2x2(-1.f, 0.f,
49+
0.f, 1.f);
50+
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_90_BIT:
51+
return float32_t2x2( 0.f, 1.f,
52+
1.f, 0.f);
53+
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_180_BIT:
54+
return float32_t2x2( 1.f, 0.f,
55+
0.f,-1.f);
56+
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_270_BIT:
57+
return float32_t2x2( 0.f,-1.f,
58+
-1.f, 0.f);
59+
default:
60+
break;
61+
}
62+
const float _nan = numeric_limits<float>::signaling_NaN;
63+
return float32_t2x2(_nan,_nan,_nan,_nan);
64+
}
65+
66+
//! [width,height] might switch to [height, width] in orientations such as 90°CW
67+
//! Usecase: Find out how big the viewport has to be after or before a tranform is applied
68+
uint16_t2 transformedExtents(const FLAG_BITS transform, const uint16_t2 screenSize)
69+
{
70+
switch (transform)
71+
{
72+
case FLAG_BITS::IDENTITY_BIT:
73+
case FLAG_BITS::HORIZONTAL_MIRROR_BIT:
74+
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_180_BIT:
75+
case FLAG_BITS::ROTATE_180_BIT:
76+
return screenSize;
77+
case FLAG_BITS::ROTATE_90_BIT:
78+
case FLAG_BITS::ROTATE_270_BIT:
79+
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_90_BIT:
80+
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_270_BIT:
81+
return screenSize.yx;
82+
default:
83+
break;
84+
}
85+
return uint16_t2(0);
86+
}
87+
88+
float transformedAspectRatio(const FLAG_BITS transform, const uint16_t2 screenSize)
89+
{
90+
const uint16_t2 newExtents = transformedExtents(transform,screenSize);
91+
return float(newExtents[1])/float(newExtents[0]);
2792
}
2893

2994
//! Use this function to apply the INVERSE of swapchain tranformation to the screenspace coordinate `coord`
@@ -35,6 +100,7 @@ enum class FLAG_BITS : uint16_t
35100
//! - Be aware that almost always you'd want to do a single transform in your rendering pipeline.
36101
uint16_t2 applyInverseToScreenSpaceCoordinate(const FLAG_BITS transform, const uint16_t2 coord, const uint16_t2 screenSize)
37102
{
103+
// TODO: use inverse(transformMatrix(transform)) somehow
38104
const uint16_t2 lastTexel = screenSize - uint16_t2(1,1);
39105
switch (transform)
40106
{
@@ -66,8 +132,9 @@ uint16_t2 applyInverseToScreenSpaceCoordinate(const FLAG_BITS transform, const u
66132
//! Warning: Be aware that almost always you'd want to do a single transform in your rendering pipeline.
67133
uint16_t2 applyToScreenSpaceCoordinate(const FLAG_BITS transform, const uint16_t2 coord, const uint16_t2 screenSize)
68134
{
69-
const uint16_t2 lastTexel = screenSize - uint16_t2(1);
70-
switch (swapchainTransform)
135+
// TODO: use transformMatrix(transform) somehow
136+
const uint16_t2 lastTexel = screenSize - uint16_t2(1, 1);
137+
switch (transform)
71138
{
72139
case FLAG_BITS::IDENTITY_BIT:
73140
return coord;
@@ -91,56 +158,13 @@ uint16_t2 applyToScreenSpaceCoordinate(const FLAG_BITS transform, const uint16_t
91158
return uint16_t2(0);
92159
}
93160

94-
//! [width,height] might switch to [height, width] in orientations such as 90°CW
95-
//! Usecase: Find out how big the viewport has to be after or before a tranform is applied
96-
uint16_t2 transformedExtents(const FLAG_BITS transform, const uint16_t2 screenSize)
97-
{
98-
switch (swapchainTransform)
99-
{
100-
case FLAG_BITS::IDENTITY_BIT:
101-
case FLAG_BITS::HORIZONTAL_MIRROR_BIT:
102-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_180_BIT:
103-
case FLAG_BITS::ROTATE_180_BIT:
104-
return screenSize;
105-
case FLAG_BITS::ROTATE_90_BIT:
106-
case FLAG_BITS::ROTATE_270_BIT:
107-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_90_BIT:
108-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_270_BIT:
109-
return screenSize.yx;
110-
default:
111-
break;
112-
}
113-
return uint16_t2(0);
114-
}
115-
116161
//! Same as `applyToScreenSpaceCoordinate` but const NDC space
117162
//! If rendering to the swapchain, you may use this function to transform the NDC coordinates directly
118163
//! to be fed into gl_Position in vertex shading
119164
//! Warning: Be aware that almost always you'd want to do a single transform in your rendering pipeline.
120165
float32_t2 applyToNDC(const FLAG_BITS transform, const float32_t2 ndc)
121166
{
122-
switch (swapchainTransform)
123-
{
124-
case FLAG_BITS::IDENTITY_BIT:
125-
return ndc;
126-
case FLAG_BITS::ROTATE_90_BIT:
127-
return float32_t2(-ndc.y,ndc.x);
128-
case FLAG_BITS::ROTATE_180_BIT:
129-
return -ndc;
130-
case FLAG_BITS::ROTATE_270_BIT:
131-
return float32_t2(ndc.y,-ndc.x);
132-
case FLAG_BITS::HORIZONTAL_MIRROR_BIT:
133-
return float32_t2(-ndc.x,ndc.y);
134-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_90_BIT:
135-
return ndc.yx;
136-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_180_BIT:
137-
return float32_t2(ndc.x,-ndc.y);
138-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_270_BIT:
139-
return -ndc.yx;
140-
default:
141-
break;
142-
}
143-
return float32_t2(0,0)/0.f;
167+
return mul(transformMatrix(transform),ndc);
144168
}
145169

146170
// TODO: This is untested
@@ -149,37 +173,8 @@ float32_t2 applyToNDC(const FLAG_BITS transform, const float32_t2 ndc)
149173
template<typename TwoColumns>
150174
TwoColumns applyToDerivatives(const FLAG_BITS transform, TwoColumns dDx_dDy)
151175
{
152-
switch (swapchainTransform)
153-
{
154-
case FLAG_BITS::IDENTITY_BIT:
155-
return dDx_dDy;
156-
case FLAG_BITS::ROTATE_90_BIT:
157-
return mul(dDx_dDy,float32_t2x2( 0.f,-1.f
158-
1.f, 0.f));
159-
case FLAG_BITS::ROTATE_180_BIT:
160-
return mul(dDx_dDy,float32_t2x2(-1.f, 0.f
161-
0.f,-1.f));
162-
case FLAG_BITS::ROTATE_270_BIT:
163-
return mul(dDx_dDy,float32_t2x2( 0.f, 1.f
164-
-1.f, 0.f));
165-
case FLAG_BITS::HORIZONTAL_MIRROR_BIT:
166-
return mul(dDx_dDy,float32_t2x2(-1.f, 0.f
167-
0.f, 1.f));
168-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_90_BIT:
169-
return mul(dDx_dDy,float32_t2x2( 0.f, 1.f
170-
1.f, 0.f));
171-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_180_BIT:
172-
return mul(dDx_dDy,float32_t2x2( 1.f, 0.f
173-
0.f,-1.f));
174-
case FLAG_BITS::HORIZONTAL_MIRROR_ROTATE_270_BIT:
175-
return mul(dDx_dDy,float32_t2x2( 0.f,-1.f
176-
-1.f, 0.f));
177-
default:
178-
break;
179-
}
180-
return dDx_dDy*(0.f/0.f);
176+
return mul(inverse(transformMatrix(transform)),dDx_dDy);
181177
}
182-
// TODO: could define the NDC in terms of inverse Derivative XForm
183178

184179
}
185180
}

include/nbl/video/ISwapchain.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ class ISwapchain : public IBackendObject
7373
ISurface::ECA_POST_MULTIPLIED_BIT,
7474
ISurface::ECA_INHERIT_BIT
7575
};
76-
constexpr static inline ISurface::E_SURFACE_TRANSFORM_FLAGS DefaultPreferredTransforms[] = {
77-
ISurface::EST_IDENTITY_BIT
76+
constexpr static inline hlsl::SurfaceTransform::FLAG_BITS DefaultPreferredTransforms[] = {
77+
hlsl::SurfaceTransform::FLAG_BITS::IDENTITY_BIT
7878
// nothing else will work out the box without us explicitly having to handle it
7979
};
8080
inline bool deduce(
8181
const IPhysicalDevice* physDev, const ISurface* surface,
8282
std::span<const ISurface::E_PRESENT_MODE> preferredPresentModes=DefaultPreferredPresentModes,
8383
std::span<const ISurface::E_COMPOSITE_ALPHA> preferredCompositeAlphas=DefaultPreferredCompositeAlphas,
84-
std::span<const ISurface::E_SURFACE_TRANSFORM_FLAGS> preferredTransforms=DefaultPreferredTransforms
84+
std::span<const hlsl::SurfaceTransform::FLAG_BITS> preferredTransforms=DefaultPreferredTransforms
8585
)
8686
{
8787
ISurface::SCapabilities caps;
@@ -174,7 +174,7 @@ class ISwapchain : public IBackendObject
174174
uint16_t height = 0u;
175175
core::bitflag<ISurface::E_COMPOSITE_ALPHA> compositeAlpha = ISurface::ECA_ALL_BITS;
176176
uint8_t arrayLayers = 1u;
177-
core::bitflag<ISurface::E_SURFACE_TRANSFORM_FLAGS> preTransform = ISurface::EST_ALL_BITS;
177+
core::bitflag<hlsl::SurfaceTransform::FLAG_BITS> preTransform = hlsl::SurfaceTransform::FLAG_BITS::ALL_BITS;
178178
// If you set it to something else then your Swapchain will be created with Mutable Format capability
179179
// NOTE: If you do that, then the bitset needs to contain `viewFormats[surfaceFormat.format] = true` which is deduced later
180180
std::bitset<asset::E_FORMAT::EF_COUNT> viewFormats = {};
@@ -315,7 +315,7 @@ class ISwapchain : public IBackendObject
315315

316316
// The value passed to `preTransform` when creating the swapchain. "pre" refers to the transform happening
317317
// as an operation before the presentation engine presents the image.
318-
inline ISurface::E_SURFACE_TRANSFORM_FLAGS getPreTransform() const { return m_params.sharedParams.preTransform.value; }
318+
inline hlsl::SurfaceTransform::FLAG_BITS getPreTransform() const { return m_params.sharedParams.preTransform.value; }
319319

320320
//
321321
inline uint64_t getAcquireCount() const {return m_acquireCounter;}

include/nbl/video/surface/ISurface.h

Lines changed: 5 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "nbl/asset/format/EFormat.h"
88

99
#include "nbl/video/IAPIConnection.h"
10+
#include "nbl/builtin/hlsl/surface_transform.h"
1011

1112
#define VK_NO_PROTOTYPES
1213
#include "vulkan/vulkan.h"
@@ -44,7 +45,8 @@ class ISurface : public core::IReferenceCounted
4445
asset::E_FORMAT format = asset::EF_UNKNOWN;
4546
SColorSpace colorSpace = {};
4647
};
47-
// TODO: move these structs and enums to HLSL header!
48+
49+
// TODO: move these enums to HLSL header!
4850
enum E_PRESENT_MODE : uint8_t
4951
{
5052
EPM_NONE = 0x0,
@@ -56,105 +58,6 @@ class ISurface : public core::IReferenceCounted
5658
EPM_UNKNOWN = 0
5759
};
5860

59-
enum E_SURFACE_TRANSFORM_FLAGS : uint16_t
60-
{
61-
EST_NONE = 0x0,
62-
EST_IDENTITY_BIT = 0x0001,
63-
EST_ROTATE_90_BIT = 0x0002,
64-
EST_ROTATE_180_BIT = 0x0004,
65-
EST_ROTATE_270_BIT = 0x0008,
66-
EST_HORIZONTAL_MIRROR_BIT = 0x0010,
67-
EST_HORIZONTAL_MIRROR_ROTATE_90_BIT = 0x0020,
68-
EST_HORIZONTAL_MIRROR_ROTATE_180_BIT = 0x0040,
69-
EST_HORIZONTAL_MIRROR_ROTATE_270_BIT = 0x0080,
70-
EST_INHERIT_BIT = 0x0100,
71-
EST_ALL_BITS = 0x01FF
72-
};
73-
74-
// A matrix that can be pre-multiplied to the projection matrix in order to apply the
75-
// surface transform.
76-
static inline core::matrix4SIMD getSurfaceTransformationMatrix(const E_SURFACE_TRANSFORM_FLAGS transform)
77-
{
78-
const float sin90 = 1.0, cos90 = 0.0,
79-
sin180 = 0.0, cos180 = -1.0,
80-
sin270 = -1.0, cos270 = 0.0;
81-
82-
switch (transform)
83-
{
84-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_ROTATE_90_BIT:
85-
return core::matrix4SIMD(
86-
cos90, -sin90, 0.0, 0.0,
87-
sin90, cos90, 0.0, 0.0,
88-
0.0, 0.0, 1.0, 0.0,
89-
0.0, 0.0, 0.0, 1.0
90-
);
91-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_ROTATE_180_BIT:
92-
return core::matrix4SIMD(
93-
cos180, -sin180, 0.0, 0.0,
94-
sin180, cos180, 0.0, 0.0,
95-
0.0, 0.0, 1.0, 0.0,
96-
0.0, 0.0, 0.0, 1.0
97-
);
98-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_ROTATE_270_BIT:
99-
return core::matrix4SIMD(
100-
cos270, -sin270, 0.0, 0.0,
101-
sin270, cos270, 0.0, 0.0,
102-
0.0, 0.0, 1.0, 0.0,
103-
0.0, 0.0, 0.0, 1.0
104-
);
105-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_HORIZONTAL_MIRROR_BIT:
106-
return core::matrix4SIMD(
107-
-1.0, 0.0, 0.0, 0.0,
108-
0.0, 1.0, 0.0, 0.0,
109-
0.0, 0.0, 1.0, 0.0,
110-
0.0, 0.0, 0.0, 1.0
111-
);
112-
// The same matricies as the rotation ones above, but with the horizontal mirror matrix
113-
// (directly above this) pre-multiplied
114-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_HORIZONTAL_MIRROR_ROTATE_90_BIT:
115-
return core::matrix4SIMD(
116-
-cos90, sin90, 0.0, 0.0,
117-
sin90, cos90, 0.0, 0.0,
118-
0.0, 0.0, 1.0, 0.0,
119-
0.0, 0.0, 0.0, 1.0
120-
);
121-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_HORIZONTAL_MIRROR_ROTATE_180_BIT:
122-
return core::matrix4SIMD(
123-
-cos180, sin180, 0.0, 0.0,
124-
sin180, cos180, 0.0, 0.0,
125-
0.0, 0.0, 1.0, 0.0,
126-
0.0, 0.0, 0.0, 1.0
127-
);
128-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_HORIZONTAL_MIRROR_ROTATE_270_BIT:
129-
return core::matrix4SIMD(
130-
-cos270, sin270, 0.0, 0.0,
131-
sin270, cos270, 0.0, 0.0,
132-
0.0, 0.0, 1.0, 0.0,
133-
0.0, 0.0, 0.0, 1.0
134-
);
135-
default:
136-
return core::matrix4SIMD();
137-
}
138-
}
139-
140-
static inline float getTransformedAspectRatio(const E_SURFACE_TRANSFORM_FLAGS transform, uint32_t w, uint32_t h)
141-
{
142-
switch (transform)
143-
{
144-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_ROTATE_90_BIT:
145-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_ROTATE_270_BIT:
146-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_HORIZONTAL_MIRROR_ROTATE_90_BIT:
147-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_HORIZONTAL_MIRROR_ROTATE_270_BIT:
148-
return float(h) / w;
149-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_ROTATE_180_BIT:
150-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_HORIZONTAL_MIRROR_BIT:
151-
case ISurface::E_SURFACE_TRANSFORM_FLAGS::EST_HORIZONTAL_MIRROR_ROTATE_180_BIT:
152-
return float(w) / h;
153-
default:
154-
return float(w) / h;
155-
}
156-
}
157-
15861
enum E_COMPOSITE_ALPHA : uint8_t
15962
{
16063
ECA_NONE = 0x0,
@@ -176,8 +79,8 @@ class ISurface : public core::IReferenceCounted
17679
uint8_t maxImageCount = 0;
17780
uint8_t maxImageArrayLayers = 0;
17881
core::bitflag<E_COMPOSITE_ALPHA> supportedCompositeAlpha = ECA_NONE;
179-
core::bitflag<E_SURFACE_TRANSFORM_FLAGS> supportedTransforms = EST_NONE;
180-
E_SURFACE_TRANSFORM_FLAGS currentTransform = EST_NONE;
82+
core::bitflag<hlsl::SurfaceTransform::FLAG_BITS> supportedTransforms = hlsl::SurfaceTransform::FLAG_BITS::NONE;
83+
hlsl::SurfaceTransform::FLAG_BITS currentTransform = hlsl::SurfaceTransform::FLAG_BITS::NONE;
18184
};
18285

18386
inline E_API_TYPE getAPIType() const { return m_api->getAPIType(); }

src/nbl/builtin/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/subgroup/basic.hlsl")
290290
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/subgroup/arithmetic_portability.hlsl")
291291
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/subgroup/arithmetic_portability_impl.hlsl")
292292
#shared header between C++ and HLSL
293-
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/surface_transform.hlsl")
293+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/surface_transform.h")
294294
#workgroup
295295
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/workgroup/arithmetic.hlsl")
296296
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/workgroup/basic.hlsl")

src/nbl/video/CSurfaceVulkan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ bool ISurfaceVulkan::isSupportedForPhysicalDevice(const IPhysicalDevice* physica
111111
capabilities.minImageExtent = vk_surfaceCapabilities.surfaceCapabilities.minImageExtent;
112112
capabilities.maxImageExtent = vk_surfaceCapabilities.surfaceCapabilities.maxImageExtent;
113113
capabilities.maxImageArrayLayers = vk_surfaceCapabilities.surfaceCapabilities.maxImageArrayLayers;
114-
capabilities.supportedTransforms = static_cast<ISurface::E_SURFACE_TRANSFORM_FLAGS>(vk_surfaceCapabilities.surfaceCapabilities.supportedTransforms);
115-
capabilities.currentTransform = static_cast<ISurface::E_SURFACE_TRANSFORM_FLAGS>(vk_surfaceCapabilities.surfaceCapabilities.currentTransform);
114+
capabilities.supportedTransforms = static_cast<hlsl::SurfaceTransform::FLAG_BITS>(vk_surfaceCapabilities.surfaceCapabilities.supportedTransforms);
115+
capabilities.currentTransform = static_cast<hlsl::SurfaceTransform::FLAG_BITS>(vk_surfaceCapabilities.surfaceCapabilities.currentTransform);
116116
capabilities.supportedCompositeAlpha = static_cast<ISurface::E_COMPOSITE_ALPHA>(vk_surfaceCapabilities.surfaceCapabilities.supportedCompositeAlpha);
117117
capabilities.supportedUsageFlags = getImageUsageFlagsFromVkImageUsageFlags(vk_surfaceCapabilities.surfaceCapabilities.supportedUsageFlags);
118118

0 commit comments

Comments
 (0)