VertexArrayObject() { glCreateVertexArrays(1,&vertexArrayObject); glCreateBuffers(3,buffer); const static constexpr float data_0[]{ -0.5f,-0.5f,0.0f,1, 0.5f,-0.5f,0.0f,1, 0.0f, 0.0f,0.0f,1, }; const static constexpr float data_1[]{ -0.5f,-0.5f,0.0f,1, 0.5f,-0.5f,0.0f,1, 0.0f, .25f,0.0f,1, }; const static constexpr float data_2[]{ -0.5f,-0.5f,0.0f,1, 0.5f,-0.5f,0.0f,1, 0.0f, 0.5f,0.0f,1, }; glNamedBufferData(buffer[0],sizeof(data_0),data_0,GL_STATIC_DRAW); glNamedBufferData(buffer[1],sizeof(data_1),data_1,GL_STATIC_DRAW); glNamedBufferData(buffer[2],sizeof(data_2),data_2,GL_STATIC_DRAW); glVertexArrayVertexBuffer(vertexArrayObject,0,buffer[0],0,sizeof(float[4])); glVertexArrayVertexBuffer(vertexArrayObject,1,buffer[1],0,sizeof(float[4])); glVertexArrayVertexBuffer(vertexArrayObject,2,buffer[2],0,sizeof(float[4])); glEnableVertexArrayAttrib(vertexArrayObject,0); glVertexArrayAttribFormat(vertexArrayObject,0,4,GL_FLOAT,false,0); glVertexArrayAttribBinding(vertexArrayObject,0,rand()%3); }
//[-------------------------------------------------------] //[ 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 } } }
void SetupModel(HWND hwnd) { RECT rect; GetClientRect(hwnd, &rect); // Vertex Buffer (position) GLuint positionLocation = 0; GLuint positionBindindex = 0; glCreateBuffers(1, &positionBuffer); glNamedBufferData(positionBuffer, sizeof(positions), positions, GL_STATIC_DRAW); // Vertex Buffer (uv) GLuint uvLocation = 1; GLuint uvBindindex = 1; glCreateBuffers(1, &uvBuffer); glNamedBufferData(uvBuffer, sizeof(uvs), uvs, GL_STATIC_DRAW); // Index Buffer glCreateBuffers(1, &indexBuffer); glNamedBufferData(indexBuffer, sizeof(indices), indices, GL_STATIC_DRAW); // Vertex Array glCreateVertexArrays(1, &vertexArray); glEnableVertexArrayAttrib(vertexArray, positionLocation); glVertexArrayAttribFormat(vertexArray, positionLocation, 2, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribBinding(vertexArray, positionLocation, positionBindindex); glVertexArrayVertexBuffer(vertexArray, positionBindindex, positionBuffer, static_cast<GLintptr>(0), sizeof(GLfloat) * 2); glEnableVertexArrayAttrib(vertexArray, uvLocation); glVertexArrayAttribFormat(vertexArray, uvLocation, 2, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribBinding(vertexArray, uvLocation, uvBindindex); glVertexArrayVertexBuffer(vertexArray, uvBindindex, uvBuffer, static_cast<GLintptr>(0), sizeof(GLfloat) * 2); glVertexArrayElementBuffer(vertexArray, indexBuffer); }
void MainComponent::start() { if (isRunning) { return; } static const float vertices[] = { 0.25, -0.25, 0.5, 1.0, -0.25, -0.25, 0.5, 1.0, 0.25, 0.25, 0.5, 1.0 }; Shader shader = Shader(); glUseProgram(shader._program); GLuint vbo; glCreateBuffers(1, &vbo); glCreateVertexArrays(1, &_vao); glBindVertexArray(_vao); glNamedBufferStorage(vbo, sizeof(vertices), vertices, 0); glBindBuffer(GL_ARRAY_BUFFER, vbo); glVertexArrayVertexBuffer(_vao, 0, vbo, 0, 16); glVertexArrayAttribFormat(_vao, 0, 4, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribBinding(_vao, 0, 0); glEnableVertexArrayAttrib(_vao, 0); glEnableVertexAttribArray(0); run(); }
VertexBuffer::VertexBuffer(GLfloat *data, GLsizei size, GLenum type) { glCreateBuffers(1, &m_BufferID); #ifdef TR_CURRENT glNamedBufferData(m_BufferID, size, data, type); #else bind(); glBufferData(GL_ARRAY_BUFFER, size, data, type); unbind(); #endif }
bool initBuffer() { bool Validated(true); GLint UniformBufferOffset(0); glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UniformBufferOffset); GLint UniformBlockSize = glm::max(GLint(sizeof(glm::mat4)), UniformBufferOffset); glCreateBuffers(buffer::MAX, &BufferName[0]); glNamedBufferData(BufferName[buffer::ELEMENT], ElementSize, ElementData, GL_STATIC_DRAW); glNamedBufferData(BufferName[buffer::VERTEX], VertexSize, VertexData, GL_STATIC_DRAW); glNamedBufferData(BufferName[buffer::TRANSFORM], UniformBlockSize, nullptr, GL_DYNAMIC_DRAW); return Validated; }
void Application::create() { compileShaders(); segs = 5000; dim = 30.0f; std::ifstream infile("config.txt"); if (infile) { infile >> dim >> segs; } for (float x = -dim; x < dim; x += dim / segs) { for (float y = -dim; y < dim; y += dim / segs) { for (float z = -dim; z < dim; z += dim / segs) { points.push_back(glm::vec4(x, y, z, 1.0f)); } } } glPointSize(1); glCreateBuffers(1, &point_buffer); glBindBuffer(GL_ARRAY_BUFFER, point_buffer); glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(glm::vec4), points.data(), GL_STATIC_DRAW); num_particles = static_cast<int64_t>(points.size()); std::cout << "Number of particles = " << points.size() << std::endl; }
bool initBuffer() { bool Validated(true); GLint MaxVertexAtomicCounterBuffers(0); GLint MaxControlAtomicCounterBuffers(0); GLint MaxEvaluationAtomicCounterBuffers(0); GLint MaxGeometryAtomicCounterBuffers(0); GLint MaxFragmentAtomicCounterBuffers(0); GLint MaxCombinedAtomicCounterBuffers(0); glGetIntegerv(GL_MAX_VERTEX_ATOMIC_COUNTER_BUFFERS, &MaxVertexAtomicCounterBuffers); glGetIntegerv(GL_MAX_TESS_CONTROL_ATOMIC_COUNTER_BUFFERS, &MaxControlAtomicCounterBuffers); glGetIntegerv(GL_MAX_TESS_EVALUATION_ATOMIC_COUNTER_BUFFERS, &MaxEvaluationAtomicCounterBuffers); glGetIntegerv(GL_MAX_GEOMETRY_ATOMIC_COUNTER_BUFFERS, &MaxGeometryAtomicCounterBuffers); glGetIntegerv(GL_MAX_FRAGMENT_ATOMIC_COUNTER_BUFFERS, &MaxFragmentAtomicCounterBuffers); glGetIntegerv(GL_MAX_COMBINED_ATOMIC_COUNTER_BUFFERS, &MaxCombinedAtomicCounterBuffers); glCreateBuffers(buffer::MAX, &BufferName[0]); glNamedBufferStorage(BufferName[buffer::VERTEX], VertexSize, VertexData, 0); glNamedBufferStorage(BufferName[buffer::ELEMENT], ElementSize, ElementData, 0); glNamedBufferStorage(BufferName[buffer::ATOMIC_COUNTER], sizeof(GLuint), nullptr, 0); glNamedBufferStorage(BufferName[buffer::TRANSFORM], sizeof(glm::mat4), nullptr, GL_MAP_WRITE_BIT); return Validated; }
GLuint BufferImplementation_DirectStateAccessARB::create() const { GLuint buffer; glCreateBuffers(1, &buffer); // create a handle as well as the actual buffer return buffer; }
ofBufferObject::Data::Data() :id(0) ,size(0) ,lastTarget(GL_ARRAY_BUFFER){ // tig: glGenBuffers does not actually create a buffer, it just // returns the next available name, and only a subsequent // call to bind() will actualy initialize the buffer in // memory. // // This is why, for direct state access, we need to call // glCreateBuffers(), so that the buffer is initialized // when we pin data to it using setData() // // see also: https://www.opengl.org/registry/specs/ARB/direct_state_access.txt #ifdef GLEW_ARB_direct_state_access if (GLEW_ARB_direct_state_access) { // the above condition is only true if GLEW can provide us // with direct state access methods. we use this to test // whether the driver is OpenGL 4.5 ready. glCreateBuffers(1,&id); return; } #endif glGenBuffers(1,&id); }
impl(int size, bool write) : size_(size) , write_(write) , target_(!write ? GL_PIXEL_PACK_BUFFER : GL_PIXEL_UNPACK_BUFFER) , flags_(GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | (write ? GL_MAP_WRITE_BIT : GL_MAP_READ_BIT)) { GL(glCreateBuffers(1, &id_)); GL(glNamedBufferStorage(id_, size_, nullptr, flags_)); data_ = GL2(glMapNamedBufferRange(id_, 0, size_, flags_)); }
OGLTexture1D::OGLTexture1D(uint32_t width, uint32_t numMipMaps, uint32_t array_size, ElementFormat format, uint32_t sample_count, uint32_t sample_quality, uint32_t access_hint) : OGLTexture(TT_1D, array_size, sample_count, sample_quality, access_hint) { format_ = format; if (0 == numMipMaps) { num_mip_maps_ = 1; uint32_t w = width; while (w != 1) { ++ num_mip_maps_; w = std::max<uint32_t>(1U, w / 2); } } else { num_mip_maps_ = numMipMaps; } width_ = width; if (glloader_GL_VERSION_4_5() || glloader_GL_ARB_direct_state_access()) { glCreateBuffers(1, &pbo_); } else { glGenBuffers(1, &pbo_); } mipmap_start_offset_.resize(num_mip_maps_ + 1); mipmap_start_offset_[0] = 0; for (uint32_t level = 0; level < num_mip_maps_; ++ level) { uint32_t const w = this->Width(level); GLsizei image_size; if (IsCompressedFormat(format_)) { uint32_t const block_size = NumFormatBytes(format_) * 4; image_size = ((w + 3) / 4) * block_size; } else { uint32_t const texel_size = NumFormatBytes(format_); image_size = w * texel_size; } mipmap_start_offset_[level + 1] = mipmap_start_offset_[level] + image_size; } }
bool initBuffer() { GLint UniformBufferOffset(0); glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UniformBufferOffset); GLint UniformBlockSize = glm::max(GLint(sizeof(glm::mat4)), UniformBufferOffset); glCreateBuffers(buffer::MAX, &BufferName[0]); glNamedBufferStorage(BufferName[buffer::ELEMENT], ElementSize, ElementData, 0); glNamedBufferStorage(BufferName[buffer::VERTEX], VertexSize, VertexData, 0); glNamedBufferStorage(BufferName[buffer::TRANSFORM], UniformBlockSize, nullptr, GL_MAP_WRITE_BIT); return true; }
void UnmappableBuffer::resizeBuffer(GLsizeiptr size) { GLuint newBuffer; size = nextPowerOfTwo(size); glCreateBuffers(1, &newBuffer); glNamedBufferData(newBuffer, size, nullptr, mUsage); glCopyNamedBufferSubData(mId, newBuffer, 0, 0, mSize); glDeleteBuffers(1, &mId); mId = newBuffer; mSize = size; }
UnmappableBuffer::UnmappableBuffer(GLsizeiptr size, void *data, GLenum usage) : mSize(nextPowerOfTwo(size)), mUsage(usage) { glCreateBuffers(1, &mId); glNamedBufferData(mId, mSize, nullptr, mUsage); if(data != nullptr) { glNamedBufferSubData(mId, 0, size, data); mOffset = size; } else mOffset = 0; }
static void setup_ubos(void) { static const char *names[NUM_UBOS] = { "ub_pos_size", "ub_color", "ub_rot" }; int i; glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment); printf("GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT = %d\n", alignment); if (test_buffer_offset) { printf("Testing buffer offset %d\n", alignment); } else { /* we use alignment as the offset */ alignment = 0; } glCreateBuffers(NUM_UBOS, buffers); for (i = 0; i < NUM_UBOS; i++) { GLint index, size; /* query UBO index */ index = glGetUniformBlockIndex(prog, names[i]); /* query UBO size */ glGetActiveUniformBlockiv(prog, index, GL_UNIFORM_BLOCK_DATA_SIZE, &size); printf("UBO %s: index = %d, size = %d\n", names[i], index, size); /* Allocate UBO */ glNamedBufferData(buffers[i], size + alignment, NULL, GL_DYNAMIC_DRAW); /* Attach UBO */ glBindBufferRange(GL_UNIFORM_BUFFER, i, buffers[i], alignment, /* offset */ size); glUniformBlockBinding(prog, index, i); if (!piglit_check_gl_error(GL_NO_ERROR)) piglit_report_result(PIGLIT_FAIL); } }
bool initBuffer() { GLint UniformBufferOffset(0); glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UniformBufferOffset); this->UniformBlockSize = glm::max(GLint(sizeof(glm::mat4)), UniformBufferOffset); glCreateBuffers(buffer::MAX, &BufferName[0]); glNamedBufferStorage(BufferName[buffer::ELEMENT], ElementSize, ElementData, 0); glNamedBufferStorage(BufferName[buffer::VERTEX], VertexSize, VertexData, 0); glNamedBufferStorage(BufferName[buffer::TRANSFORM], this->UniformBlockSize * 2, nullptr, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); this->UniformPointer = static_cast<glm::uint8*>(glMapNamedBufferRange( BufferName[buffer::TRANSFORM], 0, this->UniformBlockSize * 2, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_MAP_INVALIDATE_BUFFER_BIT)); return true; }
Buffer::Buffer(const Buffer & other) noexcept : gl::Object{other} { glCreateBuffers(1, &m_name); if (other.isValid()) { GLuint otherSize {other.getByteSize()}; glNamedBufferData(m_name, otherSize, nullptr, GL_STATIC_DRAW); glCopyNamedBufferSubData(other, m_name, 0, 0, otherSize); } }
void startup() { rendering_program = compile_shaders(); GLuint buffer[2]; GLuint vao; static const GLfloat positions[] = { 0.25f, -0.25f, 0.5f, 1.0f, -0.25f, -0.25f, 0.5f, 1.0f, 0.25f, 0.25f, 0.5f, 1.0f }; static const GLfloat colors[] = { 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0 }; // Create the vertex array object. glCreateVertexArrays(1, &vao); // Create two buffers. glCreateBuffers(2, &buffer[0]); // Initialize the first buffer. glNamedBufferStorage(buffer[0], sizeof(positions), positions, 0); // Bind it to the vertex array - offset zero, stride = sizeof(vec4) glVertexArrayVertexBuffer(vao, 0, buffer[0], 0, sizeof(vmath::vec4)); // Tell OpenGL what the format of the attribute is. glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0); // Tell OpenGL which vertex buffer binding to use for this attribute. glVertexArrayAttribBinding(vao, 0, 0); // Enable the attribute. glEnableVertexArrayAttrib(vao, 0); // Perform similar initialization for the second buffer. glNamedBufferStorage(buffer[1], sizeof(colors), colors, 0); glVertexArrayVertexBuffer(vao, 1, buffer[1], 0, sizeof(vmath::vec4)); glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribBinding(vao, 1, 1); glEnableVertexArrayAttrib(vao, 1); glEnableVertexAttribArray(1); glBindVertexArray(vao); }
bool SimpleUniformGL::InitScene() { glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a); std::vector<GLuint> shaders; shaders.push_back(GLUtil::CreateShader(GL_VERTEX_SHADER, "SimpleUniformVert.glsl")); shaders.push_back(GLUtil::CreateShader(GL_FRAGMENT_SHADER, "SimpleUniformFrag.glsl")); shaderProgram = GLUtil::CreateProgram(shaders); for_each(shaders.begin(), shaders.end(), glDeleteShader); glUseProgram(shaderProgram); GLint overrideColor = glGetUniformLocation(shaderProgram, "overrideColor"); glProgramUniform4f(shaderProgram, overrideColor, 1.0f, 0.0f, 0.0f, 1.0f); GLuint bindingPoint = 1; float colorsBlock[8] = { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0, 1.0f }; GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(colorsBlock), colorsBlock, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, buffer); GLuint blockIndex = glGetUniformBlockIndex(shaderProgram, "ColorBlock"); glUniformBlockBinding(shaderProgram, blockIndex, bindingPoint); glCreateVertexArrays(1, &vao); glBindVertexArray(vao); glCreateBuffers(1, &vbo); glNamedBufferData(vbo, sizeof(Data::vertices), Data::vertices, GL_STATIC_DRAW); glVertexArrayAttribBinding(vao, 0, 0); glVertexArrayAttribBinding(vao, 1, 0); glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(Vertex2)); glEnableVertexArrayAttrib(vao, 0); glEnableVertexArrayAttrib(vao, 1); glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2().position)); return true; }
__ThisData(){ glCreateVertexArrays(1,&vao); glCreateBuffers(1,&buffer); alignas(GLfloat) constexpr const static GLfloat data_[]{ 0,0.5f,0,1, -0.5f,-.5f,0,1, 0.5f,-.5f,0,1, }; glNamedBufferData(buffer,sizeof(data_),data_,GL_STATIC_DRAW); glEnableVertexArrayAttrib(vao,0); glVertexArrayVertexBuffer(vao,0,buffer,0,sizeof(data_)/3); glVertexArrayAttribBinding(vao,0,0); glVertexArrayAttribFormat(vao,0,4,GL_FLOAT,false,0); program = createProgram({ {GL_VERTEX_SHADER,readGLSLFile("glsl:SimpleVertexArrayObject.v.vert") }, {GL_FRAGMENT_SHADER,readGLSLFile("glsl:SimpleVertexArrayObject.f.frag")} }); }
void createAndAllocBuffer(GLsizeiptr bufferSize, GLenum usageMask, GLuint& bufferIdOut, const bufferPtr data, const char* name) { glCreateBuffers(1, &bufferIdOut); if (Config::ENABLE_GPU_VALIDATION) { glObjectLabel(GL_BUFFER, bufferIdOut, -1, name != nullptr ? name : Util::StringFormat("DVD_GENERAL_BUFFER_%d", bufferIdOut).c_str()); } assert(bufferIdOut != 0 && "GLUtil::allocBuffer error: buffer creation failed"); glNamedBufferData(bufferIdOut, bufferSize, data, usageMask); }
bufferPtr createAndAllocPersistentBuffer(GLsizeiptr bufferSize, BufferStorageMask storageMask, BufferAccessMask accessMask, GLuint& bufferIdOut, bufferPtr const data, const char* name) { glCreateBuffers(1, &bufferIdOut); if (Config::ENABLE_GPU_VALIDATION) { glObjectLabel(GL_BUFFER, bufferIdOut, -1, name != nullptr ? name : Util::StringFormat("DVD_PERSISTENT_BUFFER_%d", bufferIdOut).c_str()); } assert(bufferIdOut != 0 && "GLUtil::allocPersistentBuffer error: buffer creation failed"); return allocPersistentBuffer(bufferIdOut, bufferSize, storageMask, accessMask, data); }
void rnd_util_init() { glCreateVertexArrays(1, &cube_up_vao); glCreateBuffers(1, &cube_up_vbo_verts); glNamedBufferData(cube_up_vbo_verts, sizeof(cube_up_verts), cube_up_verts, GL_STATIC_DRAW); glVertexArrayAttribBinding(cube_up_vao, 0, 0); glVertexArrayVertexBuffer(cube_up_vao, 0, cube_up_vbo_verts, 0, 12); glEnableVertexArrayAttrib(cube_up_vao, 0); glVertexArrayAttribFormat(cube_up_vao, 0, 3, GL_FLOAT, GL_FALSE, 0); /* glCreateVertexArrays(1, &test_box_vao); glCreateBuffers(1, &test_box_vbo_verts); glNamedBufferData(test_box_vbo_verts, sizeof(test_box_verts), test_box_verts, GL_STATIC_DRAW); glVertexArrayAttribBinding(test_box_vao, 0, 0); glVertexArrayVertexBuffer(test_box_vao, 0, test_box_vbo_verts, 0, 108); glEnableVertexArrayAttrib(test_box_vao, 0); glVertexArrayAttribFormat(test_box_vao, 0, 3, GL_FLOAT, GL_FALSE, 0); */ }
bool initBuffer() { GLint const Alignement = 256; GLint BufferPageSize = 0; glGetIntegerv(GL_SPARSE_BUFFER_PAGE_SIZE_ARB, &BufferPageSize); bool Validated(true); GLintptr CopyBufferSize = glm::ceilMultiple<GLint>(VertexSize, Alignement) + glm::ceilMultiple<GLint>(ElementSize, Alignement); glCreateBuffers(buffer::MAX, &BufferName[0]); glNamedBufferStorage(BufferName[buffer::COPY], CopyBufferSize, nullptr, GL_MAP_WRITE_BIT); glm::byte* CopyBufferPointer = reinterpret_cast<glm::byte*>(glMapNamedBufferRange(BufferName[buffer::COPY], 0, CopyBufferSize, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT)); memcpy(CopyBufferPointer + 0, VertexData, VertexSize); memcpy(CopyBufferPointer + glm::ceilMultiple<GLint>(VertexSize, Alignement), ElementData, ElementSize); glUnmapNamedBuffer(BufferName[buffer::COPY]); glBindBuffer(GL_COPY_READ_BUFFER, BufferName[buffer::COPY]); glBindBuffer(GL_COPY_WRITE_BUFFER, BufferName[buffer::ELEMENT]); glBufferStorage(GL_COPY_WRITE_BUFFER, glm::ceilMultiple<GLint>(ElementSize, BufferPageSize), nullptr, GL_SPARSE_STORAGE_BIT_ARB); glBufferPageCommitmentARB(GL_COPY_WRITE_BUFFER, 0, BufferPageSize, GL_TRUE); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, glm::ceilMultiple<GLint>(VertexSize, Alignement), 0, glm::ceilMultiple<GLint>(ElementSize, Alignement)); glBindBuffer(GL_COPY_WRITE_BUFFER, BufferName[buffer::VERTEX]); glBufferStorage(GL_COPY_WRITE_BUFFER, glm::ceilMultiple<GLint>(VertexSize, BufferPageSize), nullptr, GL_SPARSE_STORAGE_BIT_ARB); glBufferPageCommitmentARB(GL_COPY_WRITE_BUFFER, 0, BufferPageSize, GL_TRUE); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, glm::ceilMultiple<GLint>(VertexSize, Alignement)); GLint UniformBufferOffset(0); glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UniformBufferOffset); GLint UniformBlockSize = glm::max(GLint(sizeof(glm::mat4)), UniformBufferOffset); glNamedBufferStorage(BufferName[buffer::TRANSFORM], UniformBlockSize, nullptr, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); return Validated; }
GLuint Mesh_Setup(GLuint shader_prog, struct Vertex *verts, GLuint num_verts) { GLuint vao; glCreateVertexArrays(1, &vao); GLuint pos_attrib_loc = glGetAttribLocation(shader_prog, "pos"); GLuint col_attrib_loc = glGetAttribLocation(shader_prog, "col"); glVertexArrayAttribFormat(vao, pos_attrib_loc, 3, GL_FLOAT, GL_FALSE, (GLuint)offsetof(struct Vertex, pos)); glVertexArrayAttribFormat(vao, col_attrib_loc, 4, GL_FLOAT, GL_FALSE, (GLuint)offsetof(struct Vertex, col)); glVertexArrayAttribBinding(vao, pos_attrib_loc, 0); glVertexArrayAttribBinding(vao, col_attrib_loc, 0); glEnableVertexArrayAttrib(vao, pos_attrib_loc); glEnableVertexArrayAttrib(vao, col_attrib_loc); GLuint vbo; glCreateBuffers(1, &vbo); glNamedBufferStorage(vbo, sizeof(struct Vertex) * num_verts, verts, 0); glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(struct Vertex)); return vao; }
void GLBuffer::create() { glCreateBuffers(1, &name); }
int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_VIDEO); // OpenGL 4.0! SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_Window* window = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); SDL_GLContext context = SDL_GL_CreateContext(window); glewExperimental = GL_TRUE; glewInit(); glGetError(); // Glew tends to generate GL_INVALID_ENUM (https://www.opengl.org/wiki/OpenGL_Loading_Library) const float triangle[] = { // X Y Z 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, }; GLuint triangleBuffer; glCreateBuffers(1, &triangleBuffer); glBindBuffer(GL_ARRAY_BUFFER, triangleBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW); //glBindBuffer(GL_ARRAY_BUFFER, 0); const int indices[] = { 0, 1, 2 }; GLuint triangleIndexBuffer; glCreateBuffers(1, &triangleIndexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIndexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); const char* vertexShaderCode[] = { "#version 400\n", // This is the only line that requires a newline, all others do not need it! "in vec3 vertex;", "void main() {", " gl_Position = vec4(vertex, 1.0);", "}", }; const char* fragmentShaderCode[] = { "#version 400\n", // This is the only line that requires a newline, all others do not need it! "out vec4 colorRGBA;", "void main() {", " colorRGBA = vec4(1.0, 0.0, 0.0, 1.0);", "}", }; GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, _ARRAYSIZE(vertexShaderCode), vertexShaderCode, nullptr); glCompileShader(vertexShader); GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, _ARRAYSIZE(fragmentShaderCode), fragmentShaderCode, nullptr); glCompileShader(fragmentShader); GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); GLuint vertArray; glGenVertexArrays(1, &vertArray); glBindVertexArray(vertArray); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, triangleBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIndexBuffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); bool done = false; while (!done) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE || event.key.keysym.sym == SDLK_ESCAPE) { done = true; break; } } glClearColor(0.0f, 1.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shaderProgram); glBindVertexArray(vertArray); //glDrawArrays(GL_TRIANGLES, 0, _ARRAYSIZE(indices)); glDrawElements(GL_TRIANGLES, _ARRAYSIZE(indices), GL_UNSIGNED_INT, 0); SDL_GL_SwapWindow(window); } SDL_GL_DeleteContext(context); SDL_DestroyWindow(window); SDL_Quit(); return 0; }
void Buffer::createImplementationDSA() { glCreateBuffers(1, &_id); _flags |= ObjectFlag::Created; }
void ImageViewerPanel::initializeGL() { //glewInit(); glGetIntegerv(GL_MAJOR_VERSION, &ogl_ver_major); glGetIntegerv(GL_MINOR_VERSION, &ogl_ver_minor); shaderP = make_unique<GLSLProgram>( "resources/shaders/img_vs.glsl", "resources/shaders/img_fs.glsl"); if (ogl_ver_major == 4 && ogl_ver_minor >= 5) { // DSA // Create VAO glCreateBuffers(1, &vbo); glNamedBufferData(vbo, sizeof(frame), frame, GL_STATIC_DRAW); // IBO GLuint indices[] = { 0,1,2,2,3,0 }; glCreateBuffers(1, &ibo); glNamedBufferData(ibo, sizeof(indices), indices, GL_STATIC_DRAW); // VAO glCreateVertexArrays(1, &vao); glEnableVertexArrayAttrib(vao, 0); // Setup the formats glVertexArrayAttribFormat(vao, 0, 2, GL_FLOAT, GL_FALSE, 0); glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(float) * 2); glVertexArrayAttribBinding(vao, 0, 0); glVertexArrayElementBuffer(vao, ibo); // Setup textures int texSize = 4; glCreateTextures(GL_TEXTURE_2D, 1, &tex); glTextureParameteri(tex, GL_TEXTURE_WRAP_S, GL_REPEAT); glTextureParameteri(tex, GL_TEXTURE_WRAP_T, GL_REPEAT); glTextureParameteri(tex, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTextureParameteri(tex, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTextureStorage2D(tex, 1, GL_RGB32F, imgsize[0], imgsize[1]); if (texLen > 0) { glTextureSubImage2D(tex, 0, 0, 0, imgsize[0], imgsize[1], GL_RGB, GL_FLOAT, textures); } texHandle = glGetTextureHandleARB(tex); glMakeTextureHandleResidentARB(texHandle); } else { // Non-DSA glGenVertexArrays(1, &vao); glBindVertexArray(vao); // Bind UV values glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(frame), frame, GL_STATIC_DRAW); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexAttribArray(0); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); } }