From 8c00af0443d8652b3da100c7d9e9c1e58a1398e2 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sun, 12 Jan 2025 20:25:13 +0100 Subject: [PATCH] adding CI --- .github/workflows/linux-build.yml | 97 ++++++++++ .github/workflows/macos-build.yml | 90 ++++++++++ .github/workflows/msys2-build.yml | 90 ++++++++++ .github/workflows/windows-build.yml | 85 +++++++++ Includes/Pulse.h | 9 +- README.md | 5 + Sources/Backends/Vulkan/VulkanBuffer.c | 45 +++++ Sources/Backends/Vulkan/VulkanBuffer.h | 4 +- .../Backends/Vulkan/VulkanDevicePrototypes.h | 1 + Sources/Backends/Vulkan/VulkanImage.c | 9 +- Sources/Backends/Vulkan/VulkanImage.h | 2 + Sources/PulseBackend.c | 1 + Sources/PulseBuffer.c | 82 +++++++++ Sources/PulseImage.c | 16 ++ Sources/PulseInternal.h | 5 + Sources/PulsePFNs.h | 6 +- Tests/Vulkan/Buffer.c | 167 ++++++++++++++++++ xmake.lua | 6 + 18 files changed, 710 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/linux-build.yml create mode 100644 .github/workflows/macos-build.yml create mode 100644 .github/workflows/msys2-build.yml create mode 100644 .github/workflows/windows-build.yml diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml new file mode 100644 index 0000000..7b2f3cf --- /dev/null +++ b/.github/workflows/linux-build.yml @@ -0,0 +1,97 @@ +name: Linux build + +on: + pull_request: + push: + paths-ignore: + - '.github/workflows/macos-build.yml' + - '.github/workflows/msys2-build.yml' + - '.github/workflows/windows-build.yml' + - '.gitignore' + - 'LICENSE' + - 'CHANGELOG.md' + - 'README.md' + +jobs: + build: + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + arch: [x86_64] + kind: [shared, static] + confs: + - { mode: debug, archive: yes } + - { mode: debug, config: --asan=y, archive: false, cache_key: -asan } + - { mode: debug, config: --lsan=y, archive: false } + - { mode: debug, config: --tsan=y, archive: false } + - { mode: release, archive: yes } + + runs-on: ${{ matrix.os }} + if: ${{ !contains(github.event.head_commit.message, 'ci skip') }} + + steps: + - name: Get current date as package key + id: cache_key + run: echo "key=$(date +'%W')" >> $GITHUB_OUTPUT + + - name: Checkout repository + uses: actions/checkout@v4 + + # Install system dependencies + - name: Install system dependencies + run: | + sudo apt-get update + sudo apt-get -y install mesa-common-dev + + # Force xmake to a specific folder (for cache) + - name: Set xmake env + run: echo "XMAKE_GLOBALDIR=${{ runner.workspace }}/xmake-global" >> $GITHUB_ENV + + # Install xmake + - name: Setup xmake + uses: xmake-io/github-action-setup-xmake@v1 + with: + xmake-version: branch@dev + actions-cache-folder: .xmake-cache-W${{ steps.cache_key.outputs.key }} + + # Update xmake repository (in order to have the file that will be cached) + - name: Update xmake repository + run: xmake repo --update + + # Fetch xmake dephash + - name: Retrieve dependencies hash + id: dep_hash + run: echo "hash=$(xmake l utils.ci.packageskey)" >> $GITHUB_OUTPUT + + # Cache xmake dependencies + - name: Restore cached xmake dependencies + id: restore-depcache + uses: actions/cache/restore@v4 + with: + path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages + key: Linux-${{ matrix.arch }}-${{ matrix.confs.mode }}${{ matrix.confs.cache_key }}-${{ steps.dep_hash.outputs.hash }}-W${{ steps.cache_key.outputs.key }} + + # Setup compilation mode and install project dependencies + - name: Configure xmake and install dependencies + run: xmake config --arch=${{ matrix.arch }} --mode=${{ matrix.confs.mode }} --static=${{ matrix.kind == 'static' && 'yes' || 'no' }} ${{ matrix.confs.config }} --ccache=n --unitybuild=y --yes + + # Save dependencies + - name: Save cached xmake dependencies + if: ${{ !steps.restore-depcache.outputs.cache-hit }} + uses: actions/cache/save@v4 + with: + path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages + key: ${{ steps.restore-depcache.outputs.cache-primary-key }} + + # Cache assets downloading + - name: Restore cached assets + id: restore-assets + uses: actions/cache/restore@v4 + with: + path: assets + key: assets-${{ hashFiles('assets/examples_version.txt', 'assets/unittests_version.txt') }} + + # Build the engine + - name: Build Pulse + run: xmake --yes diff --git a/.github/workflows/macos-build.yml b/.github/workflows/macos-build.yml new file mode 100644 index 0000000..cc4448d --- /dev/null +++ b/.github/workflows/macos-build.yml @@ -0,0 +1,90 @@ +name: macOS build + +on: + pull_request: + push: + paths-ignore: + - '.github/workflows/linux-build.yml' + - '.github/workflows/msys2-build.yml' + - '.github/workflows/windows-build.yml' + - '.gitignore' + - 'LICENSE' + - 'README.md' + +jobs: + build: + strategy: + fail-fast: false + matrix: + os: [macOS-latest] + arch: [x86_64] + kind: [shared, static] + confs: + - { mode: debug, archive: yes } + - { mode: debug, config: --asan=y, archive: false, cache_key: -asan } + - { mode: debug, config: --lsan=y, archive: false } + - { mode: debug, config: --tsan=y, archive: false } + - { mode: release, archive: yes } + + runs-on: ${{ matrix.os }} + if: ${{ !contains(github.event.head_commit.message, 'ci skip') }} + + steps: + - name: Get current date as package key + id: cache_key + run: echo "key=$(date +'%W')" >> $GITHUB_OUTPUT + + - name: Checkout repository + uses: actions/checkout@v4 + + # Force xmake to a specific folder (for cache) + - name: Set xmake env + run: echo "XMAKE_GLOBALDIR=${{ runner.workspace }}/xmake-global" >> $GITHUB_ENV + + # Install xmake + - name: Setup xmake + uses: xmake-io/github-action-setup-xmake@v1 + with: + xmake-version: branch@dev + actions-cache-folder: .xmake-cache-W${{ steps.cache_key.outputs.key }} + + # Update xmake repository (in order to have the file that will be cached) + - name: Update xmake repository + run: xmake repo --update + + # Fetch xmake dephash + - name: Retrieve dependencies hash + id: dep_hash + run: echo "hash=$(xmake l utils.ci.packageskey)" >> $GITHUB_OUTPUT + + # Cache xmake dependencies + - name: Restore cached xmake dependencies + id: restore-depcache + uses: actions/cache/restore@v4 + with: + path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages + key: macOS-${{ matrix.arch }}-${{ matrix.confs.mode }}${{ matrix.confs.cache_key }}-${{ steps.dep_hash.outputs.hash }}-W${{ steps.cache_key.outputs.key }} + + # Setup compilation mode and install project dependencies + - name: Configure xmake and install dependencies + run: xmake config --arch=${{ matrix.arch }} --mode=${{ matrix.confs.mode }} --static=${{ matrix.kind == 'static' && 'yes' || 'no' }} ${{ matrix.confs.config }} --ccache=n --unitybuild=y --yes + + # Save dependencies + - name: Save cached xmake dependencies + if: ${{ !steps.restore-depcache.outputs.cache-hit }} + uses: actions/cache/save@v4 + with: + path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages + key: ${{ steps.restore-depcache.outputs.cache-primary-key }} + + # Cache assets downloading + - name: Restore cached assets + id: restore-assets + uses: actions/cache/restore@v4 + with: + path: assets + key: assets-${{ hashFiles('assets/examples_version.txt', 'assets/unittests_version.txt') }} + + # Build the engine + - name: Build Pulse + run: xmake --yes diff --git a/.github/workflows/msys2-build.yml b/.github/workflows/msys2-build.yml new file mode 100644 index 0000000..00defad --- /dev/null +++ b/.github/workflows/msys2-build.yml @@ -0,0 +1,90 @@ +name: MSYS2 build (MinGW-w64) + +on: + pull_request: + push: + paths-ignore: + - '.github/workflows/linux-build.yml' + - '.github/workflows/macos-build.yml' + - '.github/workflows/windows-build.yml' + - '.gitignore' + - 'LICENSE' + - 'README.md' + +jobs: + build: + strategy: + matrix: + msystem: [mingw64] + os: [windows-latest] + arch: [x86_64] + mode: [debug, release] + kind: [shared, static] + + runs-on: ${{ matrix.os }} + if: ${{ !contains(github.event.head_commit.message, 'ci skip') }} + + defaults: + run: + shell: msys2 {0} + + steps: + - name: Get current date as package key + id: cache_key + shell: bash + run: echo "key=$(date +'%W')" >> $GITHUB_OUTPUT + + - name: Checkout repository + uses: actions/checkout@v4 + + # Setup MSYS2 + - uses: msys2/setup-msys2@v2 + with: + msystem: ${{ matrix.msystem }} + install: base-devel git unzip p7zip mingw-w64-${{ matrix.arch }}-toolchain mingw-w64-${{ matrix.arch }}-xmake + update: true + + # Force xmake to a specific folder (for cache) + - name: Set xmake env + run: echo "XMAKE_GLOBALDIR=${{ runner.workspace }}/xmake-global" >> $GITHUB_ENV + + # Update xmake repository (in order to have the file that will be cached) + - name: Update xmake repository + run: xmake repo --update + + # Fetch xmake dephash + - name: Retrieve dependencies hash + id: dep_hash + run: echo "hash=$(xmake l utils.ci.packageskey)" >> $GITHUB_OUTPUT + + # Cache xmake dependencies + - name: Restore cached xmake dependencies + id: restore-depcache + uses: actions/cache/restore@v4 + with: + path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages + key: MinGW-${{ matrix.arch }}-${{ matrix.mode }}-${{ steps.dep_hash.outputs.hash }}-W${{ steps.cache_key.outputs.key }} + + # Setup compilation mode and install project dependencies + - name: Configure xmake and install dependencies + run: xmake config --arch=${{ matrix.arch }} --mode=${{ matrix.mode }} --static=${{ matrix.kind == 'static' && 'yes' || 'no' }} --ccache=n --unitybuild=y --yes + + # Save dependencies + - name: Save cached xmake dependencies + if: ${{ !steps.restore-depcache.outputs.cache-hit }} + uses: actions/cache/save@v4 + with: + path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages + key: ${{ steps.restore-depcache.outputs.cache-primary-key }} + + # Cache assets downloading + - name: Restore cached assets + id: restore-assets + uses: actions/cache/restore@v4 + with: + path: assets + key: assets-${{ hashFiles('assets/examples_version.txt', 'assets/unittests_version.txt') }} + + # Build the engine + - name: Build Pulse + run: xmake --yes diff --git a/.github/workflows/windows-build.yml b/.github/workflows/windows-build.yml new file mode 100644 index 0000000..395a621 --- /dev/null +++ b/.github/workflows/windows-build.yml @@ -0,0 +1,85 @@ +name: Windows build + +on: + pull_request: + push: + paths-ignore: + - '.github/workflows/linux-build.yml' + - '.github/workflows/macos-build.yml' + - '.github/workflows/wasm-build.yml' + - '.gitignore' + - 'LICENSE' + - 'README.md' + +jobs: + build: + strategy: + matrix: + os: [windows-latest] + arch: [x64] + mode: [debug, release] + kind: [shared, static] + + runs-on: ${{ matrix.os }} + if: ${{ !contains(github.event.head_commit.message, 'ci skip') }} + + steps: + - name: Get current date as package key + id: cache_key + run: echo "key=$(date +'%W')" >> $GITHUB_OUTPUT + shell: bash + + - name: Checkout repository + uses: actions/checkout@v4 + + # Force xmake to a specific folder (for cache) + - name: Set xmake env + run: echo "XMAKE_GLOBALDIR=${{ runner.workspace }}/xmake-global" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append + + # Install xmake + - name: Setup xmake + uses: xmake-io/github-action-setup-xmake@v1 + with: + xmake-version: branch@dev + actions-cache-folder: .xmake-cache-W${{ steps.cache_key.outputs.key }} + + # Update xmake repository (in order to have the file that will be cached) + - name: Update xmake repository + run: xmake repo --update + + # Fetch xmake dephash + - name: Retrieve dependencies hash + id: dep_hash + run: echo "hash=$(xmake l utils.ci.packageskey)" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append + + # Cache xmake dependencies + - name: Restore cached xmake dependencies + id: restore-depcache + uses: actions/cache/restore@v4 + with: + path: ${{ env.XMAKE_GLOBALDIR }}\.xmake\packages + key: MSVC-${{ matrix.arch }}-${{ matrix.mode }}-${{ steps.dep_hash.outputs.hash }}-W${{ steps.cache_key.outputs.key }} + + # Setup compilation mode and install project dependencies + - name: Configure xmake and install dependencies + run: xmake config --arch=${{ matrix.arch }} --mode=${{ matrix.mode }} --static=${{ matrix.kind == 'static' && 'yes' || 'no' }} --ccache=n --unitybuild=y --yes + + # Save dependencies + - name: Save cached xmake dependencies + if: ${{ !steps.restore-depcache.outputs.cache-hit }} + uses: actions/cache/save@v4 + with: + path: ${{ env.XMAKE_GLOBALDIR }}/.xmake/packages + key: ${{ steps.restore-depcache.outputs.cache-primary-key }} + + # Cache assets downloading + - name: Restore cached assets + id: restore-assets + uses: actions/cache/restore@v4 + with: + path: assets + key: assets-${{ hashFiles('assets/examples_version.txt', 'assets/unittests_version.txt') }} + + # Build the engine + - name: Build Pulse + run: xmake --yes diff --git a/Includes/Pulse.h b/Includes/Pulse.h index bd2bf91..8ab0d09 100644 --- a/Includes/Pulse.h +++ b/Includes/Pulse.h @@ -109,6 +109,7 @@ typedef enum PulseErrorType PULSE_ERROR_INVALID_INTERNAL_POINTER, PULSE_ERROR_MAP_FAILED, PULSE_ERROR_INVALID_DEVICE, + PULSE_ERROR_INVALID_REGION, } PulseErrorType; typedef enum PulseImageType @@ -262,14 +263,14 @@ PULSE_API void PulseDestroyDevice(PulseDevice device); PULSE_API PulseBuffer PulseCreateBuffer(PulseDevice device, const PulseBufferCreateInfo* create_infos); PULSE_API bool PulseMapBuffer(PulseBuffer buffer, void** data); PULSE_API void PulseUnmapBuffer(PulseBuffer buffer); -PULSE_API bool PulseCopyBufferToBuffer(const PulseBufferRegion* src, const PulseBufferRegion* dst); -PULSE_API bool PulseCopyBufferToImage(PulseBuffer src, const PulseBufferRegion* dst); +PULSE_API bool PulseCopyBufferToBuffer(PulseCommandList cmd, const PulseBufferRegion* src, const PulseBufferRegion* dst); +PULSE_API bool PulseCopyBufferToImage(PulseCommandList cmd, const PulseBufferRegion* src, const PulseImageRegion* dst); PULSE_API void PulseDestroyBuffer(PulseDevice device, PulseBuffer buffer); PULSE_API PulseImage PulseCreateImage(PulseDevice device, const PulseImageCreateInfo* create_infos); PULSE_API bool PulseIsImageFormatValid(PulseDevice device, PulseImageFormat format, PulseImageType type, PulseImageUsageFlags usage); -PULSE_API bool PulseCopyImageToBuffer(const PulseImageRegion* src, const PulseBufferRegion* dst); -PULSE_API bool PulseBlitImage(const PulseImageRegion* src, const PulseImageRegion* dst); +PULSE_API bool PulseCopyImageToBuffer(PulseCommandList cmd, const PulseImageRegion* src, const PulseBufferRegion* dst); +PULSE_API bool PulseBlitImage(PulseCommandList cmd, const PulseImageRegion* src, const PulseImageRegion* dst); PULSE_API void PulseDestroyImage(PulseDevice device, PulseImage image); PULSE_API PulseCommandList PulseRequestCommandList(PulseDevice device, PulseCommandListUsage usage); diff --git a/README.md b/README.md index 69e2649..50a5941 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # PulseGPU + + + + + Pulse is a low level GPGPU library designed for highly intensive general GPU computations with high control over the hardware. It is built on top of Vulkan and a Metal support is in discussion. diff --git a/Sources/Backends/Vulkan/VulkanBuffer.c b/Sources/Backends/Vulkan/VulkanBuffer.c index d360d5f..1a16362 100644 --- a/Sources/Backends/Vulkan/VulkanBuffer.c +++ b/Sources/Backends/Vulkan/VulkanBuffer.c @@ -5,7 +5,9 @@ #include "Pulse.h" #include "Vulkan.h" #include "VulkanBuffer.h" +#include "VulkanImage.h" #include "VulkanDevice.h" +#include "VulkanCommandList.h" PulseBuffer VulkanCreateBuffer(PulseDevice device, const PulseBufferCreateInfo* create_infos) { @@ -70,6 +72,49 @@ void VulkanUnmapBuffer(PulseBuffer buffer) vmaUnmapMemory(vulkan_device->allocator, vulkan_buffer->allocation); } +bool VulkanCopyBufferToBuffer(PulseCommandList cmd, const PulseBufferRegion* src, const PulseBufferRegion* dst) +{ + VulkanBuffer* vulkan_src_buffer = VULKAN_RETRIEVE_DRIVER_DATA_AS(src->buffer, VulkanBuffer*); + VulkanBuffer* vulkan_dst_buffer = VULKAN_RETRIEVE_DRIVER_DATA_AS(dst->buffer, VulkanBuffer*); + VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(src->buffer->device, VulkanDevice*); + VulkanCommandList* vulkan_cmd = VULKAN_RETRIEVE_DRIVER_DATA_AS(cmd, VulkanCommandList*); + + VkBufferCopy copy_region = {}; + copy_region.srcOffset = src->offset; + copy_region.dstOffset = dst->offset; + copy_region.size = (src->size < dst->size ? src->size : dst->size); + vulkan_device->vkCmdCopyBuffer(vulkan_cmd->cmd, vulkan_src_buffer->buffer, vulkan_dst_buffer->buffer, 1, ©_region); + + return true; +} + +bool VulkanCopyBufferToImage(PulseCommandList cmd, const PulseBufferRegion* src, const PulseImageRegion* dst) +{ + VulkanBuffer* vulkan_src_buffer = VULKAN_RETRIEVE_DRIVER_DATA_AS(src->buffer, VulkanBuffer*); + VulkanImage* vulkan_dst_image = VULKAN_RETRIEVE_DRIVER_DATA_AS(dst->image, VulkanImage*); + VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(src->buffer->device, VulkanDevice*); + VulkanCommandList* vulkan_cmd = VULKAN_RETRIEVE_DRIVER_DATA_AS(cmd, VulkanCommandList*); + + VkBufferImageCopy region = { 0 }; + region.bufferOffset = src->offset; + region.bufferRowLength = 0; + region.bufferImageHeight = 0; + region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + region.imageSubresource.mipLevel = 0; + region.imageSubresource.baseArrayLayer = dst->layer; + region.imageSubresource.layerCount = 1; + region.imageOffset.x = dst->x; + region.imageOffset.y = dst->y; + region.imageOffset.z = dst->z; + region.imageExtent.width = dst->width; + region.imageExtent.height = dst->height; + region.imageExtent.depth = dst->depth; + + vulkan_device->vkCmdCopyBufferToImage(vulkan_cmd->cmd, vulkan_src_buffer->buffer, vulkan_dst_image->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); + + return true; +} + void VulkanDestroyBuffer(PulseDevice device, PulseBuffer buffer) { VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(device, VulkanDevice*); diff --git a/Sources/Backends/Vulkan/VulkanBuffer.h b/Sources/Backends/Vulkan/VulkanBuffer.h index bc54a68..c1621ef 100644 --- a/Sources/Backends/Vulkan/VulkanBuffer.h +++ b/Sources/Backends/Vulkan/VulkanBuffer.h @@ -25,8 +25,8 @@ typedef struct VulkanBuffer PulseBuffer VulkanCreateBuffer(PulseDevice device, const PulseBufferCreateInfo* create_infos); bool VulkanMapBuffer(PulseBuffer buffer, void** data); void VulkanUnmapBuffer(PulseBuffer buffer); -bool VulkanCopyBufferToBuffer(const PulseBufferRegion* src, const PulseBufferRegion* dst); -bool VulkanCopyBufferToImage(const PulseBufferRegion* src, const PulseImageRegion* dst); +bool VulkanCopyBufferToBuffer(PulseCommandList cmd, const PulseBufferRegion* src, const PulseBufferRegion* dst); +bool VulkanCopyBufferToImage(PulseCommandList cmd, const PulseBufferRegion* src, const PulseImageRegion* dst); void VulkanDestroyBuffer(PulseDevice device, PulseBuffer buffer); #endif // PULSE_VULKAN_BUFFER_H_ diff --git a/Sources/Backends/Vulkan/VulkanDevicePrototypes.h b/Sources/Backends/Vulkan/VulkanDevicePrototypes.h index aced3c8..dc0a16f 100644 --- a/Sources/Backends/Vulkan/VulkanDevicePrototypes.h +++ b/Sources/Backends/Vulkan/VulkanDevicePrototypes.h @@ -18,6 +18,7 @@ PULSE_VULKAN_DEVICE_FUNCTION(vkCmdBindDescriptorSets) PULSE_VULKAN_DEVICE_FUNCTION(vkCmdBindPipeline) PULSE_VULKAN_DEVICE_FUNCTION(vkCmdCopyBuffer) + PULSE_VULKAN_DEVICE_FUNCTION(vkCmdCopyBufferToImage) PULSE_VULKAN_DEVICE_FUNCTION(vkCmdDispatch) PULSE_VULKAN_DEVICE_FUNCTION(vkCmdDispatchIndirect) PULSE_VULKAN_DEVICE_FUNCTION(vkCmdExecuteCommands) diff --git a/Sources/Backends/Vulkan/VulkanImage.c b/Sources/Backends/Vulkan/VulkanImage.c index 544eb2d..d11c5c7 100644 --- a/Sources/Backends/Vulkan/VulkanImage.c +++ b/Sources/Backends/Vulkan/VulkanImage.c @@ -115,7 +115,6 @@ PulseImage VulkanCreateImage(PulseDevice device, const PulseImageCreateInfo* cre image->device = device; image->driver_data = vulkan_image; - image->usage = create_infos->usage; VmaAllocationCreateInfo allocation_create_info = { 0 }; allocation_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE; @@ -188,6 +187,14 @@ bool VulkanIsImageFormatValid(PulseDevice device, PulseImageFormat format, Pulse return vulkan_driver_data->instance.vkGetPhysicalDeviceImageFormatProperties(vulkan_device->physical, PulseImageFormatToVkFormat[format], (type == PULSE_IMAGE_TYPE_3D) ? VK_IMAGE_TYPE_3D : VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_STORAGE_BIT, vulkan_flags, &properties) == VK_SUCCESS; } +bool VulkanCopyImageToBuffer(PulseCommandList cmd, const PulseImageRegion* src, const PulseBufferRegion* dst) +{ +} + +bool VulkanBlitImage(PulseCommandList cmd, const PulseImageRegion* src, const PulseImageRegion* dst) +{ +} + void VulkanDestroyImage(PulseDevice device, PulseImage image) { VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(device, VulkanDevice*); diff --git a/Sources/Backends/Vulkan/VulkanImage.h b/Sources/Backends/Vulkan/VulkanImage.h index bbb253b..7b42971 100644 --- a/Sources/Backends/Vulkan/VulkanImage.h +++ b/Sources/Backends/Vulkan/VulkanImage.h @@ -25,6 +25,8 @@ typedef struct VulkanImage PulseImage VulkanCreateImage(PulseDevice device, const PulseImageCreateInfo* create_infos); bool VulkanIsImageFormatValid(PulseDevice device, PulseImageFormat format, PulseImageType type, PulseImageUsageFlags usage); +bool VulkanCopyImageToBuffer(PulseCommandList cmd, const PulseImageRegion* src, const PulseBufferRegion* dst); +bool VulkanBlitImage(PulseCommandList cmd, const PulseImageRegion* src, const PulseImageRegion* dst); void VulkanDestroyImage(PulseDevice device, PulseImage image); #endif // PULSE_VULKAN_IMAGE_H_ diff --git a/Sources/PulseBackend.c b/Sources/PulseBackend.c index 17ad3eb..4aeef30 100644 --- a/Sources/PulseBackend.c +++ b/Sources/PulseBackend.c @@ -165,6 +165,7 @@ PULSE_API const char* PulseVerbaliseErrorType(PulseErrorType error) case PULSE_ERROR_INVALID_INTERNAL_POINTER: return "invalid internal pointer"; case PULSE_ERROR_MAP_FAILED: return "memory mapping failed"; case PULSE_ERROR_INVALID_DEVICE: return "device is invalid"; + case PULSE_ERROR_INVALID_REGION: return "region is invalid"; default: return "invalid error type"; }; diff --git a/Sources/PulseBuffer.c b/Sources/PulseBuffer.c index e9d4aa3..45b57c8 100644 --- a/Sources/PulseBuffer.c +++ b/Sources/PulseBuffer.c @@ -45,6 +45,88 @@ PULSE_API void PulseUnmapBuffer(PulseBuffer buffer) buffer->is_mapped = false; } +PULSE_API bool PulseCopyBufferToBuffer(PulseCommandList cmd, const PulseBufferRegion* src, const PulseBufferRegion* dst) +{ + PULSE_CHECK_PTR_RETVAL(src, false); + PULSE_CHECK_HANDLE_RETVAL(src->buffer, false); + PULSE_CHECK_PTR_RETVAL(dst, false); + PULSE_CHECK_HANDLE_RETVAL(dst->buffer, false); + + PulseBackend backend = src->buffer->device->backend; + + if(src->buffer->device != dst->buffer->device) + { + if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend)) + PulseLogErrorFmt(backend, "source buffer has been created on a different device (%p) than the destination buffer (%p)", src->buffer->device, dst->buffer->device); + PulseSetInternalError(PULSE_ERROR_INVALID_DEVICE); + return false; + } + + if(src->size + src->offset > src->buffer->size) + { + if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend)) + PulseLogErrorFmt(backend, "source buffer region (%lld) is bigger than the buffer size (%lld)", src->size + src->offset, src->buffer->size); + PulseSetInternalError(PULSE_ERROR_INVALID_REGION); + return false; + } + + if(dst->size + dst->offset > dst->buffer->size) + { + if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend)) + PulseLogErrorFmt(backend, "destination buffer region (%lld) is bigger than the buffer size (%lld)", dst->size + dst->offset, dst->buffer->size); + PulseSetInternalError(PULSE_ERROR_INVALID_REGION); + return false; + } + + if(src->buffer == dst->buffer) + return true; + + return src->buffer->device->PFN_CopyBufferToBuffer(cmd, src, dst); +} + +PULSE_API bool PulseCopyBufferToImage(PulseCommandList cmd, const PulseBufferRegion* src, const PulseImageRegion* dst) +{ + PULSE_CHECK_HANDLE_RETVAL(src, false); + PULSE_CHECK_PTR_RETVAL(dst, false); + PULSE_CHECK_HANDLE_RETVAL(dst->image, false); + + PulseBackend backend = src->buffer->device->backend; + + if(src->buffer->device != dst->image->device) + { + if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend)) + PulseLogErrorFmt(backend, "source buffer has been created on a different device (%p) than the destination buffer (%p)", src->buffer->device, dst->image->device); + PulseSetInternalError(PULSE_ERROR_INVALID_DEVICE); + return false; + } + + if(src->size + src->offset > src->buffer->size) + { + if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend)) + PulseLogErrorFmt(backend, "source buffer region (%lld) is bigger than the buffer size (%lld)", src->size + src->offset, src->buffer->size); + PulseSetInternalError(PULSE_ERROR_INVALID_REGION); + return false; + } + + if(dst->width > dst->image->width) + { + if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend)) + PulseLogErrorFmt(backend, "destination image region width (%lld) is bigger than image width (%lld)", dst->width, dst->image->width); + PulseSetInternalError(PULSE_ERROR_INVALID_REGION); + return false; + } + + if(dst->height > dst->image->height) + { + if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend)) + PulseLogErrorFmt(backend, "destination image region height (%lld) is bigger than image height (%lld)", dst->height, dst->image->height); + PulseSetInternalError(PULSE_ERROR_INVALID_REGION); + return false; + } + + return src->buffer->device->PFN_CopyBufferToImage(cmd, src, dst); +} + PULSE_API void PulseDestroyBuffer(PulseDevice device, PulseBuffer buffer) { PULSE_CHECK_HANDLE(device); diff --git a/Sources/PulseImage.c b/Sources/PulseImage.c index 12aba7e..fb0c11a 100644 --- a/Sources/PulseImage.c +++ b/Sources/PulseImage.c @@ -102,6 +102,14 @@ PULSE_API PulseImage PulseCreateImage(PulseDevice device, const PulseImageCreate PulseImage image = device->PFN_CreateImage(device, create_infos); if(image == PULSE_NULL_HANDLE) return PULSE_NULL_HANDLE; + + image->type = create_infos->type; + image->format = create_infos->format; + image->usage = create_infos->usage; + image->width = create_infos->width; + image->height = create_infos->height; + image->layer_count_or_depth = create_infos->layer_count_or_depth; + PULSE_EXPAND_ARRAY_IF_NEEDED(device->allocated_images, PulseImage, device->allocated_images_size, device->allocated_images_capacity, 64); device->allocated_images[device->allocated_images_size] = image; device->allocated_images_size++; @@ -114,6 +122,14 @@ PULSE_API bool PulseIsImageFormatValid(PulseDevice device, PulseImageFormat form return device->PFN_IsImageFormatValid(device, format, type, usage); } +PULSE_API bool PulseCopyImageToBuffer(PulseCommandList cmd, const PulseImageRegion* src, const PulseBufferRegion* dst) +{ +} + +PULSE_API bool PulseBlitImage(PulseCommandList cmd, const PulseImageRegion* src, const PulseImageRegion* dst) +{ +} + PULSE_API void PulseDestroyImage(PulseDevice device, PulseImage image) { PULSE_CHECK_HANDLE(device); diff --git a/Sources/PulseInternal.h b/Sources/PulseInternal.h index caaca2e..69bb396 100644 --- a/Sources/PulseInternal.h +++ b/Sources/PulseInternal.h @@ -105,7 +105,12 @@ typedef struct PulseImageHandler { PulseDevice device; void* driver_data; + PulseImageType type; + PulseImageFormat format; PulseImageUsageFlags usage; + uint32_t width; + uint32_t height; + uint32_t layer_count_or_depth; } PulseImageHandler; PulseThreadID PulseGetThreadID(); diff --git a/Sources/PulsePFNs.h b/Sources/PulsePFNs.h index 06a1f78..cb6dbfa 100644 --- a/Sources/PulsePFNs.h +++ b/Sources/PulsePFNs.h @@ -31,9 +31,9 @@ typedef void (*PulseDestroyBufferPFN)(PulseDevice, PulseBuffer); typedef PulseImage (*PulseCreateImagePFN)(PulseDevice, const PulseImageCreateInfo*); typedef bool (*PulseIsImageFormatValidPFN)(PulseDevice, PulseImageFormat, PulseImageType, PulseImageUsageFlags); typedef void (*PulseDestroyImagePFN)(PulseDevice, PulseImage); -typedef bool (*PulseCopyBufferToBufferPFN)(const PulseBufferRegion*, const PulseBufferRegion*); -typedef bool (*PulseCopyBufferToImageFN)(const PulseBufferRegion*, const PulseImageRegion*); -typedef bool (*PulseCopyImageToBufferPFN)(const PulseImageRegion*, const PulseBufferRegion*); +typedef bool (*PulseCopyBufferToBufferPFN)(PulseCommandList, const PulseBufferRegion*, const PulseBufferRegion*); +typedef bool (*PulseCopyBufferToImageFN)(PulseCommandList, const PulseBufferRegion*, const PulseImageRegion*); +typedef bool (*PulseCopyImageToBufferPFN)(PulseCommandList, const PulseImageRegion*, const PulseBufferRegion*); typedef bool (*PulseBlitImagePFN)(const PulseImageRegion*, const PulseImageRegion*); #endif // PULSE_PFNS_H_ diff --git a/Tests/Vulkan/Buffer.c b/Tests/Vulkan/Buffer.c index e5ed2eb..d2c8eca 100644 --- a/Tests/Vulkan/Buffer.c +++ b/Tests/Vulkan/Buffer.c @@ -65,6 +65,7 @@ void TestBufferCreation() buffer_create_info.usage = PULSE_BUFFER_USAGE_STORAGE_READ; buffer = PulseCreateBuffer(device, &buffer_create_info); TEST_ASSERT_EQUAL(buffer, PULSE_NULL_HANDLE); + PulseGetLastErrorType(); // Just to clear the error code PulseDestroyBuffer(device, buffer); ENABLE_ERRORS; @@ -112,6 +113,170 @@ void TestBufferMapping() CleanupPulse(backend); } +void TestBufferCopy() +{ + PulseBackend backend; + SetupPulse(&backend); + PulseDevice device; + SetupDevice(backend, &device); + + const unsigned char data[8] = { 0xA1, 0xFF, 0xDF, 0x17, 0x5B, 0xCC, 0x00, 0x36 }; + + PulseBufferCreateInfo buffer_create_info = { 0 }; + buffer_create_info.size = 8; + buffer_create_info.usage = PULSE_BUFFER_USAGE_TRANSFER_UPLOAD | PULSE_BUFFER_USAGE_TRANSFER_DOWNLOAD; + PulseBuffer src_buffer = PulseCreateBuffer(device, &buffer_create_info); + TEST_ASSERT_NOT_EQUAL_MESSAGE(src_buffer, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + + { + void* ptr; + TEST_ASSERT_NOT_EQUAL_MESSAGE(PulseMapBuffer(src_buffer, &ptr), false, PulseVerbaliseErrorType(PulseGetLastErrorType())); + TEST_ASSERT_NOT_NULL(ptr); + memcpy(ptr, data, 8); + PulseUnmapBuffer(src_buffer); + } + + PulseBuffer dst_buffer = PulseCreateBuffer(device, &buffer_create_info); + TEST_ASSERT_NOT_EQUAL_MESSAGE(dst_buffer, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + + { + PulseCommandList cmd = PulseRequestCommandList(device, PULSE_COMMAND_LIST_TRANSFER_ONLY); + TEST_ASSERT_NOT_EQUAL_MESSAGE(cmd, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + PulseFence fence = PulseCreateFence(device); + TEST_ASSERT_NOT_EQUAL_MESSAGE(fence, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + + PulseBufferRegion src_region = { 0 }; + src_region.buffer = src_buffer; + src_region.size = 8; + + PulseBufferRegion dst_region = { 0 }; + dst_region.buffer = dst_buffer; + dst_region.size = 8; + + TEST_ASSERT_TRUE_MESSAGE(PulseCopyBufferToBuffer(cmd, &src_region, &dst_region), PulseVerbaliseErrorType(PulseGetLastErrorType())); + + TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType())); + TEST_ASSERT_TRUE_MESSAGE(PulseWaitForFences(device, &fence, 1, true), PulseVerbaliseErrorType(PulseGetLastErrorType())); + + PulseReleaseCommandList(device, cmd); + PulseDestroyFence(device, fence); + } + + { + void* ptr; + TEST_ASSERT_NOT_EQUAL_MESSAGE(PulseMapBuffer(dst_buffer, &ptr), false, PulseVerbaliseErrorType(PulseGetLastErrorType())); + TEST_ASSERT_NOT_NULL(ptr); + TEST_ASSERT_EQUAL(memcmp(ptr, data, 8), 0); + PulseUnmapBuffer(dst_buffer); + } + + { + PulseCommandList cmd = PulseRequestCommandList(device, PULSE_COMMAND_LIST_TRANSFER_ONLY); + TEST_ASSERT_NOT_EQUAL_MESSAGE(cmd, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + PulseFence fence = PulseCreateFence(device); + TEST_ASSERT_NOT_EQUAL_MESSAGE(fence, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + + PulseBufferRegion src_region = { 0 }; + src_region.buffer = src_buffer; + src_region.size = 8; + src_region.offset = 2; + + PulseBufferRegion dst_region = { 0 }; + dst_region.buffer = dst_buffer; + dst_region.size = 12; + + DISABLE_ERRORS; + TEST_ASSERT_FALSE_MESSAGE(PulseCopyBufferToBuffer(cmd, &src_region, &dst_region), PulseVerbaliseErrorType(PulseGetLastErrorType())); + ENABLE_ERRORS; + + TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType())); + TEST_ASSERT_TRUE_MESSAGE(PulseWaitForFences(device, &fence, 1, true), PulseVerbaliseErrorType(PulseGetLastErrorType())); + + PulseReleaseCommandList(device, cmd); + PulseDestroyFence(device, fence); + } + + DISABLE_ERRORS; + void* ptr; + TEST_ASSERT_NOT_EQUAL_MESSAGE(PulseMapBuffer(src_buffer, &ptr), false, PulseVerbaliseErrorType(PulseGetLastErrorType())); + PulseDestroyBuffer(device, src_buffer); + ENABLE_ERRORS; + + PulseDestroyBuffer(device, dst_buffer); + + CleanupDevice(device); + CleanupPulse(backend); +} + +void TestBufferCopyImage() +{ + PulseBackend backend; + SetupPulse(&backend); + PulseDevice device; + SetupDevice(backend, &device); + + const unsigned char data[8] = { 0xA1, 0xFF, 0xDF, 0x17, 0x5B, 0xCC, 0x00, 0x36 }; + + PulseBufferCreateInfo buffer_create_info = { 0 }; + buffer_create_info.size = 8; + buffer_create_info.usage = PULSE_BUFFER_USAGE_TRANSFER_UPLOAD; + PulseBuffer buffer = PulseCreateBuffer(device, &buffer_create_info); + TEST_ASSERT_NOT_EQUAL_MESSAGE(buffer, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + + { + void* ptr; + TEST_ASSERT_NOT_EQUAL_MESSAGE(PulseMapBuffer(buffer, &ptr), false, PulseVerbaliseErrorType(PulseGetLastErrorType())); + TEST_ASSERT_NOT_NULL(ptr); + memcpy(ptr, data, 8); + PulseUnmapBuffer(buffer); + } + + PulseImageCreateInfo image_create_info = { 0 }; + image_create_info.type = PULSE_IMAGE_TYPE_2D; + image_create_info.format = PULSE_IMAGE_FORMAT_R8G8B8A8_UNORM; + image_create_info.usage = PULSE_IMAGE_USAGE_STORAGE_WRITE; + image_create_info.width = 8; + image_create_info.height = 1; + image_create_info.layer_count_or_depth = 1; + PulseImage image = PulseCreateImage(device, &image_create_info); + TEST_ASSERT_NOT_EQUAL_MESSAGE(image, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + + { + PulseCommandList cmd = PulseRequestCommandList(device, PULSE_COMMAND_LIST_TRANSFER_ONLY); + TEST_ASSERT_NOT_EQUAL_MESSAGE(cmd, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + PulseFence fence = PulseCreateFence(device); + TEST_ASSERT_NOT_EQUAL_MESSAGE(fence, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType())); + + PulseBufferRegion src_region = { 0 }; + src_region.buffer = buffer; + src_region.size = 8; + + PulseImageRegion dst_region = { 0 }; + dst_region.image = image; + dst_region.width = 8; + dst_region.height = 1; + dst_region.depth = 1; + dst_region.x = 1; + dst_region.y = 1; + dst_region.z = 1; + dst_region.layer = 1; + + TEST_ASSERT_TRUE_MESSAGE(PulseCopyBufferToImage(cmd, &src_region, &dst_region), PulseVerbaliseErrorType(PulseGetLastErrorType())); + + TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType())); + TEST_ASSERT_TRUE_MESSAGE(PulseWaitForFences(device, &fence, 1, true), PulseVerbaliseErrorType(PulseGetLastErrorType())); + + PulseReleaseCommandList(device, cmd); + PulseDestroyFence(device, fence); + } + + PulseDestroyImage(device, image); + PulseDestroyBuffer(device, buffer); + + CleanupDevice(device); + CleanupPulse(backend); +} + void TestBufferDestruction() { PulseBackend backend; @@ -149,5 +314,7 @@ void TestBuffer() { RUN_TEST(TestBufferCreation); RUN_TEST(TestBufferMapping); + RUN_TEST(TestBufferCopy); + RUN_TEST(TestBufferCopyImage); RUN_TEST(TestBufferDestruction); } diff --git a/xmake.lua b/xmake.lua index 5e66cc9..02bf62d 100644 --- a/xmake.lua +++ b/xmake.lua @@ -54,6 +54,8 @@ set_dependir("build/.deps") set_optimize("fastest") +option("unitybuild", { description = "Build the library using unity build", default = false }) + for name, module in pairs(backends) do if has_config(module.option) then if module.packages then @@ -70,6 +72,10 @@ target("pulse_gpu") add_files("Sources/*.c") + if has_config("unitybuild") then + add_rules("c.unity_build", { batchsize = 6 }) + end + for name, module in pairs(backends) do if has_config(module.option) then if module.packages then