示例#1
2
	//[-------------------------------------------------------]
	//[ Public methods                                        ]
	//[-------------------------------------------------------]
	TextureBufferDsa::TextureBufferDsa(OpenGLRenderer &openGLRenderer, uint32_t numberOfBytes, Renderer::TextureFormat::Enum textureFormat, const void *data, Renderer::BufferUsage bufferUsage) :
		TextureBuffer(openGLRenderer)
	{
		if (openGLRenderer.getExtensions().isGL_ARB_direct_state_access())
		{
			{ // Buffer part
				// Create the OpenGL texture buffer
				glCreateBuffers(1, &mOpenGLTextureBuffer);

				// Upload the data
				// -> Usage: These constants directly map to "GL_ARB_vertex_buffer_object" and OpenGL ES 2 constants, do not change them
				glNamedBufferData(mOpenGLTextureBuffer, static_cast<GLsizeiptr>(numberOfBytes), data, static_cast<GLenum>(bufferUsage));
			}

			{ // Texture part
				// Create the OpenGL texture instance
				glCreateTextures(GL_TEXTURE_BUFFER_ARB, 1, &mOpenGLTexture);

				// Attach the storage for the buffer object to the buffer texture
				glTextureBuffer(mOpenGLTexture, Mapping::getOpenGLInternalFormat(textureFormat), mOpenGLTextureBuffer);
			}
		}
		else
		{
			// Create the OpenGL texture buffer
			glGenBuffersARB(1, &mOpenGLTextureBuffer);

			// Create the OpenGL texture instance
			glGenTextures(1, &mOpenGLTexture);

			// Buffer part
			// -> Upload the data
			// -> Usage: These constants directly map to "GL_ARB_vertex_buffer_object" and OpenGL ES 2 constants, do not change them
			glNamedBufferDataEXT(mOpenGLTextureBuffer, static_cast<GLsizeiptr>(numberOfBytes), data, static_cast<GLenum>(bufferUsage));

			{ // Texture part
				#ifndef OPENGLRENDERER_NO_STATE_CLEANUP
					// Backup the currently bound OpenGL texture
					GLint openGLTextureBackup = 0;
					glGetIntegerv(GL_TEXTURE_BINDING_BUFFER_ARB, &openGLTextureBackup);
				#endif

				// Make this OpenGL texture instance to the currently used one
				glBindTexture(GL_TEXTURE_BUFFER_ARB, mOpenGLTexture);

				// Attaches the storage for the buffer object to the active buffer texture
				// -> Sadly, there's no direct state access (DSA) function defined for this in "GL_EXT_direct_state_access"
				glTexBufferARB(GL_TEXTURE_BUFFER_ARB, Mapping::getOpenGLInternalFormat(textureFormat), mOpenGLTextureBuffer);

				#ifndef OPENGLRENDERER_NO_STATE_CLEANUP
					// Be polite and restore the previous bound OpenGL texture
					glBindTexture(GL_TEXTURE_BUFFER_ARB, static_cast<GLuint>(openGLTextureBackup));
				#endif
			}
		}
	}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::initResourcesGrid()
{
	//
	// Grid floor
	//
	glGenBuffers(1, &s_vboGrid);
	vec3f *data = new vec3f[GRIDDEF * 4];
	vec3f *p = data;
	int j = 0;
	for (int i = 0; i<GRIDDEF; i++)
	{
		*(p++) = vec3f(-GRIDSZ, 0.0, GRIDSZ*(-1.0f + 2.0f*(float)i / (float)GRIDDEF));
		*(p++) = vec3f(GRIDSZ*(1.0f - 2.0f / (float)GRIDDEF), 0.0, GRIDSZ*(-1.0f + 2.0f*(float)i / (float)GRIDDEF));
		*(p++) = vec3f(GRIDSZ*(-1.0f + 2.0f*(float)i / (float)GRIDDEF), 0.0, -GRIDSZ);
		*(p++) = vec3f(GRIDSZ*(-1.0f + 2.0f*(float)i / (float)GRIDDEF), 0.0, GRIDSZ*(1.0f - 2.0f / (float)GRIDDEF));
	}
	s_vboGridSz = sizeof(vec3f)*GRIDDEF * 4;
	glNamedBufferDataEXT(s_vboGrid, s_vboGridSz, data[0].vec_array, GL_STATIC_DRAW);
	delete[] data;
	//
	// Target Cross
	//
	glGenBuffers(1, &s_vboCross);
	vec3f crossVtx[6] = {
		vec3f(-CROSSSZ, 0.0f, 0.0f), vec3f(CROSSSZ, 0.0f, 0.0f),
		vec3f(0.0f, -CROSSSZ, 0.0f), vec3f(0.0f, CROSSSZ, 0.0f),
		vec3f(0.0f, 0.0f, -CROSSSZ), vec3f(0.0f, 0.0f, CROSSSZ),
	};
	s_vboCrossSz = sizeof(vec3f)* 6;
	glNamedBufferDataEXT(s_vboCross, s_vboCrossSz, crossVtx[0].vec_array, GL_STATIC_DRAW);
	return true;
}
bool initArrayBuffer()
{
	glGenBuffers(buffer::MAX, BufferName);
    glNamedBufferDataEXT(BufferName[buffer::ELEMENT], ElementSize, ElementData, GL_STATIC_DRAW);
    glNamedBufferDataEXT(BufferName[buffer::VERTEX], VertexSize, VertexData, GL_STATIC_DRAW);

	return glf::checkError("initArrayBuffer");;
}
bool initBuffer()
{
	glGenBuffers(buffer::MAX, BufferName);

	glNamedBufferDataEXT(BufferName[buffer::VERTEX], VertexSize, VertexData, GL_STATIC_DRAW);
	glNamedBufferDataEXT(BufferName[buffer::TRANSFORM], sizeof(glm::mat4), NULL, GL_DYNAMIC_DRAW);
	glNamedBufferDataEXT(BufferName[buffer::BLIT], sizeof(glm::mat4), NULL, GL_DYNAMIC_DRAW);

	return true;
}
	bool initBuffer()
	{
		GLint UniformBufferOffset(0);
		glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UniformBufferOffset);
		UniformBlockSize = glm::max(GLint(sizeof(glm::mat4)), UniformBufferOffset);

		glGenBuffers(buffer::MAX, &BufferName[0]);
		glNamedBufferDataEXT(BufferName[buffer::ELEMENT], ElementSize, ElementData, GL_STATIC_DRAW);
		glNamedBufferDataEXT(BufferName[buffer::VERTEX], VertexSize, VertexData, GL_STATIC_DRAW);
		glNamedBufferDataEXT(BufferName[buffer::TRANSFORM], UniformBlockSize, nullptr, GL_DYNAMIC_DRAW);

		return true;
	}
示例#6
0
	OGLGraphicsBuffer::OGLGraphicsBuffer(BufferUsage usage, uint32_t access_hint, GLenum target, ElementInitData const * init_data)
			: GraphicsBuffer(usage, access_hint),
				target_(target)
	{
		BOOST_ASSERT((GL_ARRAY_BUFFER == target) || (GL_ELEMENT_ARRAY_BUFFER == target));

		glGenBuffers(1, &vb_);

		if (init_data != nullptr)
		{
			size_in_byte_ = init_data->row_pitch;

			if (glloader_GL_EXT_direct_state_access())
			{
				glNamedBufferDataEXT(vb_, static_cast<GLsizeiptr>(size_in_byte_), init_data->data,
						(BU_Static == usage_) ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW);
			}
			else
			{
				OGLRenderEngine& re = *checked_cast<OGLRenderEngine*>(&Context::Instance().RenderFactoryInstance().RenderEngineInstance());
				re.BindBuffer(target_, vb_);
				glBufferData(target_,
						static_cast<GLsizeiptr>(size_in_byte_), init_data->data,
						(BU_Static == usage_) ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW);
			}
		}
	}
示例#7
0
文件: r_mesh.c 项目: raynorpat/cake
void RMesh_CreateTexCoords (model_t *mod, dmdl_t *hdr)
{
	mdlst_t *st = (mdlst_t *) Scratch_Alloc ();
	int *order = (int *) ((byte *) hdr + hdr->ofs_glcmds);

	mod->numframeverts = 0;

	glGenBuffers (1, &mod->texcoordvbo);

	while (1)
	{
		// get the vertex count and primitive type
		int count = *order++;
		int i;

		if (!count) break;
		if (count < 0) count = -count;

		for (i = 0; i < count; i++, st++, order += 3)
		{
			st->texcoords[0] = ((float *) order)[0];
			st->texcoords[1] = ((float *) order)[1];
		}

		mod->numframeverts += count;
	}

	glNamedBufferDataEXT (mod->texcoordvbo, mod->numframeverts * sizeof (mdlst_t), Scratch_Alloc (), GL_STATIC_DRAW);
}
示例#8
0
文件: r_mesh.c 项目: raynorpat/cake
void RMesh_CreateFrames (model_t *mod, dmdl_t *hdr)
{
	int i, frame;
	posevert_t *verts = (posevert_t *) Scratch_Alloc ();
	int numverts = 0;

	glGenBuffers (1, &mod->meshvbo);

	for (frame = 0; frame < hdr->num_frames; frame++)
	{
		int *order = (int *) ((byte *) hdr + hdr->ofs_glcmds);
		dtrivertx_t *vert = ((daliasframe_t *) ((byte *) hdr + hdr->ofs_frames + frame * hdr->framesize))->verts;

		while (1)
		{
			// get the vertex count and primitive type
			int count = *order++;

			if (!count) break;
			if (count < 0) count = -count;

			for (i = 0; i < count; i++, verts++, order += 3)
			{
				verts->position[0] = vert[order[2]].v[0];
				verts->position[1] = vert[order[2]].v[1];
				verts->position[2] = vert[order[2]].v[2];
				verts->position[3] = vert[order[2]].lightnormalindex;
			}

			numverts += count;
		}
	}

	glNamedBufferDataEXT (mod->meshvbo, numverts * sizeof (posevert_t), Scratch_Alloc (), GL_STATIC_DRAW);
}
示例#9
0
文件: r_mesh.c 项目: raynorpat/cake
void RMesh_CreatePrograms (void)
{
	glGetIntegerv (GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &gl_meshuboblocksize);

	if (gl_meshuboblocksize < sizeof (meshubo_t))
	{
		int addsize = gl_meshuboblocksize;

		while (gl_meshuboblocksize < sizeof (meshubo_t))
			gl_meshuboblocksize += addsize;
	}

	gl_meshprog = GL_CreateShaderFromName ("glsl/mesh.glsl", "MeshVS", "MeshFS");

	glUniformBlockBinding (gl_meshprog, glGetUniformBlockIndex (gl_meshprog, "MeshUniforms"), gl_meshubobinding);

	glProgramUniform1i (gl_meshprog, glGetUniformLocation (gl_meshprog, "diffuse"), 0);
	glProgramUniform3fv (gl_meshprog, glGetUniformLocation (gl_meshprog, "lightnormal"), 162, (float *) r_avertexnormals);

	u_meshMaxLights = glGetUniformLocation(gl_meshprog, "r_maxLights");
	u_meshEntOrig = glGetUniformLocation(gl_meshprog, "r_entOrig");
	for (int i = 0; i < MAX_LIGHTS; ++i)
	{
		u_meshLightPos[i] = glGetUniformLocation(gl_meshprog, va("Lights.origin[%i]", i));
		u_meshLightColor[i] = glGetUniformLocation(gl_meshprog, va("Lights.color[%i]", i));
		u_meshLightAtten[i] = glGetUniformLocation(gl_meshprog, va("Lights.radius[%i]", i));
	}

	glGenBuffers (1, &gl_meshubo);
	glNamedBufferDataEXT (gl_meshubo, MESH_UBO_MAX_BLOCKS * gl_meshuboblocksize, NULL, GL_STREAM_DRAW);
}
示例#10
0
void SObjModel::BindVAOs() {
    /* load descriptor*/
    //unsigned int temp_vao;
    unsigned int temp_vbo;
    unsigned int temp_ibo;
    /*
        std::string map_Ka; ambient map
        std::string map_Kd; diffuse map
        std::string map_Ns; Specular shinnes map
        std::string map_d; alpha map
        std::string map_bump;
    */

    /*https://devtalk.nvidia.com/default/topic/561172/gldrawarrays-without-vao-for-procedural-geometry-using-gl_vertexid-doesn-t-work-in-debug-context/ */
    glGenVertexArrays ( 1, &d_emptyVAO );
   // glBindVertexArray(d_emptyVAO);
        for (auto it = d_sm.begin(); it != d_sm.end();++it) {
            auto &submesh =  (*it);
            /*gen buffers for submesh*/
            /*vertixes*/
            temp_vbo = 0;
            temp_ibo = 0;
            glGenBuffers ( 1, &temp_vbo );
            glGenBuffers(1, &temp_ibo);

            //MASSERT(submesh->vn.empty());
           // MASSERT(submesh->indexes.empty());
            //gl 4.5 without EXT
            #ifndef __APPLE__
            glNamedBufferDataEXT(temp_vbo,submesh->vn.size() *sizeof(UVNVertex), submesh->vn.data(), GL_STATIC_DRAW);
            glNamedBufferDataEXT(temp_ibo,submesh->indexes.size() *sizeof(unsigned int), submesh->indexes.data(), GL_STATIC_DRAW);
            SubMeshIDs idx;
            idx.vbo = temp_vbo;
            idx.ibo = temp_ibo;

            submesh_idx[submesh->id] = idx;
            #else
            //todo
            #endif
            glBindBuffer(GL_ARRAY_BUFFER,0);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);

        }
        //glBindVertexArray(0);
}
示例#11
0
文件: Buffer.cpp 项目: voxelinc/glow
void Buffer::setData(GLsizeiptr size, const GLvoid* data, GLenum usage)
{
    if (m_directStateAccess)
    {
        glNamedBufferDataEXT(m_id, size, data, usage);
    }
    else
    {
        bind();
        glBufferData(m_target, size, data, usage);
        CheckGLError();
    }
}
示例#12
0
bool
CLGLVertexBuffer::allocate(cl_context clContext) {

    assert(clContext);

    // create GL buffer first
    int size = _numElements * _numVertices * sizeof(float);

    glGenBuffers(1, &_vbo);
#if defined(GL_EXT_direct_state_access)
    if (glNamedBufferDataEXT) {
        glNamedBufferDataEXT(_vbo, size, 0, GL_DYNAMIC_DRAW);
    } else {
#else
    {
#endif
        GLint prev = 0;
        glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prev);
        glBindBuffer(GL_ARRAY_BUFFER, _vbo);
        glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, prev);
    }

    // register vbo as cl memory
    cl_int err;
    _clMemory = clCreateFromGLBuffer(clContext,
                                     CL_MEM_READ_WRITE, _vbo, &err);

    if (err != CL_SUCCESS) return false;
    return true;
}

void
CLGLVertexBuffer::map(cl_command_queue queue) {

    if (_clMapped) return;    // XXX: what if another queue is given?
    _clQueue = queue;
    clEnqueueAcquireGLObjects(queue, 1, &_clMemory, 0, 0, 0);
    _clMapped = true;
}

void
CLGLVertexBuffer::unmap() {

    if (! _clMapped) return;
    clEnqueueReleaseGLObjects(_clQueue, 1, &_clMemory, 0, 0, 0);
    _clMapped = false;
}

}  // end namespace Osd
示例#13
0
	void OGLGraphicsBuffer::DoResize()
	{
		BOOST_ASSERT(size_in_byte_ != 0);

		if (glloader_GL_EXT_direct_state_access())
		{
			glNamedBufferDataEXT(vb_, static_cast<GLsizeiptr>(size_in_byte_), nullptr,
						(BU_Static == usage_) ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW);
		}
		else
		{
			OGLRenderEngine& re = *checked_cast<OGLRenderEngine*>(&Context::Instance().RenderFactoryInstance().RenderEngineInstance());
			re.BindBuffer(target_, vb_);
			glBufferData(target_,
					static_cast<GLsizeiptr>(size_in_byte_), nullptr,
					(BU_Static == usage_) ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW);
		}
	}
示例#14
0
文件: r_mesh.c 项目: raynorpat/cake
void RMesh_CreateIndexes (model_t *mod, dmdl_t *hdr)
{
	unsigned short *ndx = (unsigned short *) Scratch_Alloc ();
	int *order = (int *) ((byte *) hdr + hdr->ofs_glcmds);
	int firstvertex = 0;

	mod->numindexes = 0;

	glGenBuffers (1, &mod->indexbuffer);

	while (1)
	{
		// get the vertex count and primitive type
		int count = *order++;
		int i;

		if (!count) break;

		if (count < 0)
		{
			for (i = 2, count = -count; i < count; i++, ndx += 3)
			{
				ndx[0] = firstvertex + 0;
				ndx[1] = firstvertex + (i - 1);
				ndx[2] = firstvertex + i;
			}
		}
		else
		{
			for (i = 2; i < count; i++, ndx += 3)
			{
				ndx[0] = firstvertex + i - 2;
				ndx[1] = firstvertex + ((i & 1) ? i : (i - 1));
				ndx[2] = firstvertex + ((i & 1) ? (i - 1) : i);
			}
		}

		order += count * 3;
		firstvertex += count;
		mod->numindexes += (count - 2) * 3;
	}

	glNamedBufferDataEXT (mod->indexbuffer, mod->numindexes * sizeof (unsigned short), Scratch_Alloc (), GL_STATIC_DRAW);
}
示例#15
0
bool
CudaGLVertexBuffer::allocate() {

    int size = _numElements * _numVertices * sizeof(float);

    glGenBuffers(1, &_vbo);

#if defined(GL_EXT_direct_state_access)
    if (glNamedBufferDataEXT) {
        glNamedBufferDataEXT(_vbo, size, 0, GL_DYNAMIC_DRAW);
    } else {
#else
    {
#endif
        glBindBuffer(GL_ARRAY_BUFFER, _vbo);
        glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    // register vbo as cuda resource
    cudaError_t err = cudaGraphicsGLRegisterBuffer(
        &_cudaResource, _vbo, cudaGraphicsMapFlagsWriteDiscard);

    if (err != cudaSuccess) return false;
    return true;
}

void
CudaGLVertexBuffer::map() {

    if (_devicePtr) return;
    size_t num_bytes;
    void *ptr;

    cudaError_t err = cudaGraphicsMapResources(1, &_cudaResource, 0);
    if (err != cudaSuccess)
        Far::Error(Far::FAR_RUNTIME_ERROR,
                   "CudaGLVertexBuffer::map failed.\n%s\n",
                   cudaGetErrorString(err));
    err = cudaGraphicsResourceGetMappedPointer(&ptr, &num_bytes, _cudaResource);
    if (err != cudaSuccess)
        Far::Error(Far::FAR_RUNTIME_ERROR,
                   "CudaGLVertexBuffer::map failed.\n%s\n",
                   cudaGetErrorString(err));
    _devicePtr = ptr;
}

void
CudaGLVertexBuffer::unmap() {

    if (_devicePtr == NULL) return;
    cudaError_t err = cudaGraphicsUnmapResources(1, &_cudaResource, 0);
    if (err != cudaSuccess)
       Far::Error(Far::FAR_RUNTIME_ERROR,
                  "CudaGLVertexBuffer::unmap failed.\n%s\n",
                  cudaGetErrorString(err));
    _devicePtr = NULL;
}


}  // end namespace Osd
示例#16
0
	void Buffer::BufferData(GLsizeiptr size, const GLvoid* data, GLenum usage)
	{
		Generate();
		glNamedBufferDataEXT(m_handle, size, data, usage);
		m_size = size;
	}
//------------------------------------------------------------------------------
// It is possible to create one VBO for one Mesh; and one EBO for each primitive group
// however, for this sample, we will create only one VBO for all and one EBO
// meshes and primitive groups will have an offset in these buffers
//------------------------------------------------------------------------------
bool Bk3dModelStandard::initResourcesObject()
{
    LOGFLUSH();
    SHOWPROGRESS("Init resources")

    //m_pGenericModel->m_meshFile->pMeshes->n = 60000;
    //
    // create Buffer Object for materials
    //
    if(m_pGenericModel->m_meshFile->pMaterials && m_pGenericModel->m_meshFile->pMaterials->nMaterials )
    {
        //
        // Material UBO: *TABLE* of multiple materials
        // Then offset in it for various drawcalls
        //
        if(m_uboMaterial.Id == 0)
            glGenBuffers(1, &m_uboMaterial.Id);

        m_uboMaterial.Sz = sizeof(MaterialBuffer) * m_pGenericModel->m_materialNItems;
        glNamedBufferDataEXT(m_uboMaterial.Id, m_uboMaterial.Sz, m_pGenericModel->m_material, GL_STATIC_DRAW);
        glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATERIAL, m_uboMaterial.Id);

        LOGI("%d materials stored in %d Kb\n", m_pGenericModel->m_meshFile->pMaterials->nMaterials, (m_uboMaterial.Sz+512)/1024);
        LOGFLUSH();
    }

    //
    // create Buffer Object for Object-matrices
    //
    if(m_pGenericModel->m_meshFile->pTransforms && m_pGenericModel->m_meshFile->pTransforms->nBones)
    {
        //
        // Transformation UBO: *TABLE* of multiple transformations
        // Then offset in it for various drawcalls
        //
        if(m_uboObjectMatrices.Id == 0)
            glGenBuffers(1, &m_uboObjectMatrices.Id);

        m_uboObjectMatrices.Sz = sizeof(MatrixBufferObject) * m_pGenericModel->m_objectMatricesNItems;
        glNamedBufferDataEXT(m_uboObjectMatrices.Id, m_uboObjectMatrices.Sz, m_pGenericModel->m_objectMatrices, GL_STATIC_DRAW);
        glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATRIXOBJ, m_uboObjectMatrices.Id);

        LOGI("%d matrices stored in %d Kb\n", m_pGenericModel->m_meshFile->pTransforms->nBones, (m_uboObjectMatrices.Sz + 512)/1024);
        LOGFLUSH();
    }

    //
    // First pass: evaluate the size of the single VBO
    // and store offset to where we'll find data back
    //
    bk3d::Mesh *pMesh = NULL;
    for(int i=0; i< m_pGenericModel->m_meshFile->pMeshes->n; i++)
	{
        SETPROGRESSVAL(100.0f*(float)i/(float)m_pGenericModel->m_meshFile->pMeshes->n);
		pMesh = m_pGenericModel->m_meshFile->pMeshes->p[i];
        //
        // Slots: buffers for vertices
        //
        int n = pMesh->pSlots->n;
        for(int s=0; s<n; s++)
        {
            bk3d::Slot* pS = pMesh->pSlots->p[s];
            GLuint id;
            glGenBuffers(1, &id); // Buffer Object directly kept in the Slot
            pS->userData = id;
            #if 1
                glNamedBufferDataEXT(id, pS->vtxBufferSizeBytes, NULL, GL_STATIC_DRAW);
            #else
                glNamedBufferStorageEXT(id, pS->vtxBufferSizeBytes, NULL, 0); // Not working with NSight !!! https://www.opengl.org/registry/specs/ARB/buffer_storage.txt
            #endif
            glNamedBufferSubDataEXT(id, 0, pS->vtxBufferSizeBytes, pS->pVtxBufferData);
        }
        //
        // Primitive groups
        //
        for(int pg=0; pg<pMesh->pPrimGroups->n; pg++)
        {
            bk3d::PrimGroup* pPG = pMesh->pPrimGroups->p[pg];
            if(pPG->indexArrayByteSize > 0)
            {
                if((pPG->pOwnerOfIB == pPG)||(pPG->pOwnerOfIB == NULL)) // this primitive group doesn't use other's buffer
                {
                    GLuint id;
                    glGenBuffers(1, &id);
                    pPG->userPtr = (int*)id;
                #if 1
                    glNamedBufferDataEXT(id, pPG->indexArrayByteSize, NULL, GL_STATIC_DRAW);
                #else
                    glNamedBufferStorageEXT(id, pPG->indexArrayByteSize, NULL, 0); // Not working with NSight !!! https://www.opengl.org/registry/specs/ARB/buffer_storage.txt
                #endif
                    glNamedBufferSubDataEXT(id, pPG->indexArrayByteOffset, pPG->indexArrayByteSize, pPG->pIndexBufferData);
                } else {
                    pPG->userPtr = pPG->pOwnerOfIB->userPtr;
                }
            } else {
                pPG->userPtr = NULL;
            }
        }
	}
    //LOGI("meshes: %d in :%d VBOs (%f Mb) and %d EBOs (%f Mb) \n", m_pGenericModel->m_meshFile->pMeshes->n, .size(), (float)totalVBOSz/(float)(1024*1024), m_ObjEBOs.size(), (float)totalEBOSz/(float)(1024*1024));
    LOGFLUSH();
    HIDEPROGRESS()
    return true;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool RendererStandard::initGraphics(int w, int h, int MSAA)
{
	//
	// some offscreen buffer
	//
    fboInitialize(w, h, MSAA);
	//
	// Shader compilation
	//
	if(!s_shaderGrid.addVertexShaderFromString(g_glslv_grid))
        return false;
	if(!s_shaderGrid.addFragmentShaderFromString(g_glslf_grid))
        return false;
	if (!s_shaderGrid.link())
		return false;
    if(!s_shaderMesh.addVertexShaderFromString(s_glslv_mesh))
        return false;
    if(!s_shaderMesh.addFragmentShaderFromString(s_glslf_mesh))
        return false;
    if(!s_shaderMesh.link())
        return false;
    if(!s_shaderMeshLine.addVertexShaderFromString(s_glslv_mesh))
        return false;
    if(!s_shaderMeshLine.addFragmentShaderFromString(s_glslf_mesh_line))
        return false;
    if(!s_shaderMeshLine.link())
        return false;

	//
	// Create some UBO for later share their 64 bits
	//
	glGenBuffers(1, &g_uboMatrix.Id);
	g_uboMatrix.Sz = sizeof(MatrixBufferGlobal);
	glNamedBufferDataEXT(g_uboMatrix.Id, g_uboMatrix.Sz, &g_globalMatrices, GL_STREAM_DRAW);
	//glBindBufferBase(GL_UNIFORM_BUFFER,UBO_MATRIX, g_uboMatrix.Id);
	//
	// Trivial Light info...
	//
	glGenBuffers(1, &g_uboLight.Id);
	g_uboLight.Sz = sizeof(LightBuffer);
	glNamedBufferDataEXT(g_uboLight.Id, g_uboLight.Sz, &s_light, GL_STATIC_DRAW);
	//glBindBufferBase(GL_UNIFORM_BUFFER,UBO_LIGHT, g_uboLight.Id);
	//
	// Misc OGL setup
	//
	glClearColor(0.0f, 0.1f, 0.15f, 1.0f);
	glGenVertexArrays(1, &s_vao);
	glBindVertexArray(s_vao);
    //
    // Grid
    //
    if(!initResourcesGrid())
        return false;
    //
    // OpenGL timers
    //
    m_gltimers.init(10);

    LOGOK("Initialized renderer %s\n", getName());
    m_bValid = true;
	return true;
}
示例#19
0
void
Hd_SmoothNormalsComputationGPU::Execute(
    HdBufferArrayRangeSharedPtr const &range)
{
    HD_TRACE_FUNCTION();
    HF_MALLOC_TAG_FUNCTION();

    if (!glDispatchCompute)
        return;


    TF_VERIFY(_adjacency);
    HdBufferArrayRangeSharedPtr const &adjacencyRange = _adjacency->GetAdjacencyRange();
    TF_VERIFY(adjacencyRange);

    // select shader by datatype
    TfToken shaderToken;
    if (_srcDataType == GL_FLOAT) {
        if (_dstDataType == GL_FLOAT) {
            shaderToken = HdGLSLProgramTokens->smoothNormalsFloatToFloat;
        } else if (_dstDataType == GL_DOUBLE) {
            shaderToken = HdGLSLProgramTokens->smoothNormalsFloatToDouble;
        } else if (_dstDataType == GL_INT_2_10_10_10_REV) {
            shaderToken = HdGLSLProgramTokens->smoothNormalsFloatToPacked;
        }
    } else if (_srcDataType == GL_DOUBLE) {
        if (_dstDataType == GL_FLOAT) {
            shaderToken = HdGLSLProgramTokens->smoothNormalsDoubleToFloat;
        } else if (_dstDataType == GL_DOUBLE) {
            shaderToken = HdGLSLProgramTokens->smoothNormalsDoubleToDouble;
        } else if (_dstDataType == GL_INT_2_10_10_10_REV) {
            shaderToken = HdGLSLProgramTokens->smoothNormalsDoubleToPacked;
        }
    }
    if (!TF_VERIFY(!shaderToken.IsEmpty())) return;

    HdGLSLProgramSharedPtr computeProgram
        = HdGLSLProgram::GetComputeProgram(shaderToken);
    if (!computeProgram) return;

    GLuint program = computeProgram->GetProgram().GetId();

    // buffer resources for GPU computation
    HdBufferResourceSharedPtr points = range->GetResource(_srcName);
    HdBufferResourceSharedPtr normals = range->GetResource(_dstName);
    HdBufferResourceSharedPtr adjacency = adjacencyRange->GetResource();

    // prepare uniform buffer for GPU computation
    struct Uniform {
        int vertexOffset;
        int adjacencyOffset;
        int pointsOffset;
        int pointsStride;
        int normalsOffset;
        int normalsStride;
    } uniform;

    // coherent vertex offset in aggregated buffer array
    uniform.vertexOffset = range->GetOffset();
    // adjacency offset/stride in aggregated adjacency table
    uniform.adjacencyOffset = adjacencyRange->GetOffset();
    // interleaved offset/stride to points
    // note: this code (and the glsl smooth normal compute shader) assumes
    // components in interleaved vertex array are always same data type.
    // i.e. it can't handle an interleaved array which interleaves
    // float/double, float/int etc.
    uniform.pointsOffset = points->GetOffset() / points->GetComponentSize();
    uniform.pointsStride = points->GetStride() / points->GetComponentSize();
    // interleaved offset/stride to normals
    uniform.normalsOffset = normals->GetOffset() / normals->GetComponentSize();
    uniform.normalsStride = normals->GetStride() / normals->GetComponentSize();

    // The number of points is based off the size of the output,
    // However, the number of points in the adjacency table
    // is computed based off the largest vertex indexed from
    // to topology (aka topology->ComputeNumPoints).
    //
    // Therefore, we need to clamp the number of points
    // to the number of entries in the adjancency table.
    int numDestPoints = range->GetNumElements();
    int numSrcPoints = _adjacency->GetNumPoints();

    int numPoints = std::min(numSrcPoints, numDestPoints);

    // transfer uniform buffer
    GLuint ubo = computeProgram->GetGlobalUniformBuffer().GetId();
    HdRenderContextCaps const &caps = HdRenderContextCaps::GetInstance();
    // XXX: workaround for 319.xx driver bug of glNamedBufferDataEXT on UBO
    // XXX: move this workaround to renderContextCaps
    if (false && caps.directStateAccessEnabled) {
        glNamedBufferDataEXT(ubo, sizeof(uniform), &uniform, GL_STATIC_DRAW);
    } else {
        glBindBuffer(GL_UNIFORM_BUFFER, ubo);
        glBufferData(GL_UNIFORM_BUFFER, sizeof(uniform), &uniform, GL_STATIC_DRAW);
        glBindBuffer(GL_UNIFORM_BUFFER, 0);
    }

    glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, points->GetId());
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, normals->GetId());
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, adjacency->GetId());

    // dispatch compute kernel
    glUseProgram(program);

    glDispatchCompute(numPoints, 1, 1);

    glUseProgram(0);
    glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

    glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, 0);
}
void BufferImplementation_DirectStateAccessEXT::setData(const Buffer * buffer, GLsizeiptr size, const GLvoid * data, GLenum usage) const
{
    glNamedBufferDataEXT(buffer->id(), size, data, usage);
}
示例#21
0
void
Hd_SmoothNormalsComputationGPU::Execute(
    HdBufferArrayRangeSharedPtr const &range)
{
    HD_TRACE_FUNCTION();
    HD_MALLOC_TAG_FUNCTION();

    if (not glDispatchCompute)
        return;

    // XXX: workaround until the shading stuff is implemeted.
    // The drawing program is owned and set by testHdBasicDrawing now,
    // so it has to be restored if it's changed in hd.
    GLint restoreProgram = 0;
    glGetIntegerv(GL_CURRENT_PROGRAM, &restoreProgram);

    int numPoints = range->GetNumElements();

    TF_VERIFY(_adjacency);
    HdBufferArrayRangeSharedPtr const &adjacencyRange = _adjacency->GetAdjacencyRange();
    TF_VERIFY(adjacencyRange);

    // select shader by datatype
    TfToken shaderToken = (_dstDataType == GL_FLOAT ?
                           HdGLSLProgramTokens->smoothNormalsFloat :
                           HdGLSLProgramTokens->smoothNormalsDouble);

    HdGLSLProgramSharedPtr computeProgram = HdGLSLProgram::GetComputeProgram(shaderToken);
    if (not computeProgram) return;

    GLuint program = computeProgram->GetProgram().GetId();

    // buffer resources for GPU computation
    HdBufferResourceSharedPtr points = range->GetResource(_srcName);
    HdBufferResourceSharedPtr normals = range->GetResource(_dstName);
    HdBufferResourceSharedPtr adjacency = adjacencyRange->GetResource();

    // prepare uniform buffer for GPU computation
    struct Uniform {
        int vertexOffset;
        int adjacencyStride;
        int adjacencyOffset;
        int padding;
        int pointsOffset;
        int pointsStride;
        int normalsOffset;
        int normalsStride;
    } uniform;

    // coherent vertex offset in aggregated buffer array
    uniform.vertexOffset = range->GetOffset();
    // adjacency offset/stride in aggregated adjacency table
    uniform.adjacencyStride = _adjacency->GetStride();
    uniform.adjacencyOffset = adjacencyRange->GetOffset();
    uniform.padding = 0;
    // interleaved offset/stride to points
    // note: this code (and the glsl smooth normal compute shader) assumes
    // components in interleaved vertex array are always same data type.
    // i.e. it can't handle an interleaved array which interleaves
    // float/double, float/int etc.
    uniform.pointsOffset = points->GetOffset() / points->GetComponentSize();
    uniform.pointsStride = points->GetStride() / points->GetComponentSize();
    // interleaved offset/stride to normals
    uniform.normalsOffset = normals->GetOffset() / points->GetComponentSize();
    uniform.normalsStride = normals->GetStride() / points->GetComponentSize();

    // transfer uniform buffer
    GLuint ubo = computeProgram->GetGlobalUniformBuffer().GetId();
    HdRenderContextCaps const &caps = HdRenderContextCaps::GetInstance();
    // XXX: workaround for 319.xx driver bug of glNamedBufferDataEXT on UBO
    // XXX: move this workaround to renderContextCaps
    if (false and caps.directStateAccessEnabled) {
        glNamedBufferDataEXT(ubo, sizeof(uniform), &uniform, GL_STATIC_DRAW);
    } else {
        glBindBuffer(GL_UNIFORM_BUFFER, ubo);
        glBufferData(GL_UNIFORM_BUFFER, sizeof(uniform), &uniform, GL_STATIC_DRAW);
        glBindBuffer(GL_UNIFORM_BUFFER, 0);
    }

    glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, points->GetId());
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, normals->GetId());
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, adjacency->GetId());

    // dispatch compute kernel
    glUseProgram(program);

    glDispatchCompute(numPoints, 1, 1);

    glUseProgram(0);

    glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, 0);

    // XXX: workaround until shading stuff implemeted.
    glUseProgram(restoreProgram);
}