Skip to content

Commit

Permalink
plot vertex shader and graphing;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Nov 24, 2024
1 parent 2607de4 commit c082d1b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 45 deletions.
14 changes: 8 additions & 6 deletions src/calculator/renderer/VulkanWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,28 @@ namespace ui
{
QPointF current_position = ev->position();

this->scene->camera.center.x += (float)(current_position.x() - this->last_mouse_pos_.x());
this->scene->camera.center.y += (float)(current_position.y() - this->last_mouse_pos_.y());
float dx = current_position.x() - this->last_mouse_pos_.x();
float dy = current_position.y() - this->last_mouse_pos_.y();

this->scene->camera.TranslatePixel({dx, dy});

this->last_mouse_pos_ = current_position;
}
}

void wheelEvent(QWheelEvent *ev) override
{
this->scene->camera.resolution += (float)ev->angleDelta().y();
// this->scene->camera.resolution += (float)ev->angleDelta().y();
}

void resizeEvent(QResizeEvent *ev) override
{
this->scene->camera.dpi_scalar = (float)this->devicePixelRatio();
this->scene->camera.SetDPI((float)this->devicePixelRatio());

this->scene->camera.window = {
this->scene->camera.SetWindowSize({
ev->size().width(),
ev->size().height(),
};
});
}

public:
Expand Down
11 changes: 7 additions & 4 deletions src/calculator/resource/shaders/grid.frag
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#version 450

layout (binding = 0) uniform Camera {
float resolution;
float dpi_scalar;
vec2 center;
float dpi;
float fov;

vec2 location;
vec2 window;

mat4 model;
Expand All @@ -26,7 +27,9 @@ bool centerline(vec2 st, float width) {
}

void main() {
vec2 st = ((gl_FragCoord.xy - ((camera.window * camera.dpi_scalar) / 2) - (camera.center.xy * camera.dpi_scalar)) / camera.resolution);
// ((gl_FragCoord.xy - ((camera.window * camera.dpi_scalar) / 2) - (camera.center.xy * camera.dpi_scalar)) / camera.resolution);
vec4 ndc = vec4((gl_FragCoord.xy + camera.window * camera.dpi / 2) / (camera.window * camera.dpi), 0.0, 1.0);
vec2 st = (inverse(camera.projection) * ndc).xy;
vec4 color = vec4(1.0, 1.0, 1.0, 1.0);

if (centerline(st, 0.2)) {
Expand Down
14 changes: 13 additions & 1 deletion src/calculator/resource/shaders/plot.vert
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
#version 450

layout (binding = 0) uniform Camera {
float dpi;
float fov;

vec2 location;
vec2 window;

mat4 model;
mat4 view;
mat4 projection;
} camera;

layout (location = 0) in vec2 in_position;
layout (location = 1) in vec4 in_color;

layout (location = 0) out vec4 out_color;

void main() {
gl_Position = vec4(in_position, 0.0, 1.0);
gl_Position = camera.projection * camera.view * camera.model * vec4(in_position, 0.0, 1.0);
out_color = in_color;
}
14 changes: 2 additions & 12 deletions src/compiler/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ namespace unlogic

for (auto library: this->default_libraries_)
{
// Create dylib
auto dylib = jit->createJITDylib(library->name);
if (auto e = dylib.takeError())
{
throw std::runtime_error(llvm::toString(std::move(e)));
}

// Generate symbol map
llvm::orc::SymbolMap library_symbols;
for (auto symbol: library->symbols)
Expand All @@ -97,18 +90,15 @@ namespace unlogic
symbol->symbol,
});

symbol->PopulateScope(*ctx.get(), *module, program_scope);
symbol->Define(*ctx.get(), *module);
}

// Add symbol map
auto std_sym_def = dylib->define(llvm::orc::absoluteSymbols(library_symbols));
auto std_sym_def = main.define(llvm::orc::absoluteSymbols(library_symbols));
if (std_sym_def)
{
throw std::runtime_error(llvm::toString(std::move(std_sym_def)));
}

// Add lib
main.addToLinkOrder(*dylib);
}

// Parse program
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ namespace unlogic
char const *name;
llvm::orc::ExecutorSymbolDef symbol;

using PopulateScopeType = std::function<void(llvm::LLVMContext&, llvm::Module&, Scope&)>;
PopulateScopeType PopulateScope;
using DefineType = std::function<void(llvm::LLVMContext&, llvm::Module&)>;
DefineType Define;

LibrarySymbol(Library &lib, char const *name, void *native, PopulateScopeType populate_scope) : name(name), PopulateScope(std::move(populate_scope))
LibrarySymbol(Library &lib, char const *name, void *native, DefineType populate_scope) : name(name), Define(std::move(populate_scope))
{
this->symbol = {
llvm::orc::ExecutorAddr::fromPtr(native),
llvm::JITSymbolFlags::Callable,
llvm::JITSymbolFlags(),
};

lib.symbols.push_back(this);
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/std/RuntimeLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern "C"

Library unlogic::runtime("runtime");

LibrarySymbol scene_add_plot(runtime, "unlogic_scene_add_plot", (void *)unlogic_scene_add_plot, [](llvm::LLVMContext &ctx, llvm::Module &mod, Scope &scope) {
LibrarySymbol scene_add_plot(runtime, "unlogic_scene_add_plot", (void *)unlogic_scene_add_plot, [](llvm::LLVMContext &ctx, llvm::Module &mod) {
std::array<llvm::Type *, 1> plot_function_args = {
llvm::Type::getDoubleTy(ctx),
};
Expand All @@ -27,5 +27,5 @@ LibrarySymbol scene_add_plot(runtime, "unlogic_scene_add_plot", (void *)unlogic_
llvm::Type *ret = llvm::Type::getVoidTy(ctx);

llvm::FunctionType *fn = llvm::FunctionType::get(ret, args, false);
scope.Insert("scene_add_plot", llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "unlogic_scene_add_plot", mod));
llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "unlogic_scene_add_plot", mod);
});
8 changes: 4 additions & 4 deletions src/compiler/std/StandardLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ extern "C"

Library unlogic::stdlib("stdlib");

LibrarySymbol std_pow(stdlib, "pow", (void*)unlogic_std_pow, [](llvm::LLVMContext &ctx, llvm::Module &mod, Scope &scope) {
LibrarySymbol std_pow(stdlib, "unlogic_std_pow", (void*)unlogic_std_pow, [](llvm::LLVMContext &ctx, llvm::Module &mod) {
std::array args = {
llvm::Type::getDoubleTy(ctx),
llvm::Type::getDoubleTy(ctx),
};
llvm::Type *ret = llvm::Type::getDoubleTy(ctx);

llvm::FunctionType *fn = llvm::FunctionType::get(ret, args, false);
scope.Insert("pow", llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "pow", mod));
llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "unlogic_std_pow", mod);
});

LibrarySymbol std_log(stdlib, "log", (void*)unlogic_std_log, [](llvm::LLVMContext &ctx, llvm::Module &mod, Scope &scope) {
LibrarySymbol std_log(stdlib, "unlogic_std_log", (void*)unlogic_std_log, [](llvm::LLVMContext &ctx, llvm::Module &mod) {
std::array<llvm::Type*, 1> args = {
llvm::PointerType::getInt8Ty(ctx),
};
llvm::Type *ret = llvm::Type::getVoidTy(ctx);

llvm::FunctionType *fn = llvm::FunctionType::get(ret, args, false);
scope.Insert("log", llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "log", mod));
llvm::Function::Create(fn, llvm::GlobalValue::ExternalLinkage, "unlogic_std_log", mod);
});

/*
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/transformer/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ void unlogic::IRGenerator::Visit(unlogic::PotentiationNode const *node)
llvm::Value *rhs = this->values.top();
this->values.pop();

llvm::Function *pow = (llvm::Function *)*this->ctx.scope.Lookup("pow");
llvm::Function *std_pow = this->ctx.module->getFunction("unlogic_std_pow");

llvm::Value *value = this->builder.CreateCall(pow, {lhs, rhs}, "powtmp");
llvm::Value *value = this->builder.CreateCall(std_pow, {lhs, rhs}, "powtmp");
this->values.push(value);
}

Expand Down Expand Up @@ -167,7 +167,7 @@ void unlogic::IRGenerator::Visit(unlogic::PlotCommandNode const *node)
throw std::runtime_error("function not found");
}

auto scene_add_plot = (llvm::Function *)*this->ctx.scope.Lookup("scene_add_plot");
auto scene_add_plot = this->ctx.module->getFunction("unlogic_scene_add_plot");

std::array<llvm::Value *, 3> args = {scene, name, (llvm::Function *)*function};

Expand Down
51 changes: 43 additions & 8 deletions src/graphic/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,55 @@
#define CAMERA_H

#include <glm/glm.hpp>
#include <glm/ext.hpp>

namespace unlogic
{
struct Camera
class Camera
{
alignas(4) float resolution = 50.f;
alignas(4) float dpi_scalar = 1.f;
alignas(4) float dpi = 1.f;
alignas(4) float fov = 45.f;

alignas(8) glm::vec2 center = {0.f, 0.f};
alignas(8) glm::vec2 window;
/**
* Location of camera in WORLD.
*/
alignas(8) glm::vec3 location = {0.f, 0.f, 10.0f};
alignas(8) glm::vec2 window = {0.f, 0.f};

alignas(16) glm::mat4 model;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 projection;
alignas(16) glm::mat4 model = glm::mat4(1.f);
alignas(16) glm::mat4 view = glm::mat4(1.f);
alignas(16) glm::mat4 projection = glm::mat4(1.f);

protected:
void CalculateMVP()
{
// Plots come out of function in world space already.
this->model = glm::mat4(1.f);

this->view = glm::lookAt(this->location, {0.f, 0.f, 0.f}, {0.f, -1.f, 0.f});

if (this->window.y != 0)
{
this->projection = glm::perspective(glm::radians(this->fov), this->window.x / this->window.y, 0.1f, 100.f);
}
}

public:
void SetDPI(float new_dpi)
{
this->dpi = new_dpi;
this->CalculateMVP();
}

void SetWindowSize(glm::vec2 const &new_window)
{
this->window = new_window;
this->CalculateMVP();
}

void TranslatePixel(glm::vec2 const &distance) {}

void TranslateWorld(glm::vec2 const &distance) {}
};
} // namespace unlogic

Expand Down
9 changes: 8 additions & 1 deletion src/graphic/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ namespace unlogic

void AddPlot(char const *name, Plot2dFunctionType function)
{
this->plots.emplace_back(this->vertex_buffer_provider.get(), name, function, Color::Red);
std::array colors = {
Color::Red,
Color::Green,
Color::Blue,
};
Color color = colors[std::rand() % colors.size()];

this->plots.emplace_back(this->vertex_buffer_provider.get(), name, function, color);
}

Scene() = delete;
Expand Down

0 comments on commit c082d1b

Please sign in to comment.