GameMaker Convert to vertex buffer (dynamically passing positional data to shader)

xDGameStudios

GameMaker Staff
GameMaker Dev.
I found this code in an asset in the marketstore (I'm using it here because I is free, and no credits are required so there should be no problem) I'm trying to learn something about shaders and vertexes... and primitives... and vertex buffers... so here is my question.

Code:
///ui_draw_sprite_panel(sprite,subimg,border,x,y,width,height)
var bordersize = argument2 / sprite_get_width(argument0);
var x1 = argument3;
var x2 = argument3 + argument2;
var x3 = argument3 + argument5 - argument2;
var x4 = argument3 + argument5;
var y1 = argument4;
var y2 = argument4 + argument2;
var y3 = argument4 + argument6 - argument2;
var y4 = argument4 + argument6;

draw_primitive_begin_texture(pr_trianglestrip, sprite_get_texture(argument0, argument1));
draw_vertex_texture(x1, y1, 0, 0);
draw_vertex_texture(x1, y2, 0, bordersize);
draw_vertex_texture(x2, y1, bordersize, 0);
draw_vertex_texture(x2, y2, bordersize, bordersize);
draw_vertex_texture(x3, y1, 1 - bordersize, 0);
draw_vertex_texture(x3, y2, 1 - bordersize, bordersize);
draw_vertex_texture(x4, y1, 1, 0);
draw_vertex_texture(x4, y2, 1, bordersize);
draw_vertex_texture(x4, y3, 1, 1 - bordersize);
draw_vertex_texture(x3, y2, 1 - bordersize, bordersize);
draw_vertex_texture(x3, y3, 1 - bordersize, 1 - bordersize);
draw_vertex_texture(x2, y2, bordersize, bordersize);
draw_vertex_texture(x2, y3, bordersize, 1 - bordersize);
draw_vertex_texture(x1, y2, 0, bordersize);
draw_vertex_texture(x1, y3, 0, 1 - bordersize);
draw_vertex_texture(x1, y4, 0, 1);
draw_vertex_texture(x2, y3, bordersize, 1 - bordersize);
draw_vertex_texture(x2, y4, bordersize, 1);
draw_vertex_texture(x3, y3, 1 - bordersize, 1 - bordersize);
draw_vertex_texture(x3, y4, 1 - bordersize, 1);
draw_vertex_texture(x4, y3, 1, 1 - bordersize);
draw_vertex_texture(x4, y4, 1, 1);
draw_primitive_end();
can all those vertex textures draw function calls be converted into a single vertex buffer to enhance performance? Am I talking non sense (in other words, I don't know what the hell I'm saying... xD).

Someone in another post suggested me to use vertex shader to create 9patch system... and I'm yet trying to grasp the concepts of shaders. I know I have to create a buffer and even freeze it if I want and then use it to upon a texture to create the 9patch panel... but that's it... i don't know what to do :(

I think there are very few shader tutorials for GML out there :/ something simple like...
"take a square sprite and distorted into for example a trapezoid"... just to people learn how to move texture vertexes around.

Well returning to my question... how could this be done?! I wanted to make it so I could even select a different size border for each side.. (top, bottom, left, right).
 
R

renex

Guest
if you convert this to a vbuffer then you probably dont want to do this every frame as that'd be just as slow as what you already have there in legacy code.

so you'd have to build these at some point and reuse them, which might not be worth the effort. or use a shader to deform a basic mesh dynamically, like i outlined in this other thread.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
if you convert this to a vbuffer then you probably dont want to do this every frame as that'd be just as slow as what you already have there in legacy code.

so you'd have to build these at some point and reuse them, which might not be worth the effort. or use a shader to deform a basic mesh dynamically, like i outlined in this other thread.
@renex
To use a shader to deform the mesh dynamically don't I have to pass the vertex information to the shader?! I thought that was done by creating a vertex buffer... no?! That's why I asked here how could I do it dynamically? Because I need to tell the shader the top/left/right/bottom margin values and the width and the height of the deformation.

@Fel666
It isn't worth it?! I just asked because as I said I'm using a local/world matrix system to position objects on the screen... and I was trying to draw each window just once (to a surface), refreshing only when resizing and then just draw the surface... making less calls to the draw functions...
So you're saying that it is better to use draw_sprite_part_ext.... then using vertex buffer or shaders at all!?

I'm not having performance issues... it was something that was suggested to me by @renex ..but as the project I'm working on is quite ambitious.... I really didn't want to have issue in the future!
 

GMWolf

aka fel666
So you're saying that it is better to use draw_sprite_part_ext.... then using vertex buffer or shaders at all!
Well, the performance benefit won't be amazing...
Using a surface will use up more valuable video RAM.

Using the vertex buffer will speed things up a bit, but again, I doubt the performance improvement will be of much use.
I think vertex buffers truly shine once you get a lot of vertices, at which point rebuilding them becomes too expensive.

Remember that premature optimization is the root of all evil!
I would hold off for now, and only implement it later in the game if you need to squeeze out a little more CPU power. (This really won't be affecting GPU much. Vertex processing is fast!)
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
Well, the performance benefit won't be amazing...
Using a surface will use up more valuable video RAM.

Using the vertex buffer will speed things up a bit, but again, I doubt the performance improvement will be of much use.
I think vertex buffers truly shine once you get a lot of vertices, at which point rebuilding them becomes too expensive.

Remember that premature optimization is the root of all evil!
I would hold off for now, and only implement it later in the game if you need to squeeze out a little more CPU power. (This really won't be affecting GPU much. Vertex processing is fast!)
So do you think it is better to use the method I posted with the draw_vertex_texture?! or should I use the draw_sprite_part ?!
 

GMWolf

aka fel666
Presumably, you would need to call draw_sprite_part multiple times, no?
every time you call draw_sprite_part, thats a new daw call.
whilst with the vertices, you are only using one draw call.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
Presumably, you would need to call draw_sprite_part multiple times, no?
every time you call draw_sprite_part, thats a new daw call.
whilst with the vertices, you are only using one draw call.
So everything between draw_primitive_begin() and draw_primitive_end() is executed in one draw call is that right?
 

GMWolf

aka fel666
So everything between draw_primitive_begin() and draw_primitive_end() is executed in one draw call is that right?
yes. That is right,
What it does is build a vertex buffer and submits it.
This is why if the panel keeps changing size, it will not really be worth building a vertex buffer yourself.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
yes. That is right,
What it does is build a vertex buffer and submits it.
This is why if the panel keeps changing size, it will not really be worth building a vertex buffer yourself.
Can't I send width and height variables to a custom shader... create a vertex buffer.. and compute the positions (using passed width and height, inside the shader?) My problem is that I don't know how to send variables to shaders and how to use them over there xD
And I don't have any tutorial.... is there any tutorial or vertex shader I can learn from?!
 

GMWolf

aka fel666
You would probably need to use a custom vertex format to store strech factors, and use uniforms to pass to the shader.
Google glsl uniforms for more info.
Then lookup shader uniforms in the gamemaker manual.

But all this is a decent ammount of work. What is wrong witht the solution outlined above?
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
You would probably need to use a custom vertex format to store strech factors, and use uniforms to pass to the shader.
Google glsl uniforms for more info.
Then lookup shader uniforms in the gamemaker manual.

But all this is a decent ammount of work. What is wrong witht the solution outlined above?
The solution from the OP?! well i like it and I'm building on it.. ;) but this way I could learn to create shaders (something I don't know nothing about)..
well I get that I have to use uniforms but then I don't know how i can add the positioning information inside the shader.... I'll have the stretch info... but where do I add the positional info?! that is my problem :)

(One last time "I don't dislike the original post solution", it's just a self improving adventure!
 

GMWolf

aka fel666
If you want to learn shaders, this isnt the best thing to start with. Its a little complicated as you need both a custom vertrx format and uniforms.
 
Top