Changeset 67527a5 in opengl-game for shaders


Ignore:
Timestamp:
Jun 21, 2021, 4:12:08 PM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
27e580e
Parents:
b01b50c
git-author:
Dmitry Portnoy <dportnoy@…> (06/21/21 15:13:03)
git-committer:
Dmitry Portnoy <dportnoy@…> (06/21/21 16:12:08)
Message:

Switch all per-object buffers to be dynamic uniform buffers instead of shader storage buffers

Location:
shaders
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • shaders/asteroid.vert

    rb01b50c r67527a5  
    88};
    99
    10 layout (binding = 0) uniform UniformBufferObject {
     10layout (binding = 0) uniform camera_block {
    1111   mat4 view;
    1212   mat4 proj;
     13} camera;
     14
     15layout(binding = 1) uniform ubo_block {
     16   Object objects[1024];
    1317} ubo;
    14 
    15 layout(binding = 1) readonly buffer StorageBufferObject {
    16    Object objects[];
    17 } sbo;
    1818
    1919layout(location = 0) in vec3 vertex_position;
     
    3535
    3636void main() {
    37    position_eye = vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_position, 1.0));
    38    normal_eye = normalize(vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_normal, 0.0)));
     37   position_eye = vec3(camera.view * ubo.objects[obj_index].model * vec4(vertex_position, 1.0));
     38   normal_eye = normalize(vec3(camera.view * ubo.objects[obj_index].model * vec4(vertex_normal, 0.0)));
    3939
    40    float hp_percent = sbo.objects[obj_index].hp / 10.0;
     40   float hp_percent = ubo.objects[obj_index].hp / 10.0;
    4141   vec3 damage_color = vec3(1.0, 0.0, 0.0);
    4242   color = (vertex_color * hp_percent) + (damage_color * (1.0 - hp_percent));
     
    4444   fragTexCoord = inTexCoord;
    4545
    46    light_position_eye = vec3(ubo.view * vec4(light_position_world, 1.0));
    47    light2_position_eye = vec3(ubo.view * vec4(light2_position_world, 1.0));
     46   light_position_eye = vec3(camera.view * vec4(light_position_world, 1.0));
     47   light2_position_eye = vec3(camera.view * vec4(light2_position_world, 1.0));
    4848
    49    if (sbo.objects[obj_index].deleted) {
     49   if (ubo.objects[obj_index].deleted) {
    5050      gl_Position = vec4(0.0, 0.0, 2.0, 1.0);
    5151   } else {
    52       gl_Position = ubo.proj * vec4(position_eye, 1.0);
     52      gl_Position = camera.proj * vec4(position_eye, 1.0);
    5353   }
    5454}
  • shaders/explosion.vert

    rb01b50c r67527a5  
    99};
    1010
    11 layout (binding = 0) uniform UniformBufferObject {
     11layout (binding = 0) uniform camera_block {
    1212   mat4 view;
    1313   mat4 proj;
    1414   float cur_time;
     15} camera;
     16
     17layout(binding = 1) uniform ubo_block {
     18   Object objects[1024];
    1519} ubo;
    16 
    17 layout(binding = 1) readonly buffer StorageBufferObject {
    18    Object objects[];
    19 } sbo;
    2020
    2121layout(location = 0) in vec3 particle_start_velocity;
     
    2626
    2727void main() {
    28    mat4 model = sbo.objects[obj_index].model;
    29    float explosion_start_time = sbo.objects[obj_index].explosion_start_time;
    30    float explosion_duration = sbo.objects[obj_index].explosion_duration;
    31    bool deleted = sbo.objects[obj_index].deleted;
     28   mat4 model = ubo.objects[obj_index].model;
     29   float explosion_start_time = ubo.objects[obj_index].explosion_start_time;
     30   float explosion_duration = ubo.objects[obj_index].explosion_duration;
     31   bool deleted = ubo.objects[obj_index].deleted;
    3232
    33    float t = ubo.cur_time - explosion_start_time - particle_start_time;
     33   float t = camera.cur_time - explosion_start_time - particle_start_time;
    3434
    3535   if (t < 0.0) {
     
    5656      p += normalize(particle_start_velocity) * mod(t, explosion_duration) / explosion_duration * 0.3;
    5757
    58       gl_Position = ubo.proj * ubo.view * model * vec4(p, 1.0);
     58      gl_Position = camera.proj * camera.view * model * vec4(p, 1.0);
    5959   }
    6060   gl_PointSize = 10.0; // size in pixels
  • shaders/laser.frag

    rb01b50c r67527a5  
    88};
    99
    10 layout(binding = 1) readonly buffer StorageBufferObject {
    11    Object objects[];
    12 } sbo;
     10layout(binding = 1) uniform ubo_block {
     11   Object objects[1024];
     12} ubo;
    1313
    1414layout(binding = 2) uniform sampler2D laser_texture;
     
    2121void main() {
    2222   vec4 texel = texture(laser_texture, texcoords_fs);
    23    vec3 laser_color = sbo.objects[obj_index_fs].color;
     23   vec3 laser_color = ubo.objects[obj_index_fs].color;
    2424
    2525   frag_color = vec4(texel.r * laser_color.r, texel.g * laser_color.g, texel.b * laser_color.b, texel.a);
  • shaders/laser.vert

    rb01b50c r67527a5  
    88};
    99
    10 layout (binding = 0) uniform UniformBufferObject {
     10layout (binding = 0) uniform camera_block {
    1111   mat4 view;
    1212   mat4 proj;
     13} camera;
     14
     15layout(binding = 1) uniform ubo_block {
     16   Object objects[1024];
    1317} ubo;
    14 
    15 layout(binding = 1) readonly buffer StorageBufferObject {
    16    Object objects[];
    17 } sbo;
    1818
    1919layout(location = 0) in vec3 vertex_position;
     
    2525
    2626void main() {
    27    vec3 position_eye = vec3(ubo.view * sbo.objects[obj_index_vs].model * vec4(vertex_position, 1.0));
     27   vec3 position_eye = vec3(camera.view * ubo.objects[obj_index_vs].model * vec4(vertex_position, 1.0));
    2828
    2929   texcoords_fs = texcoords_vs;
    3030   obj_index_fs = obj_index_vs;
    3131
    32    if (sbo.objects[obj_index_vs].deleted) {
     32   if (ubo.objects[obj_index_vs].deleted) {
    3333      gl_Position = vec4(0.0, 0.0, 2.0, 1.0);
    3434   } else {
    35       gl_Position = ubo.proj * vec4(position_eye, 1.0);
     35      gl_Position = camera.proj * vec4(position_eye, 1.0);
    3636   }
    3737}
  • shaders/model.vert

    rb01b50c r67527a5  
    66};
    77
    8 layout (binding = 0) uniform UniformBufferObject {
     8layout (binding = 0) uniform camera_block {
    99   mat4 view;
    1010   mat4 proj;
     11} camera;
     12
     13// TODO: Verify that I can actually store 1024 values here. They last time I checked, I think I could only store 1016 values
     14layout (binding = 1) uniform ubo_block {
     15        Object objects[1024];
    1116} ubo;
    12 
    13 layout(binding = 1) readonly buffer StorageBufferObject {
    14    Object objects[];
    15 } sbo;
    16 
    17 layout (binding = 2) uniform UboInstance {
    18         mat4 model;
    19 } uboInstance;
    2017
    2118layout(location = 0) in vec3 inPosition;
     
    3128void main() {
    3229   // Using 0.0 instead of 1.0 means translations won't effect the normal
    33    normal_eye = normalize(vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_normal, 0.0)));
     30   normal_eye = normalize(vec3(camera.view * ubo.objects[obj_index].model * vec4(vertex_normal, 0.0)));
    3431
    3532   fragColor = inColor;
    3633   fragTexCoord = inTexCoord;
    3734
    38    gl_Position = ubo.proj * ubo.view * sbo.objects[obj_index].model * vec4(inPosition, 1.0);
     35   gl_Position = camera.proj * camera.view * ubo.objects[obj_index].model * vec4(inPosition, 1.0);
    3936}
  • shaders/ship.vert

    rb01b50c r67527a5  
    66};
    77
    8 layout (binding = 0) uniform UniformBufferObject {
     8layout (binding = 0) uniform camera_block {
    99   mat4 view;
    1010   mat4 proj;
     11} camera;
     12
     13layout(binding = 1) uniform ubo_block {
     14   Object objects[1024];
    1115} ubo;
    12 
    13 layout(binding = 1) readonly buffer StorageBufferObject {
    14    Object objects[];
    15 } sbo;
    1616
    1717layout(location = 0) in vec3 vertex_position;
     
    3535// Check Anton's book to see how to fix this
    3636void main() {
    37    position_eye = vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_position, 1.0));
     37   position_eye = vec3(camera.view * ubo.objects[obj_index].model * vec4(vertex_position, 1.0));
    3838
    3939   // Using 0.0 instead of 1.0 means translations won't effect the normal
    40    normal_eye = normalize(vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_normal, 0.0)));
     40   normal_eye = normalize(vec3(camera.view * ubo.objects[obj_index].model * vec4(vertex_normal, 0.0)));
    4141
    4242   color = vertex_color;
     
    4444   fragTexCoord = inTexCoord;
    4545
    46    light_position_eye = vec3(ubo.view * vec4(light_position_world, 1.0));
    47    light2_position_eye = vec3(ubo.view * vec4(light2_position_world, 1.0));
     46   light_position_eye = vec3(camera.view * vec4(light_position_world, 1.0));
     47   light2_position_eye = vec3(camera.view * vec4(light2_position_world, 1.0));
    4848
    49    gl_Position = ubo.proj * vec4(position_eye, 1.0);
     49   gl_Position = camera.proj * vec4(position_eye, 1.0);
    5050}
Note: See TracChangeset for help on using the changeset viewer.