Skip to content

Commit f5826e3

Browse files
committed
Fixed issues with vertex buffers not copying data
1 parent a85e33b commit f5826e3

20 files changed

+469
-163
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Copyright (c) 2022 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
3+
//
4+
// This code is released under an unmodified zlib license.
5+
// For conditions of distribution and use, please see:
6+
// https://opensource.org/licenses/Zlib
7+
//
8+
9+
#version 450
10+
11+
layout (location = 0) in vec4 fragColour;
12+
layout (location = 1) in vec2 uv;
13+
layout (location = 2) in flat uint texId;
14+
15+
layout (location = 0) out vec4 outColour;
16+
17+
layout(binding = 1) uniform sampler2D tex[16];
18+
19+
void main() {
20+
vec4 sampled = texture(tex[texId], vec2(uv.x, -uv.y));
21+
outColour = sampled * fragColour;
22+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#version 450
2+
3+
//
4+
// Copyright (c) 2022 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
5+
//
6+
// This code is released under an unmodified zlib license.
7+
// For conditions of distribution and use, please see:
8+
// https://opensource.org/licenses/Zlib
9+
//
10+
11+
// Per instance data
12+
layout (location = 0) in vec3 inPosition;
13+
layout (location = 1) in vec3 inScale;
14+
layout (location = 2) in vec4 inColour;
15+
layout (location = 3) in vec4 inUv;
16+
17+
layout (location = 0) out vec4 outColour;
18+
layout (location = 1) out vec2 outUv;
19+
layout (location = 2) out flat uint outTexId;
20+
21+
layout (push_constant) uniform PushConstant {
22+
uint textureIndex;
23+
} pushConstant;
24+
25+
vec3 quadVertices[4] = {
26+
vec3(1.0, 1.0, 0),
27+
vec3(1.0, -1.0, 0),
28+
vec3(-1.0, -1.0, 0),
29+
vec3(-1.0, 1.0, 0)
30+
};
31+
32+
struct CameraData
33+
{
34+
mat4 projectionMatrix;
35+
mat4 viewMatrix;
36+
};
37+
38+
layout (binding = 0) uniform GlobalData {
39+
CameraData cameraData;
40+
} globalData;
41+
42+
CameraData camera = globalData.cameraData;
43+
44+
void main() {
45+
vec4 quadInCameraSpace = camera.viewMatrix * vec4(inPosition, 1.0);
46+
47+
vec4 positionInCameraSpace = quadInCameraSpace + vec4(quadVertices[gl_VertexIndex].xy * (inScale.xy * 0.5), 0.0, 0.0);
48+
49+
gl_Position = camera.projectionMatrix * positionInCameraSpace;
50+
51+
float uvx = (float(gl_VertexIndex == 0 || gl_VertexIndex == 1) * (inUv.x + inUv.z))
52+
+ (float(gl_VertexIndex == 2 || gl_VertexIndex == 3) * (inUv.x));
53+
float uvy = (float(gl_VertexIndex == 0 || gl_VertexIndex == 3) * (inUv.y + inUv.w))
54+
+ (float(gl_VertexIndex == 1 || gl_VertexIndex == 2) * (inUv.y));
55+
56+
outColour = inColour;
57+
outUv = vec2(uvx, uvy);
58+
outTexId = pushConstant.textureIndex;
59+
}

engine/render/assets/shaders/billboard.frag

Lines changed: 0 additions & 9 deletions
This file was deleted.

engine/render/assets/shaders/billboard.vert

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//
2+
// Copyright (c) 2022 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
3+
//
4+
// This code is released under an unmodified zlib license.
5+
// For conditions of distribution and use, please see:
6+
// https://opensource.org/licenses/Zlib
7+
//
8+
9+
#include "VBuffer.h"
10+
11+
#include <utils/Logging.h>
12+
13+
#include <cstdlib>
14+
#include <cstring>
15+
16+
#include "Context.h"
17+
#include "utils/Buffer.h"
18+
19+
namespace Siege::Vulkan
20+
{
21+
22+
VBuffer::VBuffer(unsigned long bufferSize) : size {bufferSize}
23+
{
24+
CC_ASSERT(bufferSize > 0,
25+
"Cannot allocate vertex buffer of size 0. If you wanted to create an "
26+
"empty vertex buffer then please use the default constructor")
27+
28+
rawBuffer = malloc(bufferSize);
29+
30+
auto device = Context::GetVkLogicalDevice();
31+
32+
auto physicalDevice = Context::GetPhysicalDevice()->GetDevice();
33+
34+
Utils::CreateBuffer(device,
35+
physicalDevice,
36+
size,
37+
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
38+
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
39+
buffer,
40+
memory);
41+
42+
Utils::CopyData(device, memory, size, rawBuffer, 0);
43+
}
44+
45+
VBuffer::VBuffer(void* data, unsigned long bufferSize) : VBuffer(bufferSize)
46+
{
47+
memcpy(rawBuffer, data, bufferSize);
48+
}
49+
50+
void VBuffer::Copy(void* data, unsigned long dSize, unsigned long offset)
51+
{
52+
auto device = Context::GetVkLogicalDevice();
53+
54+
if (size < dSize)
55+
{
56+
}
57+
58+
void* dst = reinterpret_cast<char*>(rawBuffer) + offset;
59+
memcpy(dst, data, dSize);
60+
Utils::CopyData(device, memory, dSize, dst, offset);
61+
}
62+
63+
VBuffer::VBuffer(VBuffer&& other)
64+
{
65+
Swap(other);
66+
}
67+
68+
VBuffer::VBuffer(const VBuffer& other) : VBuffer(other.rawBuffer, other.size) {}
69+
70+
VBuffer::~VBuffer()
71+
{
72+
Free();
73+
}
74+
75+
void VBuffer::Bind(const CommandBuffer& commandBuffer,
76+
unsigned long long* offset,
77+
unsigned int binding)
78+
{
79+
vkCmdBindVertexBuffers(commandBuffer.Get(), binding, 1, &buffer, offset);
80+
}
81+
82+
void VBuffer::Free()
83+
{
84+
VkDevice device = Vulkan::Context::GetVkLogicalDevice();
85+
86+
if (device == nullptr) return;
87+
88+
free(rawBuffer);
89+
Utils::FreeBuffer(device, buffer, memory);
90+
size = 0;
91+
rawBuffer = nullptr;
92+
buffer = nullptr;
93+
memory = nullptr;
94+
}
95+
96+
VBuffer& VBuffer::operator=(const VBuffer& other)
97+
{
98+
size = other.size;
99+
rawBuffer = malloc(size);
100+
memcpy(rawBuffer, other.rawBuffer, size);
101+
return *this;
102+
}
103+
104+
VBuffer& VBuffer::operator=(VBuffer&& other)
105+
{
106+
Swap(other);
107+
return *this;
108+
}
109+
110+
void VBuffer::Allocate(void* data, unsigned long long newSize) {}
111+
112+
void VBuffer::Swap(VBuffer& other)
113+
{
114+
auto tmpRawBuffer = rawBuffer;
115+
auto tmpSize = size;
116+
auto tmpBuffer = buffer;
117+
auto tmpMemory = memory;
118+
119+
rawBuffer = other.rawBuffer;
120+
size = other.size;
121+
buffer = other.buffer;
122+
memory = other.memory;
123+
124+
other.rawBuffer = tmpRawBuffer;
125+
other.size = tmpSize;
126+
other.buffer = tmpBuffer;
127+
other.memory = tmpMemory;
128+
}
129+
} // namespace Siege::Vulkan
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Copyright (c) 2022 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
3+
//
4+
// This code is released under an unmodified zlib license.
5+
// For conditions of distribution and use, please see:
6+
// https://opensource.org/licenses/Zlib
7+
//
8+
9+
#ifndef SIEGE_ENGINE_VBUFFER_H
10+
#define SIEGE_ENGINE_VBUFFER_H
11+
12+
#include "CommandBuffer.h"
13+
#include "utils/Types.h"
14+
15+
namespace Siege::Vulkan
16+
{
17+
18+
class VBuffer
19+
{
20+
public:
21+
22+
// 'Structor
23+
24+
VBuffer() = default;
25+
VBuffer(void* data, unsigned long bufferSize);
26+
VBuffer(unsigned long bufferSize);
27+
VBuffer(VBuffer&& other);
28+
VBuffer(const VBuffer& other);
29+
~VBuffer();
30+
31+
// Operators
32+
33+
VBuffer& operator=(const VBuffer& other);
34+
VBuffer& operator=(VBuffer&& other);
35+
36+
// Functions
37+
38+
void Copy(void* data, unsigned long size, unsigned long offset = 0);
39+
void Bind(const CommandBuffer& commandBuffer,
40+
unsigned long long* offset,
41+
unsigned int binding = 0);
42+
void Free();
43+
44+
template<typename T>
45+
const T* Cast(unsigned long offset = 0)
46+
{
47+
return reinterpret_cast<T*>(rawBuffer) + offset;
48+
}
49+
50+
private:
51+
52+
void Allocate(void* data, unsigned long long newSize);
53+
void Swap(VBuffer& other);
54+
void* rawBuffer {nullptr};
55+
VkBuffer buffer {nullptr};
56+
VkDeviceMemory memory {nullptr};
57+
unsigned long size {0};
58+
};
59+
60+
} // namespace Siege::Vulkan
61+
62+
#endif // SIEGE_ENGINE_VBUFFER_H

0 commit comments

Comments
 (0)