From 9bb1a22ea5b76b6e2b5e090524015beb8b38805f Mon Sep 17 00:00:00 2001 From: justin Date: Wed, 12 Apr 2023 23:47:52 +0100 Subject: [PATCH] Change VBO to store vertex size in bytes --- src/Shader/Bind.hs | 4 ++-- src/Shader/Compile.hs | 32 +++++++++++++++++++++++++++----- src/Shader/Data.hs | 2 +- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Shader/Bind.hs b/src/Shader/Bind.hs index eeae82a81..414d47d42 100644 --- a/src/Shader/Bind.hs +++ b/src/Shader/Bind.hs @@ -21,7 +21,7 @@ bufferPokedVBO theVBO numVs = glNamedBufferSubData (_vboName theVBO) 0 - (fromIntegral $ floatSize * numVs * _vboVertexSize theVBO) + (fromIntegral $ numVs * _vboVertexBytes theVBO) (_vboPtr theVBO) bufferEBO :: EBO -> Int -> IO () @@ -37,7 +37,7 @@ bufferShaderLayers shads counts = MV.imapM_ f shads where f i shad = do let theVBO = snd shad - stride = _vboVertexSize theVBO + stride = _vboVertexBytes theVBO `div` floatSize VFSM.mapM_ (g stride theVBO) $ VFSM.enumFromStepN 0 1 6 -- [0..5] where g stride theVBO lay = do diff --git a/src/Shader/Compile.hs b/src/Shader/Compile.hs index a0f031738..b31ccd29f 100644 --- a/src/Shader/Compile.hs +++ b/src/Shader/Compile.hs @@ -97,7 +97,25 @@ setupVBO vertexsize = do (fromIntegral $ floatSize * numDrawableElements * vertexsize) nullPtr GL_STREAM_DRAW - return VBO{_vboName = vboname, _vboPtr = thePtr, _vboVertexSize = vertexsize} + return VBO{_vboName = vboname, _vboPtr = thePtr, _vboVertexBytes = floatSize * vertexsize} + +---- the input ptr is assumed to contain the correct amount of data according to +---- the specified number and type of vertices +---- note the VBO here does not have a sensible ptr value +--setupStaticVBO :: Int -> [VertexAttribute] -> Ptr a -> IO (VBO,VAO) +--setupStaticVBO numvs vas vdata = do +-- vboname <- mglCreate glCreateBuffers +-- glNamedBufferStorage +-- vboname +-- (CPtrdiff (fromIntegral (vasTightStride vas))) +-- vdata +-- 0 +-- let vbo = VBO +-- { _vboName = vboname +-- , _vboPtr = nullPtr +-- , _vboVertexSize = + + setupVBOStatic :: Int -> IO VBO setupVBOStatic vertexsize = do @@ -109,7 +127,7 @@ setupVBOStatic vertexsize = do (fromIntegral $ floatSize * numDrawableElements * vertexsize) nullPtr GL_STATIC_DRAW - return VBO{_vboName = vboname, _vboPtr = thePtr, _vboVertexSize = vertexsize} + return VBO{_vboName = vboname, _vboPtr = thePtr, _vboVertexBytes = floatSize * vertexsize} makeByteStringShaderUsingVAO :: -- | (Arbitrary) name of the shader @@ -203,11 +221,11 @@ setupVAOvbo sizes strd vbo = do setupVAOUsingVBO :: [VertexAttribute] -> VBO -> IO VAO setupVAOUsingVBO vas vbo = do - let strd = vbo ^. vboVertexSize + let strd = vbo ^. vboVertexBytes vaoname <- mglCreate glCreateVertexArrays glBindVertexArray vaoname setupVertexAttribs (vbo ^. vboName) vaoname vas - (fromIntegral strd * fromIntegral floatSize) + (fromIntegral strd) return VAO{_vaoName = vaoname} setupEBO :: VAO -> IO EBO @@ -227,8 +245,12 @@ setupVBOVAO sizes = do vaoname <- mglCreate glCreateVertexArrays glBindVertexArray vaoname vbo <- setupVBO (sum sizes) - vao <- setupVAOvbo (toFloatVAs sizes) (sum sizes) (vbo ^. vboName) + --vao <- setupVAOvbo (toFloatVAs sizes) (sum sizes) (vbo ^. vboName) + vao <- setupVAOUsingVBO (toFloatVAs sizes) vbo return (vao,vbo) + where + vas = toFloatVAs sizes + strd = vasTightStride vas toFloatVAs :: [Int] -> [VertexAttribute] toFloatVAs = go 0 diff --git a/src/Shader/Data.hs b/src/Shader/Data.hs index 72b85a8d9..27366698d 100644 --- a/src/Shader/Data.hs +++ b/src/Shader/Data.hs @@ -81,7 +81,7 @@ Vertex attributes are interleaved within the vbo. data VBO = VBO { _vboName :: GLuint , _vboPtr :: Ptr Float - , _vboVertexSize :: Int + , _vboVertexBytes :: Int -- add int for AMOUNT of data poked! }