Vulkan Dynamic Buffer With Different Texture Per Object - glsl

I am currently using a dynamic buffer to store the uniforms of multiple objects and update them individually without changing the shader. The problem is that when i am adding a VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER to my descriptor the render is this (I can't post images due to low reputation)! Just the color of the background of the texture. This is my initialization of the descriptor().
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = descriptorSet;
descriptorWrites[0].pNext = NULL;
descriptorWrites[0].dstBinding = 0;
descriptorWrites[0].dstArrayElement = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
descriptorWrites[0].descriptorCount = 1;
descriptorWrites[0].pBufferInfo = &bufferInfo;
descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[1].dstSet = descriptorSet;
descriptorWrites[1].pNext = NULL;
descriptorWrites[1].dstBinding = 1;
descriptorWrites[1].dstArrayElement = 0;
descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrites[1].descriptorCount = 1;
descriptorWrites[1].pImageInfo = &imageInfo;
And this is my fragment shader:
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(set=0, binding = 1) uniform sampler2D texSampler;
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 0) out vec4 outColor;
void main() {
outColor = texture(texSampler, fragTexCoord*2);
}
What could be the reason of this? I have updated descriptor layout, and the validation layer don't throw any errors and the dynamic buffer was working perfectly even for multiple objects. Is the binding corrupted or something like that?

Related

Vulkan Transparent 2D Textures

I'm trying to load 2D sprites containing transparent pixels using Vulkan, where so far I have been able to load the sprite, but have not been able to get the transparency working (Transparent pixels should blend with the background color blue). I'm not sure what to do to get it right.
Color Blend State:
VkPipelineColorBlendAttachmentState colorBlendAttachment {};
colorBlendAttachment.colorWriteMask =
VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_TRUE;
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
VkPipelineColorBlendStateCreateInfo colorBlendState {};
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlendState.logicOpEnable = VK_FALSE;
colorBlendState.logicOp = VK_LOGIC_OP_COPY;
colorBlendState.attachmentCount = 1;
colorBlendState.pAttachments = &colorBlendAttachment;
colorBlendState.blendConstants[0] = 1.f;
colorBlendState.blendConstants[1] = 1.f;
colorBlendState.blendConstants[2] = 1.f;
colorBlendState.blendConstants[3] = 1.f;
Depth Stencil State:
VkPipelineDepthStencilStateCreateInfo info {};
Rasterization State:
VkPipelineRasterizationStateCreateInfo info {};
info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
info.depthClampEnable = VK_FALSE;
info.rasterizerDiscardEnable = VK_FALSE;
info.polygonMode = polygonMode;
info.lineWidth = 1.0f;
info.cullMode = VK_CULL_MODE_NONE;
info.frontFace = VK_FRONT_FACE_CLOCKWISE;
info.depthBiasEnable = VK_FALSE;
info.depthBiasConstantFactor = 0.0f;
info.depthBiasClamp = 0.0f;
info.depthBiasSlopeFactor = 0.0f;
Vertex Shader:
#version 460
layout (location = 0) in vec2 vPosition;
layout (location = 1) in vec2 vTexCoord;
layout (location = 0) out vec2 texCoord;
void main()
{
gl_Position = vec4(vPosition, 0.0f, 1.0f);
texCoord = vTexCoord;
}
Fragment Shader:
#version 460
layout (location = 0) in vec2 texCoord;
layout (location = 0) out vec4 outFragColor;
layout(set = 0, binding = 0) uniform sampler2D tex;
void main()
{
vec3 color = texture(tex, texCoord).xyz;
outFragColor = vec4(color, 1.0f);
}
When rendering in Vulkan we have to be explicit with how the color blending should be performed. You can read more here.
Some common mistakes when it comes to color blending is choosing the wrong blend factors and blend operations. But it's also very common to forget enabling blending .blendEnable = true;. Because In Vulkan you're able to attach attachments without them taking effect, which is why you have to be explicit
typedef struct VkPipelineColorBlendAttachmentState
{
VkBool32 blendEnable;
VkBlendFactor srcColorBlendFactor;
VkBlendFactor dstColorBlendFactor;
VkBlendOp colorBlendOp;
VkBlendFactor srcAlphaBlendFactor;
VkBlendFactor dstAlphaBlendFactor;
VkBlendOp alphaBlendOp;
VkColorComponentFlags colorWriteMask;
} VkPipelineColorBlendAttachmentState;
It may also be that you're pixel shader is not taking your textures alpha values into consideration as shown in the question above outFragColor = vec4(color, 1.0f);. Instead one should retrieve the texel coordinate as a vector4 texture(texture, uv).xyzw;
One may also be using the wrong file format or even simply not having proper transparency in texture files. Recommended texture files for game engines: dds, png, tga
In general, fully transparent pixels of 2D textures should not be handled by blend factors in Vulkan. If you cannot guarantee ordered drawing it is very easy to end with scenarios where transparent pixels overwrite non-transparent sections.
The fast solution is to discard transparent pixels in the fragment shader. This prevents the z buffer from being written to and all is well. I use code like this in my billboard shader:
outColor = texture(global_textures[nonuniformEXT(texIndex)], fragTexCoord);
// discard transparent pixels (== do not write z-buffer)
if (outColor.w < 0.8) {
discard;
}

I’m having problems attempting to get the debugPrintfEXT working in the shaders

It seems like I got partly working, but every time it tries to log I get this message:
Validation Layer: Validation Warning: [ UNASSIGNED-DEBUG-PRINTF ] Object 0: handle = 0x19d6337ae88, type = VK_OBJECT_TYPE_DEVICE; | MessageID = 0x92394c89 | WARNING - Debug Printf message was truncated, likely due to a buffer size that was too small for the message
I'm only doing a one char test log right now untill I get it working so it shouldn't have be a problem. I've also got #extension GL_EXT_debug_printf : enable enabled in the shader.
I've gotten the debugger setting set to:
VkDebugUtilsMessengerCreateInfoEXT DebugInfo = {};
DebugInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
DebugInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;|
DebugInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;|
DebugInfo.pfnUserCallback = DebugCallBack;
Start up validation info:
DeviceExtensions.emplace_back(VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME);
VkValidationFeatureEnableEXT enabled[] = { VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT };
VkValidationFeatureDisableEXT disabled[] = {
VK_VALIDATION_FEATURE_DISABLE_THREAD_SAFETY_EXT, VK_VALIDATION_FEATURE_DISABLE_API_PARAMETERS_EXT,
VK_VALIDATION_FEATURE_DISABLE_OBJECT_LIFETIMES_EXT, VK_VALIDATION_FEATURE_DISABLE_CORE_CHECKS_EXT };
std::vector<const char*> ExtensionList = getRequiredExtensions();
VkInstanceCreateInfo VulkanCreateInfo = {};
VulkanCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
VulkanCreateInfo.pApplicationInfo = &VulkanInfo;
VulkanCreateInfo.enabledExtensionCount = static_cast<uint32_t>(ExtensionList.size());
VulkanCreateInfo.ppEnabledExtensionNames = ExtensionList.data();
#ifdef NDEBUG
VulkanCreateInfo.enabledLayerCount = 0;
VulkanCreateInfo.pNext = nullptr;
#else
VkDebugUtilsMessengerCreateInfoEXT DebugInfo;
VulkanDebug.CreateDebugMessengerInfo(DebugInfo);
VkValidationFeaturesEXT ValidationFeatures{};
ValidationFeatures.sType = VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT;
ValidationFeatures.disabledValidationFeatureCount = 4;
ValidationFeatures.enabledValidationFeatureCount = 1;
ValidationFeatures.pEnabledValidationFeatures = enabled;
ValidationFeatures.pDisabledValidationFeatures = disabled;
ValidationFeatures.pNext = (VkDebugUtilsMessengerCreateInfoEXT*)&DebugInfo;
VulkanCreateInfo.enabledLayerCount = static_cast<unsigned int>(ValidationLayers.size());
VulkanCreateInfo.ppEnabledLayerNames = ValidationLayers.data();
VulkanCreateInfo.pNext = &ValidationFeatures;
The vk_layer_settings:
khronos_validation.debug_action = VK_DBG_LAYER_ACTION_LOG_MSG
khronos_validation.report_flags = error,warn,perf
khronos_validation.log_filename = stdout
khronos_validation.printf_buffer_size = 1024
khronos_validation.printf_verbose = true
This is the compute shader that I've attempting to log from and debug.
It's supposed to do bone animation, but been having some problems with it which lead me to find out about the shader logger.
#version 460
#extension GL_ARB_separate_shader_objects : enable
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_debug_printf : enable
#include "Lighting.glsl"
struct Vertex
{
vec3 Position;
float Padding1;
vec3 Normal;
float Padding2;
vec2 TexureCoord;
vec2 Padding3;
vec3 Tangant;
float Padding4;
vec3 BiTangant;
float Padding5;
vec4 Color;
ivec4 BoneID ;
vec4 BoneWeights;
};
layout(binding = 0, scalar) buffer Vertices
{
Vertex v[];
}
vertices;
layout(binding = 2) uniform UniformBufferObject {
mat4 viewInverse;
mat4 projInverse;
mat4 model;
mat4 view;
mat4 proj;
DirectionalLight dlight;
vec3 viewPos;
PointLight plight;
float vertexSize;
mat4 PVM;
mat4 BoneTransform[100];
float timer;
} ubo;
layout(binding = 5) buffer Transform { mat4 Transform; } MeshTransform[];
layout(push_constant) uniform MeshInfo
{
uint MeshID;
uint ModelID;
uint MaterialID;
} Mesh;
void main()
{
debugPrintfEXT("H");
Vertex v0 = vertices.v[gl_GlobalInvocationID.x];
mat4 BoneTransform = mat4(1.0f);
BoneTransform = ubo.BoneTransform[v0.BoneID[0]] * v0.BoneWeights[0];
BoneTransform += ubo.BoneTransform[v0.BoneID[1]] * v0.BoneWeights[1];
BoneTransform += ubo.BoneTransform[v0.BoneID[2]] * v0.BoneWeights[2];
BoneTransform += ubo.BoneTransform[v0.BoneID[3]] * v0.BoneWeights[3];
vec4 BonePosition = BoneTransform * vec4(v0.Position, 1.0);
v0.Position = vec3(ubo.model * MeshTransform[0].Transform * BonePosition);
v0.Normal = normalize(transpose(inverse(mat3(ubo.model * MeshTransform[0].Transform * BoneTransform))) * v0.Normal);
v0.Color = vec4(v0.BoneID.xyz, 1.0f);
vertices.v[gl_GlobalInvocationID.x] = v0;
}
Was there something I missed?
Also made a bare bones sample and still getting the same message:
Okay, I made use a basic vulkan templete using vulkantutorial and put in in the shader printf settings and still got the same error.
https://github.com/ThomasDHZ/VulkanShaderLoggerTest
https://vulkan-tutorial.com/Drawing_a_triangle/Swap_chain_recreation
You print for every compute shader invocation with a high level of verbosity. This is most probably to much text for the default buffer size of 1024.
So you need to lower the printf invocations in your shader and also want to lower the verbosity of the messages to e.g. only VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT instead of VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT.

glsl UNEXPECTED NEW_IDENTIFIER, expecting $end

I'm experimenting this frustrating error while compiling a fragment shader. If I try to compile the shader as it is :
#version 450 core
in vec2 tex_coord;
in flat int vertex_id;
out vec4 color;
layout (binding = 20) uniform sampler2D buildingTex;
layout (binding = 21) uniform sampler2D groundTex;
const int cube_vertices = 36;
const int ground_vertices = 4;
void main(void)
{
if(vertex_id < cube_vertices)
{
float scaleF = .5f;
vec2 scale = vec2(scaleF,scaleF);
color = texture(buildingTex,tex_coord*scale);
}
if(vertex_id >= cube_vertices)
{
color = texture(groundTex,tex_coord);
}
}
the compailer complains with : glsl UNEXPECTED NEW_IDENTIFIER, expecting $end.
However if I add to my fragment shader another fragment shader, that i had in another file , and then i comment all its rows like follows :
// #version 450 core
// in vec2 tex_coord;
// in flat int vertex_id;
// out vec4 color;
// layout (binding = 20) uniform sampler2D buildingTex;
// layout (binding = 21) uniform sampler2D groundTex;
// int cube_vertices;
// int ground_vertices;
// void main(void)
// {
// if(vertex_id < cube_vertices)
// {
// float scaleF = .5f;
// vec2 scale = vec2(scaleF,scaleF);
// color = texture(buildingTex,tex_coord*scale);
// //color = vec4(1.0,1.0,1.0,1.0);
// }
// if(vertex_id >= cube_vertices)
// {
// color = texture(groundTex,tex_coord);
// //color = vec4(1.0,0.0,0.0,1.0);
// }
#version 450 core
in vec2 tex_coord;
in flat int vertex_id;
out vec4 color;
layout (binding = 20) uniform sampler2D buildingTex;
layout (binding = 21) uniform sampler2D groundTex;
const int cube_vertices = 36;
const int ground_vertices = 4;
void main(void)
{
if(vertex_id < cube_vertices)
{
float scaleF = .5f;
vec2 scale = vec2(scaleF,scaleF);
color = texture(buildingTex,tex_coord*scale);
}
if(vertex_id >= cube_vertices)
{
color = texture(groundTex,tex_coord);
}
}
in this case the shader is compiled!
I tried to remove the commented line of the old code, and seems like I can remove just some of them, while others I cannot touch, to have the shader compiled.
At this moment the shader is :
// #version 450 core
// in vec2 tex_coord;
// in flat int vertex_id;
// out vec4 color;
// layout (binding = 20) uniform sampler2D buildingTex;
// layout (binding = 21) uniform sampler2D groundTex;
// int cube_vertices;
// if(vertex_id < cube_vertices)
#version 450 core
in vec2 tex_coord;
in flat int vertex_id;
out vec4 color;
layout (binding = 20) uniform sampler2D buildingTex;
layout (binding = 21) uniform sampler2D groundTex;
const int cube_vertices = 36;
const int ground_vertices = 4;
void main(void)
{
if(vertex_id < cube_vertices)
{
float scaleF = .5f;
vec2 scale = vec2(scaleF,scaleF);
color = texture(buildingTex,tex_coord*scale);
}
if(vertex_id >= cube_vertices)
{
color = texture(groundTex,tex_coord);
}
}
and seems I cannot remove anymore from the commented code.
Since this seems the most illogical problem I ever had in my life,and since the others questions having this same error warning, were solved in a way that doesn't match my case, I'm opening a new quest.
ps: I tried to make a completely new shader but it didn't work.
pps: the problem comes up after I changed the major-mode (through M-x and not modifying the init file of emacs!) of emacs in order to use the ordinary shortcut I use in cpp files, in the shaders files (.vert, .frag ...), however even restoring the default-mode and restarting emacs the shader does not compile!
EDIT: this is the method I use to load the shader
const char* Shader::loadShader(std::string shaderPath)
{
std::ifstream temp_ss(shaderPath,std::ifstream::in);
if(temp_ss)
{
char c;
char *ss = new char[shader_size];
unsigned i = 0 ;
while(temp_ss.get(c))
{
ss[i++] = c;
}
std::cout<<std::ends;
//ends adds a null, and then flushes the buffer
//if i don't use it , often, the window does not show anything
//I could use std::cout<<std::endl; in place of ends,
//but ends seem a more polite solution to me
//CHECK SHADER
for(int i = 0; i < shader_size; i++)
if(ss[i] == '\0')
std::cout<<"shader.cpp::ERROR: NULL CHARACTER FOUND ON SHADER "<<
shaderPath<<std::endl;
return ss;
}
else
std::clog<<"shader.cpp::loadShader() : ERROR ::: no shaders found at the path : "<<shaderPath<<std::endl;
}
From the OP:
TO SUMMARIZE : shader files MUST be null terminated , I modify the function that loads the shader source into the buffer like follows:
const char* Shader::loadShader(std::string shaderPath)
{
std::ifstream temp_ss(shaderPath,std::ifstream::in);
if(temp_ss)
{
char c;
char *ss = new char[shader_size];
unsigned i = 0 ;
while(temp_ss.get(c))
{
ss[i++] = c;
}
//CHECK SHADER
bool nullTerminated = false;
for(int i = 0; i < shader_size; i++)
if(ss[i] == '\0')
nullTerminated = true;
if(!nullTerminated)
ss[shader_size++] = '\0';
return ss;
}
else
std::clog<<"shader.cpp::loadShader() : ERROR ::: no shaders found at the path : "<<shaderPath<<std::endl;
}
and the fragment shader compiles!

How to update array of matrices to glsl shader

I'm currently working with skeletal animation and I'm really close to getting it working. Currently, I have a struct that has a matrix with 100 spots ( this is so that I can max have 100 joints ) like so :
struct skelShader {
glm::mat4 currentJointTrans[100];
};
The struct should be binded in the shader, I've done it like this:
glGenBuffers(1, &sksBuff);
glBindBuffer(GL_UNIFORM_BUFFER, sksBuff);
// bind buffer to work further with it...
// allocate memory for the buffer in the GPU
glBufferData(GL_UNIFORM_BUFFER, sizeof(skelShader), NULL, GL_STATIC_DRAW);
// because we hard-coded "binding=3" in the shader code we can do this:
// bind Uniform Buffer to binding point 3 (without caring about index of UBO)
glBindBufferBase(GL_UNIFORM_BUFFER, 4, sksBuff);
// good practice, unbind buffer
glBindBuffer(GL_UNIFORM_BUFFER, 0);
sksBuff is just an GLuint.
I fill this array with new values every render/frame that goes by, these values are the new transformations for the joints. I do it like this:
for (int i = 0; i < skeleton.size(); i++) {
globalSkelInfo.currentJointTrans[i] = skeleton[i]->transformMat[currentFrame - 1] * skeleton[i]->globalBindPosMat;
}
This is working correctly for the root joint, but the rest of the joints/mesh remains in bind pose. The problem should be located in where I update the array. Currently I do it like this in the render function after I've done the multiplication for each joint:
for (int i = 0; i < skeleton.size(); i++) {
glUniformMatrix4fv(glGetUniformLocation(aShaderProgram, ("currentJointTrans[" + std::to_string(i) + "]").c_str()),
1, GL_FALSE, glm::value_ptr(globalSkelInfo.currentJointTrans[i]));
}
After this I draw. The root joints values seem to be moving correctly, but the rest of the mesh is in bindpose and doesn't move. In the Vertex Shader I try to update the matrix like this:
#version 440
const int maxJoints = 100;
const int maxWeights = 4;
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec2 vertex_UV;
layout(location = 2) in vec3 vertex_normal;
layout(location = 3) in vec4 vertex_weight;
layout(location = 4) in ivec4 vertex_controllers;
out vec2 outUVs;
out vec3 outNorm;
layout(binding = 3 , std140) uniform uniformBlock
{
vec3 camPos;
mat4 world;
mat4 LookAt;
mat4 projection;
mat4 MVP;
};
layout(binding = 4 , std140) uniform animationStruct
{
mat4 currentJointTrans[maxJoints];
};
void main() {
vec4 finalModelPos = vec4(0.0);
vec4 finalNormal = vec4(0.0);
for (int i = 0; i < 4; i++) {
mat4 jointTrans = currentJointTrans[vertex_controllers[i]];
vec4 posePos = jointTrans * vec4(vertex_position, 1.0);
finalModelPos += posePos * vertex_weight[i];
vec4 worldNormal = jointTrans * vec4(vertex_normal, 0.0);
finalNormal += worldNormal * vertex_weight[i];
}
gl_Position = MVP * finalModelPos;
outNorm = finalNormal.xyz;
outUVs = vertex_UV;
}
My theory is that the updating of the struct skelShader with my currentJointTrans array is incorrect. Any tips on how I should do this instead?
glUniform* calls cannot set data in uniform buffers. Indeed, the whole point of uniform buffers is that the uniform data comes from a buffer object. That's why you had to create one.
So if you want to set the uniform data for a uniform block, you set that data into the buffer object.

memoryBarrier() behaving unexpectedly in Geometry Shader

I am trying to get a hold of how memoryBarrier() works in OpenGL 4.4
I tried the following once with a texture image and once with Shader Storage Buffer Object (SSBO).
The basic idea is to create an array of flags for however many objects that need to be rendered in my scene and then perform a simple test in the geometry shader.
For each primitive in GS, if at least one vertex passes the test, it
sets the corresponding flag in the array at the location specified
by this primitive's object ID (Object IDs are passed to GS as vertex
attributes).
I then perform a memoryBarrier() to make sure all threads have written their values.
Next, I have all primitives read from the flags array and only emit a vertex if the flag is set.
Here is some code from my shaders to explain:
// Vertex Shader:
#version 440
uniform mat4 model_view;
uniform mat4 projection;
layout(location = 0) in vec3 in_pos;
layout(location = 1) in vec3 in_color;
layout(location = 2) in int lineID;
out VS_GS_INTERFACE
{
vec4 position;
vec4 color;
int lineID;
} vs_out;
void main(void) {
vec4 pos = vec4(in_pos, 1.0);
vs_out.position = pos;
vs_out.color = vec4(in_colo, 1.0);
vs_out.lineID = lineID;
gl_Position = projection * model_view * pos;
}
and here is a simple Geometry shader in which I use only a simple test based on lineID ( I realize this test doesn't need a shared data structure but this is just to test program behavior)
#version 440
layout (lines) in;
layout (line_strip, max_vertices = 2) out;
layout (std430, binding = 0) buffer BO {
int IDs[];
};
in VS_GS_INTERFACE
{
vec4 position;
vec4 color;
int lineID;
} gs_in[];
out vec4 f_color;
void main()
{
if(gs_in[0].lineID < 500)
{
IDs[gs_in[0].lineID] = 1;
}
else
{
IDs[gs_in[0].lineID] = -1;
}
memoryBarrier();
// read back the flag value
int flag = IDs[gs_in[0].lineID];
if ( flag > 0)
{
int n;
for( n = 0; n < gl_in.length(), n++)
{
f_color = gs_in[n].color;
gl_Position = gl_in[n].gl_Position;
emitVertex();
}
}
}
No matter what value I put instead of 500, this code always renders only 2 objects. If I change the condition for rendering in the GS to if( flag > = 0) it seems to me that all objects are rendered which means the -1 is never written by the time these IDs are read back by the shader.
Can someone please explain why the writes are not coherently visible to all shader invocations despite the memoryBarrier() and what would be the most efficient work around to get this to work?
Thanks.