Sign in to follow this  
Martin Perry

OpenGL Transform Feedback empty on iOS

Recommended Posts

I have used transform feedback for particle system update in my Win32 app. All is working correctly. I have transported the app to OpenGL ES 3.0 on iOS. However, when I run my app, no data are shown. If I look at content of vertex buffer, I can see positions (OUTPUT.xy) to be numbers like 1e+38 etc. Tha rand function return numbers in [0,1], so there is no reason why x and y should end in such high values (run time of app is aprox 5s, before I pause it and examine buffers).

Also, I dotn see the reason for app to fullfill the shader condition for RebornParticle, since the initalized z is always 0 and w is always > 0. It seems to me, that input values to transform feedback are not inited, but on the other hand, on Win32 all is OK and rendering  buffers without transform feedback gives correct initial result.

If I dont run transform feedback and just render initialized buffers, I can see correct random points on the screen at their initial positions.

 

Here is my code (only relevant parts)

typedef struct WindParticle
{
   float x;
   float y;
   float t;
   float maxT;
} WindParticle;

Initialization:

    auto mt = std::mt19937(rd());
    auto dist01 = std::uniform_real_distribution<float>(0.0f, 1.0f);

    std::vector<WindParticle> particles;
    for (int i = 0; i < particlesCount; i++)
    {
        WindParticle wp;
        wp.x = dist01(mt); 
        wp.y = dist01(mt);
        wp.t = 0;
        wp.maxT = 5 * dist01(mt);
        
        particles.push_back(wp);        
    }

    GL_CHECK(glGenTransformFeedbacks(2, transformFeedback));
    GL_CHECK(glGenBuffers(2, vertexBuffer));

    for (int i = 0; i < 2; i++)
    {
        GL_CHECK(glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, transformFeedback[i]));
        GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[i]));
        GL_CHECK(glBufferData(GL_ARRAY_BUFFER, sizeof(WindParticle) * particles.size(), particles.data(), GL_DYNAMIC_DRAW));
        GL_CHECK(glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vertexBuffer[i]));
    }


    //init transform feedback vao
    G_Effect * ef = MyGraphics::G_ShadersSingletonFactory::Instance()->GetEffect("particle_position_update");
    int posId = ef->attributes["PARTICLE_DATA"][0]->location;

    GL_CHECK(glGenVertexArrays(2, this->transformFeedbackVao));

    for (int i = 0; i < 2; i++)
    {
        GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[i]));
        GL_CHECK(glBindVertexArray(this->transformFeedbackVao[i]));

        GL_CHECK(glEnableVertexAttribArray(posId));
        GL_CHECK(glVertexAttribPointer(posId, 4, GL_FLOAT, GL_FALSE, sizeof(WindParticle), 0)); // x, y, time, maxTime
    }
    
    //init render vao
    ef = MyGraphics::G_ShadersSingletonFactory::Instance()->GetEffect("particle_position_render");
    posId = ef->attributes["PARTICLE_DATA"][0]->location;

    GL_CHECK(glGenVertexArrays(2, this->renderVao));

    for (int i = 0; i < 2; i++)
    {        
        GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[i]));
        GL_CHECK(glBindVertexArray(this->renderVao[i]));

        GL_CHECK(glEnableVertexAttribArray(posId));
        GL_CHECK(glVertexAttribPointer(posId, 2, GL_FLOAT, GL_FALSE, sizeof(WindParticle), (const GLvoid*)0));  // x, y        
    }

    GL_CHECK(glBindVertexArray(0));
    GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));

    //init default id
    currVB = 0;
    currTFB = 1;

Update:

    G_Effect * ef = MyGraphics::G_ShadersSingletonFactory::Instance()->GetEffect("particle_position_update");
    ef->SetFloat("rndSeed", dist01(mt));

    ef->Start();
    GL_CHECK(glEnable(GL_RASTERIZER_DISCARD));

    GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[currVB]));
    GL_CHECK(glBindVertexArray(this->transformFeedbackVao[currVB]));

    GL_CHECK(glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, transformFeedback[currTFB]));            
    GL_CHECK(glBeginTransformFeedback(GL_POINTS));
    GL_CHECK(glDrawArrays(GL_POINTS, 0, this->particlesCount));    
    GL_CHECK(glEndTransformFeedback());
    GL_CHECK(glBindVertexArray(0));

    GL_CHECK(glDisable(GL_RASTERIZER_DISCARD));

    ef->End();

Render:

    G_Effect * ef = MyGraphics::G_ShadersSingletonFactory::Instance()->GetEffect("particle_position_render");    
    
    ef->Start();
    GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer[currTFB]));    
    GL_CHECK(glBindVertexArray(this->renderVao[currTFB]));
   
    GL_CHECK(glDrawArrays(GL_POINTS, 0, this->particlesCount));

    GL_CHECK(glBindVertexArray(0));

    ef->End();

    currVB = currTFB;
    currTFB = (currTFB + 1) & 0x1;

And in shader, I have created juat a dummy update:

#version 300 es //for iOS ... in Win32, there is #vesrion 330
in vec4 PARTICLE_DATA;
out vec4 OUTPUT;

uniform float rndSeed;

float rand(vec2 co)
{
    float a = 12.9898;
    float b = 78.233;
    float c = 43758.5453;
    float dt = dot(co.xy, vec2(a, b));
    float sn = mod(dt, 3.14);
    return fract(sin(sn) * c);
}

vec4 RebornParticle(vec4 old)
{
    vec4 p = vec4(0.0);
    p.x = rand(old.xy * rndSeed);
    p.y = rand(old.yx * rndSeed);
    
    p.z = 0.0;
    p.w = old.w;
    return p;
}


void main()
{    
    vec4 p = PARTICLE_DATA;
    
    p.x += 1.0;
    if (p.z > p.w) 
    {
        OUTPUT = RebornParticle(p);
        return;
    }
    
    
    OUTPUT = p;
}

Edited by Martin Perry

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this