AGAL HELP: in 2D texture, 2D rendering, how to use agal to comput vertex by x,y offset from program constant

StackOverflow https://stackoverflow.com/questions/13509063

  •  01-12-2021
  •  | 
  •  

Question

I hope someone could shred some light on my agal question:

I have a 64x64 fixed size texture. The uv and vertex are fixed as well.

Now I need to move it in a 2D stage.

The current way I’m using is to compute and upload a new vertex every time drawing triangles from this texture.

I’ve found VertexBuffer3D.uploadFromByteArray takes significant time to process.

I believe there must be smarter ways to do this in agal.

Can I pass the x, y offset in constant registers for Vertex Shade, and compute the vertex size in agal?

If this is possible, how can I do it?

Was it helpful?

Solution

Upload your geometry once and then multiply each vertex by your Model-View-Perspective matrix in the vertex shader. The matrix can be passed to the vertex shader in the constant register (beware that one 4x4 matrix takes 4 constant registers). Here`s a nice example of this.

OTHER TIPS

If MVP matrix is insufficient and you need to manipulate individual vertex positions, you can use constant registers indexing. But beware that the number of vertices that can be processed this way is limited as there is a limit on the number of constant registers available for a vertex shader (max. 128 constants including your MVP matrix).

  1. When building your geometry, assign each vertex a unique id and upload it to the vertex data. The smallest id should equal the index of the first constant that is used to store offsets. Let's assume that this smallest id is n0 and that ids are stored in the x component of va1 register. Also, as AGAL have no equivalent of PixelBender's loadFloat opcode, it would be good to set some initial position for each vertex. Let it be stored in va0.
  2. Each time the scene is rendered, set constants n0...(n0 + m - 1), where m is the number of vertices. You can store both x and y displacements of a single vertex inside a constant using Float2 data type. Using Float4 data type, you can store two additional numbers for each vertex (it may be color/texture offset or smth else). It is also possible to store x-y offsets of two consequent vertices in a single constant using slightly more complex shader.
  3. In AGAL, you can index constant registers of a vertex shader (and only them, at least for now). Let's use this feature in our shader to obtain the data for particular vertex:

    mov vt1 vc[va1.x] // obtain the offset from constants using vertex id
    add vt1 vt1 va0   // add the offset to the initial position of the vertex
    m44 op vt1 vc0    // multiply with MVP (assuming that it is stored in vc0...vc3)
    

This approach is suitable for several specific cases, but it is not usable in general case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top