Jump to content

  • Log In with Google      Sign In   
  • Create Account

WPF & DirectX 11 via D3DImage


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 ershovdz   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 February 2012 - 05:41 AM

I want to use DirectX 11 from unmanaged C++ code and use WFP for the GUI. SlimDX is not suitable for me. I have found the solution to make working WPF with DirectX 10:

WPF & DirectX 10 via D3DImage

But I couldn't manage to work it with DirectX 11. Just blank screen with two buttons. Does anybody know how to make WPF working with DirectX 11.

Also I had seen that when I'm just running this example the CPU usage is about 4-5% for Intel i5 750 (Windows 7 64 bit, NVidia Geforce 430). I think it's too much.. Is it possible to decrease CPU usage ?

Sponsor:

#2 CornyKorn   Members   -  Reputation: 476

Like
0Likes
Like

Posted 01 February 2012 - 09:16 AM

Can you post some of your code? I don't really have any solutions to your problem. I was able to take the sample code and mostly just change "DirectX10" to "DirectX11" and using the device context. If no one gives you a solution by the time I get home from work tonight I'll post what I have.

#3 ershovdz   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 February 2012 - 09:49 AM

I have changed D3D10Scene.h:
// D3D10Scene.h
#pragma once
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <stdio.h>
#include "d3d9.h"
#include "d3dx9.h"
#include "D3D10_1.h"
#include "d3dx10.h"
#include "d3dx11.h"
#include "D3D11.h"
using namespace System;
struct float3
{
  float x;
  float y;
  float z;
  float3() {};
  float3(float px, float py, float pz) { x=px;y=py;z=pz; }
};
struct Vertex
{
  float3 pos;
  Vertex(float x, float y, float z) {pos = float3(x,y,z);}
};
ID3D11Buffer* VertexBuffer = NULL;
ID3D11VertexShader* VS = NULL;
ID3D11PixelShader* PS = NULL;
ID3D10Blob* VS_Buffer = NULL;
ID3D10Blob* PS_Buffer = NULL;
ID3D11InputLayout* vertLayout = NULL;
D3D11_INPUT_ELEMENT_DESC layout[] =
{
  { "POSITION", 0, DXGI_FORMAT_B8G8R8A8_UNORM, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = ARRAYSIZE(layout);
IntPtr wpfHWND;
namespace D3D10Scene
{
  public ref class D3D10TestScene
  {
  public:
	D3D10TestScene()
	{
	  WIDTH = 640;
	  HEIGHT = 480;
	  count = 0;
	  InitializeDirect3d11App();
	  InitScene();
   SetViews();
	}
	bool InitializeDirect3d11App()
	{
	  HRESULT hr;
	  D3D_FEATURE_LEVEL *pFeatureLevel;
	  pin_ptr<ID3D11Device*> g_DevicePtr = &g_Device;
	  pin_ptr<ID3D11DeviceContext*> g_DeviceContextPtr = &g_DeviceContext;
	  hr = D3D11CreateDevice(NULL
		, D3D_DRIVER_TYPE_HARDWARE
		, NULL
		, NULL
		, NULL
		, NULL
		, D3D11_SDK_VERSION
		, g_DevicePtr
		, pFeatureLevel
		, g_DeviceContextPtr);
	  if(FAILED(hr))
	  {
		MessageBox(NULL, "Error creating Device", "Error", MB_OK | MB_ICONERROR);
		return false;
	  }
	  return true;
	}
	bool InitScene()
	{
	  HRESULT hr;
	  D3D11_VIEWPORT viewport;
	  ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
	  viewport.TopLeftX = 0;
	  viewport.TopLeftY = 0;
	  viewport.Width = WIDTH;
	  viewport.Height = HEIGHT;
	  g_DeviceContext->RSSetViewports(1, &viewport);
	  hr = D3DX11CompileFromFile("Effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
	  if(FAILED(hr))
	  {
		MessageBox(NULL, "Error compiling vertex shader", "Error", MB_OK | MB_ICONERROR);
		return false;
	  }
	  hr = D3DX11CompileFromFile("Effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);
	  if(FAILED(hr))
	  {
		MessageBox(NULL, "Error compiling pixel shader", "Error", MB_OK | MB_ICONERROR);
		return false;
	  }
	  hr = g_Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
	  if(FAILED(hr))
	  {
		MessageBox(NULL, "Error creating vertex shader", "Error", MB_OK | MB_ICONERROR);
		return false;
	  }
	  hr = g_Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);
	  if(FAILED(hr))
	  {
		MessageBox(NULL, "Error creating pixel shader", "Error", MB_OK | MB_ICONERROR);
		return false;
	  }
	  g_DeviceContext->VSSetShader(VS, 0, 0);
	  g_DeviceContext->PSSetShader(PS, 0, 0);
	  Vertex v[] =
	  {
		Vertex( 0.0f, 0.5f, 0.5f ),
		Vertex( 0.5f, -0.5f, 0.5f ),
		Vertex( -0.5f, -0.5f, 0.5f ),
	  };
	  D3D11_BUFFER_DESC vertexBufferDesc;
	  ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );
	  vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
	  vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 3;
	  vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	  vertexBufferDesc.CPUAccessFlags = 0;
	  vertexBufferDesc.MiscFlags = 0;
	  D3D11_SUBRESOURCE_DATA vertexBufferData;
	  ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
	  vertexBufferData.pSysMem = v;
	  hr = g_Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &VertexBuffer);
	  if(FAILED(hr))
	  {
		MessageBox(NULL, "Error creating buffer", "Error", MB_OK | MB_ICONERROR);
		return false;
	  }
	  UINT stride = sizeof( Vertex );
	  UINT offset = 0;
	  g_DeviceContext->IASetVertexBuffers( 0, 1, &VertexBuffer, &stride, &offset );
	  g_Device->CreateInputLayout( layout, numElements, VS_Buffer->GetBufferPointer(),
		VS_Buffer->GetBufferSize(), &vertLayout );
	  g_DeviceContext->IASetInputLayout( vertLayout );
	  g_DeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
	  return true;
	}
	IntPtr GetRenderTarget()
	{
	  return IntPtr(g_Texture2D11);
	}
	void Render()
	{
	  ID3D11RenderTargetView*  pRenderTargetView;
	  HRESULT hr = S_OK;
	  hr = g_Device->CreateRenderTargetView(g_Texture2D11, NULL, &pRenderTargetView);
	  g_DeviceContext->OMSetRenderTargets(1, &pRenderTargetView, NULL);
	  //g_RenderTargetView = pRenderTargetView;
	  float bgColor[4] = {0.0f, 0.0f, 0.0f, 0.0f};
	  g_DeviceContext->ClearRenderTargetView(pRenderTargetView, bgColor);
	  g_DeviceContext->Draw( 3, 0 );
	  //g_SwapChain->Present(0, 0);
	  g_DeviceContext->OMSetRenderTargets(0, 0, 0);
	  pRenderTargetView->Release();
	  g_DeviceContext->Flush();
	  //count+=10;
	}
	ID3D11Texture2D*		 g_Texture2D11;
	ID3D11Device* g_Device;
	ID3D11DeviceContext* g_DeviceContext;

	UINT WIDTH;
	UINT HEIGHT;
	int count;
  private:
	HRESULT SetViews()
	{
	  ID3D11Texture2D* pTexture = NULL;
	  D3D11_TEXTURE2D_DESC Desc;
	  Desc.Width			  = WIDTH;
	  Desc.Height			 = HEIGHT;
	  Desc.MipLevels		  = 1;
	  Desc.ArraySize		  = 1;
	  Desc.Format			 = DXGI_FORMAT_B8G8R8A8_UNORM;
	  Desc.SampleDesc.Count   = 1;
	  Desc.SampleDesc.Quality = 0;
	  Desc.Usage			  = D3D11_USAGE_DEFAULT;
	  Desc.BindFlags		  = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
	  Desc.CPUAccessFlags	 = 0;
	  Desc.MiscFlags		  = D3D11_RESOURCE_MISC_SHARED;
	  HRESULT hr = g_Device->CreateTexture2D(&Desc, NULL, &pTexture);
	  g_Texture2D11 = pTexture;
	  return S_OK;
	}
  };
}

File Effects.fx:
float4 VS(float4 inPos : POSITION) : SV_POSITION
{
	return inPos;
}
float4 PS() : SV_TARGET
{
	return float4(0.0f, 0.75f, 1.0f, 1.0f);
}


And Window1.xaml.cs file:
m_d3DImageEx.SetBackBufferEx(D3DResourceTypeEx.ID3D11Texture2D, m_d3D10Scene.GetRenderTarget());

That's all. Now I can see two buttons. But don't see triangle.

Could you please provide your version of code ?

#4 CornyKorn   Members   -  Reputation: 476

Like
0Likes
Like

Posted 02 February 2012 - 09:35 AM

Shoot, sorry, I completely misread your original post. I did mine using SlimDX. I'll grab the code you're working with and give it a shot tonight if I can remember.

#5 ershovdz   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 February 2012 - 12:57 PM

Hi,

I have found my mistake. I used wrong format for vertex buffer layout:
D3D11_INPUT_ELEMENT_DESC layout[] =
{
  { "POSITION", 0, DXGI_FORMAT_B8G8R8A8_UNORM, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

But have to use:
D3D11_INPUT_ELEMENT_DESC layout[] =
{
  { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};


#6 CornyKorn   Members   -  Reputation: 476

Like
0Likes
Like

Posted 02 February 2012 - 01:33 PM

Great! Sorry I wasn't much help.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS