├── src
├── core
│ ├── pch.cpp
│ ├── refCountChecker.h
│ ├── spinlock.h
│ ├── hash.inl
│ └── spinlock.inl
├── auxiliary
│ ├── spirv
│ │ ├── blit.frag
│ │ ├── imm.frag
│ │ ├── blit.vert
│ │ ├── imm.vert
│ │ └── font.frag
│ ├── profiler.inl
│ ├── generateMipmap.h
│ ├── typedefs.h
│ └── fillRectangleVertexShader.h
├── exceptions
│ ├── exception.inl
│ ├── spirvError.h
│ ├── reflectionError.h
│ ├── exception.h
│ └── compileError.h
├── shaders
│ ├── shaderBindingTable.inl
│ └── shaderReflectionFactory.h
├── objects
│ ├── bufferView.inl
│ ├── model
│ │ ├── object.inl
│ │ ├── dispatchable.inl
│ │ └── iobject.h
│ ├── shaderModule.inl
│ ├── indirectBuffer.inl
│ ├── srcTransferBuffer.h
│ ├── dstTransferBuffer.h
│ ├── dstTransferBuffer.cpp
│ ├── srcTransferBuffer.cpp
│ ├── computePipelineLibrary.h
│ ├── pipeline.inl
│ ├── pipelineLibrary.cpp
│ ├── externalImage.h
│ ├── primaryCommandBuffer.h
│ ├── baseCountBuffer.h
│ ├── pipelineLayout.inl
│ ├── deferredOperation.h
│ ├── indexBuffer.inl
│ ├── mutableImage.h
│ ├── pipelineLibrary.h
│ ├── displayMode.h
│ ├── image.inl
│ ├── semaphore.h
│ ├── conditionalRenderingBuffer.h
│ ├── imageView.inl
│ └── computePipelineBatch.h
├── states
│ ├── depthComparisonSamplerState.inl
│ ├── stencilOpState.inl
│ ├── tesselationState.inl
│ ├── colorBlendStateAdvanced.inl
│ ├── inputAssemblyState.inl
│ ├── minmaxSamplerState.inl
│ ├── rasterizationOrderState.inl
│ ├── tesselationState.h
│ ├── provokingVertexState.inl
│ ├── streamRasterizationState.inl
│ ├── multisampleCoverageReductionState.inl
│ ├── multiViewportState.h
│ ├── multiColorBlendState.h
│ ├── formattedOutput.h
│ └── vertexAttributeType.inl
├── packed
│ ├── rcpExpPow2.h
│ ├── expPow2.h
│ ├── horzor.inl
│ ├── ieee754.h
│ ├── x10y10z10w2unorm.inl
│ ├── x10y10z10w2snorm.inl
│ ├── b5g6r5unorm.inl
│ └── r5g6b5unorm.inl
├── misc
│ ├── pushConstantRange.inl
│ ├── imageResolve.inl
│ ├── scissor.inl
│ ├── viewport.inl
│ ├── borderColor.inl
│ ├── imageResolve.h
│ ├── scissor.h
│ ├── finish.h
│ ├── extension.inl
│ ├── viewport.h
│ ├── imageSubresourceRange.inl
│ ├── application.h
│ ├── featureQuery.h
│ ├── mapBuffer.h
│ ├── mapImage.h
│ ├── application.cpp
│ ├── framebufferAttachmentImage.h
│ └── imageSubresourceRange.h
├── extensions
│ ├── extensionRegistry.inl
│ ├── deviceLayers.cpp
│ ├── deviceLayers.h
│ └── extensionRegistry.h
├── descriptors
│ ├── bufferArrayElement.inl
│ ├── descriptorSetLayoutBinding.inl
│ ├── inlineUniformBlock.inl
│ ├── arrayElement.h
│ └── bufferArrayElement.h
├── raytracing
│ ├── address.h
│ └── address.inl
├── renderpass
│ ├── attachment.inl
│ └── attachment.h
├── helpers
│ ├── executeCommandBuffer.h
│ ├── executeDeferredOperation.h
│ ├── uniformArray.inl
│ ├── mapTyped.h
│ ├── layoutTransition.h
│ ├── uniformArray.h
│ └── stringifyFlags.h
├── platform
│ ├── viSurface.h
│ ├── androidSurface.h
│ ├── metalSurface.h
│ ├── fuchsiaSurface.h
│ ├── win32Surface.h
│ ├── iosSurface.h
│ ├── macosSurface.h
│ ├── directFbSurface.h
│ ├── xlibSurface.h
│ ├── waylandSurface.h
│ ├── xcbSurface.h
│ ├── qnxSurface.h
│ ├── iosSurface.cpp
│ ├── viSurface.cpp
│ ├── macosSurface.cpp
│ ├── metalSurface.cpp
│ ├── win32ExternalSemaphore.h
│ ├── androidSurface.cpp
│ ├── xlibSurface.cpp
│ ├── win32Surface.cpp
│ ├── xcbSurface.cpp
│ ├── qnxSurface.cpp
│ ├── waylandSurface.cpp
│ └── fuchsiaSurface.cpp
├── barriers
│ └── bufferMemoryBarrier.inl
└── allocator
│ └── alignedAllocator.h
├── tools
├── structSize
│ ├── run.bat
│ ├── runObfuscate64.bat
│ ├── runObfuscate86.bat
│ └── build.bat
├── ldFeatSpec
│ ├── run.bat
│ └── build.bat
├── scNodeSpec
│ ├── run.bat
│ └── build.bat
├── pdFeatProp
│ ├── run.bat
│ └── build.bat
└── expPow
│ ├── run.bat
│ └── build.bat
├── docs
└── images
│ ├── vulkan.gif
│ └── vulkan_sdk_path.png
├── projects
└── xcode
│ └── magma.xcodeproj
│ └── project.xcworkspace
│ └── contents.xcworkspacedata
├── .gitignore
├── .gitmodules
└── unittest
├── popcount
├── build.bat
├── Makefile
└── unittest.cpp
├── packing
├── build.bat
└── Makefile
├── linkNode
├── build.bat
└── Makefile
└── structureChain
├── build.bat
└── Makefile
/src/core/pch.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 |
--------------------------------------------------------------------------------
/tools/structSize/run.bat:
--------------------------------------------------------------------------------
1 | structSize sizeOf.h
2 |
--------------------------------------------------------------------------------
/tools/ldFeatSpec/run.bat:
--------------------------------------------------------------------------------
1 | ldFeatSpec ../../src/objects/device.inl
--------------------------------------------------------------------------------
/tools/scNodeSpec/run.bat:
--------------------------------------------------------------------------------
1 | scNodeSpec ../../src/misc/structureChainNode.inl
--------------------------------------------------------------------------------
/tools/pdFeatProp/run.bat:
--------------------------------------------------------------------------------
1 | pdFeatProp physicalDevice.h physicalDeviceFeatures.cpp
--------------------------------------------------------------------------------
/tools/structSize/runObfuscate64.bat:
--------------------------------------------------------------------------------
1 | structSize ../../src/misc/sizeof64.inl
2 |
--------------------------------------------------------------------------------
/tools/structSize/runObfuscate86.bat:
--------------------------------------------------------------------------------
1 | structSize ../../src/misc/sizeof86.inl
2 |
--------------------------------------------------------------------------------
/tools/expPow/run.bat:
--------------------------------------------------------------------------------
1 | expPow "../../src/packed/expPow2.h" ../../src/packed/rcpExpPow2.h
--------------------------------------------------------------------------------
/docs/images/vulkan.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vcoda/magma/HEAD/docs/images/vulkan.gif
--------------------------------------------------------------------------------
/tools/expPow/build.bat:
--------------------------------------------------------------------------------
1 | REM Run in x64 Native Tools Command Prompt
2 | cl /O2 /EHsc expPow.cpp /link
3 |
--------------------------------------------------------------------------------
/docs/images/vulkan_sdk_path.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vcoda/magma/HEAD/docs/images/vulkan_sdk_path.png
--------------------------------------------------------------------------------
/tools/ldFeatSpec/build.bat:
--------------------------------------------------------------------------------
1 | REM Run in x64 Native Tools Command Prompt
2 | cl /O2 /EHsc ldFeatSpec.cpp /link
3 |
--------------------------------------------------------------------------------
/tools/pdFeatProp/build.bat:
--------------------------------------------------------------------------------
1 | REM Run in x64 Native Tools Command Prompt
2 | cl /O2 /EHsc pdFeatProp.cpp /link
3 |
--------------------------------------------------------------------------------
/tools/scNodeSpec/build.bat:
--------------------------------------------------------------------------------
1 | REM Run in x64 Native Tools Command Prompt
2 | cl /O2 /EHsc scNodeSpec.cpp /link
3 |
--------------------------------------------------------------------------------
/tools/structSize/build.bat:
--------------------------------------------------------------------------------
1 | REM Run in x64 Native Tools Command Prompt
2 | cl /I%VK_SDK_PATH%\Include /O2 /EHsc /std:c++17 structSize.cpp /link
3 |
--------------------------------------------------------------------------------
/projects/xcode/magma.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .vs/
2 | Debug/
3 | Release/
4 | x64/
5 | src/auxiliary/spirv/output
6 | projects/qt/build
7 |
8 | *.suo
9 | *.user
10 | *.vcxproj.user
11 |
12 | *.obj
13 | *.pch
14 | *.gch
15 | *.ipch
16 | *.pdb
17 |
18 | *.idx
19 |
20 | *.o
21 | *.a
22 |
--------------------------------------------------------------------------------
/src/auxiliary/spirv/blit.frag:
--------------------------------------------------------------------------------
1 | #version 450
2 |
3 | layout(binding = 0) uniform sampler2D s0;
4 |
5 | layout(location = 0) in vec2 texCoord;
6 | layout(location = 0) out vec4 oColor;
7 |
8 | void main()
9 | {
10 | oColor = texture(s0, texCoord);
11 | }
12 |
--------------------------------------------------------------------------------
/src/auxiliary/spirv/imm.frag:
--------------------------------------------------------------------------------
1 | #version 450
2 |
3 | layout(location = 0) in vec3 normal;
4 | layout(location = 1) in vec4 color;
5 | layout(location = 2) in vec2 texCoord;
6 | layout(location = 0) out vec4 oColor;
7 |
8 | void main()
9 | {
10 | oColor = color;
11 | }
12 |
--------------------------------------------------------------------------------
/src/exceptions/exception.inl:
--------------------------------------------------------------------------------
1 | namespace magma::exception
2 | {
3 | inline Exception::Exception(const char *message) noexcept:
4 | message(message),
5 | location{}
6 | {}
7 |
8 | inline Exception::Exception(const char *message, const source_location& location) noexcept:
9 | message(message),
10 | location(location)
11 | {}
12 | } // namespace magma::exception
13 |
--------------------------------------------------------------------------------
/src/shaders/shaderBindingTable.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | #ifdef VK_KHR_ray_tracing_pipeline
4 | template
5 | inline void ShaderBindingTable::addShaderRecord(VkShaderStageFlagBits stage, uint32_t groupIndex, const Block& block)
6 | {
7 | groups[stage].shaderRecords[groupIndex] = ShaderRecord(block);
8 | }
9 | #endif // VK_KHR_ray_tracing_pipeline
10 | } // namespace magma
11 |
--------------------------------------------------------------------------------
/src/objects/bufferView.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | template
4 | inline TBufferView::TBufferView(Pointer buffer, VkFormat format,
5 | VkDeviceSize offset /* 0 */,
6 | VkDeviceSize range /* VK_WHOLE_SIZE */,
7 | const StructureChain& extendedInfo /* default */):
8 | BufferView(buffer.get(), format, offset, range, extendedInfo),
9 | buffer(std::move(buffer))
10 | {}
11 | } // namespace magma
12 |
--------------------------------------------------------------------------------
/src/states/depthComparisonSamplerState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr DepthComparisonSamplerState::DepthComparisonSamplerState(const VkFilter magFilter, const VkFilter minFilter, const VkCompareOp compareOp_) noexcept:
4 | SamplerState(magFilter, minFilter, VK_SAMPLER_MIPMAP_MODE_NEAREST, VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER)
5 | {
6 | compareEnable = VK_TRUE;
7 | compareOp = compareOp_;
8 | borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
9 | }
10 | } // namespace magma
11 |
--------------------------------------------------------------------------------
/src/packed/rcpExpPow2.h:
--------------------------------------------------------------------------------
1 | // Generated by utility tool. DO NOT MODIFY!
2 | // 1/(2^(exp - EXP_BIAS - MANTISSA_BITS))
3 | constexpr float rcpExpPow2[MAX_VALID_BIASED_EXP + 1] = {
4 | 16777216.f, 8388608.f, 4194304.f, 2097152.f,
5 | 1048576.f, 524288.f, 262144.f, 131072.f,
6 | 65536.f, 32768.f, 16384.f, 8192.f,
7 | 4096.f, 2048.f, 1024.f, 512.f,
8 | 256.f, 128.f, 64.f, 32.f,
9 | 16.f, 8.f, 4.f, 2.f,
10 | 1.f, 0.5f, 0.25f, 0.125f,
11 | 0.0625f, 0.03125f, 0.015625f, 0.0078125f
12 | };
13 |
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "src/third-party/VulkanMemoryAllocator"]
2 | path = src/third-party/VulkanMemoryAllocator
3 | url = https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator.git
4 | branch = master
5 | [submodule "src/third-party/SPIRV-Reflect"]
6 | path = src/third-party/SPIRV-Reflect
7 | url = https://github.com/KhronosGroup/SPIRV-Reflect.git
8 | branch = master
9 | [submodule "src/third-party/pfr"]
10 | path = src/third-party/pfr
11 | url = https://github.com/boostorg/pfr.git
12 | branch = master
13 |
--------------------------------------------------------------------------------
/src/misc/pushConstantRange.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr PushConstantRange::PushConstantRange(const VkShaderStageFlags stageFlags, const uint32_t offset, const std::size_t size) noexcept:
4 | VkPushConstantRange{
5 | stageFlags,
6 | offset,
7 | static_cast(size)
8 | }
9 | {}
10 |
11 | constexpr hash_t PushConstantRange::hash() const noexcept
12 | {
13 | return core::hashArgs(
14 | stageFlags,
15 | offset,
16 | size);
17 | }
18 | } // namespace magma
19 |
--------------------------------------------------------------------------------
/src/auxiliary/spirv/blit.vert:
--------------------------------------------------------------------------------
1 | #version 450
2 |
3 | layout(location = 0) out vec2 oTexCoord;
4 |
5 | out gl_PerVertex {
6 | vec4 gl_Position;
7 | };
8 |
9 | void main()
10 | {
11 | vec2 tri[3] = vec2[3](
12 | #ifdef NV // https://www.khronos.org/registry/OpenGL/extensions/NV/NV_fill_rectangle.txt
13 | vec2(-1., -1.), vec2(-1., 1.), vec2(1., -1.));
14 | #else
15 | vec2(-1., -1.), vec2(-1., 3.), vec2(3., -1.));
16 | #endif
17 | vec2 pos = tri[gl_VertexIndex];
18 | oTexCoord = pos * .5 + .5;
19 | gl_Position = vec4(pos, 0., 1.);
20 | }
21 |
--------------------------------------------------------------------------------
/src/states/stencilOpState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr StencilOpState::StencilOpState(const VkStencilOp failOp, const VkStencilOp passOp, const VkStencilOp depthFailOp, const VkCompareOp compareOp,
4 | const uint32_t compareMask /* 0 */, const uint32_t writeMask /* 0 */, const uint32_t reference /* 0 */) noexcept:
5 | VkStencilOpState{
6 | failOp,
7 | passOp,
8 | depthFailOp,
9 | compareOp,
10 | compareMask,
11 | writeMask,
12 | reference
13 | }
14 | {}
15 | } // namespace magma
16 |
--------------------------------------------------------------------------------
/src/packed/expPow2.h:
--------------------------------------------------------------------------------
1 | // Generated by utility tool. DO NOT MODIFY!
2 | // 2^(exp - EXP_BIAS - MANTISSA_BITS)
3 | constexpr float expPow2[MAX_VALID_BIASED_EXP + 1] = {
4 | 5.96046448e-08f, 1.1920929e-07f, 2.38418579e-07f, 4.76837158e-07f,
5 | 9.53674316e-07f, 1.90734863e-06f, 3.81469727e-06f, 7.62939453e-06f,
6 | 1.52587891e-05f, 3.05175781e-05f, 6.10351563e-05f, 0.000122070313f,
7 | 0.000244140625f, 0.00048828125f, 0.0009765625f, 0.001953125f,
8 | 0.00390625f, 0.0078125f, 0.015625f, 0.03125f,
9 | 0.0625f, 0.125f, 0.25f, 0.5f,
10 | 1.f, 2.f, 4.f, 8.f,
11 | 16.f, 32.f, 64.f, 128.f
12 | };
13 |
--------------------------------------------------------------------------------
/src/objects/model/object.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | template
4 | inline Object::Object(VkObjectType objectType, Type handle, std::shared_ptr allocator) noexcept:
5 | #if (VK_USE_64_BIT_PTR_DEFINES == 0)
6 | objectType(objectType),
7 | #endif
8 | handle(handle),
9 | hostAllocator(std::move(allocator))
10 | {
11 | MAGMA_UNUSED(objectType);
12 | }
13 |
14 | template
15 | inline VkObjectType Object::getType() const noexcept
16 | {
17 | #if (VK_USE_64_BIT_PTR_DEFINES == 1)
18 | return ObjectType::getType();
19 | #else
20 | return objectType;
21 | #endif
22 | }
23 | } // namespace magma
24 |
--------------------------------------------------------------------------------
/src/states/tesselationState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr TesselationState::TesselationState(const uint32_t patchControlPoints /* 0 */) noexcept:
4 | VkPipelineTessellationStateCreateInfo{
5 | VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO,
6 | nullptr,
7 | 0, // flags
8 | patchControlPoints
9 | }
10 | {}
11 |
12 | constexpr hash_t TesselationState::hash() const noexcept
13 | {
14 | return core::hashArgs(
15 | sType,
16 | flags,
17 | patchControlPoints);
18 | }
19 |
20 | constexpr bool TesselationState::operator==(const TesselationState& other) const noexcept
21 | {
22 | return (flags == other.flags) && (patchControlPoints == other.patchControlPoints);
23 | }
24 | } // namespace magma
25 |
--------------------------------------------------------------------------------
/src/auxiliary/spirv/imm.vert:
--------------------------------------------------------------------------------
1 | #version 450
2 |
3 | layout(location = 0) in vec4 position;
4 | layout(location = 1) in vec4 normalPointSize;
5 | layout(location = 2) in vec4 color;
6 | layout(location = 3) in vec2 texCoord;
7 | layout(location = 0) out vec3 oNormal;
8 | layout(location = 1) out vec4 oColor;
9 | layout(location = 2) out vec2 oTexCoord;
10 |
11 | out gl_PerVertex {
12 | float gl_PointSize;
13 | vec4 gl_Position;
14 | };
15 |
16 | layout(push_constant) uniform PushConstants {
17 | mat4 world;
18 | mat4 viewProj;
19 | };
20 |
21 | void main()
22 | {
23 | oNormal = normalPointSize.xyz;
24 | oColor = color;
25 | oTexCoord = texCoord;
26 | gl_PointSize = normalPointSize.w;
27 | mat4 worldViewProj = world * viewProj;
28 | gl_Position = worldViewProj * position;
29 | }
30 |
--------------------------------------------------------------------------------
/src/misc/imageResolve.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | inline ImageResolve::ImageResolve(const Image *srcImage, const Image *dstImage) noexcept:
4 | VkImageResolve{
5 | srcImage->getSubresourceLayers(0),
6 | VkOffset3D{0, 0, 0},
7 | dstImage->getSubresourceLayers(0),
8 | VkOffset3D{0, 0, 0},
9 | srcImage->getExtent()
10 | }
11 | {}
12 |
13 | inline ImageResolve::ImageResolve(const Image *srcImage, uint32_t srcMipLevel,
14 | const Image *dstImage, uint32_t dstMipLevel) noexcept:
15 | VkImageResolve{
16 | srcImage->getSubresourceLayers(srcMipLevel),
17 | VkOffset3D{0, 0, 0},
18 | dstImage->getSubresourceLayers(dstMipLevel),
19 | VkOffset3D{0, 0, 0},
20 | srcImage->getExtent()
21 | }
22 | {}
23 | } // namespace magma
24 |
--------------------------------------------------------------------------------
/src/objects/model/dispatchable.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | template
4 | inline Dispatchable::Dispatchable(VkObjectType objectType, Type handle) noexcept:
5 | Object(objectType, handle, nullptr)
6 | {}
7 |
8 | template
9 | inline Dispatchable::Dispatchable(VkObjectType objectType, std::shared_ptr allocator, Type handle) noexcept:
10 | Object(objectType, handle, std::move(allocator))
11 | {}
12 |
13 | template
14 | inline VkObjectType Dispatchable::getObjectType() const noexcept
15 | {
16 | return Object::getType();
17 | }
18 |
19 | template
20 | inline uint64_t Dispatchable::getObjectHandle() const noexcept
21 | {
22 | return reinterpret_cast(Object::handle);
23 | }
24 | } // namespace magma
25 |
--------------------------------------------------------------------------------
/src/objects/shaderModule.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | template
4 | inline ShaderModule::ShaderModule(std::shared_ptr device, const SpirvWord (&bytecode)[SpirvWordCount],
5 | hash_t bytecodeHash /* 0 */,
6 | std::shared_ptr allocator /* nullptr */,
7 | bool reflect /* false */,
8 | VkShaderModuleCreateFlags flags /* 0 */,
9 | #ifdef VK_EXT_validation_cache
10 | lent_ptr validationCache /* nullptr */,
11 | #endif
12 | const StructureChain& extendedInfo /* default */):
13 | ShaderModule(std::move(device), bytecode, SpirvWordCount * sizeof(SpirvWord), bytecodeHash,
14 | std::move(allocator), reflect, flags,
15 | #ifdef VK_EXT_validation_cache
16 | std::move(validationCache),
17 | #endif
18 | extendedInfo)
19 | {}
20 | } // namespace magma
21 |
--------------------------------------------------------------------------------
/src/states/colorBlendStateAdvanced.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | inline AdvancedColorBlendState::AdvancedColorBlendState(const AdvancedColorBlendState& other) noexcept:
4 | ColorBlendState(other),
5 | colorBlendAdvancedStateInfo(other.colorBlendAdvancedStateInfo),
6 | attachments(other.attachments)
7 | {
8 | pNext = &colorBlendAdvancedStateInfo;
9 | pAttachments = attachments.data();
10 | }
11 |
12 | inline AdvancedColorBlendState::AdvancedColorBlendState(AdvancedColorBlendState&& other) noexcept:
13 | ColorBlendState(std::move(other)),
14 | colorBlendAdvancedStateInfo(other.colorBlendAdvancedStateInfo),
15 | attachments(std::move(other.attachments))
16 | {
17 | pNext = &colorBlendAdvancedStateInfo;
18 | pAttachments = attachments.data();
19 | other.pNext = nullptr;
20 | other.pAttachments = nullptr;
21 | }
22 | } // namespace magma
23 |
--------------------------------------------------------------------------------
/src/extensions/extensionRegistry.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | template
4 | inline ExtensionRegistry::ExtensionRegistry(const std::vector& properties):
5 | count(0),
6 | supportedCount(0)
7 | {
8 | for (auto const& property: properties)
9 | {
10 | if constexpr (std::is_same::value)
11 | registry.emplace(property.extensionName, property);
12 | else if constexpr (std::is_same::value)
13 | registry.emplace(property.layerName, property);
14 | }
15 | }
16 |
17 | template
18 | inline bool ExtensionRegistry::supported(const char *name) noexcept
19 | {
20 | ++count;
21 | if (registry.find(name) == registry.end())
22 | return false;
23 | ++supportedCount;
24 | return true;
25 | }
26 | } // namespace magma
27 |
--------------------------------------------------------------------------------
/src/misc/scissor.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr Scissor::Scissor(const int32_t x, const int32_t y, const VkExtent2D& extent) noexcept:
4 | VkRect2D{{x, y}, extent}
5 | {}
6 |
7 | constexpr Scissor::Scissor(const float x, const float y, const VkExtent2D& extent) noexcept:
8 | VkRect2D{
9 | VkOffset2D{static_cast(x), static_cast(y)},
10 | extent
11 | }
12 | {}
13 |
14 | constexpr Scissor::Scissor(const int32_t x, const int32_t y, const uint32_t width, const uint32_t height) noexcept:
15 | VkRect2D{{x, y}, {width, height}}
16 | {}
17 |
18 | constexpr Scissor::Scissor(const float x, const float y, const float width, const float height) noexcept:
19 | VkRect2D{
20 | VkOffset2D{static_cast(x), static_cast(y)},
21 | VkExtent2D{static_cast(width), static_cast(height)}
22 | }
23 | {}
24 | } // namespace magma
25 |
--------------------------------------------------------------------------------
/src/auxiliary/profiler.inl:
--------------------------------------------------------------------------------
1 | namespace magma::aux
2 | {
3 | inline Profiler::Sample::Sample(const char *name, uint32_t frameIndex, double time) noexcept:
4 | name(name),
5 | frameIndex(frameIndex),
6 | time(time)
7 | {}
8 |
9 | inline Profiler::Section::Section(const char *name, uint32_t frameIndex, uint32_t beginQuery) noexcept:
10 | name(name),
11 | frameIndex(frameIndex),
12 | beginQuery(beginQuery)
13 | {}
14 |
15 | inline ScopedProfile::ScopedProfile(const char *name, lent_ptr cmdBuffer, uint32_t color /* 0xFFFFFFFF */):
16 | cmdBuffer(cmdBuffer.get()),
17 | profiler(Profiler::get(Profiler::Queue::Graphics))
18 | {
19 | if (profiler)
20 | profiler->beginSection(std::move(cmdBuffer), name, color);
21 | }
22 |
23 | inline ScopedProfile::~ScopedProfile()
24 | {
25 | if (profiler)
26 | profiler->endSection(cmdBuffer);
27 | }
28 | } // namespace magma::aux
29 |
--------------------------------------------------------------------------------
/unittest/popcount/build.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | rem Magma - Abstraction layer over Khronos Vulkan API.
3 | rem Copyright (C) 2018-2024 Victor Coda.
4 |
5 | rem This program is free software: you can redistribute it and/or modify
6 | rem it under the terms of the GNU General Public License as published by
7 | rem the Free Software Foundation, either version 3 of the License, or
8 | rem (at your option) any later version.
9 |
10 | rem This program is distributed in the hope that it will be useful,
11 | rem but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | rem GNU General Public License for more details.
14 |
15 | rem You should have received a copy of the GNU General Public License
16 | rem along with this program. If not, see .
17 |
18 | cl /std:c++17 /I%VULKAN_SDK%\Include /I..\..\src\third-party\pfr\include /I..\..\src /EHsc unittest.cpp /link
19 |
--------------------------------------------------------------------------------
/unittest/packing/build.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | rem Magma - Abstraction layer over Khronos Vulkan API.
3 | rem Copyright (C) 2018-2024 Victor Coda.
4 |
5 | rem This program is free software: you can redistribute it and/or modify
6 | rem it under the terms of the GNU General Public License as published by
7 | rem the Free Software Foundation, either version 3 of the License, or
8 | rem (at your option) any later version.
9 |
10 | rem This program is distributed in the hope that it will be useful,
11 | rem but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | rem GNU General Public License for more details.
14 |
15 | rem You should have received a copy of the GNU General Public License
16 | rem along with this program. If not, see .
17 |
18 | cl /std:c++17 /EHsc /D MAGMA_SSE /I%VULKAN_SDK%\Include /I..\..\src\third-party\pfr\include /I..\..\src unittest.cpp /link
19 |
--------------------------------------------------------------------------------
/src/states/inputAssemblyState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr InputAssemblyState::InputAssemblyState(const VkPrimitiveTopology topology,
4 | const bool primitiveRestartEnable /* false */) noexcept:
5 | VkPipelineInputAssemblyStateCreateInfo{
6 | VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
7 | nullptr, // pNext
8 | 0, // flags
9 | topology,
10 | MAGMA_BOOLEAN(primitiveRestartEnable)
11 | }
12 | {}
13 |
14 | constexpr hash_t InputAssemblyState::hash() const noexcept
15 | {
16 | return core::hashArgs(
17 | sType,
18 | flags,
19 | topology,
20 | primitiveRestartEnable);
21 | }
22 |
23 | constexpr bool InputAssemblyState::operator==(const InputAssemblyState& other) const noexcept
24 | {
25 | return (flags == other.flags) &&
26 | (topology == other.topology) &&
27 | (primitiveRestartEnable == other.primitiveRestartEnable);
28 | }
29 | } // namespace magma
30 |
--------------------------------------------------------------------------------
/unittest/linkNode/build.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | rem Magma - Abstraction layer over Khronos Vulkan API.
3 | rem Copyright (C) 2018-2024 Victor Coda.
4 |
5 | rem This program is free software: you can redistribute it and/or modify
6 | rem it under the terms of the GNU General Public License as published by
7 | rem the Free Software Foundation, either version 3 of the License, or
8 | rem (at your option) any later version.
9 |
10 | rem This program is distributed in the hope that it will be useful,
11 | rem but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | rem GNU General Public License for more details.
14 |
15 | rem You should have received a copy of the GNU General Public License
16 | rem along with this program. If not, see .
17 |
18 | cl /std:c++17 /EHsc /I%VULKAN_SDK%\Include /I..\..\src\third-party\pfr\include /I..\..\src /I..\..\src\core unittest.cpp ../../src/misc/structureChain.cpp /link
19 |
--------------------------------------------------------------------------------
/unittest/structureChain/build.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 | rem Magma - Abstraction layer over Khronos Vulkan API.
3 | rem Copyright (C) 2018-2024 Victor Coda.
4 |
5 | rem This program is free software: you can redistribute it and/or modify
6 | rem it under the terms of the GNU General Public License as published by
7 | rem the Free Software Foundation, either version 3 of the License, or
8 | rem (at your option) any later version.
9 |
10 | rem This program is distributed in the hope that it will be useful,
11 | rem but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | rem GNU General Public License for more details.
14 |
15 | rem You should have received a copy of the GNU General Public License
16 | rem along with this program. If not, see .
17 |
18 | cl /std:c++17 /EHsc /I%VULKAN_SDK%\Include /I..\..\src\third-party\pfr\include /I..\..\src /I..\..\src\core unittest.cpp ../../src/misc/structureChain.cpp /link
19 |
--------------------------------------------------------------------------------
/unittest/popcount/Makefile:
--------------------------------------------------------------------------------
1 | # Magma - Abstraction layer over Khronos Vulkan API.
2 | # Copyright (C) 2018-2024 Victor Coda.
3 |
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 |
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 |
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | INCLUDE_DIR=-I$(VULKAN_SDK)/include -I../../src/third-party/pfr/include -I../../src
18 | CFLAGS=-std=c++17 -m64 $(INCLUDE_DIR)
19 |
20 | unittest: unittest.cpp
21 | g++ $(CFLAGS) unittest.cpp -o unittest
22 |
23 | clean:
24 | @rm -rf unittest
25 |
--------------------------------------------------------------------------------
/src/packed/horzor.inl:
--------------------------------------------------------------------------------
1 | namespace magma::packed
2 | {
3 | #ifdef MAGMA_SSE
4 | inline __m128i _mm_horizontal_or(__m128i v)
5 | {
6 | __m128i v2 = _mm_shuffle_epi32(v, _MM_SHUFFLE(3, 2, 3, 2));
7 | v = _mm_or_si128(v, v2); // x = x|z, y = y|w
8 | v2 = _mm_shuffle_epi32(v, _MM_SHUFFLE(1, 1, 1, 1)); // move z to the x
9 | return _mm_or_si128(v, v2); // x|y|z|w
10 | }
11 | #elif defined(MAGMA_NEON)
12 | inline int32x2_t vhorizontal_or(int32x4_t v)
13 | {
14 | int32x2_t v1 = vget_low_s32(v);
15 | int32x2_t v2 = vget_high_s32(v);
16 | v1 = vorr_s32(v1, v2); // x = x|z, y = y|w
17 | v2 = vext_s32(v1, v1, 1); // move y to x
18 | return vorr_s32(v1, v2); // x = x|y|z|w
19 | }
20 |
21 | inline uint32x2_t vhorizontal_or(uint32x4_t v)
22 | {
23 | uint32x2_t v1 = vget_low_u32(v);
24 | uint32x2_t v2 = vget_high_u32(v);
25 | v1 = vorr_u32(v1, v2); // x = x|z, y = y|w
26 | v2 = vext_u32(v1, v1, 1); // move y to x
27 | return vorr_u32(v1, v2); // x = x|y|z|w
28 | }
29 | #endif // MAGMA_NEON
30 | } // namespace magma::packed
31 |
--------------------------------------------------------------------------------
/unittest/linkNode/Makefile:
--------------------------------------------------------------------------------
1 | # Magma - Abstraction layer over Khronos Vulkan API.
2 | # Copyright (C) 2018-2024 Victor Coda.
3 |
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 |
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 |
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | INCLUDE_DIR=-I$(VULKAN_SDK)/include -I../../src/third-party/pfr/include -I../../src -I../../src/core
18 | CFLAGS=-std=c++17 -m64 $(INCLUDE_DIR)
19 | SRC_OBJS=\
20 | unittest.cpp \
21 | ../../src/misc/structureChain.cpp
22 |
23 | unittest: unittest.cpp
24 | g++ $(CFLAGS) $(SRC_OBJS) -o unittest
25 |
26 | clean:
27 | @rm -rf unittest
28 |
--------------------------------------------------------------------------------
/unittest/structureChain/Makefile:
--------------------------------------------------------------------------------
1 | # Magma - Abstraction layer over Khronos Vulkan API.
2 | # Copyright (C) 2018-2024 Victor Coda.
3 |
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 |
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 |
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | INCLUDE_DIR=-I$(VULKAN_SDK)/include -I../../src/third-party/pfr/include -I../../src -I../../src/core
18 | CFLAGS=-std=c++17 -m64 $(INCLUDE_DIR)
19 | SRC_OBJS=\
20 | unittest.cpp \
21 | ../../src/misc/structureChain.cpp
22 |
23 | unittest: unittest.cpp
24 | g++ $(CFLAGS) $(SRC_OBJS) -o unittest
25 |
26 | clean:
27 | @rm -rf unittest
28 |
--------------------------------------------------------------------------------
/src/misc/viewport.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr Viewport::Viewport(const float x, const float y, const float width, const float height,
4 | const float minDepth /* 0.f */, const float maxDepth /* 1.f */) noexcept:
5 | VkViewport{
6 | x, y,
7 | width, height,
8 | minDepth,
9 | maxDepth
10 | }
11 | {}
12 |
13 | constexpr Viewport::Viewport(const float x, const float y, const VkExtent2D& extent,
14 | const float minDepth /* 0.f */, const float maxDepth /* 1.f */) noexcept:
15 | VkViewport{
16 | x, y,
17 | static_cast(extent.width),
18 | static_cast(extent.height),
19 | minDepth,
20 | maxDepth
21 | }
22 | {}
23 |
24 | constexpr Viewport::Viewport(const uint32_t x, const uint32_t y, const uint32_t width, const uint32_t height,
25 | const float minDepth /* 0.f */, const float maxDepth /* 1.f */) noexcept:
26 | VkViewport{
27 | static_cast(x),
28 | static_cast(y),
29 | static_cast(width),
30 | static_cast(height),
31 | minDepth,
32 | maxDepth
33 | }
34 | {}
35 | } // namespace magma
36 |
--------------------------------------------------------------------------------
/src/misc/borderColor.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr BorderColor::BorderColor(VkBorderColor color) noexcept:
4 | color(color)
5 | #ifdef VK_EXT_custom_border_color
6 | ,customColor{},
7 | format(VK_FORMAT_UNDEFINED)
8 | #endif
9 | {}
10 |
11 | #ifdef VK_EXT_custom_border_color
12 | constexpr BorderColor::BorderColor(const VkClearColorValue customColor, const VkFormat format /* VK_FORMAT_UNDEFINED */) noexcept:
13 | color(VK_BORDER_COLOR_FLOAT_CUSTOM_EXT),
14 | customColor(customColor),
15 | format(format)
16 | {}
17 |
18 | constexpr bool BorderColor::custom() const noexcept
19 | {
20 | return VK_BORDER_COLOR_FLOAT_CUSTOM_EXT == color;
21 | }
22 | #endif // VK_EXT_custom_border_color
23 |
24 | constexpr hash_t BorderColor::hash() const noexcept
25 | {
26 | #ifdef VK_EXT_custom_border_color
27 | if (custom())
28 | {
29 | return core::hashArgs(
30 | customColor.float32[0],
31 | customColor.float32[1],
32 | customColor.float32[2],
33 | customColor.float32[3],
34 | format);
35 | }
36 | #endif // VK_EXT_custom_border_color
37 | return core::hash(color);
38 | }
39 | } // namespace magma
40 |
--------------------------------------------------------------------------------
/src/states/minmaxSamplerState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr MinmaxSamplerState::MinmaxSamplerState(const SamplerState& state, const VkSamplerReductionModeEXT reductionMode) noexcept:
4 | SamplerState(state),
5 | reductionModeInfo{
6 | VK_STRUCTURE_TYPE_SAMPLER_REDUCTION_MODE_CREATE_INFO_EXT,
7 | state.pNext,
8 | reductionMode
9 | }
10 | {
11 | pNext = &reductionModeInfo;
12 | }
13 |
14 | constexpr MinmaxSamplerState::MinmaxSamplerState(const MinmaxSamplerState& other) noexcept:
15 | SamplerState(other),
16 | reductionModeInfo(other.reductionModeInfo)
17 | {
18 | pNext = &reductionModeInfo;
19 | }
20 |
21 | constexpr hash_t MinmaxSamplerState::hash() const noexcept
22 | {
23 | hash_t hash = SamplerState::hash();
24 | return core::hashCombine(hash, core::hashArgs(
25 | reductionModeInfo.sType,
26 | reductionModeInfo.reductionMode));
27 | }
28 |
29 | constexpr bool MinmaxSamplerState::operator==(const MinmaxSamplerState& other) const noexcept
30 | {
31 | return SamplerState::operator==(other) &&
32 | (reductionModeInfo.reductionMode == other.reductionModeInfo.reductionMode);
33 | }
34 | } // namespace magma
35 |
--------------------------------------------------------------------------------
/unittest/packing/Makefile:
--------------------------------------------------------------------------------
1 | # Magma - Abstraction layer over Khronos Vulkan API.
2 | # Copyright (C) 2018-2024 Victor Coda.
3 |
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 |
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 |
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | ARCH = $(shell uname -m)
18 |
19 | ifeq ($(ARCH), x86_64)
20 | SIMD_FLAGS = -DMAGMA_SSE -msse4
21 | else
22 | SIMD_FLAGS = -DMAGMA_NEON
23 | endif
24 |
25 | INCLUDE_DIR=-I$(VULKAN_SDK)/include -I../../src/third-party/pfr/include -I../../src
26 | CFLAGS=-std=c++17 -m64 $(SIMD_FLAGS) $(INCLUDE_DIR)
27 |
28 | unittest: unittest.cpp
29 | g++ $(CFLAGS) unittest.cpp -o unittest
30 |
31 | clean:
32 | @rm -rf unittest
33 |
--------------------------------------------------------------------------------
/src/extensions/deviceLayers.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY), without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "deviceLayers.h"
21 | #include "../objects/physicalDevice.h"
22 |
23 | namespace magma
24 | {
25 | DeviceLayers::DeviceLayers(const std::shared_ptr& device):
26 | ExtensionRegistry(device->enumerateLayers()),
27 |
28 | MAGMA_CHECK_LAYER(AMD_switchable_graphics),
29 |
30 | MAGMA_CHECK_LAYER(KHRONOS_validation)
31 | {}
32 | } // namespace magma
33 |
--------------------------------------------------------------------------------
/src/misc/imageResolve.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/image.h"
20 |
21 | namespace magma
22 | {
23 | struct ImageResolve : VkImageResolve
24 | {
25 | explicit ImageResolve(const Image *srcImage,
26 | const Image *dstImage) noexcept;
27 | explicit ImageResolve(const Image *srcImage,
28 | uint32_t srcMipLevel,
29 | const Image *dstImage,
30 | uint32_t dstMipLevel) noexcept;
31 | };
32 | } // namespace magma
33 |
34 | #include "imageResolve.inl"
35 |
--------------------------------------------------------------------------------
/src/objects/indirectBuffer.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | template
4 | class DrawIndirectCommand final
5 | {
6 | public:
7 | explicit DrawIndirectCommand(IndirectBuffer *buffer, void *mappedData) noexcept:
8 | buffer(buffer)
9 | {
10 | MAGMA_ASSERT(buffer->getDrawCount() < buffer->getMaxDrawCount());
11 | if (buffer->persistentlyMapped())
12 | drawCmd = reinterpret_cast(mappedData) + buffer->getDrawCount();
13 | else
14 | {
15 | drawCmd = (DrawCommand *)buffer->getMemory()->map(
16 | sizeof(DrawCommand) * buffer->getDrawCount(),
17 | sizeof(DrawCommand));
18 | MAGMA_ASSERT(drawCmd);
19 | }
20 | }
21 |
22 | ~DrawIndirectCommand()
23 | {
24 | if (!buffer->persistentlyMapped())
25 | buffer->getMemory()->unmap();
26 | }
27 |
28 | DrawCommand *operator->() { return drawCmd; }
29 | DrawCommand& operator*() { return *drawCmd; }
30 |
31 | operator bool() const { return drawCmd != nullptr; }
32 |
33 | private:
34 | IndirectBuffer *const buffer;
35 | DrawCommand *drawCmd;
36 | };
37 | } // namespace magma
38 |
--------------------------------------------------------------------------------
/src/descriptors/bufferArrayElement.inl:
--------------------------------------------------------------------------------
1 | namespace magma::descriptor
2 | {
3 | inline BufferArrayElement::BufferArrayElement(DescriptorSetLayoutBinding *array,
4 | VkDescriptorBufferInfo& element, VkBufferUsageFlags usage) noexcept:
5 | ArrayElement(array, element, usage)
6 | {}
7 |
8 | inline void BufferArrayElement::operator=(lent_ptr buffer) noexcept
9 | {
10 | MAGMA_ASSERT(buffer);
11 | MAGMA_ASSERT(MAGMA_BITWISE_AND(buffer->getUsage(), this->usage));
12 | if (this->element.buffer != *buffer)
13 | { // TODO: offset, range?
14 | this->element = buffer->getDescriptor();
15 | this->setDirty();
16 | }
17 | }
18 |
19 | inline TexelBufferArrayElement::TexelBufferArrayElement(DescriptorSetLayoutBinding *array,
20 | VkBufferView& element, VkBufferUsageFlags usage) noexcept:
21 | ArrayElement(array, element, usage)
22 | {}
23 |
24 | inline void TexelBufferArrayElement::operator=(lent_ptr bufferView) noexcept
25 | {
26 | MAGMA_ASSERT(bufferView);
27 | MAGMA_ASSERT(MAGMA_BITWISE_AND(bufferView->getBuffer()->getUsage(), this->usage));
28 | if (this->element != *bufferView)
29 | {
30 | this->element = *bufferView;
31 | this->setDirty();
32 | }
33 | }
34 | } // namespace magma::descriptor
35 |
--------------------------------------------------------------------------------
/src/objects/srcTransferBuffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "buffer.h"
20 |
21 | namespace magma
22 | {
23 | /* A buffer that can be used as the source of a transfer command. */
24 |
25 | class SrcTransferBuffer : public Buffer
26 | {
27 | public:
28 | explicit SrcTransferBuffer(std::shared_ptr device,
29 | VkDeviceSize size,
30 | std::shared_ptr allocator = nullptr,
31 | const Initializer& optional = Initializer(),
32 | const Sharing& sharing = Sharing());
33 | };
34 | } // namespace magma
35 |
--------------------------------------------------------------------------------
/src/objects/dstTransferBuffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "buffer.h"
20 |
21 | namespace magma
22 | {
23 | /* A buffer that can be used as the destination of a transfer command. */
24 |
25 | class DstTransferBuffer : public Buffer
26 | {
27 | public:
28 | explicit DstTransferBuffer(std::shared_ptr device,
29 | VkDeviceSize size,
30 | std::shared_ptr allocator = nullptr,
31 | const Initializer& optional = Initializer(),
32 | const Sharing& sharing = Sharing());
33 | };
34 | } // namespace magma
35 |
--------------------------------------------------------------------------------
/src/states/rasterizationOrderState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr RasterizationOrderState::RasterizationOrderState(const RasterizationState& state,
4 | const VkRasterizationOrderAMD rasterizationOrder) noexcept:
5 | RasterizationState(state),
6 | rasterizationOrderInfo{
7 | VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD,
8 | state.pNext,
9 | rasterizationOrder
10 | }
11 | {
12 | pNext = &rasterizationOrderInfo;
13 | }
14 |
15 | constexpr RasterizationOrderState::RasterizationOrderState(const RasterizationOrderState& other) noexcept:
16 | RasterizationState(other),
17 | rasterizationOrderInfo(other.rasterizationOrderInfo)
18 | {
19 | pNext = &rasterizationOrderInfo;
20 | }
21 |
22 | constexpr hash_t RasterizationOrderState::hash() const noexcept
23 | {
24 | hash_t hash = RasterizationState::hash();
25 | return core::hashCombine(hash, core::hashArgs(
26 | rasterizationOrderInfo.sType,
27 | rasterizationOrderInfo.rasterizationOrder));
28 | }
29 |
30 | constexpr bool RasterizationOrderState::operator==(const RasterizationOrderState& other) const noexcept
31 | {
32 | return RasterizationState::operator==(other) &&
33 | (rasterizationOrderInfo.rasterizationOrder == other.rasterizationOrderInfo.rasterizationOrder);
34 | }
35 | } // namespace magma
36 |
--------------------------------------------------------------------------------
/src/auxiliary/generateMipmap.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class Image;
23 | class CommandBuffer;
24 |
25 | namespace aux
26 | {
27 | /* Generates mip levels from base texture level using a
28 | series of image blit operations. This is an analogue of
29 | glGenerateMipmap() function in OpenGL. */
30 |
31 | bool generateMipmap(lent_ptr image,
32 | uint32_t baseLevel,
33 | VkFilter filter,
34 | lent_ptr cmdBuffer) noexcept;
35 | } // namespace aux
36 | } // namespace magma
37 |
--------------------------------------------------------------------------------
/src/states/tesselationState.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | /* Each tessellation control shader invocation operates
23 | on an input patch. The input patch is sized according
24 | to tesselation state. */
25 |
26 | struct TesselationState : VkPipelineTessellationStateCreateInfo
27 | {
28 | constexpr TesselationState(uint32_t patchControlPoints = 0) noexcept;
29 | constexpr hash_t hash() const noexcept;
30 | constexpr bool operator==(const TesselationState&) const noexcept;
31 | };
32 | } // namespace magma
33 |
34 | #include "tesselationState.inl"
35 |
--------------------------------------------------------------------------------
/src/misc/scissor.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | struct Scissor : VkRect2D
23 | {
24 | constexpr explicit Scissor(int32_t x, int32_t y,
25 | const VkExtent2D& extent) noexcept;
26 | constexpr explicit Scissor(float x, float y,
27 | const VkExtent2D& extent) noexcept;
28 | constexpr explicit Scissor(int32_t x, int32_t y,
29 | uint32_t width, uint32_t height) noexcept;
30 | constexpr explicit Scissor(float x, float y,
31 | float width, float height) noexcept;
32 | };
33 | } // namespace magma
34 |
35 | #include "scissor.inl"
36 |
--------------------------------------------------------------------------------
/src/misc/finish.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class CommandBuffer;
23 |
24 | /* Submits command buffer to a queue and blocks until
25 | submitted command buffer have completed execution.
26 | If is true, it blocks until all queue
27 | operations are finished. This is an analogue of
28 | glFinish() function in OpenGL. */
29 |
30 | void finish(lent_ptr cmdBuffer,
31 | lent_ptr queue,
32 | bool waitIdle = false);
33 | void finish(lent_ptr cmdBuffer,
34 | bool waitIdle = false);
35 | } // namespace magma
36 |
--------------------------------------------------------------------------------
/src/packed/ieee754.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #ifdef _MSC_VER
20 | #pragma warning(disable: 4201) // nonstandard extension used: nameless struct/union
21 | #endif
22 |
23 | namespace magma::packed
24 | {
25 | /* IEEE 754 single-precision binary floating-point format. */
26 |
27 | union Float32
28 | {
29 | float f;
30 | uint32_t i;
31 | struct
32 | { // Little-endian byte order
33 | uint32_t m: 23;
34 | uint32_t e: 8;
35 | uint32_t sign: 1;
36 | };
37 |
38 | static constexpr int bias = 127; // Exponent bias
39 | };
40 | } // namespace magma::packed
41 |
--------------------------------------------------------------------------------
/src/raytracing/address.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | #ifdef VK_KHR_acceleration_structure
23 | VkDeviceOrHostAddressConstKHR address(VkDeviceAddress deviceAddress) noexcept;
24 | VkDeviceOrHostAddressConstKHR address(const void *hostAddress) noexcept;
25 | VkDeviceOrHostAddressConstKHR address(const Buffer *buffer) noexcept;
26 | VkDeviceAddress align(std::size_t alignment, VkDeviceSize sizeOfBufferToBeAligned,
27 | VkDeviceAddress address, VkDeviceSize& sizeOfBufferLeftToOperate) noexcept;
28 | #endif // VK_KHR_acceleration_structure
29 | } // namespace magma
30 |
31 | #include "address.inl"
32 |
--------------------------------------------------------------------------------
/src/misc/extension.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | template
4 | inline void Extension::checkProcAddress(const char *extensionName) const
5 | {
6 | if (!procAddr)
7 | {
8 | #ifndef MAGMA_NO_EXCEPTIONS
9 | throw exception::ExtensionNotPresent(extensionName);
10 | #else
11 | MAGMA_HANDLE_RESULT(VK_ERROR_EXTENSION_NOT_PRESENT, extensionName);
12 | #endif
13 | }
14 | }
15 |
16 | template
17 | inline InstanceExtension::InstanceExtension(VkInstance instance, const char *procName) noexcept:
18 | Extension(vkGetInstanceProcAddr(instance, procName))
19 | {}
20 |
21 | template
22 | inline InstanceExtension::InstanceExtension(VkInstance instance, const char *procName, const char *extensionName):
23 | InstanceExtension(instance, procName)
24 | {
25 | Extension::checkProcAddress(extensionName);
26 | }
27 |
28 | template
29 | inline DeviceExtension::DeviceExtension(VkDevice device, const char *procName) noexcept:
30 | Extension(vkGetDeviceProcAddr(device, procName))
31 | {}
32 |
33 | template
34 | inline DeviceExtension::DeviceExtension(VkDevice device, const char *procName, const char *extensionName):
35 | DeviceExtension(device, procName)
36 | {
37 | Extension::checkProcAddress(extensionName);
38 | }
39 | } // namespace magma
40 |
--------------------------------------------------------------------------------
/src/renderpass/attachment.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr AttachmentDescription::AttachmentDescription(const VkFormat format, const uint32_t sampleCount,
4 | const LoadStoreOp& colorDepthOp, const LoadStoreOp& stencilOp,
5 | const VkImageLayout initialLayout, const VkImageLayout finalLayout) noexcept:
6 | VkAttachmentDescription{
7 | 0, // flags
8 | format,
9 | static_cast(sampleCount),
10 | colorDepthOp.load,
11 | colorDepthOp.store,
12 | stencilOp.load,
13 | stencilOp.store,
14 | initialLayout,
15 | finalLayout
16 | }
17 | {
18 | if ((sampleCount < 1) || (sampleCount > 64))
19 | samples = VK_SAMPLE_COUNT_1_BIT;
20 | }
21 |
22 | constexpr AttachmentDescription::AttachmentDescription(const LoadStoreOp& colorDepthOp, const LoadStoreOp& stencilOp,
23 | const VkImageLayout initialLayout, const VkImageLayout finalLayout) noexcept:
24 | AttachmentDescription(VK_FORMAT_UNDEFINED, 1, colorDepthOp, stencilOp, initialLayout, finalLayout)
25 | {}
26 |
27 | constexpr hash_t AttachmentDescription::hash() const noexcept
28 | {
29 | return core::hashArgs(
30 | flags,
31 | format,
32 | samples,
33 | loadOp,
34 | storeOp,
35 | stencilLoadOp,
36 | stencilStoreOp,
37 | initialLayout,
38 | finalLayout);
39 | }
40 | } // namespace magma
41 |
--------------------------------------------------------------------------------
/src/states/provokingVertexState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr ProvokingVertexRasterizationState::ProvokingVertexRasterizationState(const RasterizationState& state,
4 | const VkProvokingVertexModeEXT provokingVertexMode) noexcept:
5 | RasterizationState(state),
6 | provokingVertexInfo{
7 | VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_PROVOKING_VERTEX_STATE_CREATE_INFO_EXT,
8 | state.pNext,
9 | provokingVertexMode
10 | }
11 | {
12 | pNext = &provokingVertexInfo;
13 | }
14 |
15 | constexpr ProvokingVertexRasterizationState::ProvokingVertexRasterizationState(const ProvokingVertexRasterizationState& other) noexcept:
16 | RasterizationState(other),
17 | provokingVertexInfo(other.provokingVertexInfo)
18 | {
19 | pNext = &provokingVertexInfo;
20 | }
21 |
22 | constexpr hash_t ProvokingVertexRasterizationState::hash() const noexcept
23 | {
24 | hash_t hash = RasterizationState::hash();
25 | return core::hashCombine(hash, core::hashArgs(
26 | provokingVertexInfo.sType,
27 | provokingVertexInfo.provokingVertexMode));
28 | }
29 |
30 | constexpr bool ProvokingVertexRasterizationState::operator==(const ProvokingVertexRasterizationState& other) const noexcept
31 | {
32 | return RasterizationState::operator==(other) &&
33 | (provokingVertexInfo.provokingVertexMode == other.provokingVertexInfo.provokingVertexMode);
34 | }
35 | } // namespace magma
36 |
--------------------------------------------------------------------------------
/src/extensions/deviceLayers.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "extensionRegistry.h"
20 |
21 | namespace magma
22 | {
23 | class PhysicalDevice;
24 |
25 | /* Keep list of layers in alphabetical order! */
26 |
27 | class DeviceLayers : public ExtensionRegistry
28 | {
29 | public:
30 | // Advanced Micro Devices, Inc.
31 | const VkBool32 AMD_switchable_graphics: 1;
32 |
33 | // Khronos Group
34 | const VkBool32 KHRONOS_validation: 1;
35 |
36 | explicit DeviceLayers(const std::shared_ptr&);
37 | };
38 |
39 | MAGMA_TYPEDEF_MANAGED_PTR(DeviceLayers)
40 | } // namespace magma
41 |
--------------------------------------------------------------------------------
/src/helpers/executeCommandBuffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class CommandPool;
23 | class CommandBuffer;
24 | }
25 |
26 | namespace magma::helpers
27 | {
28 | /* Helper function to execute a number of calls to command buffer.
29 | It allocates command buffer, calls begin/end() and submits it
30 | to the graphics queue. */
31 |
32 | void executeCommandBuffer(lent_ptr cmdPool,
33 | std::function&)> cmdFn,
34 | const char *blockName = "magma::helpers::executeCommandBuffer",
35 | uint32_t blockColor = 0x0);
36 | } // namespace magma::helpers
37 |
--------------------------------------------------------------------------------
/src/objects/dstTransferBuffer.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "dstTransferBuffer.h"
21 |
22 | namespace magma
23 | {
24 | DstTransferBuffer::DstTransferBuffer(std::shared_ptr device, VkDeviceSize size,
25 | std::shared_ptr allocator /* nullptr */,
26 | const Initializer& optional /* default */,
27 | const Sharing& sharing /* default */):
28 | Buffer(std::move(device), size,
29 | 0, // flags
30 | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
31 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
32 | optional, sharing, std::move(allocator))
33 | {}
34 | } // namespace magma
35 |
--------------------------------------------------------------------------------
/src/objects/srcTransferBuffer.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "srcTransferBuffer.h"
21 |
22 | namespace magma
23 | {
24 | SrcTransferBuffer::SrcTransferBuffer(std::shared_ptr device, VkDeviceSize size,
25 | std::shared_ptr allocator /* nullptr */,
26 | const Initializer& optional /* default */,
27 | const Sharing& sharing /* default */):
28 | Buffer(std::move(device), size,
29 | 0, // flags
30 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
31 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
32 | optional, sharing, std::move(allocator))
33 | {}
34 | } // namespace magma
35 |
--------------------------------------------------------------------------------
/src/shaders/shaderReflectionFactory.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class ShaderReflection;
23 |
24 | /* An abstract interface to query shader reflection for
25 | validatation purposes. By design, library doesn't performs
26 | any filesystem I/O operations. Instead, user have to provide
27 | an implementation of his loading logic. */
28 |
29 | class IShaderReflectionFactory : public IClass
30 | {
31 | public:
32 | virtual const std::unique_ptr& getReflection(std::string_view fileName) = 0;
33 | };
34 |
35 | MAGMA_TYPEDEF_MANAGED_PTR(IShaderReflectionFactory)
36 | } // namespace magma
37 |
--------------------------------------------------------------------------------
/src/descriptors/descriptorSetLayoutBinding.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | inline DescriptorSetLayoutBinding::DescriptorSetLayoutBinding(VkDescriptorType descriptorType, uint32_t descriptorCount, uint32_t binding) noexcept:
4 | VkDescriptorSetLayoutBinding{
5 | binding,
6 | descriptorType,
7 | descriptorCount,
8 | 0, // stageFlags
9 | nullptr // pImmutableSamples
10 | },
11 | imageType(VK_IMAGE_TYPE_MAX_ENUM),
12 | #ifdef VK_EXT_descriptor_indexing
13 | bindingFlags(0),
14 | #endif
15 | dirty(false)
16 | {}
17 |
18 | inline void DescriptorSetLayoutBinding::setImageType(VkImageType imageType_) noexcept
19 | {
20 | if (imageType != VK_IMAGE_TYPE_MAX_ENUM)
21 | { // Descriptor should have the same image type
22 | MAGMA_ASSERT(imageType_ == imageType);
23 | }
24 | imageType = imageType_;
25 | }
26 |
27 | inline void DescriptorSetLayoutBinding::writeDescriptor(VkDescriptorSet dstSet, VkWriteDescriptorSet& writeDescriptorSet) const noexcept
28 | {
29 | MAGMA_ASSERT(resourceBinded());
30 | writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
31 | writeDescriptorSet.pNext = nullptr;
32 | writeDescriptorSet.dstSet = dstSet;
33 | writeDescriptorSet.dstBinding = binding;
34 | writeDescriptorSet.dstArrayElement = 0;
35 | writeDescriptorSet.descriptorCount = descriptorCount;
36 | writeDescriptorSet.descriptorType = descriptorType;
37 | }
38 | } // namespace magma
39 |
--------------------------------------------------------------------------------
/src/misc/viewport.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | struct Viewport : VkViewport
23 | {
24 | constexpr explicit Viewport(float x, float y,
25 | float width, float height,
26 | float minDepth = 0.f, float maxDepth = 1.f) noexcept;
27 | constexpr explicit Viewport(float x, float y,
28 | const VkExtent2D& extent,
29 | float minDepth = 0.f, float maxDepth = 1.f) noexcept;
30 | constexpr explicit Viewport(uint32_t x, uint32_t y,
31 | uint32_t width, uint32_t height,
32 | float minDepth = 0.f, float maxDepth = 1.f) noexcept;
33 | };
34 | } // namespace magma
35 |
36 | #include "viewport.inl"
37 |
--------------------------------------------------------------------------------
/src/misc/imageSubresourceRange.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | inline ImageSubresourceRange::ImageSubresourceRange(const Image *image,
4 | uint32_t baseMipLevel /* 0 */,
5 | uint32_t levelCount /* VK_REMAINING_MIP_LEVELS */) noexcept:
6 | VkImageSubresourceRange{
7 | image->getAspectMask(),
8 | baseMipLevel,
9 | levelCount,
10 | 0, // baseArrayLayer
11 | VK_REMAINING_ARRAY_LAYERS // layerCount
12 | }
13 | {}
14 |
15 | inline ImageSubresourceRange::ImageSubresourceRange(const Image1DArray *imageArray,
16 | uint32_t baseMipLevel /* 0 */,
17 | uint32_t levelCount /* VK_REMAINING_MIP_LEVELS */,
18 | uint32_t baseArrayLayer /* 0 */,
19 | uint32_t layerCount /* VK_REMAINING_ARRAY_LAYERS */) noexcept:
20 | VkImageSubresourceRange{
21 | imageArray->getAspectMask(),
22 | baseMipLevel,
23 | levelCount,
24 | baseArrayLayer,
25 | layerCount
26 | }
27 | {}
28 |
29 | inline ImageSubresourceRange::ImageSubresourceRange(const Image2DArray *imageArray,
30 | uint32_t baseMipLevel /* 0 */,
31 | uint32_t levelCount /* VK_REMAINING_MIP_LEVELS */,
32 | uint32_t baseArrayLayer /* 0 */,
33 | uint32_t layerCount /* VK_REMAINING_ARRAY_LAYERS */) noexcept:
34 | VkImageSubresourceRange{
35 | imageArray->getAspectMask(),
36 | baseMipLevel,
37 | levelCount,
38 | baseArrayLayer,
39 | layerCount
40 | }
41 | {}
42 | } // namespace magma
43 |
--------------------------------------------------------------------------------
/src/objects/computePipelineLibrary.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "pipelineLibrary.h"
20 |
21 | namespace magma
22 | {
23 | class PipelineLayout;
24 | class PipelineShaderStage;
25 |
26 | #ifdef VK_KHR_pipeline_library
27 | class ComputePipelineLibrary : public PipelineLibrary
28 | {
29 | public:
30 | explicit ComputePipelineLibrary(std::shared_ptr device,
31 | std::shared_ptr allocator = nullptr) noexcept;
32 | void compileComputeShader(const PipelineShaderStage& shaderStage,
33 | const std::unique_ptr& layout,
34 | VkPipelineCreateFlags flags = 0);
35 | };
36 | #endif // VK_KHR_pipeline_library
37 | } // namespace magma
38 |
--------------------------------------------------------------------------------
/src/objects/pipeline.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | #ifdef VK_EXT_pipeline_creation_feedback
4 | inline bool Pipeline::cacheHit() const noexcept
5 | {
6 | return creationFeedback.flags & VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
7 | }
8 |
9 | inline bool Pipeline::basePipelineAcceleration() const noexcept
10 | { // An implementation should set the VK_PIPELINE_CREATION_FEEDBACK_BASE_PIPELINE_ACCELERATION_BIT bit
11 | // if it was able to avoid a significant amount of work by using the base pipeline.
12 | return creationFeedback.flags & VK_PIPELINE_CREATION_FEEDBACK_BASE_PIPELINE_ACCELERATION_BIT_EXT;
13 | }
14 |
15 | inline uint64_t Pipeline::getCreationDuration() const noexcept
16 | {
17 | return creationFeedback.flags & VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT ? creationFeedback.duration : 0ull;
18 | }
19 |
20 | inline uint64_t Pipeline::getStageCreationDuration(uint32_t stageIndex) const noexcept
21 | {
22 | MAGMA_ASSERT(core::countof(stageCreationFeedbacks) == stageCount);
23 | MAGMA_ASSERT(stageIndex < stageCount);
24 | if (stageIndex >= stageCount)
25 | return 0ull;
26 | // Per-stage feedback may not be provided by an implementation
27 | const VkPipelineCreationFeedbackEXT& stageCreationFeedback = stageCreationFeedbacks[stageIndex];
28 | return stageCreationFeedback.flags & VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT ? stageCreationFeedback.duration : 0ull;
29 | }
30 | #endif // VK_EXT_pipeline_creation_feedback
31 | } // namespace magma
32 |
--------------------------------------------------------------------------------
/src/platform/viSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | associated with an nn::vi::Layer. */
25 |
26 | #ifdef VK_NN_vi_surface
27 | class ViSurface : public Surface
28 | {
29 | public:
30 | explicit ViSurface(Instance *instance,
31 | nn::vi::NativeWindowHandle window,
32 | std::shared_ptr allocator = nullptr,
33 | VkViSurfaceCreateFlagsNN flags = 0);
34 | nn::vi::NativeWindowHandle getWindow() const noexcept { return window; }
35 |
36 | private:
37 | const nn::vi::NativeWindowHandle window;
38 | };
39 | #endif // VK_NN_vi_surface
40 | } // namespace magma
41 |
--------------------------------------------------------------------------------
/src/core/refCountChecker.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma::core
21 | {
22 | /* Reference counter to detect circular references that
23 | would block the destruction of Vulkan objects. */
24 |
25 | #ifdef MAGMA_DEBUG
26 | class RefCountChecker final
27 | {
28 | public:
29 | RefCountChecker() noexcept: refCount(0ll) {}
30 | ~RefCountChecker()
31 | {
32 | MAGMA_ASSERT(0ll == refCount);
33 | }
34 | void addRef() noexcept { ++refCount; }
35 | void release() noexcept { --refCount; }
36 | int64_t getRefCount() const noexcept { return refCount; }
37 |
38 | private:
39 | std::atomic refCount;
40 | };
41 | #endif // MAGMA_DEBUG
42 | } // namespace magma::core
43 |
--------------------------------------------------------------------------------
/src/exceptions/spirvError.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "errorHandler.h"
20 | #ifndef MAGMA_NO_EXCEPTIONS
21 | #include "exception.h"
22 |
23 | namespace magma::exception
24 | {
25 | /* Error result of SPIRV library. */
26 |
27 | class SpirvError : public Exception
28 | {
29 | public:
30 | explicit SpirvError(spv_result_t error,
31 | const char *message,
32 | const source_location& location) noexcept:
33 | Exception(message, location), error(error) {}
34 | spv_result_t result() const noexcept { return error; }
35 | const char *description() const noexcept;
36 |
37 | private:
38 | const spv_result_t error;
39 | };
40 |
41 | } // namespace magma::exception
42 | #endif // !MAGMA_NO_EXCEPTIONS
43 |
--------------------------------------------------------------------------------
/src/misc/application.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "structureChain.h"
20 |
21 | namespace magma
22 | {
23 | /* IHVs (independent hardware vendors) can use information
24 | about application to provide application-specific
25 | optimizations for any program. */
26 |
27 | class Application final : public VkApplicationInfo
28 | {
29 | public:
30 | explicit Application(const char *applicationName) noexcept;
31 | explicit Application(const char *applicationName,
32 | uint32_t applicationVersion,
33 | const char *engineName,
34 | uint32_t engineVersion,
35 | uint32_t apiVersion,
36 | const StructureChain& extendedInfo = StructureChain()) noexcept;
37 | };
38 | } // namespace magma
39 |
--------------------------------------------------------------------------------
/src/helpers/executeDeferredOperation.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/deferredOperation.h"
20 |
21 | namespace magma
22 | {
23 | class DeferredOperation;
24 | }
25 |
26 | namespace magma::helpers
27 | {
28 | /* Helper function to execute deferred operation concurrently.
29 | Parameter should be used only if application
30 | wants to reduce the parallelism for this task. Otherwise,
31 | deferred operation will be scheduled onto all available
32 | threads in the system. */
33 |
34 | #ifdef VK_KHR_deferred_host_operations
35 | VkResult executeDeferredOperation(lent_ptr deferredOperation,
36 | uint32_t maxConcurrency = 0);
37 | #endif // VK_KHR_deferred_host_operations
38 | } // namespace magma::helpers
39 |
--------------------------------------------------------------------------------
/src/objects/pipelineLibrary.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "pipelineLibrary.h"
21 | #include "device.h"
22 | #include "../allocator/allocator.h"
23 |
24 | namespace magma
25 | {
26 | #ifdef VK_KHR_pipeline_library
27 | PipelineLibrary::PipelineLibrary(std::shared_ptr device, std::shared_ptr allocator) noexcept:
28 | device(std::move(device)),
29 | allocator(std::move(allocator))
30 | {}
31 |
32 | PipelineLibrary::~PipelineLibrary()
33 | {
34 | for (auto handle: libraries)
35 | vkDestroyPipeline(getNativeDevice(), handle, MAGMA_OPTIONAL(allocator));
36 | }
37 |
38 | VkDevice PipelineLibrary::getNativeDevice() const noexcept
39 | {
40 | return device->getHandle();
41 | }
42 | #endif // VK_KHR_pipeline_library
43 | } // namespace magma
44 |
--------------------------------------------------------------------------------
/src/platform/androidSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | that refers to an ANativeWindow, Android's
25 | native surface type. */
26 |
27 | #ifdef VK_KHR_android_surface
28 | class AndroidSurface : public Surface
29 | {
30 | public:
31 | explicit AndroidSurface(Instance *instance,
32 | ANativeWindow *window,
33 | std::shared_ptr allocator = nullptr,
34 | VkAndroidSurfaceCreateFlagsKHR flags = 0);
35 | const ANativeWindow *getWindow() const noexcept { return window; }
36 |
37 | private:
38 | const ANativeWindow *const window;
39 | };
40 | #endif // VK_KHR_android_surface
41 | } // namespace magma
42 |
--------------------------------------------------------------------------------
/src/descriptors/inlineUniformBlock.inl:
--------------------------------------------------------------------------------
1 | namespace magma::descriptor
2 | {
3 | template
4 | inline InlineUniformBlock::InlineUniformBlock(uint32_t binding) noexcept:
5 | DescriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT, sizeof(UniformBlockType), binding),
6 | descriptor{
7 | VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT,
8 | nullptr,
9 | sizeof(UniformBlockType),
10 | nullptr
11 | }
12 | {}
13 |
14 | template
15 | inline bool InlineUniformBlock::resourceBinded() const noexcept
16 | {
17 | return (descriptor.pData != nullptr);
18 | }
19 |
20 | template
21 | inline void InlineUniformBlock::write(VkDescriptorSet dstSet, VkWriteDescriptorSet& writeDescriptorSet) const noexcept
22 | {
23 | writeDescriptor(dstSet, writeDescriptorSet);
24 | writeDescriptorSet.pNext = &descriptor;
25 | writeDescriptorSet.pImageInfo = nullptr;
26 | writeDescriptorSet.pBufferInfo = nullptr;
27 | writeDescriptorSet.pTexelBufferView = nullptr;
28 | dirty = false;
29 | }
30 |
31 | template
32 | inline InlineUniformBlock& InlineUniformBlock::operator=(const UniformBlockType& inlineUniformBlock) noexcept
33 | {
34 | if (descriptor.pData != &inlineUniformBlock)
35 | {
36 | descriptor.pData = &inlineUniformBlock;
37 | dirty = true;
38 | }
39 | return *this;
40 | }
41 | } // namespace magma::descriptor
42 |
--------------------------------------------------------------------------------
/src/platform/metalSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | from CAMetalLayer, which is the native rendering
25 | surface of Apple's Metal framework. */
26 |
27 | #ifdef VK_EXT_metal_surface
28 | class MetalSurface : public Surface
29 | {
30 | public:
31 | explicit MetalSurface(Instance *instance,
32 | const CAMetalLayer *layer,
33 | std::shared_ptr allocator = nullptr,
34 | VkMetalSurfaceCreateFlagsEXT flags = 0);
35 | const CAMetalLayer *getLayer() const noexcept { return layer; }
36 |
37 | private:
38 | const CAMetalLayer *layer;
39 | };
40 | #endif // VK_EXT_metal_surface
41 | } // namespace magma
42 |
--------------------------------------------------------------------------------
/src/states/streamRasterizationState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr StreamRasterizationState::StreamRasterizationState(const RasterizationState& state, const uint32_t rasterizationStream,
4 | const VkPipelineRasterizationStateStreamCreateFlagsEXT flags /* 0 */) noexcept:
5 | RasterizationState(state),
6 | streamRasterizationInfo{
7 | VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_STREAM_CREATE_INFO_EXT,
8 | state.pNext,
9 | flags,
10 | rasterizationStream
11 | }
12 | {
13 | pNext = &streamRasterizationInfo;
14 | }
15 |
16 | constexpr StreamRasterizationState::StreamRasterizationState(const StreamRasterizationState& other) noexcept:
17 | RasterizationState(other),
18 | streamRasterizationInfo(other.streamRasterizationInfo)
19 | {
20 | pNext = &streamRasterizationInfo;
21 | }
22 |
23 | constexpr hash_t StreamRasterizationState::hash() const noexcept
24 | {
25 | hash_t hash = RasterizationState::hash();
26 | return core::hashCombine(hash, core::hashArgs(
27 | streamRasterizationInfo.sType,
28 | streamRasterizationInfo.flags,
29 | streamRasterizationInfo.rasterizationStream));
30 | }
31 |
32 | constexpr bool StreamRasterizationState::operator==(const StreamRasterizationState& other) const noexcept
33 | {
34 | return RasterizationState::operator==(other) &&
35 | (streamRasterizationInfo.flags == other.streamRasterizationInfo.flags) &&
36 | (streamRasterizationInfo.rasterizationStream == other.streamRasterizationInfo.rasterizationStream);
37 | }
38 | } // namespace magma
39 |
--------------------------------------------------------------------------------
/src/states/multisampleCoverageReductionState.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr MultisampleCoverageReductionState::MultisampleCoverageReductionState(const MultisampleState& state,
4 | const VkCoverageReductionModeNV coverageReductionMode) noexcept:
5 | MultisampleState(state),
6 | coverageReductionInfo{
7 | VK_STRUCTURE_TYPE_PIPELINE_COVERAGE_REDUCTION_STATE_CREATE_INFO_NV,
8 | state.pNext,
9 | 0, // flags
10 | coverageReductionMode
11 | }
12 | {
13 | pNext = &coverageReductionInfo;
14 | }
15 |
16 | constexpr MultisampleCoverageReductionState::MultisampleCoverageReductionState(const MultisampleCoverageReductionState& other) noexcept:
17 | MultisampleState(other),
18 | coverageReductionInfo(other.coverageReductionInfo)
19 | {
20 | pNext = &coverageReductionInfo;
21 | }
22 |
23 | constexpr hash_t MultisampleCoverageReductionState::hash() const noexcept
24 | {
25 | hash_t hash = MultisampleState::hash();
26 | return core::hashCombine(hash, core::hashArgs(
27 | coverageReductionInfo.sType,
28 | coverageReductionInfo.flags,
29 | coverageReductionInfo.coverageReductionMode));
30 | }
31 |
32 | constexpr bool MultisampleCoverageReductionState::operator==(const MultisampleCoverageReductionState& other) const noexcept
33 | {
34 | return MultisampleState ::operator==(other) &&
35 | (coverageReductionInfo.flags == other.coverageReductionInfo.flags) &&
36 | (coverageReductionInfo.coverageReductionMode == other.coverageReductionInfo.coverageReductionMode);
37 | }
38 | } // namespace magma
39 |
--------------------------------------------------------------------------------
/src/objects/externalImage.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "image2D.h"
20 |
21 | namespace magma
22 | {
23 | #ifdef VK_ANDROID_external_memory_android_hardware_buffer
24 | class AndroidHardwareBuffer;
25 | #endif
26 |
27 | /* Image bound to external memory such as Android hardware buffer
28 | or POSIX file descriptor. */
29 |
30 | class ExternalImage2D : public Image2D
31 | {
32 | public:
33 | #ifdef VK_ANDROID_external_memory_android_hardware_buffer
34 | explicit ExternalImage2D(std::shared_ptr device,
35 | lent_ptr hardwareBuffer,
36 | std::shared_ptr allocator = nullptr,
37 | const Sharing& sharing = Sharing());
38 | #endif // VK_ANDROID_external_memory_android_hardware_buffer
39 | };
40 | } // namespace magma
41 |
--------------------------------------------------------------------------------
/src/platform/fuchsiaSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | that refers to a Fuchsia imagePipeHandle. */
25 |
26 | #ifdef VK_FUCHSIA_imagepipe_surface
27 | class FuchsiaImagePipeSurface : public Surface
28 | {
29 | public:
30 | explicit FuchsiaImagePipeSurface(Instance *instance,
31 | zx_handle_t imagePipeHandle,
32 | std::shared_ptr allocator = nullptr,
33 | VkImagePipeSurfaceCreateFlagsFUCHSIA flags = 0);
34 | zx_handle_t getImagePipe() const noexcept { return imagePipeHandle; }
35 |
36 | private:
37 | const zx_handle_t imagePipeHandle;
38 | };
39 | #endif // VK_FUCHSIA_imagepipe_surface
40 | } // namespace magma
41 |
--------------------------------------------------------------------------------
/src/core/spinlock.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__x86_64__)
20 | #include
21 | #elif defined(__arm__) || defined(__aarch64__)
22 | #include
23 | #endif
24 |
25 | namespace magma::core
26 | {
27 | /* Fast spin-lock based mutex implementation.
28 | Used in scenarios where lock contention is expected
29 | to be low and the critical section is short.
30 | Instead of sleeping, threads actively wait (spin)
31 | until the lock becomes available. */
32 |
33 | class Spinlock
34 | {
35 | public:
36 | void lock() noexcept;
37 | void unlock() noexcept;
38 |
39 | private:
40 | std::atomic_flag flag = ATOMIC_FLAG_INIT;
41 | };
42 | } // namespace magma::core
43 |
44 | #include "spinlock.inl"
45 |
--------------------------------------------------------------------------------
/src/misc/featureQuery.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class Device;
23 |
24 | /* Allows to query whether feature has been enabled in one line.
25 | Feel free to add new features if necessary. */
26 |
27 | class FeatureQuery : NonCopyable
28 | {
29 | public:
30 | bool maintenanceEnabled(uint8_t versionIndex) const noexcept;
31 | bool negativeViewportHeightEnabled() const noexcept;
32 | bool separateDepthStencilLayoutsEnabled() const noexcept;
33 | bool extendedLinesEnabled() const noexcept;
34 | bool stippledLinesEnabled() const noexcept;
35 |
36 | private:
37 | MAGMA_MAKE_UNIQUE(FeatureQuery)
38 | FeatureQuery(const Device *device) noexcept;
39 | friend Device;
40 |
41 | const Device *device;
42 | };
43 | } // namespace magma
44 |
--------------------------------------------------------------------------------
/src/auxiliary/typedefs.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma::aux
21 | {
22 | MAGMA_TYPEDEF_MANAGED_PTR(AccumulationBuffer)
23 | MAGMA_TYPEDEF_MANAGED_PTR(BlitRectangle)
24 | MAGMA_TYPEDEF_MANAGED_PTR(ComputeProfiler)
25 | MAGMA_TYPEDEF_MANAGED_PTR(FillRectangleVertexShader)
26 | MAGMA_TYPEDEF_MANAGED_PTR(Framebuffer)
27 | MAGMA_TYPEDEF_MANAGED_PTR(FrameGrabber)
28 | MAGMA_TYPEDEF_MANAGED_PTR(GraphicsPipelineCache)
29 | MAGMA_TYPEDEF_MANAGED_PTR(GraphicsProfiler)
30 | MAGMA_TYPEDEF_MANAGED_PTR(ImageDescriptorSet)
31 | MAGMA_TYPEDEF_MANAGED_PTR(ImmediateRender)
32 | MAGMA_TYPEDEF_MANAGED_PTR(IShaderInclude)
33 | MAGMA_TYPEDEF_MANAGED_PTR(Profiler)
34 | MAGMA_TYPEDEF_MANAGED_PTR(ShaderCompiler)
35 | MAGMA_TYPEDEF_MANAGED_PTR(Sprite)
36 | MAGMA_TYPEDEF_MANAGED_PTR(TextShader)
37 | } // namespace magma::aux
38 |
--------------------------------------------------------------------------------
/src/objects/primaryCommandBuffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "commandBuffer.h"
20 |
21 | namespace magma
22 | {
23 | /* Primary command buffer, which can execute secondary
24 | command buffers, and which is submitted to the queue.
25 | As CommanPool allocates only objects of base CommandBuffer
26 | class, PrimaryCommandBuffer has its own constructor. */
27 |
28 | class PrimaryCommandBuffer : public CommandBuffer
29 | {
30 | public:
31 | explicit PrimaryCommandBuffer(lent_ptr cmdPool);
32 | void executeCommands(lent_ptr cmdBuffer) noexcept;
33 | void executeCommands(const std::initializer_list>& cmdBuffers) noexcept;
34 | void executeCommands(const std::vector>& cmdBuffers) noexcept;
35 | };
36 | } // namespace magma
37 |
--------------------------------------------------------------------------------
/src/platform/win32Surface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | that refers to a Win32 HWND. */
25 |
26 | #ifdef VK_KHR_win32_surface
27 | class Win32Surface : public Surface
28 | {
29 | public:
30 | explicit Win32Surface(Instance *instance,
31 | HINSTANCE hInstance,
32 | HWND hWnd,
33 | std::shared_ptr allocator = nullptr,
34 | VkWin32SurfaceCreateFlagsKHR flags = 0);
35 | HINSTANCE getHInstance() const noexcept { return hInstance; }
36 | HWND getWindow() const noexcept { return hWnd; }
37 |
38 | private:
39 | const HINSTANCE hInstance;
40 | const HWND hWnd;
41 | };
42 | #endif // VK_KHR_win32_surface
43 | } // namespace magma
44 |
--------------------------------------------------------------------------------
/src/objects/baseCountBuffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "buffer.h"
20 |
21 | namespace magma
22 | {
23 | /* Base class of count buffer. Allows to read back
24 | counter value(s) from device to host for debugging. */
25 |
26 | class BaseCountBuffer : public Buffer
27 | {
28 | public:
29 | VkPipelineStageFlags getStageMask() const noexcept { return stageMask; }
30 | void readback(lent_ptr cmdBuffer) const;
31 |
32 | protected:
33 | BaseCountBuffer(std::shared_ptr device,
34 | uint32_t elementCount,
35 | VkPipelineStageFlags stageMask,
36 | std::shared_ptr allocator,
37 | const Sharing& sharing);
38 |
39 | const VkPipelineStageFlags stageMask;
40 | std::unique_ptr stagingBuffer;
41 | };
42 | } // namespace magma
43 |
--------------------------------------------------------------------------------
/src/platform/iosSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | based on a UIView, the native surface type of iOS,
25 | which is underpinned by a CAMetalLayer, to support
26 | rendering to the surface using Apple's Metal framework. */
27 |
28 | #ifdef VK_MVK_ios_surface
29 | class iOSSurface : public Surface
30 | {
31 | public:
32 | explicit iOSSurface(Instance *instance,
33 | const void *view,
34 | std::shared_ptr allocator = nullptr,
35 | VkIOSSurfaceCreateFlagsMVK flags = 0);
36 | const void *getView() const noexcept { return view; }
37 |
38 | private:
39 | const void *const view;
40 | };
41 | #endif // VK_MVK_ios_surface
42 | } // namespace magma
43 |
--------------------------------------------------------------------------------
/src/exceptions/reflectionError.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "errorHandler.h"
20 | #ifndef MAGMA_NO_EXCEPTIONS
21 | #include "../third-party/SPIRV-Reflect/spirv_reflect.h"
22 | #include "exception.h"
23 |
24 | namespace magma::exception
25 | {
26 | /* Error result of SPIRV-Reflect library. */
27 |
28 | class ReflectionError : public Exception
29 | {
30 | public:
31 | explicit ReflectionError(SpvReflectResult error,
32 | const char *message,
33 | const source_location& location) noexcept:
34 | Exception(message, location), error(error) {}
35 | SpvReflectResult result() const noexcept { return error; }
36 | const char *description() const noexcept;
37 |
38 | private:
39 | const SpvReflectResult error;
40 | };
41 |
42 | } // namespace magma::exception
43 | #endif // !MAGMA_NO_EXCEPTIONS
44 |
--------------------------------------------------------------------------------
/src/platform/macosSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | based on an NSView, the native surface type of macOS,
25 | which is underpinned by a CAMetalLayer, to support
26 | rendering to the surface using Apple's Metal framework. */
27 |
28 | #ifdef VK_MVK_macos_surface
29 | class MacOSSurface : public Surface
30 | {
31 | public:
32 | explicit MacOSSurface(Instance *instance,
33 | const void *view,
34 | std::shared_ptr allocator = nullptr,
35 | VkMacOSSurfaceCreateFlagsMVK flags = 0);
36 | const void *getView() const noexcept { return view; }
37 |
38 | private:
39 | const void *const view;
40 | };
41 | #endif // VK_MVK_macos_surface
42 | } // namespace magma
43 |
--------------------------------------------------------------------------------
/src/objects/pipelineLayout.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | inline PipelineLayout::PipelineLayout(std::shared_ptr device,
4 | std::shared_ptr allocator /* nullptr */,
5 | VkPipelineLayoutCreateFlags flags /* 0 */):
6 | PipelineLayout(std::move(device), {/* empty */}, std::move(allocator), flags)
7 | {}
8 |
9 | inline PipelineLayout::PipelineLayout(lent_ptr setLayout,
10 | std::shared_ptr allocator /* nullptr */,
11 | VkPipelineLayoutCreateFlags flags /* 0 */):
12 | PipelineLayout(std::move(setLayout), {/* empty */}, std::move(allocator), flags)
13 | {}
14 |
15 | inline PipelineLayout::PipelineLayout(std::shared_ptr device, const PushConstantRange& pushConstantRange,
16 | std::shared_ptr allocator /* nullptr */,
17 | VkPipelineLayoutCreateFlags flags /* 0 */):
18 | PipelineLayout(std::move(device), {pushConstantRange}, std::move(allocator), flags)
19 | {}
20 |
21 | inline PipelineLayout::PipelineLayout(lent_ptr setLayout,
22 | const PushConstantRange& pushConstantRange,
23 | std::shared_ptr allocator /* nullptr */,
24 | VkPipelineLayoutCreateFlags flags /* 0 */):
25 | PipelineLayout(std::move(setLayout), {pushConstantRange}, std::move(allocator), flags)
26 | {}
27 |
28 | inline PipelineLayout::PipelineLayout(const std::initializer_list>& setLayouts,
29 | std::shared_ptr allocator /* nullptr */,
30 | VkPipelineLayoutCreateFlags flags /* 0 */):
31 | PipelineLayout(setLayouts, {/* empty */}, std::move(allocator), flags)
32 | {}
33 | } // namespace magma
34 |
--------------------------------------------------------------------------------
/src/platform/directFbSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | that refers to a DirectFB IDirectFBSurface. */
25 |
26 | #ifdef VK_EXT_directfb_surface
27 | class DirectFBSurface : public Surface
28 | {
29 | public:
30 | explicit DirectFBSurface(Instance *instance,
31 | IDirectFB *dfb,
32 | IDirectFBSurface *surface,
33 | std::shared_ptr allocator = nullptr,
34 | VkDirectFBSurfaceCreateFlagsEXT flags = 0);
35 | IDirectFB *getDirectFB() const noexcept { return dfb; }
36 | IDirectFBSurface *getSurface() const noexcept { return surface; }
37 |
38 | private:
39 | IDirectFB *dfb;
40 | IDirectFBSurface *surface;
41 | };
42 | #endif // VK_EXT_directfb_surface
43 | } // namespace magma
44 |
--------------------------------------------------------------------------------
/src/platform/xlibSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | that refers to an X11 Window, using the Xlib
25 | client-side library. */
26 |
27 | #ifdef VK_KHR_xlib_surface
28 | class XlibSurface : public Surface
29 | {
30 | public:
31 | explicit XlibSurface(Instance *instance,
32 | ::Display *dpy,
33 | Window window,
34 | std::shared_ptr allocator = nullptr,
35 | VkXlibSurfaceCreateFlagsKHR flags = 0);
36 | const ::Display *getDisplay() const noexcept { return dpy; }
37 | Window getWindow() const noexcept { return window; }
38 |
39 | private:
40 | const ::Display *const dpy;
41 | const Window window;
42 | };
43 | #endif // VK_KHR_xlib_surface
44 | } // namespace magma
45 |
--------------------------------------------------------------------------------
/src/raytracing/address.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | #ifdef VK_KHR_acceleration_structure
4 | inline VkDeviceOrHostAddressConstKHR address(VkDeviceAddress deviceAddress) noexcept
5 | {
6 | VkDeviceOrHostAddressConstKHR addr;
7 | addr.deviceAddress = deviceAddress;
8 | return addr;
9 | }
10 |
11 | inline VkDeviceOrHostAddressConstKHR address(const void *hostAddress) noexcept
12 | {
13 | VkDeviceOrHostAddressConstKHR addr;
14 | addr.hostAddress = hostAddress;
15 | return addr;
16 | }
17 |
18 | inline VkDeviceOrHostAddressConstKHR address(const Buffer *buffer) noexcept
19 | {
20 | VkDeviceOrHostAddressConstKHR addr;
21 | addr.deviceAddress = buffer ? buffer->getDeviceAddress() : MAGMA_NULL;
22 | return addr;
23 | }
24 |
25 | inline VkDeviceAddress align(std::size_t alignment, VkDeviceSize sizeOfBufferToBeAligned,
26 | VkDeviceAddress address, VkDeviceSize& sizeOfBufferLeftToOperate) noexcept
27 | {
28 | MAGMA_ASSERT(sizeOfBufferLeftToOperate);
29 | std::size_t size = static_cast(sizeOfBufferToBeAligned);
30 | void *ptr = reinterpret_cast(address);
31 | std::size_t space = static_cast(sizeOfBufferLeftToOperate);
32 | void *aligned = std::align(alignment, size, ptr, space);
33 | MAGMA_ASSERT(aligned);
34 | if (!aligned)
35 | return MAGMA_NULL;
36 | VkDeviceAddress alignedAddress = static_cast(reinterpret_cast(aligned));
37 | MAGMA_ASSERT(alignedAddress % alignment == 0);
38 | sizeOfBufferLeftToOperate = static_cast(space);
39 | return alignedAddress;
40 | }
41 | #endif // VK_KHR_acceleration_structure
42 | } // namespace magma
43 |
--------------------------------------------------------------------------------
/src/objects/deferredOperation.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "model/nondispatchable.h"
20 |
21 | namespace magma
22 | {
23 | /* Deferred host operations enables driver operations,
24 | including ray tracing pipeline compilation or CPU-based
25 | acceleration structure construction, to be offloaded
26 | to application-managed CPU thread pools. */
27 |
28 | #ifdef VK_KHR_deferred_host_operations
29 | class DeferredOperation : public NonDispatchable
30 | {
31 | public:
32 | explicit DeferredOperation(std::shared_ptr device,
33 | std::shared_ptr allocator = nullptr);
34 | ~DeferredOperation();
35 | uint32_t getMaxConcurrency() const noexcept;
36 | VkResult getResult() const noexcept;
37 | VkResult join() const noexcept;
38 | };
39 | #endif // VK_KHR_deferred_host_operations
40 | } // namespace magma
41 |
--------------------------------------------------------------------------------
/src/descriptors/arrayElement.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "descriptorSetLayoutBinding.h"
20 |
21 | namespace magma::descriptor
22 | {
23 | /* Base element of array of buffer or image descriptors.
24 | Allows to mark array of descriptors as dirty if
25 | single element of this array has been updated. */
26 |
27 | template
28 | class ArrayElement
29 | {
30 | protected:
31 | ArrayElement(DescriptorSetLayoutBinding *binding, Type& element, VkFlags usage) noexcept:
32 | element(element), usage(usage), binding(binding) {}
33 | void setImageType(VkImageType imageType) noexcept { binding->setImageType(imageType); }
34 | void setDirty() noexcept { binding->dirty = true; }
35 |
36 | Type& element;
37 | const VkFlags usage;
38 |
39 | private:
40 | DescriptorSetLayoutBinding *binding;
41 | };
42 | } // namespace magma::descriptor
43 |
--------------------------------------------------------------------------------
/src/platform/waylandSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | that refers to a Wayland wl_surface. */
25 |
26 | #ifdef VK_KHR_wayland_surface
27 | class WaylandSurface : public Surface
28 | {
29 | public:
30 | explicit WaylandSurface(Instance *instance,
31 | wl_display *display,
32 | wl_surface *surface,
33 | std::shared_ptr allocator = nullptr,
34 | VkWaylandSurfaceCreateFlagsKHR flags = 0);
35 | const wl_display *getDisplay() const noexcept { return display; }
36 | const wl_surface *getSurface() const noexcept { return surface; }
37 |
38 | private:
39 | const wl_display *const display;
40 | const wl_surface *const surface;
41 | };
42 | #endif // VK_KHR_wayland_surface
43 | } // namespace magma
44 |
--------------------------------------------------------------------------------
/src/extensions/extensionRegistry.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | /* A registry of instance- or device-level extensions. */
23 |
24 | template
25 | class ExtensionRegistry
26 | {
27 | public:
28 | uint32_t getCount() const noexcept { return count; }
29 | uint32_t getSupportedCount() const noexcept { return supportedCount; }
30 |
31 | protected:
32 | ExtensionRegistry(const std::vector& properties);
33 | bool supported(const char *name) noexcept;
34 |
35 | private:
36 | std::map registry;
37 | uint32_t count;
38 | uint32_t supportedCount;
39 | };
40 | } // namespace magma
41 |
42 | #include "extensionRegistry.inl"
43 |
44 | #define MAGMA_CHECK_LAYER(name) name(supported(MAGMA_LAYER_PREFIX #name))
45 | #define MAGMA_CHECK_EXTENSION(name) name(supported(MAGMA_EXTENSION_PREFIX #name))
46 |
--------------------------------------------------------------------------------
/src/auxiliary/spirv/font.frag:
--------------------------------------------------------------------------------
1 | // "96-bit 8x12 Font" by Flyguy: https://www.shadertoy.com/view/Mt2GWD
2 | #version 450
3 |
4 | #define GLYPH_SIZE vec2(8, 12)
5 |
6 | struct String
7 | {
8 | vec2 pos;
9 | uint first, last;
10 | vec4 color;
11 | };
12 |
13 | layout(push_constant) uniform pushConstants {
14 | uint stringCount;
15 | };
16 |
17 | layout(set = 0, binding = 0) readonly buffer stringBuffer {
18 | String strings[];
19 | };
20 |
21 | layout(set = 0, binding = 1) readonly buffer charBuffer {
22 | uvec4 chars[];
23 | };
24 |
25 | layout(location = 0) out vec4 oColor;
26 |
27 | bool extractBit(uint n, uint b)
28 | {
29 | return (n & (1 << b)) > 0 && (b < 24);
30 | }
31 |
32 | bool sprite(uvec4 glyph, vec2 size, vec2 pos)
33 | {
34 | bvec4 bounds;
35 | pos = floor(pos);
36 | bounds.xy = greaterThanEqual(pos, 0.0.xx);
37 | bounds.zw = lessThanEqual(pos, size);
38 | if (all(bounds)) // clip
39 | {
40 | vec2 d = size - pos;
41 | uvec4 bits = uvec4(d.x + d.y * size.x) - uvec4(72, 48, 24, 0);
42 | return
43 | extractBit(glyph.x, bits.x) ||
44 | extractBit(glyph.y, bits.y) ||
45 | extractBit(glyph.z, bits.z) ||
46 | extractBit(glyph.w, bits.w);
47 | }
48 | return false;
49 | }
50 |
51 | void main()
52 | {
53 | vec2 fragCoord = floor(gl_FragCoord.xy);
54 | oColor = 0.0.xxxx;
55 | for (uint i = 0; i < stringCount; ++i)
56 | {
57 | String s = strings[i];
58 | for (uint j = s.first; j <= s.last; ++j)
59 | {
60 | bool mask = sprite(chars[j], GLYPH_SIZE, fragCoord - s.pos);
61 | oColor += s.color * (mask ? 1. : 0.);
62 | s.pos.x += GLYPH_SIZE.x;
63 | }
64 | }
65 | if (0. == oColor.a)
66 | discard;
67 | }
68 |
--------------------------------------------------------------------------------
/src/objects/indexBuffer.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | template
4 | template
5 | inline TIndexBuffer::TIndexBuffer(lent_ptr cmdBuffer, const IndexType (&indices)[IndexCount],
6 | std::shared_ptr allocator /* nullptr */,
7 | const Initializer& optional /* default */,
8 | const Sharing& sharing /* default */,
9 | CopyMemoryFn copyMem /* nullptr */):
10 | IndexBuffer(std::move(cmdBuffer), magma::getIndexType(), IndexCount * sizeof(IndexType), indices,
11 | std::move(allocator), optional, sharing, std::move(copyMem))
12 | {}
13 |
14 | template
15 | constexpr VkIndexType getIndexType() noexcept
16 | {
17 | static_assert(
18 | #if defined(VK_KHR_index_type_uint8) || defined(VK_EXT_index_type_uint8)
19 | std::is_same::value ||
20 | #endif
21 | std::is_same::value || std::is_same::value,
22 | #if defined(VK_KHR_index_type_uint8) || defined(VK_EXT_index_type_uint8)
23 | "index should be of unsigned char, short or int type");
24 | #else
25 | "index should be of unsigned short or int type");
26 | #endif
27 | #if defined(VK_KHR_index_type_uint8) || defined(VK_EXT_index_type_uint8)
28 | if constexpr (std::is_same::value)
29 | #ifdef VK_KHR_index_type_uint8
30 | return VK_INDEX_TYPE_UINT8_KHR;
31 | #else
32 | return VK_INDEX_TYPE_UINT8_EXT;
33 | #endif
34 | #endif // VK_KHR_index_type_uint8 || VK_EXT_index_type_uint8
35 | if constexpr (std::is_same::value)
36 | return VK_INDEX_TYPE_UINT16;
37 | return VK_INDEX_TYPE_UINT32;
38 | }
39 | } // namespace magma
40 |
--------------------------------------------------------------------------------
/src/helpers/uniformArray.inl:
--------------------------------------------------------------------------------
1 | namespace magma::helpers
2 | {
3 | template
4 | inline UniformArray::UniformArray(void *const buffer, const uint32_t arraySize) noexcept:
5 | buffer(reinterpret_cast(buffer)),
6 | arraySize(arraySize),
7 | minIndex(std::numeric_limits::max()),
8 | maxIndex(std::numeric_limits::min())
9 | {
10 | MAGMA_ASSERT(buffer);
11 | MAGMA_ASSERT(arraySize > 0);
12 | }
13 |
14 | template
15 | inline Type& UniformArray::operator[](const uint32_t index) noexcept
16 | {
17 | MAGMA_ASSERT(index < arraySize);
18 | minIndex = std::min(minIndex, index);
19 | maxIndex = std::max(maxIndex, index);
20 | return buffer[index];
21 | }
22 |
23 | template
24 | class UniformArray::Iterator
25 | {
26 | public:
27 | explicit Iterator(Type *ptr) noexcept:
28 | ptr(ptr)
29 | {}
30 |
31 | Iterator& operator++() noexcept
32 | {
33 | ++ptr;
34 | return *this;
35 | }
36 |
37 | Iterator operator++(int) noexcept
38 | {
39 | Iterator temp = *this;
40 | ++ptr;
41 | return temp;
42 | }
43 |
44 | Iterator& operator--() noexcept
45 | {
46 | --ptr;
47 | return *this;
48 | }
49 |
50 | Iterator operator--(int) noexcept
51 | {
52 | Iterator temp = *this;
53 | --ptr;
54 | return temp;
55 | }
56 |
57 | bool operator!=(const Iterator& it) const noexcept
58 | {
59 | return ptr != it.ptr;
60 | }
61 |
62 | Type& operator*() noexcept
63 | {
64 | return *ptr;
65 | }
66 |
67 | private:
68 | Type *ptr;
69 | };
70 | } // namespace magma::helpers
71 |
--------------------------------------------------------------------------------
/src/objects/mutableImage.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "image.h"
20 |
21 | namespace magma
22 | {
23 | /* Mutable image can be used to create an image view with
24 | a different format from the image. For multi-planar formats,
25 | image view can be created of a plane of the image. */
26 |
27 | class MutableImage : public Image
28 | {
29 | public:
30 | bool blockTexelViewCompatible() const noexcept;
31 |
32 | protected:
33 | MutableImage(std::shared_ptr device,
34 | VkImageType imageType,
35 | VkFormat format,
36 | const VkExtent3D& extent,
37 | uint32_t mipLevels,
38 | uint32_t arrayLayers,
39 | uint32_t samples,
40 | VkImageCreateFlags flags,
41 | const Initializer& optional,
42 | const Sharing& sharing,
43 | std::shared_ptr allocator);
44 | };
45 | } // namespace magma
46 |
--------------------------------------------------------------------------------
/src/platform/xcbSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | that refers to an X11 Window, using the XCB
25 | client-side library. */
26 |
27 | #ifdef VK_KHR_xcb_surface
28 | class XcbSurface : public Surface
29 | {
30 | public:
31 | explicit XcbSurface(Instance *instance,
32 | xcb_connection_t *connection,
33 | xcb_window_t window,
34 | std::shared_ptr allocator = nullptr,
35 | VkXcbSurfaceCreateFlagsKHR flags = 0);
36 | const xcb_connection_t *getConnection() const noexcept { return connection; }
37 | xcb_window_t getWindow() const noexcept { return window; }
38 |
39 | private:
40 | const xcb_connection_t *const connection;
41 | const xcb_window_t window;
42 | };
43 | #endif // VK_KHR_xcb_surface
44 | } // namespace magma
45 |
--------------------------------------------------------------------------------
/src/states/multiViewportState.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "viewportState.h"
20 |
21 | namespace magma
22 | {
23 | /* If VkPhysicalDeviceFeatures::multiViewport is supported,
24 | then multiple viewports and scissor rectangles can be defined. */
25 |
26 | class MultiViewportState : public ViewportState
27 | {
28 | public:
29 | explicit MultiViewportState(const std::vector& viewports);
30 | explicit MultiViewportState(const std::vector& viewports,
31 | const std::vector& scissors);
32 | MultiViewportState(const MultiViewportState&);
33 | MultiViewportState& operator=(const MultiViewportState&);
34 | hash_t hash() const noexcept override;
35 | bool operator==(const MultiViewportState&) const noexcept;
36 |
37 | private:
38 | std::vector viewports;
39 | std::vector scissors;
40 | };
41 | } // namespace magma
42 |
--------------------------------------------------------------------------------
/src/objects/model/iobject.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../../misc/compatibility.h"
20 |
21 | namespace magma
22 | {
23 | /* Base interface class of Vulkan object. At the API level,
24 | all objects are referred to by handles. There are two
25 | classes of handles, dispatchable and non-dispatchable. */
26 |
27 | class IObject : public IClass
28 | {
29 | public:
30 | enum class Class : uint8_t;
31 | virtual Class getClass() const noexcept = 0;
32 | virtual VkObjectType getObjectType() const noexcept = 0;
33 | virtual uint64_t getObjectHandle() const noexcept = 0;
34 | };
35 |
36 | /* Dispatchable handle types are a pointer to an opaque type.
37 | Non-dispatchable handle types are a 64-bit integer type
38 | whose meaning is implementation-dependent. */
39 |
40 | enum class IObject::Class : uint8_t
41 | {
42 | Dispatchable, NonDispatchable
43 | };
44 | } // namespace magma
45 |
--------------------------------------------------------------------------------
/src/platform/qnxSurface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/surface.h"
20 |
21 | namespace magma
22 | {
23 | /* Provides a mechanism to create a surface object
24 | that refers to a QNX Screen window. */
25 |
26 | #ifdef VK_QNX_screen_surface
27 | class QnxSurface : public Surface
28 | {
29 | public:
30 | explicit QnxSurface(Instance *instance,
31 | struct _screen_context *context,
32 | struct _screen_window *window,
33 | std::shared_ptr allocator = nullptr,
34 | VkScreenSurfaceCreateFlagsQNX flags = 0);
35 | struct _screen_context const *getContext() const noexcept { return context; }
36 | struct _screen_window const *getWindow() const noexcept { return window; }
37 |
38 | private:
39 | struct _screen_context const *const context;
40 | struct _screen_window const *const window;
41 | };
42 | #endif // VK_QNX_screen_surface
43 | } // namespace magma
44 |
--------------------------------------------------------------------------------
/src/states/multiColorBlendState.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "colorBlendState.h"
20 |
21 | namespace magma
22 | {
23 | /* Multiple attachment color blend state takes care
24 | about array of blend attachment states and thereof
25 | is copyable, but not constexpr-constructible. */
26 |
27 | struct MultiColorBlendState final : ColorBlendState
28 | {
29 | explicit MultiColorBlendState(const std::initializer_list& attachments,
30 | VkPipelineColorBlendStateCreateFlags flags = 0,
31 | const std::initializer_list& blendConstants = {1.f, 1.f, 1.f, 1.f}) noexcept;
32 | MultiColorBlendState(const ColorBlendState&) noexcept;
33 | MultiColorBlendState& operator=(const MultiColorBlendState&) noexcept;
34 | ~MultiColorBlendState();
35 | hash_t hash() const noexcept;
36 | bool operator==(const MultiColorBlendState&) const noexcept;
37 | };
38 | } // namespace magma
39 |
--------------------------------------------------------------------------------
/src/states/formattedOutput.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class VertexInputState;
23 | struct InputAssemblyState;
24 | struct TesselationState;
25 | class ViewportState;
26 | struct RasterizationState;
27 | struct MultisampleState;
28 | struct DepthStencilState;
29 | struct ColorBlendState;
30 | } // namespace magma
31 |
32 | std::ostream& operator<<(std::ostream&, const magma::VertexInputState&);
33 | std::ostream& operator<<(std::ostream&, const magma::InputAssemblyState&);
34 | std::ostream& operator<<(std::ostream&, const magma::TesselationState&);
35 | std::ostream& operator<<(std::ostream&, const magma::ViewportState&);
36 | std::ostream& operator<<(std::ostream&, const magma::RasterizationState&);
37 | std::ostream& operator<<(std::ostream&, const magma::MultisampleState&);
38 | std::ostream& operator<<(std::ostream&, const magma::DepthStencilState&);
39 | std::ostream& operator<<(std::ostream&, const magma::ColorBlendState&);
40 |
--------------------------------------------------------------------------------
/src/core/hash.inl:
--------------------------------------------------------------------------------
1 | namespace magma::core
2 | {
3 | template
4 | constexpr hash_t hash(T x) noexcept
5 | {
6 | ConstexprHash hasher;
7 | return hasher(x);
8 | }
9 |
10 | template
11 | constexpr hash_t hash(T *p) noexcept
12 | {
13 | ConstexprHash hasher;
14 | return hasher(reinterpret_cast(p));
15 | }
16 |
17 | template
18 | constexpr hash_t hashArg(hash_t seed, const T& arg) noexcept
19 | {
20 | return hashCombine(seed, hash(arg));
21 | }
22 |
23 | template
24 | constexpr hash_t hashArg(hash_t seed, const T& arg, Args... args) noexcept
25 | {
26 | hash_t value = hashCombine(seed, hash(arg));
27 | return hashArg(value, args...);
28 | }
29 |
30 | template
31 | constexpr hash_t hashArgs(const T& arg, Args... args) noexcept
32 | {
33 | return hashArg(hash(arg), args...);
34 | }
35 |
36 | template
37 | constexpr hash_t hashArray(const T (&arr)[N]) noexcept
38 | {
39 | return hashing::Fnv1a().hash(arr);
40 | }
41 |
42 | template
43 | inline hash_t hashArray(const T arr[], std::size_t count) noexcept
44 | {
45 | std::hash hasher;
46 | hash_t hash = hasher(arr[0]);
47 | for (std::size_t i = 1; i < count; ++i)
48 | hash = hashCombine(hash, hasher(arr[i]));
49 | return hash;
50 | }
51 |
52 | template
53 | constexpr hash_t hashString(const T *str) noexcept
54 | {
55 | return hashing::string::Fnv1a().hash(str);
56 | }
57 |
58 | template
59 | inline hash_t hashString(const std::basic_string& str) noexcept
60 | {
61 | std::hash> hasher;
62 | return hasher(str);
63 | }
64 | } // namespace magma::core
65 |
--------------------------------------------------------------------------------
/src/helpers/mapTyped.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/deviceMemory.h"
20 | #include "../objects/buffer.h"
21 |
22 | namespace magma::helpers
23 | {
24 | template
25 | inline Type *map(lent_ptr buffer,
26 | VkDeviceSize offset = 0,
27 | VkDeviceSize size = VK_WHOLE_SIZE,
28 | VkMemoryMapFlags flags = 0) noexcept
29 | {
30 | MAGMA_ASSERT(sizeof(Type) <= buffer->getSize());
31 | if (sizeof(Type) > buffer->getSize())
32 | return nullptr;
33 | if ((size != VK_WHOLE_SIZE) && (sizeof(Type) > size))
34 | return nullptr;
35 | void *ptr = buffer->getMemory()->map(offset, size, flags);
36 | MAGMA_ASSERT(ptr);
37 | return reinterpret_cast(ptr);
38 | }
39 |
40 | inline bool unmap(lent_ptr buffer) noexcept
41 | {
42 | auto& memory = buffer->getMemory();
43 | if (!memory->mapped() || memory->persistentlyMapped())
44 | return false;
45 | memory->unmap();
46 | return true;
47 | }
48 | } // namespace magma::helpers
49 |
--------------------------------------------------------------------------------
/src/helpers/layoutTransition.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class Image;
23 | class CommandBuffer;
24 | }
25 |
26 | namespace magma::helpers
27 | {
28 | /* Helper function to change layout of the image. As example,
29 | for VK_DESCRIPTOR_TYPE_STORAGE_IMAGE allowed layout is
30 | VK_IMAGE_LAYOUT_GENERAL, which isn't set by default. */
31 |
32 | bool layoutTransition(lent_ptr image,
33 | VkImageLayout newLayout,
34 | lent_ptr cmdBuffer,
35 | VkDependencyFlags dependencyFlags = 0);
36 |
37 | struct LayoutTransition
38 | {
39 | VkImageLayout newLayout = VK_IMAGE_LAYOUT_UNDEFINED;
40 | uint32_t baseMipLevel = 0;
41 | uint32_t baseArrayLayer = 0;
42 | };
43 |
44 | bool batchLayoutTransition(const std::unordered_map, LayoutTransition>& imageLayouts,
45 | lent_ptr cmdBuffer,
46 | VkDependencyFlags dependencyFlags = 0);
47 | } // namespace magma::helpers
48 |
--------------------------------------------------------------------------------
/src/descriptors/bufferArrayElement.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "arrayElement.h"
20 |
21 | namespace magma::descriptor
22 | {
23 | /* Element of array of buffer descriptors. */
24 |
25 | class BufferArrayElement final : ArrayElement
26 | {
27 | public:
28 | explicit BufferArrayElement(DescriptorSetLayoutBinding *array,
29 | VkDescriptorBufferInfo& element,
30 | VkBufferUsageFlags usage) noexcept;
31 | void operator=(lent_ptr) noexcept;
32 | };
33 |
34 | /* Element of array of texel buffer descriptors. */
35 |
36 | class TexelBufferArrayElement final : ArrayElement
37 | {
38 | public:
39 | explicit TexelBufferArrayElement(DescriptorSetLayoutBinding *array,
40 | VkBufferView& element,
41 | VkBufferUsageFlags usage) noexcept;
42 | void operator=(lent_ptr) noexcept;
43 | };
44 | } // namespace magma::descriptor
45 |
46 | #include "bufferArrayElement.inl"
47 |
--------------------------------------------------------------------------------
/src/barriers/bufferMemoryBarrier.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | constexpr BufferMemoryBarrier::BufferMemoryBarrier(const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask) noexcept:
4 | VkBufferMemoryBarrier{
5 | VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
6 | nullptr, // pNext
7 | srcAccessMask,
8 | dstAccessMask,
9 | VK_QUEUE_FAMILY_IGNORED,
10 | VK_QUEUE_FAMILY_IGNORED,
11 | VK_NULL_HANDLE, // buffer
12 | 0, // offset
13 | VK_WHOLE_SIZE
14 | }
15 | {}
16 |
17 | inline BufferMemoryBarrier::BufferMemoryBarrier(const Buffer *buffer,
18 | VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask,
19 | VkDeviceSize offset /* 0 */, VkDeviceSize size /* VK_WHOLE_SIZE */) noexcept:
20 | VkBufferMemoryBarrier{
21 | VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
22 | nullptr, // pNext
23 | srcAccessMask,
24 | dstAccessMask,
25 | VK_QUEUE_FAMILY_IGNORED,
26 | VK_QUEUE_FAMILY_IGNORED,
27 | *buffer,
28 | offset,
29 | size
30 | }
31 | {}
32 |
33 | inline BufferMemoryBarrier::BufferMemoryBarrier(const Buffer *buffer_, const BufferMemoryBarrier& barrier) noexcept:
34 | VkBufferMemoryBarrier{barrier}
35 | {
36 | buffer = *buffer_;
37 | }
38 |
39 | inline BufferMemoryBarrier::BufferMemoryBarrier(const Buffer *buffer, const BufferMemoryBarrier& barrier,
40 | VkDeviceSize offset, VkDeviceSize size) noexcept:
41 | VkBufferMemoryBarrier{
42 | barrier.sType,
43 | barrier.pNext,
44 | barrier.srcAccessMask,
45 | barrier.dstAccessMask,
46 | barrier.srcQueueFamilyIndex,
47 | barrier.dstQueueFamilyIndex,
48 | *buffer,
49 | offset,
50 | size
51 | }
52 | {}
53 | } // namespace magma
54 |
--------------------------------------------------------------------------------
/src/packed/x10y10z10w2unorm.inl:
--------------------------------------------------------------------------------
1 | namespace magma::packed
2 | {
3 | inline X10y10z10w2Unorm::X10y10z10w2Unorm(float x, float y, float z, uint32_t w /* 0 */) noexcept
4 | {
5 | #ifdef MAGMA_SSE
6 | __m128 v = _mm_set_ps(float(w), z, y, x);
7 | v = _mm_max_ps(v, _mm_setzero_ps());
8 | v = _mm_min_ps(v, _mm_set_ps(3.f, 1.f, 1.f, 1.f));
9 | __m128 scale = _mm_set_ps(1.f, 1023.f, 1023.f, 1023.f);
10 | v = _mm_mul_ps(v, scale);
11 | v = _mm_round_ps(v, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
12 | __m128 bitshift = _mm_set_ps(1024.f * 1024.f * 1024.f, 1024.f * 1024.f, 1024.f, 1.f);
13 | v = _mm_mul_ps(v, bitshift);
14 | __m128i iv = _mm_cvtps_epi32(v);
15 | iv = _mm_horizontal_or(iv);
16 | this->v = _mm_cvtsi128_si32(iv);
17 | #elif defined(MAGMA_NEON)
18 | float32x4_t v = {x, y, z, float(w)};
19 | v = vmaxq_f32(v, vdupq_n_f32(0.f));
20 | float32x4_t max = {1.f, 1.f, 1.f, 3.f};
21 | v = vminq_f32(v, max);
22 | float32x4_t scale = {1023.f, 1023.f, 1023.f, 1.f};
23 | v = vmulq_f32(v, scale);
24 | uint32x4_t iv = vcvtnq_u32_f32(v);
25 | // mask off any fraction
26 | uint32x4_t mask = {0x3FF, 0x3FF, 0x3FF, 0x3};
27 | iv = vandq_u32(iv, mask);
28 | int32x4_t bitshift = {0, 10, 20, 30};
29 | iv = vshlq_u32(iv, bitshift);
30 | uint32x2_t ored = vhorizontal_or(iv);
31 | this->v = vget_lane_u32(ored, 0);
32 | #else // FPU
33 | x = std::clamp(x, 0.f, 1.f);
34 | y = std::clamp(y, 0.f, 1.f);
35 | z = std::clamp(z, 0.f, 1.f);
36 | x = std::roundf(x * 1023.f);
37 | y = std::roundf(y * 1023.f);
38 | z = std::roundf(z * 1023.f);
39 | this->x = uint32_t(x) & 0x3FF;
40 | this->y = uint32_t(y) & 0x3FF;
41 | this->z = uint32_t(z) & 0x3FF;
42 | this->w = w & 0x3;
43 | #endif // FPU
44 | }
45 | } // namespace magma::packed
46 |
--------------------------------------------------------------------------------
/src/objects/pipelineLibrary.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class Device;
23 | class IAllocator;
24 |
25 | /* A pipeline library is a special pipeline that cannot be bound,
26 | instead it defines a set of shaders and shader groups which
27 | can be linked into other pipelines. */
28 |
29 | #ifdef VK_KHR_pipeline_library
30 | class PipelineLibrary : public IClass
31 | {
32 | public:
33 | ~PipelineLibrary();
34 | uint32_t getLibraryCount() const noexcept { return core::countof(libraries); }
35 | const VkPipeline *getLibraries() const noexcept { return libraries.data(); }
36 |
37 | protected:
38 | PipelineLibrary(std::shared_ptr device,
39 | std::shared_ptr allocator) noexcept;
40 | VkDevice getNativeDevice() const noexcept;
41 |
42 | std::shared_ptr device;
43 | std::shared_ptr allocator;
44 | std::vector libraries;
45 | };
46 | #endif // VK_KHR_pipeline_library
47 | } // namespace magma
48 |
--------------------------------------------------------------------------------
/src/exceptions/exception.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2025 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #ifndef MAGMA_NO_EXCEPTIONS
20 | #include "sourcelocation.h"
21 |
22 | namespace magma::exception
23 | {
24 | /* Base exception class. Provides optional information
25 | such as an error message, file name, and line number
26 | where the exception was thrown. Meant to work with
27 | string literals only, to prevent memory allocations
28 | while handling exceptions. */
29 |
30 | class Exception : public std::exception
31 | {
32 | public:
33 | explicit Exception(const char *message = "unknown exception") noexcept;
34 | explicit Exception(const char *message,
35 | const source_location& location) noexcept;
36 | const char *what() const noexcept override { return message; }
37 | const source_location& where() const noexcept { return location; }
38 |
39 | private:
40 | const char *message;
41 | source_location location;
42 | };
43 | } // namespace magma::exception
44 |
45 | #include "exception.inl"
46 | #endif // !MAGMA_NO_EXCEPTIONS
47 |
--------------------------------------------------------------------------------
/unittest/popcount/unittest.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "core/pch.h"
19 | #include "core/popcount.h"
20 |
21 | int main()
22 | {
23 | constexpr uint32_t a = 0b00000000000000000000000000000000;
24 | constexpr uint32_t b = 0b01010101010101010101010101010101;
25 | constexpr uint32_t c = 0b11111111111111111111111111111111;
26 | constexpr uint64_t d = 0b0000000000000000000000000000000000000000000000000000000000000000;
27 | constexpr uint64_t e = 0b0101010101010101010101010101010101010101010101010101010101010101;
28 | constexpr uint64_t f = 0b1111111111111111111111111111111111111111111111111111111111111111;
29 | std::cout << "Bit count: " << magma::core::popcount(a) << std::endl;
30 | std::cout << "Bit count: " << magma::core::popcount(b) << std::endl;
31 | std::cout << "Bit count: " << magma::core::popcount(c) << std::endl;
32 | std::cout << "Bit count: " << magma::core::popcount(d) << std::endl;
33 | std::cout << "Bit count: " << magma::core::popcount(e) << std::endl;
34 | std::cout << "Bit count: " << magma::core::popcount(f) << std::endl;
35 | return 0;
36 | }
37 |
--------------------------------------------------------------------------------
/src/misc/mapBuffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/buffer.h"
20 |
21 | namespace magma
22 | {
23 | template
24 | inline void mapRange(lent_ptr buffer, VkDeviceSize offset, VkDeviceSize size,
25 | std::function mapMem)
26 | {
27 | MAGMA_ASSERT(buffer);
28 | MAGMA_ASSERT(offset + (VK_WHOLE_SIZE == size ? 0 : size) <= buffer->getSize());
29 | auto& bufferMemory = buffer->getMemory();
30 | if (bufferMemory)
31 | {
32 | void *data = bufferMemory->map(offset, size);
33 | if (data) try
34 | {
35 | MAGMA_ASSERT(mapMem);
36 | mapMem(reinterpret_cast(data));
37 | bufferMemory->unmap();
38 | }
39 | catch (...)
40 | {
41 | bufferMemory->unmap();
42 | MAGMA_THROW;
43 | }
44 | }
45 | }
46 |
47 | template
48 | inline void map(lent_ptr buffer, std::function mapMem)
49 | {
50 | mapRange(std::move(buffer), 0, VK_WHOLE_SIZE, std::move(mapMem));
51 | }
52 | } // namespace magma
53 |
--------------------------------------------------------------------------------
/src/objects/displayMode.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "model/nondispatchable.h"
20 |
21 | namespace magma
22 | {
23 | class Display;
24 |
25 | /* Each display has one or more supported modes associated
26 | with it by default. */
27 |
28 | #ifdef VK_KHR_display
29 | class DisplayMode : public NonDispatchable
30 | {
31 | public:
32 | explicit DisplayMode(std::shared_ptr display,
33 | const VkExtent2D& visibleRegion,
34 | uint32_t refreshRate,
35 | std::shared_ptr allocator = nullptr,
36 | const StructureChain& extendedInfo = StructureChain());
37 | VkDisplayPlaneCapabilitiesKHR getDisplayPlaneCapabilities(uint32_t planeIndex) const;
38 | const VkExtent2D& getVisibleRegion() const noexcept { return visibleRegion; }
39 | uint32_t getRefreshRate() const noexcept { return refreshRate; }
40 |
41 | private:
42 | const VkExtent2D visibleRegion;
43 | const uint32_t refreshRate;
44 | };
45 | #endif // VK_KHR_display
46 | } // namespace magma
47 |
--------------------------------------------------------------------------------
/src/platform/iosSurface.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "iosSurface.h"
21 | #include "../objects/instance.h"
22 | #include "../objects/device.h"
23 | #include "../allocator/allocator.h"
24 | #include "../misc/extension.h"
25 | #include "../exceptions/errorResult.h"
26 |
27 | namespace magma
28 | {
29 | #ifdef VK_MVK_ios_surface
30 | iOSSurface::iOSSurface(Instance *instance,
31 | const void *view,
32 | std::shared_ptr allocator /* nullptr */,
33 | VkIOSSurfaceCreateFlagsMVK flags /* 0 */):
34 | Surface(instance, std::move(allocator)),
35 | view(view)
36 | {
37 | VkIOSSurfaceCreateInfoMVK surfaceInfo;
38 | surfaceInfo.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
39 | surfaceInfo.pNext = nullptr;
40 | surfaceInfo.flags = flags;
41 | surfaceInfo.pView = view;
42 | const VkResult result = vkCreateIOSSurfaceMVK(*instance, &surfaceInfo,
43 | MAGMA_OPTIONAL(hostAllocator), &handle);
44 | MAGMA_HANDLE_RESULT(result, "failed to create iOS surface");
45 | }
46 | #endif // VK_MVK_ios_surface
47 | } // namespace magma
48 |
--------------------------------------------------------------------------------
/src/platform/viSurface.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "viSurface.h"
21 | #include "../objects/instance.h"
22 | #include "../objects/device.h"
23 | #include "../allocator/allocator.h"
24 | #include "../misc/extension.h"
25 | #include "../exceptions/errorResult.h"
26 |
27 | namespace magma
28 | {
29 | #ifdef VK_NN_vi_surface
30 | ViSurface::ViSurface(Instance *instance,
31 | nn::vi::NativeWindowHandle window,
32 | std::shared_ptr allocator /* nullptr */,
33 | VkViSurfaceCreateFlagsNN flags /* 0 */):
34 | Surface(instance, std::move(allocator)),
35 | window(window)
36 | {
37 | VkViSurfaceCreateInfoNN surfaceInfo;
38 | surfaceInfo.sType = VK_STRUCTURE_TYPE_VI_SURFACE_CREATE_INFO_NN;
39 | surfaceInfo.pNext = nullptr;
40 | surfaceInfo.flags = flags;
41 | surfaceInfo.window = window;
42 | const VkResult result = vkCreateViSurfaceNN(*instance, &surfaceInfo,
43 | MAGMA_OPTIONAL(hostAllocator), &handle);
44 | MAGMA_HANDLE_RESULT(result, "failed to create Vi surface");
45 | }
46 | #endif // VK_NN_vi_surface
47 | } // namespace magma
48 |
--------------------------------------------------------------------------------
/src/platform/macosSurface.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "macosSurface.h"
21 | #include "../objects/instance.h"
22 | #include "../objects/device.h"
23 | #include "../allocator/allocator.h"
24 | #include "../misc/extension.h"
25 | #include "../exceptions/errorResult.h"
26 |
27 | namespace magma
28 | {
29 | #ifdef VK_MVK_macos_surface
30 | MacOSSurface::MacOSSurface(Instance *instance,
31 | const void *view,
32 | std::shared_ptr allocator /* nullptr */,
33 | VkMacOSSurfaceCreateFlagsMVK flags /* 0 */):
34 | Surface(instance, std::move(allocator)),
35 | view(view)
36 | {
37 | VkMacOSSurfaceCreateInfoMVK surfaceInfo;
38 | surfaceInfo.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
39 | surfaceInfo.pNext = nullptr;
40 | surfaceInfo.flags = flags;
41 | surfaceInfo.pView = view;
42 | const VkResult result = vkCreateMacOSSurfaceMVK(*instance, &surfaceInfo,
43 | MAGMA_OPTIONAL(hostAllocator), &handle);
44 | MAGMA_HANDLE_RESULT(result, "failed to create macOS surface");
45 | }
46 | #endif // VK_MVK_macos_surface
47 | } // namespace magma
48 |
--------------------------------------------------------------------------------
/src/core/spinlock.inl:
--------------------------------------------------------------------------------
1 | namespace magma::core
2 | {
3 | inline void Spinlock::lock() noexcept
4 | {
5 | while (flag.test_and_set(std::memory_order_acquire))
6 | {/* The pause intrinsic is used in spin-wait loops
7 | with the processors implementing dynamic execution
8 | (especially out-of-order execution). In the spin-
9 | wait loop, the pause intrinsic improves the speed
10 | at which the code detects the release of the lock
11 | and provides especially significant performance gain.
12 | The execution of the next instruction is delayed
13 | for an implementation-specific amount of time. */
14 | #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__x86_64__)
15 | _mm_pause();
16 | #elif defined(_M_ARM) || defined(_M_ARM64)
17 | /* In a Symmetric Multi-Threading (SMT) design,
18 | a thread can use a Yield instruction to give a hint
19 | to the processor that it is running on. The Yield
20 | hint indicates that whatever the thread is currently
21 | doing is of low importance, and so could yield. For
22 | example, the thread might be sitting in a spin-lock. */
23 | __yield();
24 | #elif defined(__arm__) || defined(__aarch64__)
25 | #if defined(__ARM_ACLE)
26 | __yield();
27 | #elif defined(__has_builtin)
28 | #if __has_builtin(__builtin_arm_yield)
29 | __builtin_arm_yield();
30 | #else
31 | __asm__ __volatile__("yield");
32 | #endif
33 | #else
34 | __asm__ __volatile__("yield");
35 | #endif
36 | #else // !x86-64 && !ARM
37 | std::this_thread::yield();
38 | #endif
39 | }
40 | }
41 |
42 | inline void Spinlock::unlock() noexcept
43 | {
44 | flag.clear(std::memory_order_release);
45 | }
46 | } // namespace magma::core
47 |
--------------------------------------------------------------------------------
/src/platform/metalSurface.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "metalSurface.h"
21 | #include "../objects/instance.h"
22 | #include "../objects/device.h"
23 | #include "../allocator/allocator.h"
24 | #include "../misc/extension.h"
25 | #include "../exceptions/errorResult.h"
26 |
27 | namespace magma
28 | {
29 | #ifdef VK_EXT_metal_surface
30 | MetalSurface::MetalSurface(Instance *instance,
31 | const CAMetalLayer *layer,
32 | std::shared_ptr allocator /* nullptr */,
33 | VkMetalSurfaceCreateFlagsEXT flags /* 0 */):
34 | Surface(instance, std::move(allocator)),
35 | layer(layer)
36 | {
37 | VkMetalSurfaceCreateInfoEXT surfaceInfo;
38 | surfaceInfo.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
39 | surfaceInfo.pNext = nullptr;
40 | surfaceInfo.flags = flags;
41 | surfaceInfo.pLayer = layer;
42 | const VkResult result = vkCreateMetalSurfaceEXT(*instance, &surfaceInfo,
43 | MAGMA_OPTIONAL(hostAllocator), &handle);
44 | MAGMA_HANDLE_RESULT(result, "failed to create Metal surface");
45 | }
46 | #endif // VK_EXT_metal_surface
47 | } // namespace magma
48 |
--------------------------------------------------------------------------------
/src/platform/win32ExternalSemaphore.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma
21 | {
22 | class Semaphore;
23 |
24 | /* An application using external memory may wish to
25 | synchronize access to that memory using semaphores.
26 | This extension enables an application to export
27 | semaphore payload to and import semaphore payload
28 | from Windows handles. */
29 |
30 | #ifdef VK_KHR_external_semaphore_win32
31 | class Win32ExternalSemaphore
32 | {
33 | public:
34 | ~Win32ExternalSemaphore();
35 | HANDLE getNtHandle() const;
36 |
37 | protected:
38 | Win32ExternalSemaphore(const Semaphore *self,
39 | VkExternalSemaphoreHandleTypeFlagBitsKHR handleType) noexcept;
40 | void importNtHandle(HANDLE hSemaphore,
41 | LPCWSTR name,
42 | VkSemaphoreImportFlagsKHR flags);
43 |
44 | const VkExternalSemaphoreHandleTypeFlagBitsKHR handleType;
45 |
46 | private:
47 | const Semaphore *self;
48 | mutable HANDLE hSemaphore;
49 | };
50 | #endif // VK_KHR_external_semaphore_win32
51 | } // namespace magma
52 |
--------------------------------------------------------------------------------
/src/helpers/uniformArray.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 |
20 | namespace magma::helpers
21 | {
22 | /* Proxy array that allows to iterate and access
23 | the elements of UniformBuffer object. */
24 |
25 | template
26 | class UniformArray
27 | {
28 | public:
29 | class Iterator;
30 | explicit UniformArray(void *const buffer, uint32_t arraySize) noexcept;
31 | uint32_t getArraySize() const noexcept { return arraySize; }
32 | uint32_t getFirstIndex() const noexcept { return minIndex; }
33 | uint32_t getUpdatedRange() const noexcept { return maxIndex - minIndex + 1; }
34 | constexpr std::size_t getElementSize() const noexcept { return sizeof(Type); }
35 | Iterator begin() const noexcept { return Iterator(buffer); }
36 | Iterator end() const noexcept { return Iterator(buffer + arraySize); }
37 | Type& operator[](uint32_t index) noexcept;
38 |
39 | private:
40 | Type *const buffer;
41 | const uint32_t arraySize;
42 | uint32_t minIndex, maxIndex;
43 | };
44 | } // namespace magma::helpers
45 |
46 | #include "uniformArray.inl"
47 |
--------------------------------------------------------------------------------
/src/auxiliary/fillRectangleVertexShader.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../shaders/pipelineShaderStage.h"
20 |
21 | namespace magma
22 | {
23 | class Device;
24 | class ShaderModule;
25 | class IAllocator;
26 |
27 | struct RasterizationState;
28 |
29 | namespace aux
30 | {
31 | /* Vertex shader that rasterizes full-screen quad.
32 | Uses oversized triangle fallback if not supported.
33 | https://www.khronos.org/registry/OpenGL/extensions/NV/NV_fill_rectangle.txt
34 | https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VK_NV_fill_rectangle.html */
35 |
36 | class FillRectangleVertexShader : public PipelineShaderStage
37 | {
38 | public:
39 | explicit FillRectangleVertexShader(std::shared_ptr device,
40 | std::shared_ptr allocator = nullptr);
41 | const RasterizationState& getRasterizationState() const noexcept { return rasterizationState; }
42 |
43 | private:
44 | const RasterizationState& rasterizationState;
45 | };
46 | } // namespace aux
47 | } // namespace magma
48 |
--------------------------------------------------------------------------------
/src/platform/androidSurface.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "androidSurface.h"
21 | #include "../objects/instance.h"
22 | #include "../objects/device.h"
23 | #include "../allocator/allocator.h"
24 | #include "../misc/extension.h"
25 | #include "../exceptions/errorResult.h"
26 |
27 | namespace magma
28 | {
29 | #ifdef VK_KHR_android_surface
30 | AndroidSurface::AndroidSurface(Instance *instance,
31 | ANativeWindow *window,
32 | std::shared_ptr allocator /* nullptr */,
33 | VkAndroidSurfaceCreateFlagsKHR flags /* 0 */):
34 | Surface(instance, std::move(allocator)),
35 | window(window)
36 | {
37 | VkAndroidSurfaceCreateInfoKHR surfaceInfo;
38 | surfaceInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
39 | surfaceInfo.pNext = nullptr;
40 | surfaceInfo.flags = flags;
41 | surfaceInfo.window = window;
42 | const VkResult result = vkCreateAndroidSurfaceKHR(*instance,
43 | &surfaceInfo, MAGMA_OPTIONAL(hostAllocator), &handle);
44 | MAGMA_HANDLE_RESULT(result, "failed to create Android surface");
45 | }
46 | #endif // VK_KHR_android_surface
47 | } // namespace magma
48 |
--------------------------------------------------------------------------------
/src/objects/image.inl:
--------------------------------------------------------------------------------
1 | namespace magma
2 | {
3 | inline Image::Mip::Mip() noexcept:
4 | extent{0, 0, 0},
5 | bufferOffset(0ull),
6 | texels(nullptr)
7 | {}
8 |
9 | inline Image::Mip::Mip(uint32_t width, uint32_t height, VkDeviceSize bufferOffset) noexcept:
10 | extent{width, height, 1},
11 | bufferOffset(bufferOffset),
12 | texels(nullptr)
13 | {}
14 |
15 | inline Image::Mip::Mip(const VkExtent2D& extent2D, VkDeviceSize bufferOffset) noexcept:
16 | extent{extent2D.width, extent2D.height, 1},
17 | bufferOffset(bufferOffset),
18 | texels(nullptr)
19 | {}
20 |
21 | inline Image::Mip::Mip(const VkExtent3D& extent3D, VkDeviceSize bufferOffset) noexcept:
22 | extent(extent3D),
23 | bufferOffset(bufferOffset),
24 | texels(nullptr)
25 | {}
26 |
27 | inline Image::Mip::Mip(uint32_t width, uint32_t height, VkDeviceSize size, const void *texels) noexcept:
28 | extent{width, height, 1},
29 | size(size),
30 | texels(texels)
31 | {}
32 |
33 | inline Image::Mip::Mip(const VkExtent2D& extent2D, VkDeviceSize size, const void *texels) noexcept:
34 | extent{extent2D.width, extent2D.height, 1},
35 | size(size),
36 | texels(texels)
37 | {}
38 |
39 | inline Image::Mip::Mip(const VkExtent3D& extent3D, VkDeviceSize size, const void *texels) noexcept:
40 | extent(extent3D),
41 | size(size),
42 | texels(texels)
43 | {}
44 |
45 | inline bool Image::hasUniformLayout() const noexcept
46 | {
47 | for (uint32_t level = 1; level < mipLevels; ++level)
48 | {
49 | if (mipLayouts[level] != mipLayouts[0])
50 | return false;
51 | }
52 | return true;
53 | }
54 |
55 | inline uint32_t Image::getTexelCount() const noexcept
56 | {
57 | uint32_t texelCount = 0;
58 | for (uint32_t level = 0; level < mipLevels; ++level)
59 | texelCount += getLevelTexelCount(level);
60 | return texelCount * arrayLayers;
61 | }
62 | } // namespace magma
63 |
--------------------------------------------------------------------------------
/src/renderpass/attachment.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "loadStoreOp.h"
20 |
21 | namespace magma
22 | {
23 | /* An attachment description describes the properties
24 | of an attachment including its format, sample count,
25 | and how its contents are treated at the beginning
26 | and end of each render pass instance. */
27 |
28 | struct AttachmentDescription : VkAttachmentDescription
29 | {
30 | constexpr AttachmentDescription(VkFormat format,
31 | uint32_t sampleCount,
32 | const LoadStoreOp& colorDepthOp,
33 | const LoadStoreOp& stencilOp,
34 | VkImageLayout initialLayout,
35 | VkImageLayout finalLayout) noexcept;
36 | constexpr AttachmentDescription(const LoadStoreOp& colorDepthOp,
37 | const LoadStoreOp& stencilOp,
38 | VkImageLayout initialLayout,
39 | VkImageLayout finalLayout) noexcept;
40 | constexpr uint32_t sampleCount() const noexcept { return static_cast(samples); }
41 | constexpr hash_t hash() const noexcept;
42 | };
43 | } // namespace magma
44 |
45 | #include "attachment.inl"
46 |
--------------------------------------------------------------------------------
/src/objects/semaphore.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "model/nondispatchable.h"
20 |
21 | namespace magma
22 | {
23 | /* Semaphores are a synchronization primitive that can be used
24 | to insert a dependency between batches submitted to queues.
25 | Semaphores have two states - signaled and unsignaled. The
26 | state of a semaphore can be signaled after execution of a
27 | batch of commands is completed. A batch can wait for a
28 | semaphore to become signaled before it begins execution,
29 | and the semaphore is also unsignaled before the batch
30 | begins execution. */
31 |
32 | class Semaphore : public NonDispatchable
33 | {
34 | public:
35 | explicit Semaphore(std::shared_ptr device,
36 | std::shared_ptr allocator = nullptr,
37 | VkSemaphoreCreateFlags flags = 0,
38 | const StructureChain& extendedInfo = StructureChain());
39 | ~Semaphore();
40 |
41 | protected:
42 | Semaphore(std::shared_ptr allocator,
43 | std::shared_ptr device) noexcept;
44 | };
45 | } // namespace magma
46 |
--------------------------------------------------------------------------------
/src/objects/conditionalRenderingBuffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "buffer.h"
20 |
21 | namespace magma
22 | {
23 | /* A buffer containing the predicate for conditional rendering. */
24 |
25 | #ifdef VK_EXT_conditional_rendering
26 | class ConditionalRenderingBuffer : public Buffer
27 | {
28 | public:
29 | explicit ConditionalRenderingBuffer(lent_ptr cmdBuffer,
30 | VkDeviceSize size,
31 | const void *data,
32 | std::shared_ptr allocator = nullptr,
33 | const Initializer& optional = Initializer(),
34 | const Sharing& sharing = Sharing(),
35 | CopyMemoryFn copyMem = nullptr);
36 | explicit ConditionalRenderingBuffer(lent_ptr cmdBuffer,
37 | lent_ptr srcBuffer,
38 | std::shared_ptr allocator = nullptr,
39 | VkDeviceSize size = 0,
40 | VkDeviceSize srcOffset = 0,
41 | const Initializer& optional = Initializer(),
42 | const Sharing& sharing = Sharing());
43 | };
44 | #endif // VK_EXT_conditional_rendering
45 | } // namespace magma
46 |
--------------------------------------------------------------------------------
/src/platform/xlibSurface.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #include "pch.h"
19 | #pragma hdrstop
20 | #include "xlibSurface.h"
21 | #include "../objects/instance.h"
22 | #include "../objects/device.h"
23 | #include "../allocator/allocator.h"
24 | #include "../misc/extension.h"
25 | #include "../exceptions/errorResult.h"
26 |
27 | namespace magma
28 | {
29 | #ifdef VK_KHR_xlib_surface
30 | XlibSurface::XlibSurface(Instance *instance,
31 | ::Display *dpy, Window window,
32 | std::shared_ptr allocator /* nullptr */,
33 | VkXlibSurfaceCreateFlagsKHR flags /* 0 */):
34 | Surface(instance, std::move(allocator)),
35 | dpy(dpy),
36 | window(window)
37 | {
38 | VkXlibSurfaceCreateInfoKHR surfaceInfo;
39 | surfaceInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
40 | surfaceInfo.pNext = nullptr;
41 | surfaceInfo.flags = flags;
42 | surfaceInfo.dpy = dpy;
43 | surfaceInfo.window = window;
44 | const VkResult result = vkCreateXlibSurfaceKHR(*instance, &surfaceInfo,
45 | MAGMA_OPTIONAL(hostAllocator), &handle);
46 | MAGMA_HANDLE_RESULT(result, "failed to create Xlib surface");
47 | }
48 | #endif // VK_KHR_xlib_surface
49 | } // namespace magma
50 |
--------------------------------------------------------------------------------
/src/misc/mapImage.h:
--------------------------------------------------------------------------------
1 | /*
2 | Magma - Abstraction layer over Khronos Vulkan API.
3 | Copyright (C) 2018-2024 Victor Coda.
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 | #pragma once
19 | #include "../objects/image.h"
20 |
21 | namespace magma
22 | {
23 | template
24 | inline void mapImageRange(lent_ptr image, VkDeviceSize offset, VkDeviceSize size,
25 | std::function mapMem)
26 | {
27 | MAGMA_ASSERT(image);
28 | MAGMA_ASSERT(image->getTiling() == VK_IMAGE_TILING_LINEAR);
29 | MAGMA_ASSERT(offset + (VK_WHOLE_SIZE == size ? 0 : size) <= image->getSize());
30 | auto& imageMemory = image->getMemory();
31 | if (imageMemory)
32 | {
33 | void *data = imageMemory->map(offset, size);
34 | MAGMA_ASSERT(data);
35 | if (data) try
36 | {
37 | MAGMA_ASSERT(mapMem);
38 | mapMem(static_cast(data));
39 | imageMemory->unmap();
40 | }
41 | catch (...)
42 | {
43 | imageMemory->unmap();
44 | MAGMA_THROW;
45 | }
46 | }
47 | }
48 |
49 | template
50 | inline void mapImage(lent_ptr