Skip to content

Commit

Permalink
gfx_api: Use a single (huge) triangle that covers the screen to rende…
Browse files Browse the repository at this point in the history
…r offscreen framebuffer
  • Loading branch information
past-due committed Jun 26, 2023
1 parent 65ace3f commit f5ca02d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
5 changes: 2 additions & 3 deletions data/base/shaders/vk/world_to_screen.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ layout(std140, set = 0, binding = 0) uniform cbuffer {
};

layout(location = 0) in vec2 vertexPos;
layout(location = 1) in vec2 vertexTexCoords;

layout(location = 0) out vec2 texCoords;

void main()
{
texCoords = vertexTexCoords;
gl_Position = vec4(vertexPos.x, vertexPos.y, 0.f, 1.f);
gl_Position = vec4(vertexPos.x, vertexPos.y, 0.0, 1.0);
texCoords = 0.5 * gl_Position.xy + vec2(0.5);
// gl_Position.y *= -1.;
// gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;
}
6 changes: 2 additions & 4 deletions data/base/shaders/world_to_screen.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

#if (!defined(GL_ES) && (__VERSION__ >= 130)) || (defined(GL_ES) && (__VERSION__ >= 300))
in vec2 vertexPos;
in vec2 vertexTexCoords;
#else
attribute vec2 vertexPos;
attribute vec2 vertexTexCoords;
#endif

#if (!defined(GL_ES) && (__VERSION__ >= 130)) || (defined(GL_ES) && (__VERSION__ >= 300))
Expand All @@ -17,6 +15,6 @@ varying vec2 texCoords;

void main()
{
texCoords = vertexTexCoords;
gl_Position = vec4(vertexPos.x, vertexPos.y, 0.f, 1.f);
gl_Position = vec4(vertexPos.x, vertexPos.y, 0.0, 1.0);
texCoords = 0.5 * gl_Position.xy + vec2(0.5);
}
5 changes: 2 additions & 3 deletions lib/ivis_opengl/gfx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1250,9 +1250,8 @@ namespace gfx_api
using WorldToScreenPSO = typename gfx_api::pipeline_state_helper<rasterizer_state<REND_OPAQUE, DEPTH_CMP_ALWAYS_WRT_OFF, 255, polygon_offset::disabled, stencil_mode::stencil_disabled, cull_mode::none>, primitive_type::triangles, index_type::u16,
std::tuple<constant_buffer_type<SHADER_WORLD_TO_SCREEN>>,
std::tuple<
vertex_buffer_description<4 * sizeof(gfxFloat), gfx_api::vertex_attribute_input_rate::vertex,
vertex_attribute_description<position, gfx_api::vertex_attribute_type::float2, 0>,
vertex_attribute_description<texcoord, gfx_api::vertex_attribute_type::float2, sizeof(gfxFloat)*2>
vertex_buffer_description<2 * sizeof(gfxFloat), gfx_api::vertex_attribute_input_rate::vertex,
vertex_attribute_description<position, gfx_api::vertex_attribute_type::float2, 0>
>
>,
std::tuple<texture_description<0, sampler_type::bilinear, pixel_format_target::texture_2d>>, SHADER_WORLD_TO_SCREEN>;
Expand Down
29 changes: 12 additions & 17 deletions src/display3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static WzText txtShowOrders;
// show Droid visible/draw counts text
static WzText droidText;

static gfx_api::buffer* pScreenQuadVBO = nullptr;
static gfx_api::buffer* pScreenTriangleVBO = nullptr;


/******************** Variables ********************/
Expand Down Expand Up @@ -1455,10 +1455,10 @@ static void drawTiles(iView *player)
// Draw the scene to the default framebuffer
gfx_api::WorldToScreenPSO::get().bind();
gfx_api::WorldToScreenPSO::get().bind_constants({1.0f});
gfx_api::WorldToScreenPSO::get().bind_vertex_buffers(pScreenQuadVBO);
gfx_api::WorldToScreenPSO::get().bind_vertex_buffers(pScreenTriangleVBO);
gfx_api::WorldToScreenPSO::get().bind_textures(gfx_api::context::get().getSceneTexture());
gfx_api::WorldToScreenPSO::get().draw(6, 0);
gfx_api::WorldToScreenPSO::get().unbind_vertex_buffers(pScreenQuadVBO);
gfx_api::WorldToScreenPSO::get().unbind_vertex_buffers(pScreenTriangleVBO);
}

/// Initialise the fog, skybox and some other stuff
Expand Down Expand Up @@ -1507,19 +1507,14 @@ bool init3DView()
batchedObjectStatusRenderer.initialize();


// vertex attributes for screen-filling quad in NDC
gfx_api::gfxFloat screenQuadVertices[] = {
// position // texCoord
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
//
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
// vertex attributes for a single triangle that covers the screen
gfx_api::gfxFloat screenTriangleVertices[] = {
-1.f, -1.f,
3.f, -1.f,
-1.f, 3.f
};
pScreenQuadVBO = gfx_api::context::get().create_buffer_object(gfx_api::buffer::usage::vertex_buffer, gfx_api::context::buffer_storage_hint::static_draw, "screenQuadVertices");
pScreenQuadVBO->upload(sizeof(screenQuadVertices), screenQuadVertices);
pScreenTriangleVBO = gfx_api::context::get().create_buffer_object(gfx_api::buffer::usage::vertex_buffer, gfx_api::context::buffer_storage_hint::static_draw, "screenTriangleVertices");
pScreenTriangleVBO->upload(sizeof(screenTriangleVertices), screenTriangleVertices);

return true;
}
Expand Down Expand Up @@ -1550,8 +1545,8 @@ void shutdown3DView_FullReset()
// in this function, which is called from stageTwoShutdown (as opposed to stageThreeShutdown, which is called from levReleaseMissionData)...
batchedObjectStatusRenderer.reset();

delete pScreenQuadVBO;
pScreenQuadVBO = nullptr;
delete pScreenTriangleVBO;
pScreenTriangleVBO = nullptr;
}

/// set the view position from save game
Expand Down

0 comments on commit f5ca02d

Please sign in to comment.