2015-11-17 39 views
2

我最近启动了DirectX。我的主要学习来源是Frank D. Luna所着的“DirectX 11的3D游戏编程”。 在输入集成阶段,在渲染管线,它具有以下几行代码:DirectX输入汇编器阶段问题

ID3DX11Effect* mFX; 
ID3DX11EffectTechnique* mTech; 
/* ...create the effect... */ 
mTech = mFX->GetTechniqueByName("Tech"); 
D3DX11_PASS_DESC passDesc; 
mTech->GetPassByIndex(0)->GetDesc(&passDesc); 
HR(md3dDevice->CreateInputLayout(vertexDesc, 
4, 
passDesc. pIAInputSignature, 
passDesc.IAInputSignatureSize, 
&mInputLayout)); 

我的代码是这样的:

// include the basic windows header files and the Direct3D header files 

#include <windows.h> 
#include <windowsx.h> 
#include <d3d11.h> 
#include <d3dx11.h> 
#include <d3dx10.h> 
#include <xnamath.h> 
#include <effects.h> 




#define SCREEN_WIDTH 800 
#define SCREEN_HEIGHT 600 

// include the Direct3D Library file 
#pragma comment (lib, "d3d11.lib") 
#pragma comment (lib, "d3dx11.lib") 
#pragma comment (lib, "d3dx10.lib") 

// global declarations 
IDXGISwapChain *swapchain;    // the pointer to the swap chain interface 
ID3D11Device *dev;      // the pointer to our Direct3D device interface 
ID3D11DeviceContext *devcon;   // the pointer to our Direct3D device context 
ID3D11RenderTargetView *backbuffer; // the pointer to our back buffer          
ID3D11VertexShader *pVS; // the vertex shader 
ID3D11PixelShader *pPS;  // the pixel shader 

             // function prototypes 
void InitD3D(HWND hWnd); // sets up and initializes Direct3D 
void RenderFrame(void);  // renders a single frame 
void CleanD3D(void);  // closes Direct3D and releases memory 
void IA(void); 
          // the WindowProc function prototype 
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 


// the entry point for any Windows program 
int WINAPI WinMain(HINSTANCE hInstance, 
    HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, 
    int nCmdShow) 
{ 
    HWND hWnd; 
    WNDCLASSEX wc; 

    ZeroMemory(&wc, sizeof(WNDCLASSEX)); 

    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc = WindowProc; 
    wc.hInstance = hInstance; 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW; 
    wc.lpszClassName = L"WindowClass"; 

    RegisterClassEx(&wc); 

    RECT wr = { 0, 0, 800, 600 }; 
    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); 

    hWnd = CreateWindowEx(NULL, 
     L"WindowClass", 
     L"Our First Direct3D Program", 
     WS_OVERLAPPEDWINDOW, 
     SCREEN_WIDTH, 
     SCREEN_HEIGHT, 
     wr.right - wr.left, 
     wr.bottom - wr.top, 
     NULL, 
     NULL, 
     hInstance, 
     NULL); 

    ShowWindow(hWnd, nCmdShow); 

    // set up and initialize Direct3D 
    InitD3D(hWnd); 

    // enter the main loop: 

    MSG msg; 

    while (TRUE) 
    { 
     if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 

      if (msg.message == WM_QUIT) 
       break; 
     } 

     RenderFrame(); 
    } 


    // clean up DirectX and COM 
    CleanD3D(); 


    return msg.wParam; 
} 


// this is the main message handler for the program 
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message) 
    { 
    case WM_DESTROY: 
    { 
     PostQuitMessage(0); 
     return 0; 
    } break; 
    } 

    return DefWindowProc(hWnd, message, wParam, lParam); 
} 


// this function initializes and prepares Direct3D for use 
void InitD3D(HWND hWnd) 
{ 
    // create a struct to hold information about the swap chain 
    DXGI_SWAP_CHAIN_DESC scd; 

    // clear out the struct for use 
    ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); 

    // fill the swap chain description struct 
    scd.BufferCount = 1;         // one back buffer 
    scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;  // use 32-bit color 
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;  // how swap chain is to be used 
    scd.OutputWindow = hWnd;        // the window to be used 
    scd.SampleDesc.Count = 1;        // how many multisamples 
    scd.SampleDesc.Quality = 0;        // multisample quality level 
    scd.Windowed = TRUE; 
    scd.BufferDesc.Width = SCREEN_WIDTH; 
    scd.BufferDesc.Height = SCREEN_HEIGHT; 
    scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; 

    // windowed/full-screen mode 

    // create a device, device context and swap chain using the information in the scd struct 
    D3D11CreateDeviceAndSwapChain(NULL, 
     D3D_DRIVER_TYPE_HARDWARE, 
     NULL, 
     NULL, 
     NULL, 
     NULL, 
     D3D11_SDK_VERSION, 
     &scd, 
     &swapchain, 
     &dev, 
     NULL, 
     &devcon); 


    // get the address of the back buffer 
    ID3D11Texture2D *pBackBuffer; 
    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); 

    // use the back buffer address to create the render target 
    dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer); 
    pBackBuffer->Release(); 

    // set the render target as the back buffer 
    devcon->OMSetRenderTargets(1, &backbuffer, NULL); 


    // Set the viewport 
    D3D11_VIEWPORT viewport; 
    ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT)); 

    viewport.TopLeftX = 0; 
    viewport.TopLeftY = 0; 
    viewport.Width = SCREEN_WIDTH; 
    viewport.Height = SCREEN_HEIGHT; 

    devcon->RSSetViewports(1, &viewport); 
} 


// this is the function used to render a single frame 
void RenderFrame(void) 
{ 
    // clear the back buffer to a deep blue 
    devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f)); 

    // do 3D rendering on the back buffer here 

    // switch the back buffer and the front buffer 
    swapchain->Present(0, 0); 
} 


// this is the function that cleans up Direct3D and COM 
void CleanD3D(void) 
{ 
    swapchain->SetFullscreenState(FALSE, NULL); // switch to windowed mode 

                // close and release all existing COM objects 
    swapchain->Release(); 
    backbuffer->Release(); 
    dev->Release(); 
    devcon->Release(); 
    pVS->Release(); 
    pPS->Release(); 
} 

//RENDERING PIPELINE// 




//input layout// 

void IA() 
{ 

    struct Vertex 
    { 
     XMFLOAT3 Pos; //0 bit 
     XMFLOAT3 Normal; // 12 bit 
     XMFLOAT3 tex0; // 24 bit 
     XMFLOAT3 tex1; // 32 bit 
    }; 


    D3D11_INPUT_ELEMENT_DESC VertexDesc[] = { 
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
    { "TEXCOORD", 1, DXGI_FORMAT_R32G32_FLOAT, 0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0 } 
    }; 


    ID3D10Effect * mFX; 
    ID3D10EffectTechnique * mTech; 
    ID3D11InputLayout* mInputLayout; 
    D3D10_PASS_DESC passDesc; 


    mTech = mFX->GetTechniqueByName("Tech"); 
    mTech->GetPassByIndex(0)->GetDesc(&passDesc); 

    dev->CreateInputLayout(
     VertexDesc, 
     4, 
     passDesc.pIAInputSignature, 
     passDesc.IAInputSignatureSize, 
     &mInputLayout); 

    //Set I.L to the device 
    devcon->IAGetInputLayout(&mInputLayout); 



} 

编译得到的错误后:

C4700 uninitialized local variable 'mFX' used

但也有我已经有这些错误我认为:

Severity Code Description Project File Line Warning C4005 'D3D10_ERROR_FILE_NOT_FOUND': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\d3d10.h 609 Warning C4005 'D3D10_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\d3d10.h 608 Warning C4005 'D3D11_ERROR_DEFERRED_CONTEXT_MAP_WITHOUT_INITIAL_DISCARD': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\d3d11.h 920 Warning C4005 'D3D11_ERROR_FILE_NOT_FOUND': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\d3d11.h 918 Warning C4005 'D3D11_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\d3d11.h 917 Warning C4005 'D3D11_ERROR_TOO_MANY_UNIQUE_VIEW_OBJECTS': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\d3d11.h 919 Warning C4005 'DXGI_ERROR_DEVICE_HUNG': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 26 Warning C4005 'DXGI_ERROR_DEVICE_REMOVED': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 25 Warning C4005 'DXGI_ERROR_DEVICE_RESET': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 27 Warning C4005 'DXGI_ERROR_DRIVER_INTERNAL_ERROR': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 31 Warning C4005 'DXGI_ERROR_FRAME_STATISTICS_DISJOINT': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 29 Warning C4005 'DXGI_ERROR_GRAPHICS_VIDPN_SOURCE_IN_USE': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 30 Warning C4005 'DXGI_ERROR_INVALID_CALL': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 21 Warning C4005 'DXGI_ERROR_MORE_DATA': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 23 Warning C4005 'DXGI_ERROR_NONEXCLUSIVE': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 32 Warning C4005 'DXGI_ERROR_NOT_CURRENTLY_AVAILABLE': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 33 Warning C4005 'DXGI_ERROR_NOT_FOUND': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 22 Warning C4005 'DXGI_ERROR_REMOTE_CLIENT_DISCONNECTED': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 34 Warning C4005 'DXGI_ERROR_REMOTE_OUTOFMEMORY': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 35 Warning C4005 'DXGI_ERROR_UNSUPPORTED': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 24 Warning C4005 'DXGI_ERROR_WAS_STILL_DRAWING': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 28 Warning C4005 'DXGI_STATUS_CLIPPED': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 13 Warning C4005 'DXGI_STATUS_GRAPHICS_VIDPN_SOURCE_IN_USE': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 16 Warning C4005 'DXGI_STATUS_MODE_CHANGED': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 17 Warning C4005 'DXGI_STATUS_MODE_CHANGE_IN_PROGRESS': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 18 Warning C4005 'DXGI_STATUS_NO_DESKTOP_ACCESS': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 15 Warning C4005 'DXGI_STATUS_NO_REDIRECTION': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 14 Warning C4005 'DXGI_STATUS_OCCLUDED': macro redefinition DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\dxgitype.h 12 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2662 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2663 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2664 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2665 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2666 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2667 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2668 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2689 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2690 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2691 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2694 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2695 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2698 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2702 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2712 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2713 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2715 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2716 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2717 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2727 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2728 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2729 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2730 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2731 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2732 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2742 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2743 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamath.h 2754 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathconvert.inl 2254 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathconvert.inl 4523 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathconvert.inl 4577 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathconvert.inl 4639 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathconvert.inl 4707 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathconvert.inl 4770 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathconvert.inl 4832 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathmisc.inl 518 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathmisc.inl 519 Warning C4838 conversion from 'unsigned int' to 'INT' requires a narrowing conversion DX11proj c:\users\spyros\documents\microsoft directx sdk (june 2010)\include\xnamathvector.inl 1269

我正在使用Visual Studio 2015

在此先感谢您阅读的所有这些代码。我可能不应该把它全部放在一个文件中。

+0

您的'mTech = mFX-> GetTechniqueByName(“Tech”);'函数失败,因为'mFX'未初始化。你应该在'GetTechniqueByName'调用之前使用'CreateFromFile'(或类似的东西)。如果这是固定的,你可以检查其他错误。 – Stefan

回答

1

您必须先通过调用函数来创建效果。

D3D10CreateEffectFromMemory(<paste_here_your_effect_data_buffer>, <paste_here_your_effect_data_buffer_length>, 
     0, <device_interface>, <effect_pool>, &mFX); 

哪里可以从效果文件中读取效果数据。

读取数据过程

std::fstream file; 
    file.open("effect.fx", std::ios::binary | std::ios::in); 

    char* pcbBuffer = NULL; 

    file.seekg(0, std::ios::end); 
    size_t nLength = file.tellg(); 
    file.seekg(0, std::ios::beg); 

    pcbBuffer = new char[nLength]; 

    file.read(pcbBuffer, nLength); 
    file.close(); 

其中pcbBuffer将是我们的效果数据,nLength我们的缓冲区的长度。

+0

什么是我的效果数据缓冲区长度,设备接口和效果池?谢谢 – Frostice100

+0

@ Frostice100:您可以使用'fstream'或本机'CreateFile'读取效果文件。在一个大缓冲区中读这个文件到最后。长度将是文件的长度。设备接口是'ID3D10Device *',效果池'ID3D10EffectPool'。 – Mykola

+0

究竟是什么效果池?你说它会是这样的:D3D10CreateEffectFromMemory,fstream, 0,dev,,&mFX);再次感谢您的帮助? – Frostice100