1
\$\begingroup\$

I use PyOpengl and Python 3.

I have 50 thousand vertices. Position of each vertex could be calculated in vertex shader as

#version 300 es

uniform v_coefficients_weights[COEFFICIENTS_AMOUNT];
in vec3 v_coefficients[COEFFICIENTS_AMOUNT];
in vec3 v_initial_position;
out vec3 v_position;

void main() {
    v_position = v_initial_position + v_coefficients * coefficients_weights;
}

Amount of coefficients varies between 0 and 199 — it's not a problem for me to generate 200 vertex shaders to cover each situation. I need to change position of vertices tens of thousands times per application calls as fast as it's possible, so I cannot calculate them once on start.

Though, I cannot provide arrays for each vertex because documentation says:

Attribute variables cannot be declared as arrays or structures.

I see following solutions of this issue:

  • (will not work) hardcode suffices of variables names to emulate arrays:
    • use vectors with names like coefficients_000, coefficients_001, ..., coefficients_199;
    • use matrices to have 4 vectors in each of variable — will it be better than previous solution with vectors (maybe performance of matrices product is faster than 4 products of vectors)?
  • calculate vertices positions by myself using C/NumPy;
  • calculate vertices positions by myself in parallel using OpenCL;
  • (will not work) store all coefficients in uniform int v_coefficients[COEFFICIENTS_AMOUNT*VERTICES_AMOUNT] and access needed ones according to vertex number, which will be stored in in int vertex_id;
  • store coefficients in textures (proposed by @HolyBlackCat).

Are there any other solutions of my issue? Is there optimal solution in proposed above? Will I go out of memory if I will use 200 float 3-vectors for each vertex?

I imagine the solution with hardcoded indices as:

#version 300 es

uniform v_coefficients_weights[COEFFICIENTS_AMOUNT];

in vec3 v_coefficients_000;
in vec3 v_coefficients_001;
...
in vec3 v_coefficients_199;

in vec3 v_initial_position;
out vec3 v_position;

void main() {
    v_position = v_initial_position
        + v_coefficients[0] * coefficients_weights_000
        + v_coefficients[1] * coefficients_weights_001
        + ...
        + v_coefficients[199] * coefficients_weights_199;
}
\$\endgroup\$
  • \$\begingroup\$ What exactly you're trying to achieve with that code? How often the coefficients change? If coefficients don't change often, I'd precompute all vertex positions on the CPU. \$\endgroup\$ – HolyBlackCat Jul 30 '16 at 12:14
  • \$\begingroup\$ @HolyBlackCat coefficients are being changed tens of thousands times per application call and I try to do this as fast as it's possible. Now I'm calculating this using C functions and it takes 40 minutes, which is too slow for me. Thanks for help in specifying my question! \$\endgroup\$ – Charlie Jul 30 '16 at 12:20
  • 2
    \$\begingroup\$ Ok, more questions. Are coefficients different for each vertex? Also, why exactly you need those computations? (What you're trying to achieve?) \$\endgroup\$ – HolyBlackCat Jul 30 '16 at 12:30
  • 1
    \$\begingroup\$ @HolyBlackCat Yes, I have only uniform coefficients_weights array. Other coefficients are different for each vertex. I use a model of face faces.cs.unibas.ch/bfm/?nav=1-0&id=basel_face_model. Uniform coefficients weights are parameters of the model, while coefficients themselves are constant numbers. I try to find best fitting parameters of model for given image using gradient descent algorithm. \$\endgroup\$ – Charlie Jul 30 '16 at 12:58
  • 1
    \$\begingroup\$ What does this have to do with game development and why would a game developer answer this better than an other type of programmer? \$\endgroup\$ – user5665 Jul 31 '16 at 22:06
0
\$\begingroup\$

The best solutions appeared to be

  • Store coefficients in texture (proposed by @HolyBlackCat). OpenGL allows to save array of floats there.
  • Calculate all needed values before passing to GPU. I've faced a performance issue when stored array in texture.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.