Skip to content
Snippets Groups Projects
Unverified Commit cd391860 authored by John Hunter's avatar John Hunter :minidisc:
Browse files

Working simple implicit implementation

parent 636a4938
Branches
No related tags found
No related merge requests found
/* Copyright (c) 2021, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
#version 450
#extension GL_EXT_mesh_shader:require
layout(push_constant)uniform PushConstantData{
mat4 world;
mat4 view;
mat4 proj;
}pc;
layout(local_size_x=1,local_size_y=1,local_size_z=1)in;
layout(triangles,max_vertices=3,max_primitives=1)out;
layout(location=0)out VertexOutput
{
vec4 color;
}vertexOutput[];
const vec4[3]positions={
vec4(0.,-1.,0.,1.),
vec4(-1.,1.,0.,1.),
vec4(1.,1.,0.,1.)
};
const vec4[3]colors={
vec4(0.,1.,0.,1.),
vec4(0.,0.,1.,1.),
vec4(1.,0.,0.,1.)
};
void main()
{
uint iid=gl_LocalInvocationID.x;
vec4 offset=vec4(0.,0.,gl_GlobalInvocationID.x,0.);
SetMeshOutputsEXT(3,1);
mat4 mvp=pc.proj*pc.view*pc.world;
gl_MeshVerticesEXT[0].gl_Position=mvp*(positions[0]+offset);
gl_MeshVerticesEXT[1].gl_Position=mvp*(positions[1]+offset);
gl_MeshVerticesEXT[2].gl_Position=mvp*(positions[2]+offset);
vertexOutput[0].color=colors[0];
vertexOutput[1].color=colors[1];
vertexOutput[2].color=colors[2];
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex]=uvec3(0,1,2);
}
\ No newline at end of file
use egui::{ use egui::{
plot::{Line, Plot, PlotPoints}, plot::{Line, Plot, PlotPoints},
Color32, Frame, Id, ScrollArea, TextEdit, TextStyle, Color32, Frame, Id,
}; };
use egui_winit_vulkano::Gui; use egui_winit_vulkano::Gui;
...@@ -10,14 +10,6 @@ fn sized_text(ui: &mut egui::Ui, text: impl Into<String>, size: f32) { ...@@ -10,14 +10,6 @@ fn sized_text(ui: &mut egui::Ui, text: impl Into<String>, size: f32) {
ui.label(egui::RichText::new(text).size(size)); ui.label(egui::RichText::new(text).size(size));
} }
const CODE: &str = r#"
# Some markup
```
let mut gui = Gui::new(&event_loop, renderer.surface(), renderer.queue());
```
Vulkan(o) is hard, that I know...
"#;
#[derive(Debug)] #[derive(Debug)]
pub struct GState { pub struct GState {
pub cursor_sensitivity: f32, pub cursor_sensitivity: f32,
...@@ -44,7 +36,6 @@ impl Default for GState { ...@@ -44,7 +36,6 @@ impl Default for GState {
} }
pub fn gui_up(gui: &mut Gui, state: &mut GState) { pub fn gui_up(gui: &mut Gui, state: &mut GState) {
let mut code = CODE.to_owned();
gui.immediate_ui(|gui| { gui.immediate_ui(|gui| {
let ctx = gui.context(); let ctx = gui.context();
egui::SidePanel::left(Id::new("main_left")) egui::SidePanel::left(Id::new("main_left"))
......
// Implicit Fragment shader
#version 450
layout(push_constant)uniform PushConstantData{
mat4 world;
}pc;
layout(set=0,binding=0)uniform Lights{
vec4[32]pos;
vec4[32]col;
uint light_count;
}light_uniforms;
layout(set=0,binding=1)uniform Camera{
mat4 view;
mat4 proj;
vec3 campos;
}camera_uniforms;
layout(constant_id=0)const uint RES_X=1920;
layout(constant_id=1)const uint RES_Y=1080;
layout(location=0)in VertexInput
{
vec4 position;
}vertexInput;
layout(location=0)out vec4 f_color;
const float EPSILON=.0001;
const uint MAX_STEPS=50;
float scene(vec3 p)
{
return length(p-vec3(5.))-5.;
}
vec3 getNormal(vec3 p,float dens){
vec3 n;
n.x=scene(vec3(p.x+EPSILON,p.y,p.z));
n.y=scene(vec3(p.x,p.y+EPSILON,p.z));
n.z=scene(vec3(p.x,p.y,p.z+EPSILON));
return normalize(n-scene(p));
}
vec2 spheretracing(vec3 ori,vec3 dir,out vec3 p){
vec2 td=vec2(0.);
for(int i=0;i<MAX_STEPS;i++){
p=ori+dir*td.x;
td.y=scene(p);
if(td.y<EPSILON)break;
td.x+=(td.y)*.9;
}
return td;
}
#define frac_pi_2 1.57079632679489661923132169163975144
void main(){
vec3 raypos=vertexInput.position.xyz;
vec2 iResolution=vec2(RES_X,RES_Y);
vec2 iuv=gl_FragCoord.xy/iResolution.xy*2.-1.;
vec2 uv=iuv;
uv.x*=iResolution.x/iResolution.y;
vec3 p;
vec3 raydir=normalize(raypos-camera_uniforms.campos);
//raydir=(camera_uniforms.view*vec4(raydir,1.)).xyz;
vec2 td=spheretracing(raypos,raydir,p);
vec3 n=getNormal(p,td.y);
if(td.y<EPSILON)
{
vec3 accum=vec3(0.,0.,0.);
for(int i=0;i<light_uniforms.light_count;i++)
{
accum+=light_uniforms.col[i].xyz*((dot(normalize(n),light_uniforms.pos[i].xyz)*.5)+.5);
}
f_color=vec4(accum,1.);
}
else
{
//f_color=vec4(raydir,0.);
discard;
}
}
\ No newline at end of file
// Implicit Mesh shader
#version 450
#extension GL_EXT_mesh_shader:require
layout(push_constant)uniform PushConstantData{
mat4 world;
}pc;
layout(set=0,binding=0)uniform Lights{
vec4[32]pos;
vec4[32]col;
uint light_count;
}light_uniforms;
layout(set=0,binding=1)uniform Camera{
mat4 view;
mat4 proj;
vec3 campos;
}camera_uniforms;
layout(local_size_x=1,local_size_y=1,local_size_z=1)in;
layout(triangles,max_vertices=64,max_primitives=162)out;
layout(location=0)out VertexOutput
{
vec4 color;
}vertexOutput[];
const vec4[8]positions={
vec4(0.,0.,0.,1.),
vec4(0.,0.,1.,1.),
vec4(0.,1.,0.,1.),
vec4(0.,1.,1.,1.),
vec4(1.,0.,0.,1.),
vec4(1.,0.,1.,1.),
vec4(1.,1.,0.,1.),
vec4(1.,1.,1.,1.),
};
const mat4 scale=mat4(
10.,0.,0.,0.,
0.,10.,0.,0.,
0.,0.,10.,0.,
0.,0.,0.,1.
);
void main()
{
uint iid=gl_LocalInvocationID.x;
vec4 offset=vec4(0.,0.,gl_GlobalInvocationID.x,0.);
SetMeshOutputsEXT(8,12);
mat4 mvp=camera_uniforms.proj*camera_uniforms.view*scale;
gl_MeshVerticesEXT[0].gl_Position=mvp*(positions[0]+offset);
gl_MeshVerticesEXT[1].gl_Position=mvp*(positions[1]+offset);
gl_MeshVerticesEXT[2].gl_Position=mvp*(positions[2]+offset);
gl_MeshVerticesEXT[3].gl_Position=mvp*(positions[3]+offset);
gl_MeshVerticesEXT[4].gl_Position=mvp*(positions[4]+offset);
gl_MeshVerticesEXT[5].gl_Position=mvp*(positions[5]+offset);
gl_MeshVerticesEXT[6].gl_Position=mvp*(positions[6]+offset);
gl_MeshVerticesEXT[7].gl_Position=mvp*(positions[7]+offset);
vertexOutput[0].color=scale*positions[0];
vertexOutput[1].color=scale*positions[1];
vertexOutput[2].color=scale*positions[2];
vertexOutput[3].color=scale*positions[3];
vertexOutput[4].color=scale*positions[4];
vertexOutput[5].color=scale*positions[5];
vertexOutput[6].color=scale*positions[6];
vertexOutput[7].color=scale*positions[7];
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+0]=uvec3(0,1,2);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+1]=uvec3(1,2,3);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+2]=uvec3(4,5,6);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+3]=uvec3(5,6,7);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+4]=uvec3(0,2,4);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+5]=uvec3(2,4,6);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+6]=uvec3(1,3,5);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+7]=uvec3(3,5,7);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+8]=uvec3(2,3,6);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+9]=uvec3(3,6,7);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+10]=uvec3(0,1,4);
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex+11]=uvec3(1,4,5);
}
/*
0 1 2 3 0
4 5 6 7 4
0 1 2
1 2 3
4 5 6
5 6 7
0 1 4
1 4 5
1 2
*/
\ No newline at end of file
This diff is collapsed.
use std::{collections::HashMap, io::Read, sync::Arc}; use std::{collections::HashMap, io::Read};
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use cgmath::{Deg, Euler, Matrix3, Point3, SquareMatrix, Vector3}; use cgmath::{Deg, Euler, Point3, Vector3};
use obj::{LoadConfig, ObjData}; use obj::{LoadConfig, ObjData};
use vulkano::{ use vulkano::{
buffer::{Buffer, BufferAllocateInfo, BufferUsage, Subbuffer}, buffer::{Buffer, BufferAllocateInfo, BufferUsage, Subbuffer},
......
#version 450 #version 450
layout(location=0)in vec3 normal; layout(push_constant)uniform PushConstantData{
layout(location=0)out vec4 f_color; mat4 world;
}pc;
layout(set=0,binding=0)uniform Data{ layout(set=0,binding=0)uniform Lights{
vec4[32]pos; vec4[32]pos;
vec4[32]col; vec4[32]col;
uint light_count; uint light_count;
}uniforms; }light_uniforms;
layout(set=0,binding=1)uniform Camera{
mat4 view;
mat4 proj;
vec3 campos;
}camera_uniforms;
layout(location=0)in vec3 normal;
layout(location=0)out vec4 f_color;
void main(){ void main(){
vec3 accum=vec3(0.,0.,0.); vec3 accum=vec3(0.,0.,0.);
for(int i=0;i<uniforms.light_count;i++) for(int i=0;i<light_uniforms.light_count;i++)
{ {
accum+=uniforms.col[i].xyz*((dot(normalize(normal),uniforms.pos[i].xyz)*.5)+.5); accum+=light_uniforms.col[i].xyz*((dot(normalize(normal),light_uniforms.pos[i].xyz)*.5)+.5);
} }
f_color=vec4(accum,1.); f_color=vec4(accum,1.);
......
#version 450
layout(push_constant)uniform PushConstantData{
mat4 world;
}pc;
layout(set=0,binding=0)uniform Lights{
vec4[32]pos;
vec4[32]col;
uint light_count;
}light_uniforms;
layout(set=0,binding=1)uniform Camera{
mat4 view;
mat4 proj;
vec3 campos;
}camera_uniforms;
layout(location=0)in vec3 position;
layout(location=1)in vec3 normal;
layout(location=0)out vec3 v_normal;
void main(){
mat4 worldview=camera_uniforms.view*pc.world;
v_normal=normal;//normalize(transpose(inverse(mat3(worldview))) * normal);
gl_Position=camera_uniforms.proj*worldview*vec4(position*1000.,1.);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment