Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualCryZe

Posted 27 November 2012 - 07:57 AM

Think of it this way (pseudo code):

void DrawInstanced(..., int vertexCount, int instanceCount, ...)
{
    for (int instanceId = 0; instanceId < instanceCount; instanceId++)
    {
        for (int vertexId = 0; vertexId < vertexCount; vertexId++)
        {
            Vertex vertex;
            for (int inputLayoutIndex = 0; inputLayoutIndex < inputLayout.getCount(); inputLayoutIndex++)
            {
                InputLayoutElement element = inputLayout.get(inputLayoutIndex);
                int vertexBufferIndex = element.getVertexBufferIndex();
                int vertexBufferOffset = element.getVertexBufferOffset();
			   
                VertexBuffer vertexBuffer = vertexBuffers[vertexBufferIndex];
                int stride = vertexBuffer.getByteStride();
	
                Object value;
                if (element.getClassification() == Classification.Instance)
                {
                    value = vertexBuffer[instanceId * stride + vertexBufferOffset];
                }
                else
                {
                    value = vertexBuffer[vertexId * stride + vertexBufferOffset];
                }

                String semantic = element.getSemantic();
                vertex.setValue(semantic, value);
            }

            VertexShader(vertex);
        }
    }
}

That means that your input layout could look like this:

[Semantic, VertexBufferIndex, VertexBufferOffset, Classification]
["VERTEX_VALUE_0", 0, 0, Vertex]
["VERTEX_VALUE_1", 0, 8, Vertex]
["VERTEX_VALUE_2", 0, 16, Vertex]
["INSTANCE_VALUE_0", 1, 0, Instance]
["INSTANCE_VALUE_1", 1, 8, Instance]

You than simply use 2 vertex buffers:

VertexBuffer 0: [[vertexValue0, vertexValue1, vertexValue2], [vertexValue0, vertexValue1, vertexValue2], [vertexValue0, vertexValue1, vertexValue2], ...]
VertexBuffer 1: [[instanceValue0, instanceValue1], [instanceValue0, instanceValue1], [instanceValue0, instanceValue1], ...]

And it basically takes a cartesian product of both sets to call the vertex shader.

#3CryZe

Posted 27 November 2012 - 07:43 AM

Think of it this way (pseudo code):

void DrawInstanced(..., int vertexCount, int instanceCount, ...)
{
    for (int instanceId = 0; instanceId < instanceCount; instanceId++)
    {
        for (int vertexId = 0; vertexId < vertexCount; vertexId++)
        {
            Vertex vertex;
            for (int inputLayoutIndex = 0; inputLayoutIndex < inputLayout.getCount(); inputLayoutIndex++)
            {
                InputLayoutElement element = inputLayout.get(inputLayoutIndex);
                int vertexBufferIndex = element.getVertexBufferIndex();
                int vertexBufferOffset = element.getVertexBufferOffset();
			   
                VertexBuffer vertexBuffer = vertexBuffers[vertexBufferIndex];
                int stride = vertexBuffer.getByteStride();
	
                Object value;
                if (element.getClassification() == Classification.Instance)
                {
                    value = vertexBuffer[instanceId * stride + vertexBufferOffset];
                }
                else
                {
                    value = vertexBuffer[vertexId * stride + vertexBufferOffset];
                }

                String semantic = element.getSemantic();
                vertex.setValue(semantic, value);
            }

            VertexShader(vertex);
        }
    }
}

#2CryZe

Posted 27 November 2012 - 07:40 AM

Think of it this way (pseudo code):

void DrawInstanced(..., int vertexCount, int instanceCount, ...)
{
    for (int instanceId = 0; instanceId < instanceCount; instanceId++)
    {
        for (int vertexId = 0; vertexId < vertexCount; vertexId++)
        {
            Vertex vertex;
            for (int inputLayoutIndex = 0; inputLayoutIndex < inputLayout.getCount(); inputLayoutIndex++)
            {
                InputLayoutElement element = inputLayout.get(inputLayoutIndex);
                int vertexBufferIndex = element.getVertexBufferIndex();
                int vertexBufferOffset = element.getVertexBufferOffset();
			   
                VertexBuffer vertexBuffer = vertexBuffers[vertexBufferIndex];
	
                Object value;
                if (element.getClassification() == Classification.Instance)
                {
                    value = vertexBuffer[instanceId * instanceSize + vertexBufferOffset];
                }
                else
                {
                    value = vertexBuffer[vertexId * vertexSize + vertexBufferOffset];
                }

                String semantic = element.getSemantic();
                vertex.setValue(semantic, value);
            }

            VertexShader(vertex);
        }
    }
}

#1CryZe

Posted 27 November 2012 - 07:37 AM

Think of it this way (pseudo code):

void DrawInstanced(..., int vertexCount, int instanceCount, ...)
{
    for (int instanceId = 0; instanceId < instanceCount; instanceId++)
    {
	    for (int vertexId = 0; vertexId < vertexCount; vertexId++)
	    {
		    Vertex vertex;
		    for (int inputLayoutIndex = 0; inputLayoutIndex < inputLayout.getCount(); inputLayoutIndex++)
		    {
			    InputLayoutElement element = inputLayout.get(inputLayoutIndex);
			    int vertexBufferIndex = element.getVertexBufferIndex();
			    int vertexBufferOffset = element.getVertexBufferOffset();
			   
			    VertexBuffer vertexBuffer = vertexBuffers[vertexBufferIndex];
	
			    Object value;
			    if (element.getClassification() == Classification.Instance)
			    {
				    value = vertexBuffer[instanceId * instanceSize + vertexBufferOffset];
			    }
			    else
			    {
				    value = vertexBuffer[vertexId * vertexSize + vertexBufferOffset];
			    }
			    String semantic = element.getSemantic();
			    vertex.setValue(semantic, value);
		    }
            VertexShader(vertex);

	    }
    }
}

PARTNERS