Beispiel #1
0
void Renderable::Render()
{
  BindBuffer(GL_ARRAY_BUFFER, __shader->vertexBuffer, _mesh->VertexData(), 3, GL_DYNAMIC_DRAW, __shader->vertex);
  BindBuffer(GL_ARRAY_BUFFER, __shader->normalBuffer, _mesh->NormalData(), 3, GL_DYNAMIC_DRAW, __shader->normal);
  BindBuffer(GL_ARRAY_BUFFER, __shader->colorBuffer, *_colorData, 4, GL_STATIC_DRAW, __shader->color);
  BindBuffer(GL_ELEMENT_ARRAY_BUFFER, __shader->indexBuffer, _mesh->IndexData(), 1, GL_STATIC_DRAW, -1);

  glDrawElements(_mesh->DrawMode(), _mesh->IndexData().size(), GL_UNSIGNED_INT, NULL);
}
Beispiel #2
0
int
rb_vertex_attrib_array
  (struct rb_vertex_array* array,
   struct rb_buffer* buffer,
   int count,
   const struct rb_buffer_attrib* attrib)
{
  intptr_t offset = 0;
  int i = 0;
  int err = 0;

  if(!array
  || !buffer
  || !attrib
  || count < 0
  || buffer->target != GL_ARRAY_BUFFER)
    goto error;

  OGL(BindVertexArray(array->name));
  OGL(BindBuffer(buffer->target, buffer->name));

  for(i=0; i < count; ++i) {

    if(attrib[i].type == RB_UNKNOWN_TYPE) {
      OGL(BindBuffer
        (buffer->target, 
         array->ctxt->state_cache.buffer_binding[buffer->binding]));
      goto error;
    }

    offset = attrib[i].offset;
    OGL(EnableVertexAttribArray(attrib[i].index));
    OGL(VertexAttribPointer
        (attrib[i].index,
         ogl3_attrib_nb_components(attrib[i].type),
         GL_FLOAT,
         GL_FALSE,
         attrib[i].stride,
         (void*)offset));
  }

  OGL(BindVertexArray(array->ctxt->state_cache.vertex_array_binding));
  OGL(BindBuffer
    (buffer->target, array->ctxt->state_cache.buffer_binding[buffer->binding]));

exit:
  return err;

error:
  err = -1;
  goto exit;
}
Beispiel #3
0
int
rb_vertex_index_array(struct rb_vertex_array* array, struct rb_buffer* buffer)
{
  if(!array || (buffer && buffer->target != GL_ELEMENT_ARRAY_BUFFER))
    return -1;

  OGL(BindVertexArray(array->name));
  OGL(BindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer ? buffer->name : 0));
  OGL(BindVertexArray(array->ctxt->state_cache.vertex_array_binding));
  OGL(BindBuffer
    (GL_ELEMENT_ARRAY_BUFFER,
     array->ctxt->state_cache.buffer_binding[RB_OGL3_BIND_INDEX_BUFFER]));

  return 0;
}
Beispiel #4
0
//-----------------------------------------------------------------------
void GraphicsContextGL4::DrawIndexedInstanced(
    std::size_t indexCount,
    std::size_t instanceCount)
{
    ApplyPipelineState();

    // Bind index buffer
    POMDOG_ASSERT(indexBuffer);
    auto indexBufferGL = dynamic_cast<IndexBufferGL4*>(indexBuffer->NativeIndexBuffer());
    POMDOG_ASSERT(indexBufferGL != nullptr);
    indexBufferGL->BindBuffer();

    // Draw
    POMDOG_ASSERT(indexCount > 0);
    POMDOG_ASSERT(indexCount <= indexBuffer->IndexCount());
    POMDOG_ASSERT(instanceCount > 0);
    POMDOG_ASSERT(instanceCount < static_cast<decltype(instanceCount)>(std::numeric_limits<GLsizei>::max()));

    glDrawElementsInstanced(
        primitiveTopology.value,
        static_cast<GLsizei>(indexCount),
        ToIndexElementType(indexBuffer->ElementSize()),
        nullptr,
        static_cast<GLsizei>(instanceCount));
    POMDOG_CHECK_ERROR_GL4("glDrawElementsInstanced");
}
Beispiel #5
0
static uint32_t setup_quad_index_buffer(const GrGLInterface* gl) {
    static const int kMaxQuads = 1;//1 << 12; // max possible: (1 << 14) - 1;
    GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
    static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
    static const int kPatternSize = 6;
    static const int kVertCount = 4;
    static const int kIndicesCount = kPatternSize * kMaxQuads;
    int size = kPatternSize * kMaxQuads * sizeof(uint16_t);

    uint16_t* data = SkNEW_ARRAY(uint16_t, kMaxQuads * kPatternSize);

    for (int i = 0; i < kMaxQuads; ++i) {
        int baseIdx = i * kPatternSize;
        uint16_t baseVert = (uint16_t)(i * kVertCount);
        for (int j = 0; j < kPatternSize; ++j) {
            data[baseIdx+j] = baseVert + kPattern[j];
        }
    }

    GrGLuint quadIBO;
    GR_GL_CALL(gl, GenBuffers(1, &quadIBO));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, quadIBO));
    GR_GL_CALL(gl, BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, data, GR_GL_STATIC_DRAW));

    SkDELETE_ARRAY(data);
    return kIndicesCount;
}
void GLCpuPosInstancedArraysBench::setupSingleVbo(const GrGLInterface* gl,
                                                  const SkMatrix* viewMatrices) {
    // Constants for our various shader programs
    Vertex vertices[kVerticesPerTri * kNumTri];
    for (uint32_t i = 0; i < kNumTri; i++) {
        Vertex* v = &vertices[i * kVerticesPerTri];
        v[0].fPositions.set(-1.0f, -1.0f);
        v[1].fPositions.set( 1.0f, -1.0f);
        v[2].fPositions.set( 1.0f,  1.0f);

        SkPoint* position = reinterpret_cast<SkPoint*>(v);
        viewMatrices[i].mapPointsWithStride(position, sizeof(Vertex), kVerticesPerTri);

        // set colors
        float color = i == kNumTri - 1 ? 1.0f : 0.0f;
        for (uint32_t j = 0; j < kVerticesPerTri; j++) {
            uint32_t offset = 0;
            v->fColors[offset++] = color; v->fColors[offset++] = 0.0f; v->fColors[offset++] = 0.0f;
            v++;
        }
    }

    GrGLuint vbo;
    // setup VBO
    GR_GL_CALL(gl, GenBuffers(1, &vbo));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, vbo));
    GR_GL_CALL(gl, EnableVertexAttribArray(0));
    GR_GL_CALL(gl, EnableVertexAttribArray(1));
    GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
                                       (GrGLvoid*)0));
    GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
                                       (GrGLvoid*)(sizeof(SkPoint))));
    GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(vertices), vertices, GR_GL_STATIC_DRAW));
    fBuffers.push_back(vbo);
}
Beispiel #7
0
int
rb_ogl3_bind_buffer
  (struct rb_context* ctxt,
   struct rb_buffer* buffer,
   enum rb_ogl3_buffer_target target)
{
  GLenum current_name = 0;
  GLenum name = 0;
  int err = 0;

  if(!ctxt || (buffer && (buffer->binding != target)))
    goto error;

  current_name = ctxt->state_cache.buffer_binding[target];
  name = buffer ? buffer->name : 0;

  if(current_name != name) {
    OGL(BindBuffer(rb_to_ogl3_buffer_target(target), name));
    ctxt->state_cache.buffer_binding[target] = name;
  }

exit:
  return err;
error:
  err = -1;
  goto exit;
}
void GLVec4ScalarBench::teardown(const GrGLInterface* gl) {
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0));
    GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
    GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
    GR_GL_CALL(gl, DeleteTextures(1, &fFboTextureId));
    GR_GL_CALL(gl, DeleteProgram(fProgram));
    GR_GL_CALL(gl, DeleteBuffers(1, &fVboId));
}
void GL3CharacterRenderer::buildHead() {
	GL(GenVertexArrays(1, &headVao));
	GL(GenBuffers(1, &headVbo));
	GL(BindVertexArray(headVao));
	GL(BindBuffer(GL_ARRAY_BUFFER, headVbo));
	GL(VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 40, 0));
	GL(VertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 40, (void *) 12));
	GL(VertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 40, (void *) 24));
	GL(EnableVertexAttribArray(0));
	GL(EnableVertexAttribArray(1));
	GL(EnableVertexAttribArray(2));
	GL(BindVertexArray(0));

	VertexData vertexData[36];
	int vertexIndices[6] = {0, 1, 2, 0, 2, 3};
	int index = 0;
	for (int d = 0; d < 6; d++) {
		vec3f normal(0.0);
		normal[d % 3] = DIRS[d][d % 3];
		vec3f vertices[4];
		for (int i = 0; i < 4; i++) {
			vertices[i] = vec3f(
				(DIR_QUAD_CORNER_CYCLES_3D[d][i][0] - 0.5f) * HEAD_SIZE[0],
				(DIR_QUAD_CORNER_CYCLES_3D[d][i][1] - 0.5f) * HEAD_SIZE[1],
				(DIR_QUAD_CORNER_CYCLES_3D[d][i][2] - 0.5f) * HEAD_SIZE[2] + HEAD_Z_OFFSET
			) * (1.0f / RESOLUTION);
		}
		for (int j = 0; j < 6; j++) {
			vertexData[index].xyz[0] = vertices[vertexIndices[j]][0];
			vertexData[index].xyz[1] = vertices[vertexIndices[j]][1];
			vertexData[index].xyz[2] = vertices[vertexIndices[j]][2];
			vertexData[index].nxyz[0] = normal[0];
			vertexData[index].nxyz[1] = normal[1];
			vertexData[index].nxyz[2] = normal[2];
			vertexData[index].rgba[0] = CHARACTER_COLOR[0];
			vertexData[index].rgba[1] = CHARACTER_COLOR[1];
			vertexData[index].rgba[2] = CHARACTER_COLOR[2];
			vertexData[index].rgba[3] = 1.0f;
			index++;
		}
	}

	GL(BufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW));
	GL(BindBuffer(GL_ARRAY_BUFFER, 0));
}
Beispiel #10
0
static void SetAttributeFormat( const VertexAttribute* pAttr, unsigned int numAttr, unsigned int v_offset)
{
	GLenum type = GL_FLOAT;		
	unsigned int currentVBO = GetCurrentBuffer(GL_ARRAY_BUFFER_BINDING);
	unsigned int i;

	TransferAndDraw();

	if(vtx.n_vertices != 0){
		printf("ermergerd");
	}

	BindVAO();

	for(i=0;i<10;i++)glDisableVertexAttribArray(i);

	for(i=0; i<numAttr; i++)
	{
		if(pAttr[i].vbo != 0)
			BindBuffer(GL_ARRAY_BUFFER, pAttr[i].vbo);

		glEnableVertexAttribArray(pAttr[i].idx);
		glVertexAttribDivisor(pAttr[i].idx, pAttr[i].divisor);
		
		switch(pAttr[i].type)
		{
		default:
		case STREAM_FLOAT:			
		case STREAM_UCHAR:
			glVertexAttribPointer(pAttr[i].idx, pAttr[i].size, enumToGLAttribType[ pAttr[i].type ], pAttr[i].normalized, pAttr[i].stride, BUFFER_OFFSET(v_offset + pAttr[i].offset));
			break;		
		case STREAM_CHAR:		
		case STREAM_SHORT:
		case STREAM_INT:
			glVertexAttribIPointer(pAttr[i].idx, pAttr[i].size, enumToGLAttribType[ pAttr[i].type ], pAttr[i].stride, BUFFER_OFFSET(v_offset + pAttr[i].offset));
			break;
		}
		if(pAttr[i].vbo != 0)
			BindBuffer(GL_ARRAY_BUFFER, currentVBO);
	}
	UnbindVAO();
}
Beispiel #11
0
void GLCpuPosInstancedArraysBench::teardown(const GrGLInterface* gl) {
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0));
    GR_GL_CALL(gl, BindVertexArray(0));
    GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
    GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
    GR_GL_CALL(gl, DeleteTextures(1, &fTexture));
    GR_GL_CALL(gl, DeleteProgram(fProgram));
    GR_GL_CALL(gl, DeleteBuffers(fBuffers.count(), fBuffers.begin()));
    GR_GL_CALL(gl, DeleteVertexArrays(1, &fVAO));
    fBuffers.reset();
}
Beispiel #12
0
void GLCpuPosInstancedArraysBench::setupInstanceVbo(const GrGLInterface* gl,
                                                    const SkMatrix* viewMatrices) {
    // We draw all of the instances at a single place because we aren't allowed to have per vertex
    // per instance attributes
    SkPoint positions[kVerticesPerTri];
    positions[0].set(-1.0f, -1.0f);
    positions[1].set( 1.0f, -1.0f);
    positions[2].set( 1.0f,  1.0f);
    viewMatrices[0].mapPointsWithStride(positions, sizeof(SkPoint), kVerticesPerTri);

    // setup colors so we can detect we are actually drawing instances(the last triangle will be
    // a different color)
    GrGLfloat colors[kVerticesPerTri * kNumTri];
    for (uint32_t i = 0; i < kNumTri; i++) {
        // set colors
        uint32_t offset = i * kVerticesPerTri;
        float color = i == kNumTri - 1 ? 1.0f : 0.0f;
        colors[offset++] = color; colors[offset++] = 0.0f; colors[offset++] = 0.0f;
    }

    GrGLuint posVBO;
    // setup position VBO
    GR_GL_CALL(gl, GenBuffers(1, &posVBO));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, posVBO));
    GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(positions), positions, GR_GL_STATIC_DRAW));
    GR_GL_CALL(gl, EnableVertexAttribArray(0));
    GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 2 * sizeof(GrGLfloat),
                                       (GrGLvoid*)0));

    // setup color VBO
    GrGLuint instanceVBO;
    GR_GL_CALL(gl, GenBuffers(1, &instanceVBO));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, instanceVBO));
    GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(colors), colors, GR_GL_STATIC_DRAW));
    GR_GL_CALL(gl, EnableVertexAttribArray(1));
    GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 3 * sizeof(GrGLfloat),
                                       (GrGLvoid*)0));
    GR_GL_CALL(gl, VertexAttribDivisor(1, 1));
    fBuffers.push_back(posVBO);
    fBuffers.push_back(instanceVBO);
}
GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(const GrGLIndexBuffer* buffer) {
    GrGLAttribArrayState* state = this->bind();
    if (NULL != state && NULL != buffer) {
        GrGLuint bufferID = buffer->bufferID();
        if (!fIndexBufferIDIsValid || bufferID != fIndexBufferID) {
            GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, bufferID));
            fIndexBufferIDIsValid = true;
            fIndexBufferID = bufferID;
        }
    }
    return state;
}
Beispiel #14
0
void GLCpuPosInstancedArraysBench::setupDoubleVbo(const GrGLInterface* gl,
                                                  const SkMatrix* viewMatrices) {
    // Constants for our various shader programs
    SkPoint positions[kVerticesPerTri * kNumTri];
    GrGLfloat colors[kVerticesPerTri * kNumTri * 3];
    for (uint32_t i = 0; i < kNumTri; i++) {
        SkPoint* position = &positions[i * kVerticesPerTri];
        position[0].set(-1.0f, -1.0f);
        position[1].set( 1.0f, -1.0f);
        position[2].set( 1.0f,  1.0f);
        viewMatrices[i].mapPointsWithStride(position, sizeof(SkPoint), kVerticesPerTri);

        // set colors
        float color = i == kNumTri - 1 ? 1.0f : 0.0f;
        uint32_t offset = i * kVerticesPerTri * 3;
        for (uint32_t j = 0; j < kVerticesPerTri; j++) {
            colors[offset++] = color; colors[offset++] = 0.0f; colors[offset++] = 0.0f;
        }
    }

    GrGLuint posVBO, colorVBO;
    // setup position VBO
    GR_GL_CALL(gl, GenBuffers(1, &posVBO));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, posVBO));
    GR_GL_CALL(gl, EnableVertexAttribArray(0));
    GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 2 * sizeof(GrGLfloat),
                                       (GrGLvoid*)0));
    GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(positions), positions, GR_GL_STATIC_DRAW));

    // setup color VBO
    GR_GL_CALL(gl, GenBuffers(1, &colorVBO));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, colorVBO));
    GR_GL_CALL(gl, EnableVertexAttribArray(1));
    GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 3 * sizeof(GrGLfloat),
                                       (GrGLvoid*)0));
    GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(colors), colors, GR_GL_STATIC_DRAW));

    fBuffers.push_back(posVBO);
    fBuffers.push_back(colorVBO);
}
Beispiel #15
0
/*******************************************************************************
 *
 * Private functions.
 *
 ******************************************************************************/
int
rb_ogl3_create_buffer
  (struct rb_context* ctxt,
   const struct rb_ogl3_buffer_desc* desc,
   const void* init_data,
   struct rb_buffer** out_buffer)
{
  struct rb_buffer* buffer = NULL;

  if(!ctxt
  || !desc
  || !out_buffer
  || (desc->target == RB_OGL3_NB_BUFFER_TARGETS)
  || (desc->usage == RB_USAGE_IMMUTABLE && init_data == NULL))
    return -1;

  buffer = MEM_ALLOC(ctxt->allocator, sizeof(struct rb_buffer));
  if(!buffer)
    return -1;
  ref_init(&buffer->ref);
  RB(context_ref_get(ctxt));
  buffer->ctxt = ctxt;

  buffer->target = rb_to_ogl3_buffer_target(desc->target);
  buffer->usage = rb_to_ogl3_usage(desc->usage);
  buffer->size = (GLsizei)desc->size;
  buffer->binding = desc->target;

  OGL(GenBuffers(1, &buffer->name));
  OGL(BindBuffer(buffer->target, buffer->name));
  OGL(BufferData(buffer->target, buffer->size, init_data, buffer->usage));
  OGL(BindBuffer
    (buffer->target, 
     ctxt->state_cache.buffer_binding[buffer->binding]));

  *out_buffer = buffer;
  return 0;
}
Beispiel #16
0
	void Model::Draw(const Matrix4f& transform) const
	{
		if (_vbuffer == 0)
			return;

		auto video = _context->GetModule<Video>();

		if (!video->BindBuffer(_vbuffer, Vertex3::DECLARE.size, Vertex3::DECLARE.types, Vertex3::DECLARE.count))
			return;

		glPushMatrix();
		glMultMatrixf(transform.GetData());

		video->BindTexture(_texture);
		if (_ibuffer && video->BindBuffer(_ibuffer, 0, 0, 0))
		{
			video->DrawIndexedPrimitives(_topology, _icount, VALUE_USHORT);
			video->UnbindBuffer(_ibuffer, 0, 0);
		}
		else video->DrawPrimitives(_topology, _vcount, 0);
		glPopMatrix();

		video->UnbindBuffer(_vbuffer, Vertex3::DECLARE.types, Vertex3::DECLARE.count);
	}
Beispiel #17
0
//ray tracing
void CPURayTracer::RayTrace()
{
	//reset the timer
	m_Timer.Reset();
	//start the timer
	m_Timer.Start();

	//bind the buffer
	BindBuffer();

	for( int j = m_iImageHeight - 1 ; j > -1 ; j-- )
	{
		for( int i = 0 ; i < m_iImageWidth; i++ )
		{
			//current index
			int	currentIndex = j * m_iImageWidth + i;

			//current ray
			_float4 dir , ori;

			//generate a ray for current pixel
			GenerateRay( i , j , &ori , &dir );

			//copy the image
			m_pImageBackBuffer[ currentIndex ] = Trace( ori , dir );

			//copy the image
			m_pImageBuffer[ currentIndex ] = RGB_FLOAT4( m_pImageBackBuffer[ currentIndex ] );

			//update current pixel number
			m_iCurrentPixelNum++;

			if( m_bForceStop )
				break;
		}

		if( m_bForceStop )
			break;
	}

	//clear the noise
	ClearNoise( m_pImageBackBuffer );

	//end the timer
	m_Timer.Stop();
	m_ElapsedTime = (int)m_Timer.GetElapsedTime();
}
Beispiel #18
0
static void
release_buffer(struct ref* ref)
{
  struct rb_buffer* buffer = NULL;
  struct rb_context* ctxt = NULL;
  ASSERT(ref);

  buffer = CONTAINER_OF(ref, struct rb_buffer, ref);
  ctxt = buffer->ctxt;

  if(buffer->name == ctxt->state_cache.buffer_binding[buffer->binding])
    OGL(BindBuffer(buffer->target, 0));
 
  OGL(DeleteBuffers(1, &buffer->name));
  MEM_FREE(ctxt->allocator, buffer);
  RB(context_ref_put(ctxt));
}
CMemoryBuffer CHardwareTextureBuffer::LockSource(size_t offset, size_t length, THardwareBufferLock opt)
{
	nova::byte *mapper = NULL;
	CMemoryBuffer result;

	if((offset + length) > mSize)
		throw NOVA_EXP("HardwareVertexBuffer::LockSource(): Calling length and size very large..", BAD_OPERATION);

	BindBuffer();


	GLenum method;
	if(mUsage == GL_PIXEL_UNPACK_BUFFER_ARB)
	{
	// Note that glMapBufferARB() causes sync issue.
	// If GPU is working with this buffer, glMapBufferARB() will wait(stall)
	// until GPU to finish its job. To avoid waiting (idle), you can call
	// first glBufferDataARB() with NULL pointer before glMapBufferARB().
	// If you do that, the previous data in PBO will be discarded and
	// glMapBufferARB() returns a new allocated pointer immediately
	// even if GPU is still working with the previous data.
		glBufferDataARB(mUsage, mSize, 0, GL_STREAM_DRAW_ARB);
		method = GL_WRITE_ONLY_ARB;
	}
	else
		method = GL_READ_ONLY_ARB;

	
	if((mapper = static_cast<nova::byte *>(glMapBufferARB(mUsage, method))) != NULL)
	{
		mapper += offset;
		CMemoryBuffer res(mapper, length);

		result = res;
	}
	else
	{
		UnbindBuffer();
		throw NOVA_EXP("HardwareVertexBuffer::LockSource(): Can not map v-buffer memory..", BAD_OPERATION);
	}

	//UnbindBuffer();

	return result;
}
//---------------------------------------------------------------------------------------------------------
handle IndicesBufferResourceGL::_DoCreateBuffer( uint datasize, uint typesize, const void* dataptr, BufferObjectStatus::MemoryUseage use )
{
    handle h;
    glGenBuffers(1, &h);
    CHECK_ERROR_RENDER;
    BindBuffer();
    switch( use )
    {
    case BufferObjectStatus::MU_STATIC:
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, datasize*typesize, dataptr, GL_STATIC_DRAW);
        break;
    case BufferObjectStatus::MU_DYNAMIC:
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, datasize*typesize, dataptr, GL_DYNAMIC_DRAW);
        break;
    }
    CHECK_ERROR_RENDER;
    return h;
}
Beispiel #21
0
void GLInstancedArraysBench::onPerCanvasPostDraw(SkCanvas* canvas) {
    // This bench exclusively tests GL calls directly
    const GrGLContext* ctx = get_gl_context(canvas);
    if (!ctx) {
        return;
    }

    const GrGLInterface* gl = ctx->interface();

    // teardown
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0));
    GR_GL_CALL(gl, BindVertexArray(0));
    GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
    GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
    GR_GL_CALL(gl, DeleteTextures(1, &fTexture));

    this->teardown(gl);
}
Beispiel #22
0
Buffer::Buffer(GraphicsDevice* graphicsDevice,BufferType bufferType, BufferUsage bufferUsage, 
			   unsigned int elementSize, unsigned int numberOfElements, 
			   GLvoid* bufferData): _graphicsDevice(graphicsDevice),
			   _bufferType(bufferType), _bufferUsage(bufferUsage),
			   _buffer(0), _stride(0), 
			   _elementSize(elementSize), _numberOfElements(numberOfElements)
{
	//Generate the buffer
	_graphicsDevice->GenerateBuffers(1, &_buffer);
    
	//Bind the buffer to the context
	BindBuffer();

	//Set the buffer data
	_graphicsDevice->SetBufferData(_bufferType, (_elementSize * _numberOfElements), bufferData, _bufferUsage);

	//Unbind the buffer
	UnbindBuffer();
}
void GLVec4ScalarBench::setupSingleVbo(const GrGLInterface* gl, const SkMatrix* viewMatrices) {
    // triangles drawn will alternate between the top-right half of the screen and the bottom-left
    // half of the screen
    Vertex vertices[kVerticesPerTri * kNumTriPerDraw];
    for (uint32_t i = 0; i < kNumTriPerDraw; i++) {
        Vertex* v = &vertices[i * kVerticesPerTri];
        if (i % 2 == 0) {
            v[0].fPositions.set(-1.0f, -1.0f);
            v[1].fPositions.set( 1.0f, -1.0f);
            v[2].fPositions.set( 1.0f,  1.0f);
        } else {
            v[0].fPositions.set(-1.0f, -1.0f);
            v[1].fPositions.set( 1.0f, 1.0f);
            v[2].fPositions.set( -1.0f, 1.0f);
        }
        SkPoint* position = reinterpret_cast<SkPoint*>(v);
        viewMatrices[i].mapPointsWithStride(position, sizeof(Vertex), kVerticesPerTri);

        GrGLfloat color[3] = {1.0f, 0.0f, 1.0f};
        for (uint32_t j = 0; j < kVerticesPerTri; j++) {
            v->fColors[0] = color[0];
            v->fColors[1] = color[1];
            v->fColors[2] = color[2];
            v++;
        }
    }

    GR_GL_CALL(gl, GenBuffers(1, &fVboId));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, fVboId));
    GR_GL_CALL(gl, EnableVertexAttribArray(0));
    GR_GL_CALL(gl, EnableVertexAttribArray(1));
    GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
                                       (GrGLvoid*)0));
    GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
                                       (GrGLvoid*)(sizeof(SkPoint))));
    GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(vertices), vertices, GR_GL_STATIC_DRAW));
}
////////////// Index buffer implementation ////////////////////////////////////
CHardwareTextureBuffer::CHardwareTextureBuffer(size_t size, THardwareBufferLock opt)
{
	if(mBufID > 0)
	{
		if(!CRenderSystem::GetSingelton().CheckCapabilities(BASIC_CATEGORY, CAP_PBO))
			throw NOVA_EXP("CHardwareTextureBuffer: You card do not support pbo extention..", BAD_OPERATION);

		if(size == 0)
			throw NOVA_EXP("CHardwareTextureBuffer(): You trying to create v-buffer with zero length..", BAD_OPERATION);

		BindBuffer();

		GLuint usage;
		switch(opt)
		{
			case HWB_DYNAMIC:
				usage = GL_STREAM_READ_ARB;
				mUsage = GL_PIXEL_PACK_BUFFER_ARB;
				break;
			default:
				usage = GL_STREAM_DRAW_ARB;
				mUsage = GL_PIXEL_UNPACK_BUFFER_ARB;
				break;
		}

		// Preparing pixel buffer
		glBufferDataARB(mUsage, size, 0, usage);

		mReady = true;
		mSize = size;

		UnbindBuffer();
	}
	else
		throw NOVA_EXP("CHardwareTextureBuffer(): v-buffer id not created..", BAD_OPERATION);
}
Beispiel #25
0
void GLGpuPosInstancedArraysBench::setup(const GrGLInterface* gl) {
    setup_framebuffer(gl, kScreenWidth, kScreenHeight);

    // compile and use shaders
    GrGLint shaderProgram = compile_shader(gl, gpu_vertex_shader, fragment_shader);

    // translations
    int index = 0;
    GrGLfloat viewMatrices[fNumQuads * fSkMatrixNumCells];
    setup_matrices(fNumQuads, [&index, &viewMatrices](const SkMatrix& m) {
        GrGLGetMatrix<3>(&viewMatrices[index], m);
        index += fSkMatrixNumCells;
    });

    // Constants for our various shader programs
    GrGLfloat quad_vertices[] = {
            // Positions // Colors
            -1.0f,  1.0f,  1.0f, 0.0f, 0.0f,
             1.0f, -1.0f,  0.0f, 1.0f, 0.0f,
            -1.0f, -1.0f,  0.0f, 0.0f, 1.0f,

            -1.0f,  1.0f,  1.0f, 0.0f, 0.0f,
             1.0f, -1.0f,  0.0f, 1.0f, 0.0f,
             1.0f,  1.0f,  0.0f, 1.0f, 1.0f
    };

    // update vertex data
    GrGLuint quadVAO, quadVBO;
    GR_GL_CALL(gl, GenVertexArrays(1, &quadVAO));
    GR_GL_CALL(gl, GenBuffers(1, &quadVBO));
    GR_GL_CALL(gl, BindVertexArray(quadVAO));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, quadVBO));
    GR_GL_CALL(gl, EnableVertexAttribArray(0));
    GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 5 * sizeof(GrGLfloat), (GrGLvoid*)0));
    GR_GL_CALL(gl, EnableVertexAttribArray(1));
    GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 5 * sizeof(GrGLfloat), (GrGLvoid*)(2 * sizeof(GrGLfloat))));
    GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GR_GL_STATIC_DRAW));

    // Also set instance data
    GrGLuint instanceVBO;
    GR_GL_CALL(gl, GenBuffers(1, &instanceVBO));
    GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, instanceVBO));
    GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(GrGLfloat) * fSkMatrixNumCells * fNumQuads,
                              &viewMatrices[0], GR_GL_STATIC_DRAW));
    GR_GL_CALL(gl, EnableVertexAttribArray(2));
    GR_GL_CALL(gl, EnableVertexAttribArray(3));
    GR_GL_CALL(gl, EnableVertexAttribArray(4));
    GR_GL_CALL(gl, VertexAttribPointer(2, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeof(GrGLfloat), (GrGLvoid*)0));
    GR_GL_CALL(gl, VertexAttribPointer(3, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeof(GrGLfloat), (GrGLvoid*)(3 * sizeof(GrGLfloat))));
    GR_GL_CALL(gl, VertexAttribPointer(4, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeof(GrGLfloat), (GrGLvoid*)(6 * sizeof(GrGLfloat))));
    GR_GL_CALL(gl, VertexAttribDivisor(2, 1));
    GR_GL_CALL(gl, VertexAttribDivisor(3, 1));
    GR_GL_CALL(gl, VertexAttribDivisor(4, 1));

    // draw
    GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f));
    GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT));

    // set us up to draw
    GR_GL_CALL(gl, UseProgram(shaderProgram));
    GR_GL_CALL(gl, BindVertexArray(quadVAO));
}
void GLInstancedRendering::onBeginFlush(GrResourceProvider* rp) {
    // Count what there is to draw.
    BatchList::Iter iter;
    iter.init(this->trackedBatches(), BatchList::Iter::kHead_IterStart);
    int numGLInstances = 0;
    int numGLDrawCmds = 0;
    while (Batch* b = iter.get()) {
        GLBatch* batch = static_cast<GLBatch*>(b);
        iter.next();

        numGLInstances += batch->fNumDraws;
        numGLDrawCmds += batch->numGLCommands();
    }
    if (!numGLDrawCmds) {
        return;
    }
    SkASSERT(numGLInstances);

    // Lazily create a vertex array object.
    if (!fVertexArrayID) {
        GL_CALL(GenVertexArrays(1, &fVertexArrayID));
        if (!fVertexArrayID) {
            return;
        }
        this->glGpu()->bindVertexArray(fVertexArrayID);

        // Attach our index buffer to the vertex array.
        SkASSERT(!this->indexBuffer()->isCPUBacked());
        GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER,
                           static_cast<const GrGLBuffer*>(this->indexBuffer())->bufferID()));

        // Set up the non-instanced attribs.
        this->glGpu()->bindBuffer(kVertex_GrBufferType, this->vertexBuffer());
        GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeCoords));
        GL_CALL(VertexAttribPointer((int)Attrib::kShapeCoords, 2, GR_GL_FLOAT, GR_GL_FALSE,
                                    sizeof(ShapeVertex), (void*) offsetof(ShapeVertex, fX)));
        GL_CALL(EnableVertexAttribArray((int)Attrib::kVertexAttrs));
        GL_CALL(VertexAttribIPointer((int)Attrib::kVertexAttrs, 1, GR_GL_INT, sizeof(ShapeVertex),
                                     (void*) offsetof(ShapeVertex, fAttrs)));

        SkASSERT(SK_InvalidUniqueID == fInstanceAttribsBufferUniqueId);
    }

    // Create and map instance and draw-indirect buffers.
    SkASSERT(!fInstanceBuffer);
    fInstanceBuffer.reset(
        rp->createBuffer(sizeof(Instance) * numGLInstances, kVertex_GrBufferType,
                         kDynamic_GrAccessPattern,
                         GrResourceProvider::kNoPendingIO_Flag |
                         GrResourceProvider::kRequireGpuMemory_Flag));
    if (!fInstanceBuffer) {
        return;
    }

    SkASSERT(!fDrawIndirectBuffer);
    fDrawIndirectBuffer.reset(
        rp->createBuffer(sizeof(GrGLDrawElementsIndirectCommand) * numGLDrawCmds,
                         kDrawIndirect_GrBufferType, kDynamic_GrAccessPattern,
                         GrResourceProvider::kNoPendingIO_Flag |
                         GrResourceProvider::kRequireGpuMemory_Flag));
    if (!fDrawIndirectBuffer) {
        return;
    }

    Instance* glMappedInstances = static_cast<Instance*>(fInstanceBuffer->map());
    int glInstancesIdx = 0;

    auto* glMappedCmds = static_cast<GrGLDrawElementsIndirectCommand*>(fDrawIndirectBuffer->map());
    int glDrawCmdsIdx = 0;

    bool baseInstanceSupport = this->glGpu()->glCaps().baseInstanceSupport();

    if (GR_GL_LOG_INSTANCED_BATCHES || !baseInstanceSupport) {
        fGLDrawCmdsInfo.reset(numGLDrawCmds);
    }

    // Generate the instance and draw-indirect buffer contents based on the tracked batches.
    iter.init(this->trackedBatches(), BatchList::Iter::kHead_IterStart);
    while (Batch* b = iter.get()) {
        GLBatch* batch = static_cast<GLBatch*>(b);
        iter.next();

        batch->fEmulatedBaseInstance = baseInstanceSupport ? 0 : glInstancesIdx;
        batch->fGLDrawCmdsIdx = glDrawCmdsIdx;

        const Batch::Draw* draw = batch->fHeadDraw;
        SkASSERT(draw);
        do {
            int instanceCount = 0;
            IndexRange geometry = draw->fGeometry;
            SkASSERT(!geometry.isEmpty());

            do {
                glMappedInstances[glInstancesIdx + instanceCount++] = draw->fInstance;
                draw = draw->fNext;
            } while (draw && draw->fGeometry == geometry);

            GrGLDrawElementsIndirectCommand& glCmd = glMappedCmds[glDrawCmdsIdx];
            glCmd.fCount = geometry.fCount;
            glCmd.fInstanceCount = instanceCount;
            glCmd.fFirstIndex = geometry.fStart;
            glCmd.fBaseVertex = 0;
            glCmd.fBaseInstance = baseInstanceSupport ? glInstancesIdx : 0;

            if (GR_GL_LOG_INSTANCED_BATCHES || !baseInstanceSupport) {
                fGLDrawCmdsInfo[glDrawCmdsIdx].fInstanceCount = instanceCount;
#if GR_GL_LOG_INSTANCED_BATCHES
                fGLDrawCmdsInfo[glDrawCmdsIdx].fGeometry = geometry;
#endif
            }

            glInstancesIdx += instanceCount;
            ++glDrawCmdsIdx;
        } while (draw);
    }

    SkASSERT(glDrawCmdsIdx == numGLDrawCmds);
    fDrawIndirectBuffer->unmap();

    SkASSERT(glInstancesIdx == numGLInstances);
    fInstanceBuffer->unmap();
}
void
vsDisplayList::AppendOp(vsDisplayList::op * o)
{
	OpCode type = o->type;

	switch( type )
	{
		case OpCode_SetColor:
			SetColor( o->data.GetColor() );
			break;
		case OpCode_SetColors:
			SetColors( (vsColor*)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_SetColorsBuffer:
			SetColorsBuffer( (vsRenderBuffer*)o->data.p );
			break;
		case OpCode_VertexArray:
			VertexArray( (vsVector3D *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_NormalArray:
			NormalArray( (vsVector3D *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_TexelArray:
			TexelArray( (vsVector2D *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_ColorArray:
			ColorArray( (vsColor *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_VertexBuffer:
			VertexBuffer( (vsRenderBuffer *)o->data.p );
			break;
		case OpCode_TexelBuffer:
			TexelBuffer( (vsRenderBuffer *)o->data.p );
			break;
		case OpCode_ColorBuffer:
			ColorBuffer( (vsRenderBuffer *)o->data.p );
			break;
		case OpCode_BindBuffer:
			BindBuffer( (vsRenderBuffer *)o->data.p );
			break;
		case OpCode_UnbindBuffer:
			UnbindBuffer( (vsRenderBuffer *)o->data.p );
			break;
		case OpCode_ClearVertexArray:
			ClearVertexArray();
			break;
		case OpCode_ClearNormalArray:
			ClearNormalArray();
			break;
		case OpCode_ClearTexelArray:
			ClearTexelArray();
			break;
		case OpCode_ClearColorArray:
			ClearColorArray();
			break;
		case OpCode_ClearArrays:
			ClearArrays();
			break;
		case OpCode_LineListArray:
			LineListArray( (int *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_LineStripArray:
			LineStripArray( (int *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_TriangleListArray:
			TriangleListArray( (int *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_TriangleStripArray:
			TriangleStripArray( (int *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_PointsArray:
			PointsArray( (int *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_LineListBuffer:
			LineListBuffer( (vsRenderBuffer *)o->data.p );
			break;
		case OpCode_LineStripBuffer:
			LineStripBuffer( (vsRenderBuffer *)o->data.p );
			break;
		case OpCode_TriangleStripBuffer:
			TriangleStripBuffer( (vsRenderBuffer *)o->data.p );
			break;
		case OpCode_TriangleFanArray:
			TriangleFanArray( (int *)o->data.p, o->data.GetUInt() );
			break;
		case OpCode_PushTransform:
			PushTransform( o->data.GetTransform() );
			break;
		case OpCode_SetCameraTransform:
			SetCameraTransform( o->data.GetTransform() );
			break;
		case OpCode_SetMatrix4x4:
			SetMatrix4x4( o->data.GetMatrix4x4() );
			break;
		case OpCode_PushMatrix4x4:
			PushMatrix4x4( o->data.GetMatrix4x4() );
			break;
		case OpCode_SetMatrices4x4:
			SetMatrices4x4( (vsMatrix4x4*)o->data.p, o->data.i );
			break;
		case OpCode_SetMatrices4x4Buffer:
			SetMatrices4x4Buffer( (vsRenderBuffer*)o->data.p );
			break;
		case OpCode_SetWorldToViewMatrix4x4:
			SetWorldToViewMatrix4x4( o->data.GetMatrix4x4() );
			break;
		case OpCode_Set3DProjection:
			Set3DProjection( o->data.fov, o->data.nearPlane, o->data.farPlane );
			break;
		case OpCode_SetProjectionMatrix4x4:
			SetProjectionMatrix4x4( o->data.GetMatrix4x4() );
			break;
		case OpCode_PopTransform:
			PopTransform();
			break;
		case OpCode_EnableStencil:
			EnableStencil();
			break;
		case OpCode_DisableStencil:
			DisableStencil();
			break;
		case OpCode_EnableScissor:
			EnableScissor( o->data.GetBox2D() );
			break;
		case OpCode_DisableScissor:
			DisableScissor();
			break;
		case OpCode_ClearStencil:
			ClearStencil();
			break;
		case OpCode_SetViewport:
			SetViewport( o->data.GetBox2D() );
			break;
		case OpCode_ClearViewport:
			ClearViewport();
			break;
		case OpCode_Debug:
			Debug( o->data.GetString() );
			break;
		default:
			break;
	}
}
//---------------------------------------------------------------------------------------------------------
void IndicesBufferResourceGL::_DoChangeData( handle h, uint offset, uint datasize, uint typesize, const void* dataptr )
{
    BindBuffer();
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, datasize*typesize, dataptr);
    CHECK_ERROR_RENDER;
}
void GrGLIndexBuffer::bind() const {
    GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID));
    GPUGL->notifyIndexBufferBind(this);
}
void GrGLVertexBuffer::bind() const {
    GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, fBufferID));
    GPUGL->notifyVertexBufferBind(this);
}