Page 1 of 1

Can we store 3d vertices in GPU memory?

Posted: Sat Nov 09, 2019 7:36 pm
by kicknbritt
So I'm working with GroverBurgers SS3D library (Thanks a ton dude.) and I'm working on a voxel game where I create meshes made up of 24x24x24 blocks.
The problem Im having is I have to store all those vertices in tables, and that takes up around 500-800 mb for only about ‭216‬ chunks!
Is there any way I can store vertices in a more efficient way? This seems like way too much memory usage.

This is what I use to draw models

Code: Select all

        
        -- go through all models in modelList and draw them
        for i=1, #self.modelList do
            local model = self.modelList[i]
            if model ~= nil and model.visible and #model.verts > 0 then
                self.threeShader:send("model_matrix", model.transform)
                self.threeShader:send("model_matrix_inverse", TransposeMatrix(InvertMatrix(model.transform)))

                love.graphics.setWireframe(model.wireframe)
                if model.culling then
                    love.graphics.setMeshCullMode("back")
                end

                love.graphics.draw(model.mesh, -self.renderWidth/2, -self.renderHeight/2)

                love.graphics.setMeshCullMode("none")
                love.graphics.setWireframe(false)
            end
        end
And this is the "self.threeShader" called above

Code: Select all

	-- define the shaders used in rendering the scene
    scene.threeShader = love.graphics.newShader[[
        uniform mat4 view;
        uniform mat4 model_matrix;
        uniform mat4 model_matrix_inverse;
        uniform float ambientLight;
        uniform vec3 ambientVector;

        varying mat4 modelView;
        varying mat4 modelViewProjection;
        varying vec3 normal;
        varying vec3 vposition;

        #ifdef VERTEX
            attribute vec4 VertexNormal;

            vec4 position(mat4 transform_projection, vec4 vertex_position) {
                modelView = view * model_matrix;
                modelViewProjection = view * model_matrix * transform_projection;

                normal = vec3(model_matrix_inverse * vec4(VertexNormal));
                vposition = vec3(model_matrix * vertex_position);

                return view * model_matrix * vertex_position;
            }
        #endif

        #ifdef PIXEL
            vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
                vec4 texturecolor = Texel(texture, texture_coords);

                // if the alpha here is zero just don't draw anything here
                // otherwise alpha values of zero will render as black pixels
                if (texturecolor.a == 0.0)
                {
                    discard;
                }

                float light = max(dot(normalize(ambientVector), normal), 0);
                texturecolor.rgb *= max(light, ambientLight);

                return color*texturecolor;
            }
        #endif
    ]]

Re: Can we store 3d vertices in GPU memory?

Posted: Sat Nov 09, 2019 7:48 pm
by raidho36
Firstly a note, unless you use those as pixels in 3d, those are not voxels, just 3d blocks - Minecraft is not voxel.

Do you store your voxels as 3d cubes? If you do, you can only store geometry that's visible from the outside, which will cut down massively on amount of vertices in a typical object. Additionally, you can use volumetric compression such as octree representation, which reduces the amount of memory needed by simply storing less data. If you store them as textures, you can use texture compression, for example by using 8 bit palette color, or by generating hardware-supported compressed textures.

Re: Can we store 3d vertices in GPU memory?

Posted: Sun Nov 10, 2019 11:03 am
by kicknbritt
Yes I am culling non visible faces/vertices.
The data structure I use to store the "Blocks" in data is only storing 2 bytes of information per block so 27,648‬ bytes a chunk, and since lua strings are stored by reference that is little to nothing.
My problem is each block face is generating 2 triangles, 6 vertices so 36 total vertices.
but that is 36 Nested tables, and with many blocks the memory quickly becomes infeasible even with Culling and Greedy Meshing.
I guess I just want to know if there is any way to store the vertices for love.graphics.newMesh more efficiently?
Because lua tables are not working very well for me.

See how thousands and thousands of these could add up?

Code: Select all

VertList[#VertList+1] = {x+VertSize,y+VertSize,z+VertSize, BlockTypeTable.TopUV[1],BlockTypeTable.TopUV[2],BlockLight,BlockLight,BlockLight}

Re: Can we store 3d vertices in GPU memory?

Posted: Sun Nov 10, 2019 11:32 am
by raidho36
You can create blank mesh and populate it with plain values. Anyway if you actually intend to use voxels, which implies absolutely massive amount of them, I suggest you store them as textures and use volumetic texture rendering, e.g. cube marching.

Re: Can we store 3d vertices in GPU memory?

Posted: Sun Nov 10, 2019 11:32 am
by pgimeno
kicknbritt wrote:
Sun Nov 10, 2019 11:03 am
My problem is each block face is generating 2 triangles, 6 vertices so 36 total vertices.
Using a vertex list should reduce that to 8, adding the overhead of the list of course which is 36 single numbers. If you manage to share them between blocks, the 8 would be reduced even further, to 1 per block plus the ones in the boundaries. For 24x24x24 blocks, that's 15625 vertices and a list of 497,664 numbers if my math is correct.

Anyway, to answer your question in the OP, as far as I know the mesh object is stored more efficiently than the Lua code used to generate it. I'm not sure about where it's stored; it may depend on the SpriteBatchUsage you give to the mesh.

Re: Can we store 3d vertices in GPU memory?

Posted: Sun Nov 10, 2019 12:07 pm
by kicknbritt
Okay. Well thanks for all the feedback. Im going to try to check out vertex lists and the "static" mode on SpriteBatchUsage looks promising.

Re: Can we store 3d vertices in GPU memory?

Posted: Sun Nov 10, 2019 5:34 pm
by pgimeno
Note I meant Mesh:setVertexMap. But now that I think about it, you probably can't use that, since the UVs need to be different depending on the face the vertex belongs to.

Re: Can we store 3d vertices in GPU memory?

Posted: Sun Nov 10, 2019 6:43 pm
by kicknbritt
Oh rip. Well thanks anyway
[Edit] Actually it looks like it supports textures. Ill check it out