Skip to content

Commit

Permalink
terrain: Bind programs to stages of the pipeline of the context
Browse files Browse the repository at this point in the history
  • Loading branch information
onox committed Apr 7, 2024
1 parent 95b2ccf commit 2ee257b
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 111 deletions.
16 changes: 16 additions & 0 deletions orka/src/orka/orka-rendering-programs.adb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ package body Orka.Rendering.Programs is
Paths : String_Array) return Shader_Program
is (Create_Program (Modules.Shader_Module_Array'([for Path of Paths => Modules.Create_Module (Location, Kind, Path.all)])));

function Create_Program
(Location : Orka.Resources.Locations.Location_Ptr;
Kind : Shader_Kind;
Paths : String_Array;
Render_Modules : Programs.Modules.Module_Array) return Shader_Program
is
use type Programs.Modules.Module_Array;

Modules_From_Paths : constant Programs.Modules.Module_Array :=
[for Path of Paths => Programs.Modules.Module (Programs.Modules.Create_Module (Location, Kind, Path.all))];

All_Modules : constant Programs.Modules.Module_Array := Modules_From_Paths & Render_Modules;
begin
return Shader_Program'(Create_Separable_Program (All_Modules, True) with Kind => Kind);
end Create_Program;

function Create_Program
(Location : Orka.Resources.Locations.Location_Ptr;
Kind : Shader_Kind;
Expand Down
6 changes: 6 additions & 0 deletions orka/src/orka/orka-rendering-programs.ads
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ package Orka.Rendering.Programs is
Kind : Shader_Kind;
Path : String) return Shader_Program;

function Create_Program
(Location : Orka.Resources.Locations.Location_Ptr;
Kind : Shader_Kind;
Paths : String_Array;
Render_Modules : Programs.Modules.Module_Array) return Shader_Program;

private

type Program is tagged record
Expand Down
15 changes: 12 additions & 3 deletions orka_plugin_terrain/data/shaders/terrain/terrain-render-wires.geom
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

layout(location = 0) in vec2 i_TexCoord[];
layout(location = 0) out vec2 o_TexCoord;

layout(location = 1) in vec4 i_WorldPos[];
layout(location = 1) out vec4 o_WorldPos;

layout(location = 0) out vec2 o_TexCoord;
layout(location = 1) out vec4 o_WorldPos;
layout(location = 2) noperspective out vec3 o_Distance;

uniform vec4 u_ScreenResolution;
// Only using first vec2 (xy, containing width and height)

in gl_PerVertex
{
vec4 gl_Position;
} gl_in[];

out gl_PerVertex
{
vec4 gl_Position;
};

void main()
{
vec2 p0 = u_ScreenResolution.xy * gl_in[0].gl_Position.xy / gl_in[0].gl_Position.w;
Expand Down
7 changes: 6 additions & 1 deletion orka_plugin_terrain/data/shaders/terrain/terrain-render.tesc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int leb_MaxDepth(const int lebID);

layout(vertices = 3) out;

flat in uint vs_NodeDepth[];
layout(location = 0) flat in uint vs_NodeDepth[];

layout(std140, binding = 0) uniform MatrixBuffer {
mat4 u_ModelMatrix;
Expand All @@ -43,6 +43,11 @@ const float E = 2.718281828459045;

const float MAX_DISTANCE = 512.0;

in gl_PerVertex
{
vec4 gl_Position;
} gl_in[];

void main()
{
if (gl_InvocationID == 0) {
Expand Down
9 changes: 9 additions & 0 deletions orka_plugin_terrain/data/shaders/terrain/terrain-render.tese
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ layout(location = 1) out vec4 o_WorldPos;

uniform int u_LebID;

in gl_PerVertex
{
vec4 gl_Position;
} gl_in[];

out gl_PerVertex {
vec4 gl_Position;
};

void main()
{
// Barycentric interpolation
Expand Down
7 changes: 6 additions & 1 deletion orka_plugin_terrain/data/shaders/terrain/terrain-render.vert
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ layout(std430, binding = 1) readonly restrict buffer NodeBuffer {
uint u_LebNodeBuffer[];
};

flat out uint vs_NodeDepth;
layout(location = 0) flat out uint vs_NodeDepth;

out gl_PerVertex
{
vec4 gl_Position;
};

void main()
{
Expand Down
22 changes: 13 additions & 9 deletions orka_plugin_terrain/src/orka-features-terrain-planets.adb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ package body Orka.Features.Terrain.Planets is
end Get_Spheroid_Parameters;

function Create_Terrain_Planet
(Min_Depth, Max_Depth : Subdivision_Depth;
(Context : aliased Orka.Contexts.Context'Class;
Min_Depth, Max_Depth : Subdivision_Depth;
Wireframe : Boolean;
Location : Resources.Locations.Location_Ptr;
Initialize_Render : access procedure
(Program : Rendering.Programs.Program);
(Programs : Rendering.Programs.Shaders.Shader_Programs);
Data : aliased Orka.Features.Atmosphere.Model_Data;
Parameters : Features.Atmosphere.Rendering.Model_Parameters;
Atmosphere : Orka.Features.Atmosphere.Cache.Cached_Atmosphere;
Expand Down Expand Up @@ -144,22 +145,23 @@ package body Orka.Features.Terrain.Planets is
(Atmosphere.Shader_Module,
Modules.Create_Module_From_Sources (FS => Terrain_FS_Shader));

procedure Initialize_Program_Render (Program : Orka.Rendering.Programs.Program) is
procedure Initialize_Program_Render (Programs : Orka.Rendering.Programs.Shaders.Shader_Programs) is
begin
Program.Uniform_Sampler ("u_DmapSampler").Verify_Compatibility (Height_Map);
Programs (Fragment_Shader).Value.Uniform_Sampler ("u_DmapSampler").Verify_Compatibility (Height_Map);

if Initialize_Render /= null then
Initialize_Render.all (Program);
Initialize_Render.all (Programs);
end if;
end Initialize_Program_Render;
begin
return Result : Terrain_Planet :=
(Terrain => Orka.Features.Terrain.Create_Terrain
(Sphere, 6, Min_Depth, Max_Depth, Wireframe, Location, Modules_Terrain_Render,
(Context, Sphere, 6, Min_Depth, Max_Depth, Wireframe, Location, Modules_Terrain_Render,
Initialize_Program_Render'Access),
Spheres => Create_Buffer ((others => False), Terrain_Spheres),
Planet_Radius => Planet_Radius,
Planet_Unit_Length => Orka.Float_32 (Data.Length_Unit_In_Meters),
Context => Context'Access,
others => <>)
do
Result.Terrain.Set_Data
Expand All @@ -185,6 +187,8 @@ package body Orka.Features.Terrain.Planets is

CP : constant Orka.Types.Singles.Vector4 :=
VC.Convert (Camera.View_Position * (1.0 / Orka.Float_64 (Object.Planet_Unit_Length)));

use all type Rendering.Programs.Shader_Kind;
begin
Object.Terrain.Set_Data
(Rotation => Rotation,
Expand All @@ -194,9 +198,9 @@ package body Orka.Features.Terrain.Planets is
Freeze => Freeze,
Wires => Wires);

Object.Terrain.Program_Render.Uniform ("camera_pos").Set_Vector (CP);
Object.Terrain.Program_Render.Uniform ("earth_radius").Set_Single (Object.Planet_Radius);
Object.Terrain.Program_Render.Uniform ("sun_direction").Set_Vector (Star);
Object.Terrain.Program_Render (Fragment_Shader).Value.Uniform ("camera_pos").Set_Vector (CP);
Object.Terrain.Program_Render (Fragment_Shader).Value.Uniform ("earth_radius").Set_Single (Object.Planet_Radius);
Object.Terrain.Program_Render (Fragment_Shader).Value.Uniform ("sun_direction").Set_Vector (Star);
end Set_Data;

overriding procedure Run (Object : Terrain_Program_Callback) is
Expand Down
11 changes: 6 additions & 5 deletions orka_plugin_terrain/src/orka-features-terrain-planets.ads
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ with Orka.Features.Atmosphere.Rendering;

package Orka.Features.Terrain.Planets is

type Terrain_Planet is tagged limited private;
type Terrain_Planet (Context : not null access constant Orka.Contexts.Context'Class) is tagged limited private;

function Create_Terrain_Planet
(Min_Depth, Max_Depth : Subdivision_Depth;
(Context : aliased Orka.Contexts.Context'Class;
Min_Depth, Max_Depth : Subdivision_Depth;
Wireframe : Boolean;
Location : Resources.Locations.Location_Ptr;
Initialize_Render : access procedure (Program : Rendering.Programs.Program);
Initialize_Render : access procedure (Programs : Rendering.Programs.Shaders.Shader_Programs);
Data : aliased Orka.Features.Atmosphere.Model_Data;
Parameters : Features.Atmosphere.Rendering.Model_Parameters;
Atmosphere : Features.Atmosphere.Cache.Cached_Atmosphere;
Expand Down Expand Up @@ -66,8 +67,8 @@ private

overriding procedure Run (Object : Terrain_Program_Callback);

type Terrain_Planet is tagged limited record
Terrain : aliased Orka.Features.Terrain.Terrain (Count => 6);
type Terrain_Planet (Context : not null access constant Orka.Contexts.Context'Class) is tagged limited record
Terrain : aliased Orka.Features.Terrain.Terrain (Context => Context, Count => 6);

Callback : aliased Terrain_Program_Callback (Terrain_Planet'Access);

Expand Down
Loading

0 comments on commit 2ee257b

Please sign in to comment.