Skip to content

Instantly share code, notes, and snippets.

#version 460 core
#extension GL_NV_gpu_shader5: enable // for uint8_t
#extension GL_NV_shader_thread_shuffle: enable
layout(local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
layout(r8i, binding = 4) restrict uniform iimage3D df_img;
#include "gpu_voxels.glsl"
#if RENDERER_PROFILING
template <int N=4>
struct TimerZone {
int next = 0; // next timer to be used for measurement
int count = 0; // number of timers 'buffered', ie measurement was started by result not read yet
GLuint queries[N];
TimerZone () {
glGenQueries(N, queries);
// Blocks of pixels
#define LOCAL_SIZE_X 8
#define LOCAL_SIZE_Y 8
// 12 cones for VCT around a hemisphere
#define LOCAL_SIZE_Z 12
#define NUM_CONES LOCAL_SIZE_Z
struct Geometry {
// flip coordinate space such that ray is always positive (simplifies stepping logic)
// keep track of flip via flipmask
bool3 ray_neg = dir < 0.0;
float3 flipped = ray_neg ? -pos : pos;
int3 flipmask = ray_neg ? -1 : 0;
float3 absDir = abs(dir);
// WORLD_SIZE = POT size of 3d texture with voxel data for world eg. #define WORLD_SIZE 512
// OCTREE_MIPS = number of mips of texture
const float WORLD_SIZEf = float(WORLD_SIZE);
const uint ROUNDMASK = -1;
const uint FLIPMASK = WORLD_SIZE-1;
//// pos is relative to world 3d texture
bool trace_ray (vec3 pos, vec3 dir, float max_dist, out Hit hit) {
bool trace_ray (vec3 pos, vec3 dir, float max_dist, out Hit hit, bool sunray) {
// make ray relative to world texture and bring coordinates into [1.0, 2.0] float space
// for float mantissa optimization (avoid int -> float conversion for projection)
// basially treat float mantissa like integer for stepping but use the whole float for projection calculations
vec3 coord = pos + WORLD_SIZE_HALF;
coord = coord * INV_WORLD_SIZEf + 1.0; // to [1.0, 2.0]
coord = clamp(coord, 1.0, 2.0);
// flip coordinate space such that ray is always positive (simplifies stepping logic)
// keep track of flip via flipmask
// make ray relative to world texture
ray_pos += float(WORLD_SIZE/2 * CHUNK_SIZE);
// flip coordinate space for ray such that ray dir is all positive
// keep track of this flip via flipmask
ivec3 flipmask = mix(ivec3(0), ivec3(-1), lessThan(ray_dir, vec3(0.0)));
ray_pos *= mix(vec3(1), vec3(-1), lessThan(ray_dir, vec3(0.0)));
ray_dir = abs(ray_dir);
#define GLFW_INCLUDE_VULKAN
#include "GLFW/glfw3.h"
#include "kissmath.hpp"
#include "assert.h"
#include <vector>
const bool enable_validation_layers = true;
const std::vector<char const*> validation_layers = {
"VK_LAYER_KHRONOS_validation"
};
#define _CRT_SECURE_NO_WARNINGS
// img loading
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_BMP 1
#define STBI_ONLY_PNG 1
#define STBI_ONLY_TGA 1
#define STBI_ONLY_JPEG 1
uniform isampler1D BlockTileInfo_texture;
uniform sampler2DArray tile_textures;
// BlockTileInfo format:
/*
struct BlockTileInfo {
int base_index;
// side is always at base_index