Skip to content

Commit

Permalink
Change vertex format for 2d rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSmith33 committed Dec 21, 2022
1 parent f699d71 commit fe711a5
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 101 deletions.
1 change: 1 addition & 0 deletions host/deps/kernel32.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ int GetLastError(); // llvm complains if types do not match
void LocalFree();
void FormatMessageA();
bool SetThreadPriority(void* hThread, int nPriority);
void GetCurrentThreadId();
void* GetCurrentThread();
size_t SetThreadAffinityMask(void* hThread, size_t dwThreadAffinityMask);
1 change: 1 addition & 0 deletions plugins/core/src/core/kernel32.vx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module core.kernel32;

u32 GetLastError();

u32 GetCurrentThreadId();
noreturn ExitProcess(u32 uExitCode);
noreturn ExitThread(u32 uExitCode);
u64 GetTickCount64();
Expand Down
12 changes: 6 additions & 6 deletions plugins/core/src/core/utils.vx
Original file line number Diff line number Diff line change
Expand Up @@ -380,28 +380,28 @@ enum INT_BUF_SIZE = 66;
void printInt(i64 i, u32 minSize = 1, u8 base = 10)
{
u8[INT_BUF_SIZE] buf;
u8[] res = formatInt(i, &buf, 1, base);
print(res);
u8[] res = formatInt(i, &buf, minSize, base);
printString(res);
}
void printUint(u64 i, u32 minSize = 1, u8 base = 10)
{
u8[INT_BUF_SIZE] buf;
u8[] res = formatUint(i, &buf, 1, base);
print(res);
u8[] res = formatUint(i, &buf, minSize, base);
printString(res);
}

void print_f32(f32 val)
{
u8[64] buf;
u8[] res = format_f32(&buf, val);
print(res);
printString(res);
}

void print_f64(f64 val)
{
u8[64] buf;
u8[] res = format_f64(&buf, val);
print(res);
printString(res);
}

@extern(module, "host") u8[] format_f32(u8[64]* buf, f32 f);
Expand Down
18 changes: 13 additions & 5 deletions plugins/core/src/core/vector.vx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module core.vector;
import core.math;
import core.utils;

struct ivec2 { i32 x; i32 y; }
struct uvec2 { u32 x; u32 y; }
struct u16vec2 { u16 x; u16 y; }

struct vec2 { f32 x; f32 y; }
struct vec3 { f32 x; f32 y; f32 z; }
struct vec3x { f32 x; f32 y; f32 z; f32 pad; }
Expand All @@ -16,25 +19,30 @@ f32 degtorad(f32 deg) {
}

vec3 cross_vec3(vec3 a, vec3 b) {
return vec3 (
return vec3(
(a.y * b.z) - (a.z * b.y),
(a.z * b.x) - (a.x * b.z),
(a.x * b.y) - (a.y * b.x),
);
}

vec2 add_vec2(vec2 a, vec2 b) {
return vec2(
a.x + b.x,
a.y + b.y,
);
}

vec3 add_vec3(vec3 a, vec3 b) {
return vec3
(
return vec3(
a.x + b.x,
a.y + b.y,
a.z + b.z,
);
}

vec3 sub_vec3(vec3 a, vec3 b) {
return vec3
(
return vec3(
a.x - b.x,
a.y - b.y,
a.z - b.z,
Expand Down
4 changes: 2 additions & 2 deletions plugins/hello_triangle/res/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;

layout(binding = 1) uniform sampler2D texSampler;
layout(set = 0, binding = 1) uniform sampler2D texSampler;

layout(location = 0) out vec4 outColor;

void main() {
outColor = vec4(fragColor * texture(texSampler, fragTexCoord).rgb, 1.0);
outColor = vec4(fragColor, 1.0) * texture(texSampler, fragTexCoord);
}
6 changes: 3 additions & 3 deletions plugins/hello_triangle/res/shader.vert
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#version 450

layout(row_major) layout(binding = 0) uniform UniformBufferObject {
layout(row_major) layout(set = 0, binding = 0) uniform UniformBufferObject {
mat4 model;
mat4 view;
mat4 proj;
} ubo;

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;
layout(location = 1) in vec2 inTexCoord;
layout(location = 2) in vec3 inColor;

layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;
Expand Down
14 changes: 14 additions & 0 deletions plugins/hello_triangle/res/ui.shader.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 450

layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;

layout(set = 0, binding = 1) uniform sampler2D texSampler;

layout(location = 0) out vec4 outColor;

void main() {
// outColor = vec4(fragColor, 1.0) * texture(texSampler, fragTexCoord);
// outColor = vec4(fragColor, 1.0) * texture(texSampler, fragTexCoord / 200);
outColor = vec4(fragColor, 1.0) * texture(texSampler, fragTexCoord / textureSize(texSampler, 0));
}
19 changes: 19 additions & 0 deletions plugins/hello_triangle/res/ui.shader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 450

layout(row_major) layout(set = 0, binding = 0) uniform UniformBufferObject {
vec2 screenSize;
} ubo;

layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec2 inTexCoord;
layout(location = 2) in vec3 inColor;

layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;

void main() {
// gl_Position = vec4(inPosition, 0.0, 1.0);
gl_Position = vec4((inPosition / ubo.screenSize) * 2 - 1, 0.0, 1.0);
fragColor = inColor;
fragTexCoord = inTexCoord;
}
19 changes: 19 additions & 0 deletions plugins/hello_triangle/src/hello_triangle/image.vx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module hello_triangle.image;

import core.utils;
import core.vector;
import core.mimalloc;

// 4 channels, u8u8u8u8 rgba
struct ImageData {
u8* ptr;
uvec2 size;

u32 byteLength() { return size.x * size.y * 4; }

void free() {
mi_free(ptr);
ptr = null;
size = uvec2(0, 0);
}
}
Loading

0 comments on commit fe711a5

Please sign in to comment.