bool
OsdCudaD3D11VertexBuffer::allocate(ID3D11Device *device) {

    D3D11_BUFFER_DESC hBufferDesc;
    hBufferDesc.ByteWidth           = _numElements * _numVertices * sizeof(float);
    hBufferDesc.Usage               = D3D11_USAGE_DYNAMIC;
    hBufferDesc.BindFlags           = D3D11_BIND_VERTEX_BUFFER | D3D11_BIND_SHADER_RESOURCE;
    hBufferDesc.CPUAccessFlags      = D3D11_CPU_ACCESS_WRITE;
    hBufferDesc.MiscFlags           = 0;
    hBufferDesc.StructureByteStride = sizeof(float);

    HRESULT hr;
    hr = device->CreateBuffer(&hBufferDesc, NULL, &_d3d11Buffer);
    if(FAILED(hr)) {
        OsdError(OSD_D3D11_VERTEX_BUFFER_CREATE_ERROR,
                 "Fail in CreateBuffer\n");
        return false;
    }
    
    // register d3d11buffer as cuda resource
    cudaError_t err = cudaGraphicsD3D11RegisterResource(
        &_cudaResource, _d3d11Buffer, cudaGraphicsRegisterFlagsNone);

    if (err != cudaSuccess) return false;
    return true;
}
Esempio n. 2
0
D3D11CudaTexture< TD3D11ResourceType, TCudaType >::D3D11CudaTexture( ID3D11Device*        d3d11Device,
                                                                     ID3D11DeviceContext* d3d11DeviceContext,
                                                                     D3D11_TEXTURE2D_DESC resourceDesc,
                                                                     VolumeDescription    volumeDescription ) :
    mCudaGraphicsResource   ( NULL ),
    mD3D11Resource          ( NULL ),
    mD3D11ShaderResourceView( NULL ),
    mD3D11DeviceContext     ( d3d11DeviceContext )
{
    D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
    ZeroMemory( &shaderResourceViewDesc, sizeof( D3D11_SHADER_RESOURCE_VIEW_DESC ) );

    shaderResourceViewDesc.Format              = resourceDesc.Format;
    shaderResourceViewDesc.ViewDimension       = D3D11_SRV_DIMENSION_TEXTURE2D;
    shaderResourceViewDesc.Texture2D.MipLevels = -1;

    MOJO_D3D_SAFE( d3d11Device->CreateTexture2D( &resourceDesc, NULL, &mD3D11Resource ) );
    MOJO_D3D_SAFE( d3d11Device->CreateShaderResourceView( mD3D11Resource, &shaderResourceViewDesc, &mD3D11ShaderResourceView ) );
    MOJO_CUDA_SAFE( cudaGraphicsD3D11RegisterResource( &mCudaGraphicsResource, mD3D11Resource, cudaGraphicsRegisterFlagsNone ) );

    mD3D11DeviceContext->UpdateSubresource(
        mD3D11Resource,
        0,
        NULL,
        volumeDescription.data,
        volumeDescription.numVoxels.x * volumeDescription.numBytesPerVoxel,
        volumeDescription.numVoxels.y * volumeDescription.numVoxels.x * volumeDescription.numBytesPerVoxel );
}
Esempio n. 3
0
				void TriangleMesh::bind(TriangleMeshData& data)
				{
					data.numVertices = 0;
					data.numIndices = 0;
					data.maxNumVertices = static_cast<unsigned int>(maxNumVertices);
					data.maxNumIndices = static_cast<unsigned int>(maxNumIndices);
					if (!bound)
					{
						if (!registered)
						{
							PGA_CUDA_checkedCall(cudaGraphicsD3D11RegisterResource(&cudaVertexAttributesBuffer, vertexAttributesBuffer, cudaGraphicsRegisterFlagsNone /* the only flag working for now */));
							PGA_CUDA_checkedCall(cudaGraphicsD3D11RegisterResource(&cudaIndexBuffer, indexBuffer, cudaGraphicsRegisterFlagsNone /* the only flag working for now */));
							registered = true;
						}
						PGA_CUDA_checkedCall(cudaGraphicsMapResources(1, &cudaVertexAttributesBuffer, 0));
						PGA_CUDA_checkedCall(cudaGraphicsMapResources(1, &cudaIndexBuffer, 0));
						bound = true;
					}
					size_t size;
					PGA_CUDA_checkedCall(cudaGraphicsResourceGetMappedPointer((void**)&data.verticesAttributes, &size, cudaVertexAttributesBuffer));
					PGA_CUDA_checkedCall(cudaGraphicsResourceGetMappedPointer((void**)&data.indices, &size, cudaIndexBuffer));
				}
Esempio n. 4
0
D3D11CudaTexture< TD3D11ResourceType, TCudaType >::D3D11CudaTexture( ID3D11Device*        d3d11Device,
                                                                     ID3D11DeviceContext* d3d11DeviceContext,
                                                                     D3D11_TEXTURE1D_DESC resourceDesc ) :
    mCudaGraphicsResource   ( NULL ),
    mD3D11Resource          ( NULL ),
    mD3D11ShaderResourceView( NULL ),
    mD3D11DeviceContext     ( d3d11DeviceContext )
{
    D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
    ZeroMemory( &shaderResourceViewDesc, sizeof( D3D11_SHADER_RESOURCE_VIEW_DESC ) );

    shaderResourceViewDesc.Format              = resourceDesc.Format;
    shaderResourceViewDesc.ViewDimension       = D3D11_SRV_DIMENSION_TEXTURE1D;
    shaderResourceViewDesc.Texture1D.MipLevels = -1;

    MOJO_D3D_SAFE( d3d11Device->CreateTexture1D( &resourceDesc, NULL, &mD3D11Resource ) );
    MOJO_D3D_SAFE( d3d11Device->CreateShaderResourceView( mD3D11Resource, &shaderResourceViewDesc, &mD3D11ShaderResourceView ) );
    MOJO_CUDA_SAFE( cudaGraphicsD3D11RegisterResource( &mCudaGraphicsResource, mD3D11Resource, cudaGraphicsRegisterFlagsNone ) );
}
HRESULT DX11CustomRenderTarget::OnResize( ID3D11Device* pd3dDevice, UINT width, UINT height)
{
	HRESULT hr = S_OK;

	if (width == m_Width && height == m_Height)	return hr;
	OnD3D11DestroyDevice();

	m_Width = width;
	m_Height = height;

	m_Targets = new ID3D11Texture2D*[m_uNumTargets];
	m_TargetsRTV = new ID3D11RenderTargetView*[m_uNumTargets];
	m_TargetsSRV = new ID3D11ShaderResourceView*[m_uNumTargets];
	
	//creating targets
	{
		for (UINT i = 0; i < m_uNumTargets; i++)
		{
			D3D11_TEXTURE2D_DESC textureDesc;
			ZeroMemory(&textureDesc, sizeof(D3D11_TEXTURE2D_DESC));
			textureDesc.Width = m_Width;
			textureDesc.Height = height;
			textureDesc.MipLevels = 1;
			textureDesc.ArraySize = 1;
			textureDesc.Format = m_TextureFormats[i];
			//TODO fix this for MSAA/CSAA
			textureDesc.SampleDesc.Count = 1;
			textureDesc.SampleDesc.Quality = 0;
			textureDesc.Usage = D3D11_USAGE_DEFAULT;
			textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
			textureDesc.CPUAccessFlags = 0;
			textureDesc.MiscFlags = 0;
	
			D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
			ZeroMemory(&rtvDesc, sizeof(D3D11_RENDER_TARGET_VIEW_DESC));
			rtvDesc.Format = textureDesc.Format;
			rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
			rtvDesc.Texture2D.MipSlice = 0;

			D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
			ZeroMemory(&srvDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
			srvDesc.Format = textureDesc.Format;
			srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
			srvDesc.Texture2D.MipLevels = 1;
			srvDesc.Texture2D.MostDetailedMip = 0;

			V_RETURN(pd3dDevice->CreateTexture2D(&textureDesc, NULL, &m_Targets[i]));
			V_RETURN(pd3dDevice->CreateRenderTargetView(m_Targets[i], &rtvDesc, &m_TargetsRTV[i]));
			V_RETURN(pd3dDevice->CreateShaderResourceView(m_Targets[i], &srvDesc, &m_TargetsSRV[i]));
		}
	}

	//creating depth stencil
	{
		D3D11_TEXTURE2D_DESC desc;
		ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC));
		desc.Usage = D3D11_USAGE_DEFAULT;
		desc.Format = DXGI_FORMAT_R32_TYPELESS;
		desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_DEPTH_STENCIL;
		desc.MipLevels = 1;
		desc.ArraySize = 1;
		desc.SampleDesc.Count = 1;
		desc.SampleDesc.Quality = 0;
		desc.CPUAccessFlags = 0;
		desc.Height = height;
		desc.Width = width;
		V_RETURN(pd3dDevice->CreateTexture2D(&desc, NULL, &m_DepthStencil));


		D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
		ZeroMemory(&descDSV, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
		descDSV.Format = DXGI_FORMAT_D32_FLOAT;
		if (desc.SampleDesc.Count > 1)	{
			descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
		} else {
			descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
			descDSV.Texture2D.MipSlice = 0;
		}

		V_RETURN(pd3dDevice->CreateDepthStencilView(m_DepthStencil, &descDSV, &m_DepthStencilDSV));

		D3D11_SHADER_RESOURCE_VIEW_DESC descSRV;
		ZeroMemory(&descSRV, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
		descSRV.Format = DXGI_FORMAT_R32_FLOAT;
		descSRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
		descSRV.Texture2D.MipLevels = 1;
		descSRV.Texture2D.MostDetailedMip = 0;

		V_RETURN(pd3dDevice->CreateShaderResourceView(m_DepthStencil, &descSRV, &m_DepthStencilSRV));
	}

	if (m_dCudaFloat) {
		for (UINT i = 0; i < m_uNumTargets; i++) {
			cutilSafeCall(cudaGraphicsUnregisterResource(m_dCudaFloat[i]));
		}
		SAFE_DELETE_ARRAY(m_dCudaFloat)
	}

	m_dCudaFloat = new cudaGraphicsResource*[m_uNumTargets];
	for (UINT i = 0; i < m_uNumTargets; i++) {
		cutilSafeCall(cudaGraphicsD3D11RegisterResource(&m_dCudaFloat[i], m_Targets[i], cudaGraphicsRegisterFlagsNone));
		cutilSafeCall(cudaGraphicsResourceSetMapFlags(m_dCudaFloat[i], cudaGraphicsMapFlagsReadOnly));
	}

	return hr;
}