I have been experimenting with batched sprite rendering, and I've got a solution that works well on my desktop PC. However, trying it on my integrated Intel UHD 620 laptop I get the following performance warnings:
[21:42:03 error] OpenGL: API - Performance - Recompiling fragment shader for program 27
[21:42:03 error] OpenGL: API - Performance - multisampled FBO 0->1
Presumably because of the source of these performance warnings, frames that take 1-2 milliseconds on my dedicated graphics card machine are about 100 milliseconds on my laptop.
Here is my renderer code:
BatchedSpriteRenderer::BatchedSpriteRenderer(ResourceManager &resource_manager)
: resource_manager(&resource_manager),
max_sprites(100000),
vertex_array(std::make_unique<VertexArray>()),
vertex_buffer(std::make_unique<VertexBuffer>())
{
resource_manager.load_shader("batched_texture",
"shaders/texture_batched.vert",
"shaders/texture.frag");
std::vector<unsigned int> sprite_indices;
for (int i = 0; i < max_sprites; ++i)
{
unsigned int sprite_number = i * 4;
sprite_indices.push_back(0 + sprite_number);
sprite_indices.push_back(1 + sprite_number);
sprite_indices.push_back(2 + sprite_number);
sprite_indices.push_back(2 + sprite_number);
sprite_indices.push_back(3 + sprite_number);
sprite_indices.push_back(0 + sprite_number);
}
element_buffer = std::make_unique<ElementBuffer>(sprite_indices.data(), max_sprites * 6);
VertexBufferLayout layout;
layout.push<float>(2);
layout.push<float>(2);
layout.push<float>(4);
vertex_array->add_buffer(*vertex_buffer, layout);
}
void BatchedSpriteRenderer::draw(const std::string &texture,
const std::vector<glm::mat4> &transforms,
const glm::mat4 &view)
{
vertex_array->bind();
auto shader = resource_manager->shader_store.get("batched_texture");
shader->bind();
std::vector<SpriteVertex> vertices;
vertices.reserve(transforms.size() * 4);
for (const auto &transform : transforms)
{
glm::vec4 transformed_position = transform * glm::vec4(0.0, 1.0, 1.0, 1.0);
vertices.push_back({glm::vec2(transformed_position.x, transformed_position.y),
glm::vec2(0.0, 1.0),
glm::vec4(1.0, 1.0, 1.0, 1.0)});
transformed_position = transform * glm::vec4(0.0, 0.0, 1.0, 1.0);
vertices.push_back({glm::vec2(transformed_position.x, transformed_position.y),
glm::vec2(0.0, 0.0),
glm::vec4(1.0, 1.0, 1.0, 1.0)});
transformed_position = transform * glm::vec4(1.0, 0.0, 1.0, 1.0);
vertices.push_back({glm::vec2(transformed_position.x, transformed_position.y),
glm::vec2(1.0, 0.0),
glm::vec4(1.0, 1.0, 1.0, 1.0)});
transformed_position = transform * glm::vec4(1.0, 1.0, 1.0, 1.0);
vertices.push_back({glm::vec2(transformed_position.x, transformed_position.y),
glm::vec2(1.0, 1.0),
glm::vec4(1.0, 1.0, 1.0, 1.0)});
}
vertex_buffer->add_data(vertices.data(),
sizeof(SpriteVertex) * vertices.size(),
GL_DYNAMIC_DRAW);
shader->set_uniform_mat4f("u_view", view);
shader->set_uniform_1i("u_texture", 0);
resource_manager->texture_store.get(texture)->bind();
glDrawElements(GL_TRIANGLES, transforms.size() * 6, GL_UNSIGNED_INT, 0);
}
Hopefully my abstractions should be fairly self explanatory. Each of the abstraction classes (VertexArray, VertexBuffer, ElementBuffer,VertexBufferLayout) manage the lifetime of their equivalent OpenGL object.
Here are the shaders being used:
texture_batched.vert
#version 430 core
layout(location = 0)in vec2 v_position;
layout(location = 1)in vec2 v_tex_coord;
layout(location = 2)in vec4 v_color;
out vec4 color;
out vec2 tex_coord;
uniform mat4 u_view;
void main()
{
tex_coord = v_tex_coord;
gl_Position = u_view * vec4(v_position, 0.0, 1.0);
color = v_color;
}
texture.frag
#version 430 core
in vec4 color;
in vec2 tex_coord;
out vec4 frag_color;
uniform sampler2D u_texture;
void main()
{
frag_color = texture(u_texture, tex_coord);
frag_color *= color;
}
What's causing these performance issues, and how can I fix them?
EDIT: I completely forgot to mention that the actual image rendered with this is completely messed up, I'll try and grab a screenshot of it working correctly when I get to my desktop PC, but here's what the broken version looks like:
It should be a neat grid of 200x200 white circles.
EDIT 2: I tried it on another computer, this time with a GTX 1050 Ti, and it is also broken. No error messages or warnings this time. The warning may have been unrelated.
It ended up being unrelated to OpenGL, as far as I can see.
In the draw function, I create a vector called vertices, then put all the vertices in it. For some reason, when I recreate that vector every frame the following push_back calls aren't properly adding to the vector. The members of the SpriteVertex structs were getting mixed up. As such, instead of the correct layout:
pos tex_coord color
pos tex_coord color
pos tex_coord color
pos tex_coord color
It was being filled in the following layout:
pos tex_coord color
tex_coord pos color
tex_coord pos color
tex_coord pos color
Or something to that effect, at least.
I changed it so that the vertices vector is a member of the BatchedSpriteRenderer class, reserving space for the maximum possible number of vertices.
void BatchedSpriteRenderer::draw(const std::string &texture,
const std::vector<glm::mat4> &transforms,
const glm::mat4 &view)
{
vertex_array->bind();
auto shader = resource_manager->shader_store.get("batched_texture");
shader->bind();
for (unsigned int i = 0; i < transforms.size(); ++i)
{
const auto &transform = transforms[i];
glm::vec4 transformed_position = transform * glm::vec4(0.0, 1.0, 1.0, 1.0);
vertices[i * 4] = {glm::vec2(transformed_position.x,
transformed_position.y),
glm::vec2(0.0, 1.0),
glm::vec4(1.0, 1.0, 1.0, 1.0)};
transformed_position = transform * glm::vec4(0.0, 0.0, 1.0, 1.0);
vertices[i * 4 + 1] = {glm::vec2(transformed_position.x,
transformed_position.y),
glm::vec2(0.0, 0.0),
glm::vec4(1.0, 1.0, 1.0, 1.0)};
transformed_position = transform * glm::vec4(1.0, 0.0, 1.0, 1.0);
vertices[i * 4 + 2] = {glm::vec2(transformed_position.x,
transformed_position.y),
glm::vec2(1.0, 0.0),
glm::vec4(1.0, 1.0, 1.0, 1.0)};
transformed_position = transform * glm::vec4(1.0, 1.0, 1.0, 1.0);
vertices[i * 4 + 3] = {glm::vec2(transformed_position.x,
transformed_position.y),
glm::vec2(1.0, 1.0),
glm::vec4(1.0, 1.0, 1.0, 1.0)};
}
vertex_buffer->add_data(vertices.data(),
sizeof(SpriteVertex) * (transforms.size() * 4),
GL_DYNAMIC_DRAW);
shader->set_uniform_mat4f("u_view", view);
shader->set_uniform_1i("u_texture", 0);
resource_manager->texture_store.get(texture)->bind();
glDrawElements(GL_TRIANGLES, transforms.size() * 6, GL_UNSIGNED_INT, 0);
}
Related
I'm trying to draw to a cubemap in a single pass using a geometry shade in OpenGL.
Basically need I do this to copy the content of a cubemap into another cubemap, and the may not have the same resolution and pixel layout.
I'm trying to achieve the result I want feeding a single point to the vertex shader and then, from the geometry shader, select each layer (face of the cubemap) and emit a quad and texture coordinates.
So far I've tried this method emitting only two of the cubemap faces (positive and negative X) to see if it could work, but it doesn't.
Using NSight I can see that there is something wrong.
This is the source cubemap:
And this is the result cubemap:
The only face that's being drawn to is the positive X and still it's not correct.
This is my geometry shader:
#version 330 core
layout(points) in;
layout(triangle_strip, max_vertices = 8) out;
in vec3 pos[];
out vec3 frag_textureCoord;
void main()
{
const vec4 positions[4] = vec4[4] ( vec4(-1.0, -1.0, 0.0, 0.0),
vec4( 1.0, -1.0, 0.0, 0.0),
vec4(-1.0, 1.0, 0.0, 0.0),
vec4( 1.0, 1.0, 0.0, 0.0) );
// Positive X
gl_Layer = 0;
gl_Position = positions[0];
frag_textureCoord = vec3(1.0, -1.0, -1.0);
EmitVertex();
gl_Position = positions[1];
frag_textureCoord = vec3(1.0, -1.0, 1.0);
EmitVertex();
gl_Position = positions[2];
frag_textureCoord = vec3(1.0, 1.0, -1.0);
EmitVertex();
gl_Position = positions[3];
frag_textureCoord = vec3(1.0, 1.0, 1.0);
EmitVertex();
EndPrimitive();
// Negative X
gl_Layer = 1;
gl_Position = positions[0];
frag_textureCoord = vec3(-1.0, -1.0, 1.0);
EmitVertex();
gl_Position = positions[1];
frag_textureCoord = vec3(-1.0, -1.0, -1.0);
EmitVertex();
gl_Position = positions[2];
frag_textureCoord = vec3(-1.0, 1.0, 1.0);
EmitVertex();
gl_Position = positions[3];
frag_textureCoord = vec3(-1.0, 1.0, -1.0);
EmitVertex();
EndPrimitive();
}
And this is my fragment shader:
#version 150 core
uniform samplerCube AtmosphereMap;
in vec3 frag_textureCoord;
out vec4 FragColor;
void main()
{
FragColor = texture(AtmosphereMap, frag_textureCoord) * 1.0f;
}
UPDATE
Further debugging with NSight shows that for the positive x face every fragment gets a value of frag_textureCoord of vec3(~1.0, ~0.0, ~0.0) (I've used ~ since the values are not exactly those but approximated). The negative x face instead never reaches the fragment shader stage.
UPDATE
Changing the definition of my vertex position from vec4(x, y, z, 0.0) to vec4(x, y, z, 1.0) makes my shader render correctly the positive X face, but the negative is still wrong, even if debugging the fragment shader I see that the right color is selected and applied, but then it becomes black.
gl_Layer = 0;
This is a Geometry Shader output. Calling EmitVertex will cause the value of all output variables to become undefined. Therefore, you must always set each output for each vertex to which that output applies.
I'm trying to do point source directional lighting in OpenGL using my textbooks examples. I'm showing a rectangle centered at the origin, and doing the lighting computations in the shader. The rectangle appears, but it is black even when I try to put colored lights on it. Normals for the rectangle are all (0, 1.0, 0). I'm not doing any non-uniform scaling, so the regular model view matrix should also transform the normals.
I have code that sets the light parameters(as uniforms) and material parameters(also as uniforms) for the shader. There is no per vertex color information.
void InitMaterial()
{
color material_ambient = color(1.0, 0.0, 1.0);
color material_diffuse = color(1.0, 0.8, 0.0);
color material_specular = color(1.0, 0.8, 0.0);
float material_shininess = 100.0;
// set uniforms for current program
glUniform3fv(glGetUniformLocation(Programs[lightingType], "materialAmbient"), 1, material_ambient);
glUniform3fv(glGetUniformLocation(Programs[lightingType], "materialDiffuse"), 1, material_diffuse);
glUniform3fv(glGetUniformLocation(Programs[lightingType], "materialSpecular"), 1, material_specular);
glUniform1f(glGetUniformLocation(Programs[lightingType], "shininess"), material_shininess);
}
For the lights:
void InitLight()
{
// need light direction and light position
point4 light_position = point4(0.0, 0.0, -1.0, 0.0);
color light_ambient = color(0.2, 0.2, 0.2);
color light_diffuse = color(1.0, 1.0, 1.0);
color light_specular = color(1.0, 1.0, 1.0);
glUniform3fv(glGetUniformLocation(Programs[lightingType], "lightPosition"), 1, light_position);
glUniform3fv(glGetUniformLocation(Programs[lightingType], "lightAmbient"), 1, light_ambient);
glUniform3fv(glGetUniformLocation(Programs[lightingType], "lightDiffuse"), 1, light_diffuse);
glUniform3fv(glGetUniformLocation(Programs[lightingType], "lightSpecular"), 1, light_specular);
}
The fragment shader is a simple pass through shader that sets the color to the one input from the vertex shader. Here is the vertex shader :
#version 150
in vec4 vPosition;
in vec3 vNormal;
out vec4 color;
uniform vec4 materialAmbient, materialDiffuse, materialSpecular;
uniform vec4 lightAmbient, lightDiffuse, lightSpecular;
uniform float shininess;
uniform mat4 modelView;
uniform vec4 lightPosition;
uniform mat4 projection;
void main()
{
// Transform vertex position into eye coordinates
vec3 pos = (modelView * vPosition).xyz;
vec3 L = normalize(lightPosition.xyz - pos);
vec3 E = normalize(-pos);
vec3 H = normalize(L + E);
// Transform vertex normal into eye coordinates
vec3 N = normalize(modelView * vec4(vNormal, 0.0)).xyz;
// Compute terms in the illumination equation
vec4 ambient = materialAmbient * lightAmbient;
float Kd = max(dot(L, N), 0.0);
vec4 diffuse = Kd * materialDiffuse * lightDiffuse;
float Ks = pow(max(dot(N, H), 0.0), shininess);
vec4 specular = Ks * materialSpecular * lightSpecular;
if(dot(L, N) < 0.0) specular = vec4(0.0, 0.0, 0.0, 1.0);
gl_Position = projection * modelView * vPosition;
color = ambient + diffuse + specular;
color.a = 1.0;
}
Ok, it's working now. The solution was to replace glUniform3fv with glUniform4fv, I guess because the glsl counterpart is a vec4 instead of a vec3. I thought that it would be able to recognize this and simply add a 1.0 to the end, but no.
I'm trying to get a sun shader to work, but I can't get it to work.
What I currently get is a quarter of a circle/elipsis on the lower left of my screen, that is really stuck to my screen (if I move the camera, it also moves).
All I do is render two triangles to form a screen-covering quad, with screen width and height in uniforms.
Vertex Shader
#version 430 core
void main(void) {
const vec4 vertices[6] = vec4[](
vec4(-1.0, -1.0, 1.0, 1.0),
vec4(-1.0, 1.0, 1.0, 1.0),
vec4(1.0, 1.0, 1.0, 1.0),
vec4(1.0, 1.0, 1.0, 1.0),
vec4(1.0, -1.0, 1.0, 1.0),
vec4(-1.0, -1.0, 1.0, 1.0)
);
gl_Position = vertices[gl_VertexID];
}
Fragment Shader
#version 430 core
layout(location = 7) uniform int screen_width;
layout(location = 8) uniform int screen_height;
layout(location = 1) uniform mat4 view_matrix;
layout(location = 2) uniform mat4 proj_matrix;
out vec4 color;
uniform vec3 light_pos = vec3(-20.0, 7.5, -20.0);
void main(void) {
//calculate light position in screen space and get x, y components
vec2 screen_space_light_pos = (proj_matrix * view_matrix * vec4(light_pos, 1.0)).xy;
//calculate fragment position in screen space
vec2 screen_space_fragment_pos = vec2(gl_FragCoord.x / screen_width, gl_FragCoord.y / screen_height);
//check if it is in the radius of the sun
if (length(screen_space_light_pos - screen_space_fragment_pos) < 0.1) {
color = vec4(1.0, 1.0, 0.0, 1.0);
}
else {
discard;
}
}
What I think it does:
Get the position of the sun (light_pos) in screen space.
Get the fragment position in screen space.
If the distance between them is below a certain value, draw fragment with yellow color;
Else discard.
screen_space_light_pos is not yet in clip space. You've missed perspective division:
vec3 before_division = (proj_matrix * view_matrix * vec4(light_pos, 1.0)).xyw;
vec2 screen_space_light_pos = before_division.xy / before_division.z;
With common proj_matrix configurations, screen_space_light_pos will be in [-1,1]x[-1,1]. To match screen_space_fragment_pos range, you probably need to adjust screen_space_light_pos:
screen_space_light_pos = screen_space_light_pos * 0.5 + 0.5;
I want to draw a square from point data with the geometry shader.
In the vertex shader, I emit a single point.
#version 330 core
void main() {
gl_Position = vec4(0, 0, 0, 1.0);
}
In the geometry shader, I want to create a triangle strip forming a square.
The size is irrelevant at the moment so the model should have a size of 1 (ranging from (-0.5, -0.5) from the initial point position to (+0.5, +0.5).
I need help to calculate the position of the emitted vertices, as visible in the code:
#version 330 core
layout(points) in;
layout(triangle_strip, max_vertices=4) out;
out vec2 tex_coord;
uniform mat4x4 model;
uniform mat4x4 view;
uniform mat4x4 projection;
void main() {
int i;
for(i = 0; i < gl_in.length(); ++i) {
mat4x4 trans;
trans = //???
gl_Position = projection * view * model * trans * gl_in[i].gl_Position;
tex_coord = vec2(0, 0);
EmitVertex();
trans = //???
gl_Position = projection * view * model * trans * gl_in[i].gl_Position;
tex_coord = vec2(1, 0);
EmitVertex();
trans = //???
gl_Position = projection * view * model * trans * gl_in[i].gl_Position;
tex_coord = vec2(1, 1);
EmitVertex();
trans = //???
gl_Position = projection * view * model * trans * gl_in[i].gl_Position;
tex_coord = vec2(0, 1));
EmitVertex();
}
EndPrimitive();
}
I thought to use trans to translate the initial point to the desired coordinates. How would I realize this?
Edit for clarity
I want to generate from a single point what else would be given by the vertex buffer; a single plane:
float vertex[] {
-0.5, 0.5, 0.0,
0.5, 0.5, 0.0,
0.5, -0.5, 0.0,
-0.5, -0.5, 0.0
}
Instead I give only a point in the middle of these points and want to generate the real points by subtracting and adding the differences to the center (0.5 and -0.5 etc.). All I need is to know how to apply this transformation in the code (where the ??? are).
Judging by your updated question, I think this pseudo-code should get you pointed in the right direction. It seems to me that all you want to do is offset the x and y coordinates of your point by a constant amount, so an array is the perfect way to do this.
const vec3 offset [4] =
vec3 [] ( vec3 (-0.5, 0.5, 0.0),
vec3 ( 0.5, 0.5, 0.0),
vec3 ( 0.5, -0.5, 0.0),
vec3 (-0.5, -0.5, 0.0) );
const vec2 tc [4] =
vec2 [] ( vec2 (0.0, 0.0),
vec2 (1.0, 0.0),
vec2 (1.0, 1.0),
vec2 (0.0, 1.0) );
void
main (void)
{
int i;
for (i = 0; i < gl_in.length (); ++i) {
gl_Position = projection * view * model * (gl_in [i].gl_Position + offset [0]);
tex_coord = tc [0];
EmitVertex ();
gl_Position = projection * view * model * (gl_in [i].gl_Position + offset [1]);
tex_coord = tc [1];
EmitVertex ();
gl_Position = projection * view * model * (gl_in [i].gl_Position + offset [2]);
tex_coord = tc [2];
EmitVertex ();
gl_Position = projection * view * model * (gl_in [i].gl_Position + offset [3]);
tex_coord = tc [3];
EmitVertex ();
}
EndPrimitive ();
}
i have implemented shadowmapping with an FBO and GLSL.
it is used on a heightfield. that is some objects (trees, plants, ...) cast shadows on the heightfield.
the problem i have, is that the shadows are only visible on the ground of the heightfield. that is, where the heightfield's height = 0. as soon as there is some height involved, the shadows disappear. if i look at the shadowmap itself, everything looks fine... objects that are closer to the light are darker.
here is my GLSL vertexshader:
uniform mat4 lightView, lightProjection;
const mat4 biasMatrix = mat4( 0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0); //bias from [-1, 1] to [0, 1]
void main()
{
gl_Position = ftransform();
mat4 shadowMatrix = biasMatrix * lightProjection * lightView;
shadowTexCoord = shadowMatrix * gl_Vertex;
}
fragmentshader:
uniform sampler2DShadow shadowmap;
varying vec4 shadowTexCoord;
void main()
{
vec4 shadow = shadow2DProj(shadowmap, shadowTexCoord, 0.0);
float colorshadow = shadow.r < 0.1 ? 0.5 : 1.0;
vec4 color = vec4(1,1,1,1);
gl_FragColor = vec4( color*colorshadow, color.w );
}
thanks a lot for any help on this!
I think there might be some confusion between the different spaces here. As written, it looks like your code would only work if gl_ModelViewMatrix for the ground contains only camera transformations. This is because ftransform basically goes
gl_Position = gl_ProjectionMatrix * (gl_ModelViewMatrix * gl_Vertex)
that means that the gl_Vertex is specified in object coordinates. However typically the view matrix of the light maps from world coordinates to the light's view space so this code would only work if object space = world space. So basically, lets say you scale the terrain, well then object space doesn't equal world space anymore. Because of this you need to separate out the gl_ModelViewMatrix into two parts: the camera view matrix and the modeling transform (eg object -> world space)
I havent tested this code, but I would try something like this:
uniform mat4 lightView, lightProjection;
uniform mat4 camView, camProj, modelTrans;
const mat4 biasMatrix = mat4( 0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0); //bias from [-1, 1] to [0, 1]
void main()
{
mat4 modelViewProjMatrix = camProj * camView * modelTrans;
gl_Position = modelViewProjMatrix * gl_Vertex;
mat4 shadowMatrix = biasMatrix * lightProjection * lightView * modelTrans;
shadowTexCoord = shadowMatrix * gl_Vertex;
}
Technically it's faster to multiply the matrices on the CPU and only pass the exact ones you need but for getting stuff working sometimes its easier to do this way.
Maybe you just missed it copy-pasting, but I don't see shadowTexCoord as varying in the vertex shader. This should result in a compilation error, though.