Skip to content

Commit

Permalink
upd: start working at hud render
Browse files Browse the repository at this point in the history
  • Loading branch information
C4e10VeK committed Dec 15, 2023
1 parent 15341d9 commit bf5413d
Show file tree
Hide file tree
Showing 28 changed files with 320 additions and 47 deletions.
2 changes: 1 addition & 1 deletion res/shaders-vk/lines.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout (location = 1) in vec4 a_color;

layout(location = 0) out vec4 v_color;

layout(binding = 0) uniform Projview {
layout(set = 0, binding = 0) uniform Projview {
mat4 u_projview;
};

Expand Down
4 changes: 3 additions & 1 deletion res/shaders-vk/main.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ layout (set = 0, binding = 1) uniform Fog {
};

void main(){
vec3 fogColor = texture(u_cubemap, a_dir).rgb;
vec3 dir = a_dir;
dir.z = -dir.z;
vec3 fogColor = texture(u_cubemap, dir).rgb;
vec4 tex_color = texture(u_texture0, a_texCoord);
float depth = (a_distance/256.0);
float alpha = a_color.a * tex_color.a;
Expand Down
2 changes: 1 addition & 1 deletion res/shaders-vk/main.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void main(){
a_color = vec4(pow(light, vec3(u_gamma)),1.0f);
a_texCoord = v_texCoord;

vec3 skyLightColor = texture(u_cubemap, vec3(-0.4f, -0.05f, -0.4f)).rgb;
vec3 skyLightColor = texture(u_cubemap, vec3(-0.4f, -0.05f, 0.4f)).rgb;
skyLightColor.g *= 0.9;
skyLightColor.b *= 0.8;
skyLightColor = min(vec3(1.0), skyLightColor*SKY_LIGHT_MUL);
Expand Down
2 changes: 1 addition & 1 deletion res/shaders-vk/ui.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ layout (location = 2) in vec4 v_color;
layout(location = 0) out vec2 a_textureCoord;
layout(location = 1) out vec4 a_color;

layout(binding = 0) uniform Projview {
layout(set = 0, binding = 0) uniform Projview {
mat4 u_projview;
};

Expand Down
Binary file modified res/shaders/lines.vert.spv
Binary file not shown.
Binary file modified res/shaders/main.frag.spv
Binary file not shown.
Binary file modified res/shaders/main.vert.spv
Binary file not shown.
Binary file modified res/shaders/ui.vert.spv
Binary file not shown.
4 changes: 3 additions & 1 deletion src/assets/asset_loaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ bool assetload::shader(Assets* assets,
const string filename,
const string name) {
#ifdef USE_VULKAN
path vertexFile = paths->find(filename + ".vert.spv");
path fragmentFile = paths->find(filename + ".frag.spv");
const ShaderType shaderType = toShaderType(name);
IShader* shader = vulkan::loadShader(filename.string() + ".vert.spv", filename.string() + ".frag.spv", shaderType);
IShader* shader = vulkan::loadShader(vertexFile, fragmentFile, shaderType);
#else
path vertexFile = paths->find(filename+".glslv");
path fragmentFile = paths->find(filename+".glslf");
Expand Down
11 changes: 6 additions & 5 deletions src/frontend/BlocksPreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../graphics/Texture.h"
#include "../graphics/Atlas.h"
#include "../graphics/Batch3D.h"
#include "../graphics-vk/Batch3D.h"
#include "../window/Camera.h"
#include "../voxels/Block.h"
#include "ContentGfxCache.h"
Expand All @@ -18,7 +19,7 @@ BlocksPreview::BlocksPreview(IShader* shader,
Atlas* atlas,
const ContentGfxCache* cache)
: shader(shader), atlas(atlas), cache(cache) {
batch = new Batch3D(1024);
batch = new vulkan::Batch3D(1024);
}

BlocksPreview::~BlocksPreview() {
Expand All @@ -28,10 +29,10 @@ BlocksPreview::~BlocksPreview() {
void BlocksPreview::begin(const Viewport* viewport) {
this->viewport = viewport;
shader->use();
shader->uniformMatrix("u_projview",
glm::ortho(0.0f, float(viewport->getWidth()),
0.0f, float(viewport->getHeight()),
-1000.0f, 1000.0f) *
shader->uniformMatrix("u_projview",
glm::ortho(0.0f, float(viewport->getWidth()),
0.0f, float(viewport->getHeight()),
-1000.0f, 1000.0f) *
glm::lookAt(vec3(2, 2, 2), vec3(0.0f), vec3(0, 1, 0)));
atlas->getTexture()->bind();
}
Expand Down
6 changes: 5 additions & 1 deletion src/frontend/BlocksPreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "../typedefs.h"
#include <glm/glm.hpp>

namespace vulkan {
class Batch3D;
}

class Viewport;
class IShader;
class Atlas;
Expand All @@ -14,7 +18,7 @@ class ContentGfxCache;
class BlocksPreview {
IShader* shader;
Atlas* atlas;
Batch3D* batch;
vulkan::Batch3D* batch;
const ContentGfxCache* const cache;
const Viewport* viewport;
public:
Expand Down
34 changes: 19 additions & 15 deletions src/frontend/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ HudRenderer::HudRenderer(Engine* engine,
gui(engine->getGUI()),
cache(cache) {
auto menu = gui->getMenu();
// blocksPreview = new BlocksPreview(assets->getShader("ui3d"),
// assets->getAtlas("blocks"),
// cache);
blocksPreview = new BlocksPreview(assets->getShader("ui3d"),
assets->getAtlas("blocks"),
cache);

uicamera = new Camera(vec3(), 1);
uicamera->perspective = false;
Expand All @@ -92,7 +92,11 @@ HudRenderer::HudRenderer(Engine* engine,
return L"fps: "+this->fpsString;
})));
panel->add(shared_ptr<Label>(create_label([this](){
#ifdef USE_VULKAN
return L"meshes: " + std::to_wstring(vulkan::meshesCount);
#else
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
#endif
})));
panel->add(shared_ptr<Label>(create_label([=](){
auto& settings = engine->getSettings();
Expand Down Expand Up @@ -344,19 +348,19 @@ void HudRenderer::draw(const GfxContext& ctx){
batch->rect(width - 68, height - 68, 68, 68);
batch->setColor(vec4(1.0f));
batch->render();
batch->end();
// batch->end();

// blocksPreview->begin(&ctx.getViewport());
// {
// Window::clearDepth();
// GfxContext subctx = ctx.sub();
// subctx.depthTest(true);
// subctx.cullFace(true);
//
// Block* cblock = contentIds->getBlockDef(player->choosenBlock);
// assert(cblock != nullptr);
// blocksPreview->draw(cblock, width - 56, uicamera->getFov() - 56, 48, vec4(1.0f));
// }
blocksPreview->begin(&ctx.getViewport());
{
Window::clearDepth();
GfxContext subctx = ctx.sub();
subctx.depthTest(true);
subctx.cullFace(true);

Block* cblock = contentIds->getBlockDef(player->choosenBlock);
assert(cblock != nullptr);
blocksPreview->draw(cblock, width - 56, uicamera->getFov() - 56, 48, vec4(1.0f));
}
uishader->use();
#ifndef USE_VULKAN
batch->begin();
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/screens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void MenuScreen::update(float delta) {
void MenuScreen::draw(float delta) {
Window::clear();
Window::setBgColor(vec3(0.2f));
vulkan::VulkanContext::get().beginScreenDraw(0.2f, 0.2f, 0.2f);
vulkan::VulkanContext::get().beginScreenDraw(0.0f, 0.0f, 0.0f);

uicamera->setFov(Window::height);
IShader* uishader = engine->getAssets()->getShader("ui");
Expand Down
2 changes: 2 additions & 0 deletions src/graphics-common/IShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#ifdef USE_VULKAN
#include <vulkan/vulkan_core.h>
struct ProjectionViewConstant;
struct DynamicConstants;
#endif

Expand All @@ -30,6 +31,7 @@ class IShader {
#ifdef USE_VULKAN
virtual void use(VkCommandBuffer commandBuffer, VkExtent2D extent2D) { }
virtual void pushConstant(const DynamicConstants &constants) { }
virtual void pushConatnt(const ProjectionViewConstant &constant) { }
#endif
};

Expand Down
5 changes: 3 additions & 2 deletions src/graphics-vk/Batch2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace vulkan {
255, 255, 255, 255
};

m_blank = new Image2d(pixels, 1, 1, VK_FORMAT_R8G8B8A8_SRGB);
m_blank = new Image2d(pixels, 1, 1, VK_FORMAT_R8G8B8A8_UNORM);
m_texture = m_blank;
}

Expand All @@ -39,9 +39,11 @@ namespace vulkan {
render();
if (texture == nullptr) {
m_texture = m_blank;
m_texture->bind();
return;
}
m_texture = texture;
m_texture->bind();
}

void Batch2D::begin() {
Expand Down Expand Up @@ -201,7 +203,6 @@ namespace vulkan {
void Batch2D::render() {
const VertexOffset offset = {m_currentOffset, m_index - m_currentOffset};
if (offset.count == 0 || offset.offset >= m_capacity) return;
m_texture->bind();
m_mesh->draw(offset);

m_currentOffset = m_index;
Expand Down
1 change: 0 additions & 1 deletion src/graphics-vk/Batch2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ITexture;
namespace vulkan {

class Batch2D {
std::vector<VertexOffset> m_offsets{};
Vertex2D *m_buffer = nullptr;
Mesh<Vertex2D> *m_mesh = nullptr;
size_t m_capacity = 0;
Expand Down
166 changes: 166 additions & 0 deletions src/graphics-vk/Batch3D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//
// Created by chelovek on 12/11/23.
//

#include "Batch3D.h"

#include "texture/Image2d.h"
#include "../graphics/UVRegion.h"

namespace vulkan {
void Batch3D::vertex(float x, float y, float z, float u, float v, float r, float g, float b, float a) {
m_buffer[m_index++] = {{x, y, z}, {u, v}, {r, g, b, a}};
}

void Batch3D::vertex(glm::vec3 coord, float u, float v, float r, float g, float b, float a) {
m_buffer[m_index++] = {coord, {u, v}, {r, g, b, a}};
}

void Batch3D::vertex(glm::vec3 point, glm::vec2 uvpoint, float r, float g, float b, float a) {
m_buffer[m_index++] = {point, uvpoint, {r, g, b, a}};
}

void Batch3D::vertex(glm::vec3 point, glm::vec2 uvpoint, glm::vec4 color) {
m_buffer[m_index++] = {point, uvpoint, color};
}

void Batch3D::face(const glm::vec3& coord, float w, float h,
const glm::vec3& axisX,
const glm::vec3& axisY,
const UVRegion& region,
const glm::vec4& tint) {
vertex(coord, region.u1, region.v1,
tint.r, tint.g, tint.b, tint.a);
vertex(coord + axisX * w, region.u2, region.v1,
tint.r, tint.g, tint.b, tint.a);
vertex(coord + axisX * w + axisY * h, region.u2, region.v2,
tint.r, tint.g, tint.b, tint.a);

vertex(coord, region.u1, region.v1,
tint.r, tint.g, tint.b, tint.a);
vertex(coord + axisX * w + axisY * h, region.u2, region.v2,
tint.r, tint.g, tint.b, tint.a);
vertex(coord + axisY * h, region.u1, region.v2,
tint.r, tint.g, tint.b, tint.a);
}

Batch3D::Batch3D(size_t capacity) : m_capacity(capacity) {
m_mesh = new Mesh<Vertex3DUI>(nullptr, capacity);
m_mesh->mapVertex(&m_buffer);

constexpr unsigned char pixels[] = {
255, 255, 255, 255
};

m_blank = new Image2d(pixels, 1, 1, VK_FORMAT_R8G8B8A8_UNORM);
m_texture = m_blank;
}

Batch3D::~Batch3D() {
delete m_mesh;
delete m_blank;
}

void Batch3D::begin() {
m_texture->bind();
m_mesh->bind();
m_index = 0;
m_currentOffset = 0;
}

void Batch3D::texture(ITexture* texture) {
if (m_texture == texture)
return;
flush();
if (texture == nullptr) {
m_texture = m_blank;
m_texture->bind();
return;
}
m_texture = texture;
m_texture->bind();
}

void Batch3D::sprite(glm::vec3 pos, glm::vec3 up, glm::vec3 right, float w, float h, const UVRegion& uv,
glm::vec4 color) {

if (m_index >= m_capacity) return;

const float r = color.r;
const float g = color.g;
const float b = color.b;
const float a = color.a;

vertex(pos.x - right.x * w - up.x * h,
pos.y - right.y * w - up.y * h,
pos.z - right.z * w - up.z * h,
uv.u1, uv.v1,
r,g,b,a);

vertex(pos.x + right.x * w + up.x * h,
pos.y + right.y * w + up.y * h,
pos.z + right.z * w + up.z * h,
uv.u2, uv.v2,
r,g,b,a);

vertex(pos.x - right.x * w - up.x * h,
pos.y + right.y * w + up.y * h,
pos.z - right.z * w - up.z * h,
uv.u1, uv.v2,
r,g,b,a);

vertex(pos.x - right.x * w - up.x * h,
pos.y - right.y * w - up.y * h,
pos.z - right.z * w - up.z * h,
uv.u1, uv.v1,
r,g,b,a);

vertex(pos.x + right.x * w + up.x * h,
pos.y - right.y * w - up.y * h,
pos.z + right.z * w + up.z * h,
uv.u2, uv.v1,
r,g,b,a);

vertex(pos.x + right.x * w + up.x * h,
pos.y + right.y * w + up.y * h,
pos.z + right.z * w + up.z * h,
uv.u2, uv.v2,
r,g,b,a);
}

inline glm::vec4 do_tint(float value) {
return {value, value, value, 1.0f};
}

void Batch3D::xSprite(float w, float h, const UVRegion& uv, const glm::vec4 tint, bool shading) {
if (m_index >= m_capacity) return;

face(glm::vec3(-w * 0.25f, 0.0f, 0.0f - w * 0.25f), w, h, glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), uv, (shading ? do_tint(1.0f)*tint : tint));
face(glm::vec3(w * 0.25f, 0.0f, w * 0.5f - w * 0.25f), w, h, glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), uv, (shading ? do_tint(0.9f)*tint : tint));
}

void Batch3D::blockCube(const glm::vec3 size, const UVRegion(& texfaces)[6], const glm::vec4 tint, bool shading) {
if (m_index >= m_capacity) return;
const glm::vec3 coord = (1.0f - size) * -0.45f;
face(coord + glm::vec3(0.0f, 0.0f, 0.0f), size.x, size.y, glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), texfaces[5], (shading ? do_tint(0.8)*tint : tint));
face(coord + glm::vec3(size.x, 0.0f, -size.z), size.x, size.y, glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), texfaces[4], (shading ? do_tint(0.8f)*tint : tint));
face(coord + glm::vec3(0.0f, size.y, 0.0f), size.x, size.z, glm::vec3(1, 0, 0), glm::vec3(0, 0, -1), texfaces[3], (shading ? do_tint(1.0f)*tint : tint));
face(coord + glm::vec3(0.0f, 0.0f, -size.z), size.x, size.z, glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), texfaces[2], (shading ? do_tint(0.7f)*tint : tint));
face(coord + glm::vec3(0.0f, 0.0f, -size.z), size.z, size.y, glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), texfaces[0], (shading ? do_tint(0.9f)*tint : tint));
face(coord + glm::vec3(size.x, 0.0f, 0.0f), size.z, size.y, glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), texfaces[1], (shading ? do_tint(0.9f)*tint : tint));
}

void Batch3D::flush() {
const VertexOffset offset = {m_currentOffset, m_index - m_currentOffset};
if (offset.count == 0 || offset.offset >= m_capacity) return;
end();
m_mesh->bind();
m_mesh->draw(offset);

m_currentOffset = m_index;
}

void Batch3D::end() {
m_mesh->reload(nullptr, m_capacity);
}
} // vulkan
Loading

0 comments on commit bf5413d

Please sign in to comment.