Example #1
0
	Microsoft::WRL::ComPtr<ID3D12Resource> Renderer::UploadIndexData(void *data, long dataSize)
	{
		WaitForGpu();

		// Command list allocators can only be reset when the associated 
		// command lists have finished execution on the GPU; apps should use 
		// fences to determine GPU execution progress.
		ThrowIfFailed(_commandAllocators[_frameIndex]->Reset());

		// However, when ExecuteCommandList() is called on a particular command 
		// list, that command list can then be reset at any time and must be before 
		// re-recording.
		ThrowIfFailed(_commandList->Reset(_commandAllocators[_frameIndex].Get(), NULL));

		Microsoft::WRL::ComPtr<ID3D12Resource> indexBuffer;
		Microsoft::WRL::ComPtr<ID3D12Resource> indexBufferUpload;

		ThrowIfFailed(_device->CreateCommittedResource(
			&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
			D3D12_HEAP_FLAG_NONE,
			&CD3DX12_RESOURCE_DESC::Buffer(dataSize),
			D3D12_RESOURCE_STATE_COPY_DEST,
			nullptr,
			IID_PPV_ARGS(&indexBuffer)));

		ThrowIfFailed(_device->CreateCommittedResource(
			&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
			D3D12_HEAP_FLAG_NONE,
			&CD3DX12_RESOURCE_DESC::Buffer(dataSize),
			D3D12_RESOURCE_STATE_GENERIC_READ,
			nullptr,
			IID_PPV_ARGS(&indexBufferUpload)));

		// Copy data to the intermediate upload heap and then schedule a copy 
		// from the upload heap to the vertex buffer.
		UINT8* pIndexDataBegin;
		ThrowIfFailed(indexBufferUpload->Map(0, &CD3DX12_RANGE(0, dataSize), reinterpret_cast<void**>(&pIndexDataBegin)));
		memcpy(pIndexDataBegin, data, dataSize);
		indexBufferUpload->Unmap(0, nullptr);

		_commandList->CopyBufferRegion(indexBuffer.Get(), 0, indexBufferUpload.Get(), 0, dataSize);
		_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(indexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_INDEX_BUFFER));

		// Close the command list and execute it to begin the vertex buffer copy into
		// the default heap.
		ThrowIfFailed(_commandList->Close());
		ID3D12CommandList* ppCommandLists[] = { _commandList.Get() };
		_commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);

		WaitForGpu();

		return indexBuffer;
	}
Example #2
0
void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data)
{
    if( g_pD3DDevice == 0 )
    {
        LOGInfo( LOGTag, "Trying to buffer data before d3d is initialized!\n" );
        return;
    }

    int buffer = -1;

    if( target == GL_ARRAY_BUFFER )
        buffer = g_ActiveBufferObject_Array;
    else if( target == GL_ELEMENT_ARRAY_BUFFER )
        buffer = g_ActiveBufferObject_ElementArray;

    if( buffer == -1 )
    {
        LOGInfo( LOGTag, "glBufferSubData() buffer not found!\n" );
        return;
    }

    assert( g_D3DBufferObjects[buffer].m_InUse == true );

    if( g_D3DBufferObjects[buffer].m_Buffer == 0 )
    {
        LOGInfo( LOGTag, "glBufferSubData() buffer not allocated!\n" );
        return;
    }

    // set the buffer data:
    if( data != 0 )
    {
        D3D11_MAPPED_SUBRESOURCE mappedResource;

        // Lock the vertex buffer so it can be written to.
        HRESULT result = g_pD3DContext->Map( g_D3DBufferObjects[buffer].m_Buffer.Get(), 0,
                                             D3D11_MAP_WRITE_DISCARD, 0, &mappedResource );
        if( FAILED(result) )
            return;

        // Get a pointer to the data in the vertex buffer.
        void* verticesPtr = (void*)((int*)mappedResource.pData + offset);

        // Copy the data into the vertex buffer.
        memcpy( verticesPtr, (void*)data, size );

        // Unlock the vertex buffer.
        g_pD3DContext->Unmap( g_D3DBufferObjects[buffer].m_Buffer.Get(), 0 );
    }
}
Example #3
0
void glBufferData(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage)
{
    if( g_pD3DDevice == 0 )
    {
        LOGInfo( LOGTag, "Trying to initialize a buffer before d3d is initialized!\n" );
        return;
    }

    int buffer = -1;

    if( target == GL_ARRAY_BUFFER )
        buffer = g_ActiveBufferObject_Array;
    else if( target == GL_ELEMENT_ARRAY_BUFFER )
        buffer = g_ActiveBufferObject_ElementArray;

    if( buffer == -1 )
        return;

    assert( g_D3DBufferObjects[buffer].m_InUse == true );

    if( g_D3DBufferObjects[buffer].m_Buffer == 0 )
    {
        D3D11_BUFFER_DESC BufferDesc;

        // TODO: handle usage
        BufferDesc.Usage = D3D11_USAGE_DYNAMIC;
        BufferDesc.ByteWidth = size;
        if( target == GL_ARRAY_BUFFER )
            BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
        else if( target == GL_ELEMENT_ARRAY_BUFFER )
            BufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;    
        BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
        BufferDesc.MiscFlags = 0;
        BufferDesc.StructureByteStride = 0;

        // Now create the vertex buffer.
        HRESULT result = g_pD3DDevice->CreateBuffer( &BufferDesc, 0, &g_D3DBufferObjects[buffer].m_Buffer );
        if( FAILED(result) )
            return;
    }

    // set the buffer data:
    if( data != 0 )
    {
        D3D11_MAPPED_SUBRESOURCE mappedResource;

        // Lock the vertex buffer so it can be written to.
        HRESULT result = g_pD3DContext->Map( g_D3DBufferObjects[buffer].m_Buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource );
        if( FAILED(result) )
            return;

        // Get a pointer to the data in the vertex buffer.
        void* verticesPtr = (void*)mappedResource.pData;

        // Copy the data into the vertex buffer.
        memcpy( verticesPtr, (void*)data, size );

        // Unlock the vertex buffer.
        g_pD3DContext->Unmap( g_D3DBufferObjects[buffer].m_Buffer.Get(), 0 );
    }
}