//-------------------------------------------------------------------------------------- // Draw geometry using effect //-------------------------------------------------------------------------------------- HRESULT CEffect::DrawGeom(char *a_TechniqueName, CGeom *a_Geom) { UINT cPasses, iPass; HRESULT hr; V_RETURN( m_pEffect->SetTechnique( a_TechniqueName ) ); V_RETURN( m_pEffect->Begin(&cPasses, 0) ); for (iPass = 0; iPass < cPasses; iPass++) { V_RETURN( m_pEffect->BeginPass(iPass)); //draw scene geometry V_RETURN(a_Geom->Draw()); V_RETURN( m_pEffect->EndPass()); } V_RETURN( m_pEffect->End()); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); // Load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"dwarf\\dwarf.x" ) ); // Find the mesh's center, then generate a centering matrix. IDirect3DVertexBuffer9* pVB = NULL; V_RETURN( g_Mesh.m_pMesh->GetVertexBuffer( &pVB ) ); void* pVertices = NULL; hr = pVB->Lock( 0, 0, &pVertices, 0 ); if( FAILED( hr ) ) { SAFE_RELEASE( pVB ); return hr; } hr = D3DXComputeBoundingSphere( ( D3DXVECTOR3* )pVertices, g_Mesh.m_pMesh->GetNumVertices(), D3DXGetFVFVertexSize( g_Mesh.m_pMesh->GetFVF() ), &g_vObjectCenter, &g_fObjectRadius ); pVB->Unlock(); SAFE_RELEASE( pVB ); if( FAILED( hr ) ) return hr; D3DXMatrixTranslation( &g_mCenterWorld, -g_vObjectCenter.x, -g_vObjectCenter.y, -g_vObjectCenter.z ); // Read the D3DX effect file TCHAR str[MAX_PATH]; hr = DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT( "CompiledEffect.fxo" ) ); if( FAILED( hr ) ) { MessageBox( DXUTGetHWND(), TEXT( "Could not locate \"CompiledEffect.fxo\".\n\n" ) TEXT( "This file is created as part of the project build process,\n" ) TEXT( "so the associated project must be compiled inside Visual\n" ) TEXT( "Studio before attempting to run this sample.\n\n" ) TEXT( "If receiving this error even after compiling the project,\n" ) TEXT( "it's likely there was a problem compiling the effect file.\n" ) TEXT( "Check the build log to verify the custom build step was\n" ) TEXT( "run and to look for possible fxc compile errors." ), TEXT( "File Not Found" ), MB_OK ); return E_FAIL; } // Since we are loading a binary file here and this effect has already been compiled, // you can not pass compiler flags here (for example to debug the shaders). // To debug the shaders, you must pass these flags to the compiler that generated the // binary (for example fxc.exe). In this sample, there are 2 extra Visual Studio configurations // called "Debug Shaders" and "Unicode Debug Shaders" that pass the debug shader flags to fxc.exe. V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, D3DXFX_NOT_CLONEABLE, NULL, &g_pEffect, NULL ) ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT SoftParticles::OnD3D11CreateDevice(ID3D11Device* pd3dDevice) { HRESULT hr; V_RETURN( LoadEffectFromFile(pd3dDevice, L"SoftParticles.hlsl", &g_pEffect11) ); // Obtain the technique handles g_pRenderBillboardParticlesHard = g_pEffect11->GetTechniqueByName( "RenderBillboardParticles_Hard" ); g_pRenderBillboardParticlesODepth = g_pEffect11->GetTechniqueByName( "RenderBillboardParticles_ODepth" ); g_pRenderBillboardParticlesSoft = g_pEffect11->GetTechniqueByName( "RenderBillboardParticles_Soft" ); g_pRenderBillboardParticlesODepthSoft = g_pEffect11->GetTechniqueByName( "RenderBillboardParticles_ODepthSoft" ); g_pRenderVolumeParticlesHard = g_pEffect11->GetTechniqueByName( "RenderVolumeParticles_Hard" ); g_pRenderVolumeParticlesSoft = g_pEffect11->GetTechniqueByName( "RenderVolumeParticles_Soft" ); g_pRenderVolumeParticlesSoftMSAA = g_pEffect11->GetTechniqueByName( "RenderVolumeParticles_Soft_MSAA" ); g_pRenderVolumeParticlesHardMSAA = g_pEffect11->GetTechniqueByName( "RenderVolumeParticles_Hard_MSAA" ); g_pRenderBillboardParticlesSoftMSAA = g_pEffect11->GetTechniqueByName( "RenderBillboardParticles_Soft_MSAA" ); g_pRenderBillboardParticlesODepthSoftMSAA = g_pEffect11->GetTechniqueByName( "RenderBillboardParticles_ODepthSoft_MSAA" ); // Obtain the parameter handles g_pmWorldViewProj = g_pEffect11->GetVariableByName( "g_mWorldViewProj" )->AsMatrix(); g_pmWorldView = g_pEffect11->GetVariableByName( "g_mWorldView" )->AsMatrix(); g_pmWorld = g_pEffect11->GetVariableByName( "g_mWorld" )->AsMatrix(); g_pmInvView = g_pEffect11->GetVariableByName( "g_mInvView" )->AsMatrix(); g_pmInvProj = g_pEffect11->GetVariableByName( "g_mInvProj" )->AsMatrix(); g_pfFadeDistance = g_pEffect11->GetVariableByName( "g_fFadeDistance" )->AsScalar(); g_pfSizeZScale = g_pEffect11->GetVariableByName( "g_fSizeZScale" )->AsScalar(); g_pvViewLightDir1 = g_pEffect11->GetVariableByName( "g_vViewLightDir1" )->AsVector(); g_pvViewLightDir2 = g_pEffect11->GetVariableByName( "g_vViewLightDir2" )->AsVector(); g_pvWorldLightDir1 = g_pEffect11->GetVariableByName( "g_vWorldLightDir1" )->AsVector(); g_pvWorldLightDir2 = g_pEffect11->GetVariableByName( "g_vWorldLightDir2" )->AsVector(); g_pvEyePt = g_pEffect11->GetVariableByName( "g_vEyePt" )->AsVector(); g_pvViewDir = g_pEffect11->GetVariableByName( "g_vViewDir" )->AsVector(); g_pvOctaveOffsets = g_pEffect11->GetVariableByName( "g_OctaveOffsets" )->AsVector(); g_pvScreenSize = g_pEffect11->GetVariableByName( "g_vScreenSize" )->AsVector(); g_pDiffuseTex = g_pEffect11->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); g_pNormalTex = g_pEffect11->GetVariableByName( "g_txNormal" )->AsShaderResource(); g_pColorGradient = g_pEffect11->GetVariableByName( "g_txColorGradient" )->AsShaderResource(); g_pVolumeDiffTex = g_pEffect11->GetVariableByName( "g_txVolumeDiff" )->AsShaderResource(); g_pVolumeNormTex = g_pEffect11->GetVariableByName( "g_txVolumeNorm" )->AsShaderResource(); g_pDepthTex = g_pEffect11->GetVariableByName( "g_txDepth" )->AsShaderResource(); g_pDepthMSAATex = g_pEffect11->GetVariableByName( "g_txDepthMSAA" )->AsShaderResource(); // Create our vertex input layouts D3DX11_PASS_DESC PassDesc; const D3D11_INPUT_ELEMENT_DESC particlelayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "VELOCITY", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "LIFE", 0, DXGI_FORMAT_R32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "SIZE", 0, DXGI_FORMAT_R32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; g_pRenderBillboardParticlesHard->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( particlelayout, sizeof(particlelayout)/sizeof(particlelayout[0]), PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pParticleVertexLayout ) ); // Create the particles V_RETURN( CreateParticleBuffers( pd3dDevice ) ); // Create the noise volume V_RETURN( CreateNoiseVolume( pd3dDevice, 32 ) ); // Load the Particle Texture V_RETURN( D3DX11CreateShaderResourceViewFromFile( pd3dDevice, L"Resource\\SmokeTexture\\smokevol1.dds", NULL, NULL, &g_pParticleTexRV, NULL ) ); V_RETURN( D3DX11CreateShaderResourceViewFromFile( pd3dDevice, L"Resource\\SmokeTexture\\colorgradient.dds", NULL, NULL, &g_pColorGradTexRV, NULL ) ); g_pfFadeDistance->SetFloat( g_fFadeDistance ); //--------------------------------- //resize swap chain //--------------------------------- // Create a new Depth-Stencil texture to replace the DXUT created one D3D11_TEXTURE2D_DESC descDepth; descDepth.Width = 800; descDepth.Height = 600; descDepth.MipLevels = 1; descDepth.ArraySize = 1; descDepth.Format = DXGI_FORMAT_R32_TYPELESS; // Use a typeless type here so that it can be both depth-stencil and shader resource. // This will fail if we try a format like D32_FLOAT descDepth.SampleDesc.Count = 1; descDepth.SampleDesc.Quality = 0; descDepth.Usage = D3D11_USAGE_DEFAULT; descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE; descDepth.CPUAccessFlags = 0; descDepth.MiscFlags = 0; V_RETURN( pd3dDevice->CreateTexture2D( &descDepth, NULL, &g_pDepthStencilTexture ) ); // Create the depth stencil view D3D11_DEPTH_STENCIL_VIEW_DESC descDSV; if( 1 == descDepth.SampleDesc.Count ) { descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; } else { descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS; } descDSV.Flags = 0; descDSV.Format = DXGI_FORMAT_D32_FLOAT; // Make the view see this as D32_FLOAT instead of typeless descDSV.Texture2D.MipSlice = 0; V_RETURN( pd3dDevice->CreateDepthStencilView( g_pDepthStencilTexture, &descDSV, &g_pDepthStencilDSV ) ); // Create the shader resource view if( 1 == descDepth.SampleDesc.Count ) { D3D11_SHADER_RESOURCE_VIEW_DESC descSRV; descSRV.Format = DXGI_FORMAT_R32_FLOAT; // Make the shaders see this as R32_FLOAT instead of typeless descSRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; descSRV.Texture2D.MipLevels = 1; descSRV.Texture2D.MostDetailedMip = 0; V_RETURN( pd3dDevice->CreateShaderResourceView( g_pDepthStencilTexture, &descSRV, &g_pDepthStencilSRV ) ); } g_iWidth = 800; g_iHeight = 600; g_iSampleCount = 1; //---------------------------------- // frame move //---------------------------------- return S_OK; }
HRESULT LineDraw_Hlsl_Shader::OnRestore(Scene *pScene) { HRESULT hr = S_OK; SAFE_RELEASE(m_pEffect); SAFE_RELEASE(m_pcbDiffuseColor); SAFE_RELEASE(m_pVertexLayout11); SAFE_RELEASE(m_pcbChangePerFrame); SAFE_RELEASE(m_pcbRenderTargetSize); shared_ptr<D3DRenderer11> d3dRenderer11 = static_pointer_cast<D3DRenderer11>(pScene->GetRenderer()); //======================================================== // Set up the vertex shader and related constant buffers // Compile the vertex shader using the lowest possible profile for broadest feature level support ID3DBlob* pVertexShaderBuffer = NULL; std::string hlslFileName = "Effects\\LineDraw.hlsl"; Resource resource(hlslFileName.c_str()); shared_ptr<ResHandle> pResourceHandle = g_pApp->m_ResCache->GetHandle(&resource); // this actually loads the HLSL file from the zip file // Compile effect from HLSL file into binary Blob in memory // The code in this function was found here - http://asawicki.info/news_1371_effects_in_directx_11.html ID3D10Blob *effectBlob = 0, *errorsBlob = 0; hr = D3DX11CompileFromMemory( pResourceHandle->Buffer(), // srcData pResourceHandle->Size(), // srcLen 0, 0, 0, 0, // fileName, pDefines, pInclude, functionName "fx_5_0", 0, 0, 0, // profile, flags1, flags2, pump &effectBlob, &errorsBlob, 0); // shader, errorMsg, pResult assert(SUCCEEDED(hr) && effectBlob); if (errorsBlob) errorsBlob->Release(); // Create D3DX11 effect from compiled binary memory block if (FAILED(D3DX11CreateEffectFromMemory( effectBlob->GetBufferPointer(), effectBlob->GetBufferSize(), 0, g_pDX11W->GetD3D11Device(), &m_pEffect))) { return hr; } effectBlob->Release(); m_EffectTechnique = m_pEffect->GetTechniqueByIndex(0); AC_ASSERT(m_EffectTechnique && m_EffectTechnique->IsValid()); m_EffectPass = m_EffectTechnique->GetPassByIndex(0); AC_ASSERT(m_EffectPass && m_EffectPass->IsValid()); D3DX11_PASS_SHADER_DESC effectVsDesc; m_EffectPass->GetVertexShaderDesc(&effectVsDesc); D3DX11_EFFECT_SHADER_DESC effectVsDesc2; effectVsDesc.pShaderVariable->GetShaderDesc(effectVsDesc.ShaderIndex, &effectVsDesc2); const void *vsCodePtr = effectVsDesc2.pBytecode; unsigned vsCodeLen = effectVsDesc2.BytecodeLength; if (SUCCEEDED (g_pDX11W->GetD3D11Device()->CreateInputLayout( D3D11VertexLayout_Position, ARRAYSIZE( D3D11VertexLayout_Position ), vsCodePtr, vsCodeLen, &m_pVertexLayout11))) { //DXUT_SetDebugName(m_pVertexLayout11, "Primary"); // Setup constant buffers D3D11_BUFFER_DESC Desc; Desc.Usage = D3D11_USAGE_DYNAMIC; Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; Desc.MiscFlags = 0; Desc.ByteWidth = sizeof(Vec4); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&Desc, NULL, &m_pcbRenderTargetSize)); //DXUT_SetDebugName( m_pcbRenderTargetSize, "Vec4_RenderTargetSize" ); Desc.ByteWidth = sizeof(Mat4x4); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&Desc, NULL, &m_pcbChangePerFrame)); //DXUT_SetDebugName( m_pcbChangePerFrame, "LineDrawerChangePerFrameBuffer" ); Desc.ByteWidth = sizeof(Vec4); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&Desc, NULL, &m_pcbDiffuseColor)); //DXUT_SetDebugName( m_pcbDiffuseColor, "DiffuseColor" ); } SAFE_RELEASE(pVertexShaderBuffer); return S_OK; }
bool InitGL(HWND hwnd) { HWND dummy = CreateWindow("TestClass", "Dummy", WS_CLIPCHILDREN|WS_CLIPSIBLINGS, 0, 0, 100, 100, 0, 0, GetModuleHandle(0), 0); PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 32, // color 0, 0, 0, 0, 0, 0, 0, // alpha 0, 0, 0, 0, 0, 0, 24, 8, 0, // depth, stencil, aux PFD_MAIN_PLANE, 0, 0, 0, 0 }; hdc = GetDC(dummy); V_RETURN(false, "InitGL(): Could not get device context", hdc); int pixelformat = ChoosePixelFormat(hdc, &pfd); V_RETURN(false, "InitGL(): Could not find suitable pixel format", pixelformat); BOOL success = SetPixelFormat(hdc, pixelformat, &pfd); V_RETURN(false, "InitGL(): Could not setup pixel format", success); hrc = wglCreateContext(hdc); V_RETURN(false, "InitGL(): Context creation failed", hrc); success = wglMakeCurrent(hdc, hrc); V_RETURN(false, "InitGL(): Could not acquire context", success); const char* str = (const char*)glGetString(GL_VENDOR); std::cout << "Vendor: " << str << "\n"; str = (const char*)glGetString(GL_RENDERER); std::cout << "Renderer: " << str << "\n"; str = (const char*)glGetString(GL_VERSION); std::cout << "OpenGL version: " << str << "\n\n"; int major, minor; sscanf_s(str, "%1d.%2d %*s", &major, &minor); if( major < 3 || (major == 3 && minor < 2) ) { std::cout << "Device does not support OpenGL 3.2\n"; return false; } typedef const char* (APIENTRY *WGLGETEXTENSIONSSTRINGARBPROC)(HDC hdc); typedef HGLRC (APIENTRY *WGLCREATECONTEXTATTRIBSARBPROC)(HDC hDC, HGLRC hShareContext, const int *attribList); WGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL; WGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = (WGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB"); if( wglGetExtensionsStringARB ) { if( Quadron::wIsSupported("WGL_ARB_pixel_format", hdc) ) { std::cout << "WGL_ARB_pixel_format present, querying pixel formats...\n"; typedef BOOL (APIENTRY *WGLGETPIXELFORMATATTRIBIVARBPROC)(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues); typedef BOOL (APIENTRY *WGLGETPIXELFORMATATTRIBFVARBPROC)(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues); typedef BOOL (APIENTRY *WGLCHOOSEPIXELFORMATARBPROC)(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats); WGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = (WGLGETPIXELFORMATATTRIBIVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB"); WGLGETPIXELFORMATATTRIBFVARBPROC wglGetPixelFormatAttribfvARB = (WGLGETPIXELFORMATATTRIBFVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribfvARB"); WGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = (WGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB"); int attrib[128]; int i = 0; UINT numformats; memset(attrib, 0, sizeof(attrib)); attrib[i++] = 0x2001; // WGL_DRAW_TO_WINDOW_ARB; attrib[i++] = TRUE; attrib[i++] = 0x2003; // WGL_ACCELERATION_ARB; attrib[i++] = 0x2027; // WGL_FULL_ACCELERATION_ARB attrib[i++] = 0x2010; // WGL_SUPPORT_OPENGL_ARB attrib[i++] = TRUE; attrib[i++] = 0x2011; // WGL_DOUBLE_BUFFER_ARB attrib[i++] = TRUE; attrib[i++] = 0x2013; // WGL_PIXEL_TYPE_ARB attrib[i++] = 0x202B; // WGL_TYPE_RGBA_ARB attrib[i++] = 0x2014; // WGL_COLOR_BITS_ARB attrib[i++] = pfd.cColorBits = 32; attrib[i++] = 0x201B; // WGL_ALPHA_BITS_ARB attrib[i++] = pfd.cAlphaBits = 0; attrib[i++] = 0x2022; // WGL_DEPTH_BITS_ARB attrib[i++] = pfd.cDepthBits = 24; attrib[i++] = 0x2023; // WGL_STENCIL_BITS_ARB attrib[i++] = pfd.cStencilBits = 8; attrib[i++] = 0; if( attrib[1] ) pfd.dwFlags |= PFD_DRAW_TO_WINDOW; if( attrib[5] ) pfd.dwFlags |= PFD_SUPPORT_OPENGL; if( attrib[7] ) pfd.dwFlags |= PFD_DOUBLEBUFFER; if( attrib[9] == 0x2029 ) pfd.dwFlags |= PFD_SWAP_COPY; wglChoosePixelFormatARB(hdc, attrib, 0, 1, &pixelformat, &numformats); if( numformats != 0 ) { std::cout << "Selected pixel format: " << pixelformat <<"\n"; if( Quadron::wIsSupported("WGL_ARB_create_context", hdc) && Quadron::wIsSupported("WGL_ARB_create_context_profile", hdc) ) { wglCreateContextAttribsARB = (WGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB"); } success = wglMakeCurrent(hdc, NULL); V_RETURN(false, "InitGL(): Could not release dummy context", success); success = wglDeleteContext(hrc); V_RETURN(false, "InitGL(): Could not delete dummy context", success); DestroyWindow(dummy); dummy = 0; hdc = GetDC(hwnd); hrc = 0; int success = SetPixelFormat(hdc, pixelformat, &pfd); V_RETURN(false, "InitGL(): Could not setup pixel format", success); if( wglCreateContextAttribsARB ) { int contextattribs[] = { 0x2091, // WGL_CONTEXT_MAJOR_VERSION_ARB major, 0x2092, // WGL_CONTEXT_MINOR_VERSION_ARB minor, 0x2094, // WGL_CONTEXT_FLAGS_ARB #ifdef _DEBUG //0x0001, // WGL_CONTEXT_DEBUG_BIT #endif 0x0002, // WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x9126, // WGL_CONTEXT_PROFILE_MASK_ARB 0x00000001, // WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0 }; hrc = wglCreateContextAttribsARB(hdc, NULL, contextattribs); V_RETURN(false, "InitGL(): Context creation failed", hrc); std::cout << "Created core profile context...\n"; } if( !hrc ) { hrc = wglCreateContext(hdc); V_RETURN(false, "InitGL(): Context creation failed", hrc); std::cout << "Created compatibility profile context...\n"; } success = wglMakeCurrent(hdc, hrc); V_RETURN(false, "InitGL(): Could not acquire context", success); std::cout << std::endl; } else std::cout << "wglChoosePixelFormat failed, using classic context...\n"; } else std::cout << "WGL_ARB_pixel_format not supported\n"; } else std::cout << "wglGetExtensionsStringARB not supported\n"; if( dummy ) { std::cout << "Selected pixel format: " << pixelformat <<"\n"; DestroyWindow(dummy); hdc = GetDC(hwnd); int success = SetPixelFormat(hdc, pixelformat, &pfd); V_RETURN(false, "InitGL(): Could not setup pixel format", success); hrc = wglCreateContext(hdc); V_RETURN(false, "InitGL(): Context creation failed", hrc); success = wglMakeCurrent(hdc, hrc); V_RETURN(false, "InitGL(): Could not acquire context", success); std::cout << std::endl; } return true; }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_D3DSettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); // D3DXVECTOR3 vCenter( 0.25767413f, -28.503521f, 111.00689f ); FLOAT fObjectRadius = 378.15607f; lightpos = D3DXVECTOR3(300,300,-200); //test = new testing(); hr = test.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = tessplane.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = tesscube.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,80,D3DXVECTOR3(300,50,-200));//D3DXVECTOR3(300,50,-200 hr = lightsphere.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,10,lightpos); hr = fuse.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = board1.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,D3DXVECTOR3(300,-300,-1000)); hr = deboard.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,D3DXVECTOR3(-300,-300,-1000)); hr = geo_alien.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = FirePart.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,80,0); //0 = fire hr = sky.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,D3DXVECTOR3(0,0,0)); hr = buildings.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = MissilePart.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,20,1); g_LightControl.SetRadius( 2000 ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 50.0f, -1000.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetRotateButtons(true,false,false); g_Camera.SetViewParams( &vecEye, &vecAt ); g_Camera.SetEnablePositionMovement( true ); g_Camera.SetScalers( 0.005f, 500.0f ); D3D11_DEPTH_STENCIL_DESC descDS; ZeroMemory(&descDS, sizeof(descDS)); descDS.DepthEnable = false; descDS.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; descDS.DepthFunc = D3D11_COMPARISON_LESS; descDS.StencilEnable = FALSE; hr = pd3dDevice->CreateDepthStencilState( &descDS, &g_DepthState); //setup stuff for post process ID3DBlob* pVertexShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"PP1.hlsl", "VS", "vs_4_0", &pVertexShaderBuffer ) ); ID3DBlob* pPixelShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"PP1.hlsl", "PS", "ps_4_0", &pPixelShaderBuffer ) ); V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &VSPPFirstPass ) ); DXUT_SetDebugName( VSPPFirstPass, "VSPost1" ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &PSPPFirstPass ) ); DXUT_SetDebugName( PSPPFirstPass, "PSPost1" ); pVertexShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"Gaussblur.hlsl", "VS", "vs_4_0", &pVertexShaderBuffer ) ); pPixelShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"Gaussblur.hlsl", "PS", "ps_4_0", &pPixelShaderBuffer ) ); V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &VSPPBlur ) ); DXUT_SetDebugName( VSPPBlur, "VSBlur" ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &PSPPBlur ) ); DXUT_SetDebugName( PSPPBlur, "PSBlur" ); pVertexShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"bloom_combine.hlsl", "VS", "vs_4_0", &pVertexShaderBuffer ) ); pPixelShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"bloom_combine.hlsl", "PS", "ps_4_0", &pPixelShaderBuffer ) ); V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &VSPPComb ) ); DXUT_SetDebugName( VSPPComb, "VSComb" ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &PSPPComb ) ); DXUT_SetDebugName( PSPPComb, "PSComb" ); D3D11_BUFFER_DESC Desc; Desc.Usage = D3D11_USAGE_DYNAMIC; Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; Desc.MiscFlags = 0; Desc.ByteWidth = sizeof( blur_cbuffer ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &blur_cb_buffer ) ); DXUT_SetDebugName( blur_cb_buffer, "blur_cb" ); //g_Camera.SetRadius( fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f ); return S_OK; }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // created, which will happen during application initialization and windowed/full screen // toggles. This is the best location to create D3DPOOL_MANAGED resources since these // resources need to be reloaded whenever the device is destroyed. Resources created // here should be released in the OnDestroyDevice callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the // shader debugger. Debugging vertex shaders requires either REF or software vertex // processing, and debugging pixel shaders requires REF. The // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the // shader debugger. It enables source level debugging, prevents instruction // reordering, prevents dead code elimination, and forces the compiler to compile // against the next higher available software target, which ensures that the // unoptimized shaders do not exceed the shader model limitations. Setting these // flags will cause slower rendering since the shaders will be unoptimized and // forced into software. See the DirectX documentation for more information about // using the shader debugger. DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DXSHADER_DEBUG; #endif #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif // Determine which LDPRT texture and SH coefficient cubemap formats are supported IDirect3D9* pD3D = DXUTGetD3D9Object(); D3DCAPS9 Caps; pd3dDevice->GetDeviceCaps( &Caps ); D3DDISPLAYMODE DisplayMode; pd3dDevice->GetDisplayMode( 0, &DisplayMode ); GetSupportedTextureFormat( pD3D, &Caps, DisplayMode.Format, &g_fmtTexture, &g_fmtCubeMap ); if( D3DFMT_UNKNOWN == g_fmtTexture || D3DFMT_UNKNOWN == g_fmtCubeMap ) return E_FAIL; // Create the skybox g_Skybox.OnCreateDevice( pd3dDevice, 50, L"Light Probes\\rnl_cross.dds", L"SkyBox.fx" ); V( D3DXSHProjectCubeMap( 6, g_Skybox.GetEnvironmentMap(), g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ) ); // Now compute the SH projection of the skybox... LPDIRECT3DCUBETEXTURE9 pSHCubeTex = NULL; V( D3DXCreateCubeTexture( pd3dDevice, 256, 1, 0, D3DFMT_A16B16G16R16F, D3DPOOL_MANAGED, &pSHCubeTex ) ); SHCubeProj projData; projData.Init( g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ); V( D3DXFillCubeTexture( pSHCubeTex, SHCubeFill, &projData ) ); g_Skybox.InitSH( pSHCubeTex ); // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT( "LocalDeformablePRT.fx" ) ) ); // If this fails, there should be debug output as to they the .fx file failed to compile V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) ); V_RETURN( LoadTechniqueObjects( "bat" ) ); V_RETURN( g_LightControl.StaticOnD3D9CreateDevice( pd3dDevice ) ); g_LightControl.SetRadius( 2.0f ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, 0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); // Set the model's initial orientation D3DXQUATERNION quatRotation; D3DXQuaternionRotationYawPitchRoll( &quatRotation, -0.5f, 0.7f, 0.0f ); g_Camera.SetWorldQuat( quatRotation ); return hr; }
HRESULT WEHDR::Create(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext) { HRESULT hr; m_pd3dDevice = pd3dDevice; m_pImmediateContext = pd3dImmediateContext; // Create views. V_RETURN(CreateViews()); // Create shaders. ID3DBlob* pPSBlob2x2 = NULL; ID3DBlob* pPSBlob3x3 = NULL; ID3DBlob* pPSBlobBrightPass = NULL; ID3DBlob* pPSBlobBloom = NULL; ID3DBlob* pPSBlobFinal = NULL; V_RETURN(WE::CompileShaderFromFile(L"shaders\\HDR.hlsl", NULL, "DownScale2x2_Lum", "ps_5_0", &pPSBlob2x2)); V_RETURN(WE::CompileShaderFromFile(L"shaders\\HDR.hlsl", NULL, "DownScale3x3", "ps_5_0", &pPSBlob3x3)); V_RETURN(WE::CompileShaderFromFile(L"shaders\\HDR.hlsl", NULL, "DownScale3x3_BrightPass", "ps_5_0", &pPSBlobBrightPass)); V_RETURN(WE::CompileShaderFromFile(L"shaders\\HDR.hlsl", NULL, "Bloom", "ps_5_0", &pPSBlobBloom)); V_RETURN(WE::CompileShaderFromFile(L"shaders\\HDR.hlsl", NULL, "FinalPass", "ps_5_0", &pPSBlobFinal)); V_RETURN(pd3dDevice->CreatePixelShader(pPSBlob2x2->GetBufferPointer(), pPSBlob2x2->GetBufferSize(), NULL, &m_pPSDownScale2x2Lum)); V_RETURN(pd3dDevice->CreatePixelShader(pPSBlob3x3->GetBufferPointer(), pPSBlob3x3->GetBufferSize(), NULL, &m_pPSDownScale3x3)); V_RETURN(pd3dDevice->CreatePixelShader(pPSBlobBrightPass->GetBufferPointer(), pPSBlobBrightPass->GetBufferSize(), NULL, &m_pPSBrightPass)); V_RETURN(pd3dDevice->CreatePixelShader(pPSBlobBloom->GetBufferPointer(), pPSBlobBloom->GetBufferSize(), NULL, &m_pPSBloom)); V_RETURN(pd3dDevice->CreatePixelShader(pPSBlobFinal->GetBufferPointer(), pPSBlobFinal->GetBufferSize(), NULL, &m_pPSFinalPass)); SAFE_RELEASE(pPSBlob2x2); SAFE_RELEASE(pPSBlob3x3); SAFE_RELEASE(pPSBlobBrightPass); SAFE_RELEASE(pPSBlobBloom); SAFE_RELEASE(pPSBlobFinal); // Create the texture sampler state. D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 1; samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; samplerDesc.BorderColor[0] = 0; samplerDesc.BorderColor[1] = 0; samplerDesc.BorderColor[2] = 0; samplerDesc.BorderColor[3] = 0; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; V_RETURN(WE::D3DDevice()->CreateSamplerState(&samplerDesc, &m_pSamplePoint)); samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; V_RETURN(WE::D3DDevice()->CreateSamplerState(&samplerDesc, &m_pSampleLinear)); // Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class. D3D11_BUFFER_DESC bufferDesc; bufferDesc.Usage = D3D11_USAGE_DYNAMIC; bufferDesc.ByteWidth = sizeof(BufferType); bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bufferDesc.MiscFlags = 0; bufferDesc.StructureByteStride = 0; V_RETURN(pd3dDevice->CreateBuffer(&bufferDesc, NULL, &m_pcbBloom)); // V_RETURN(CreateViews()); return S_OK; }
HRESULT DX11SceneRep::OnD3D11CreateDevice( ID3D11Device* pd3dDevice ) { HRESULT hr = S_OK; D3D11_BUFFER_DESC bDesc; ZeroMemory(&bDesc, sizeof(D3D11_BUFFER_DESC)); bDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bDesc.Usage = D3D11_USAGE_DYNAMIC; bDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bDesc.MiscFlags = 0; bDesc.ByteWidth = sizeof(CB_VOXEL_HASH); V_RETURN(pd3dDevice->CreateBuffer(&bDesc, NULL, &s_VoxelHashCB)); char blockSize[5]; sprintf_s(blockSize, "%d", THREAD_GROUP_SIZE_SCENE_REP); D3D_SHADER_MACRO macro[] = {{"groupthreads", blockSize}, {0}}; ID3DBlob* pBlob = NULL; V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "integrateCS", "cs_5_0", &pBlob, macro)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashIntegrate)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "resetCS", "cs_5_0", &pBlob, macro)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashReset)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "starveCS", "cs_5_0", &pBlob, macro)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashStarve)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "starveVisibleCS", "cs_5_0", &pBlob, macro)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashStarveVisible)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "starveRegulizeNoiseCS", "cs_5_0", &pBlob, macro)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashStarveRegulizeNoise)); D3D_SHADER_MACRO macro_out = {"MOVE_OUT_FRUSTUM", "1"}; D3D_SHADER_MACRO macro_in = {"MOVE_IN_FRUSTUM", "1"}; D3D_SHADER_MACRO macro_and_out[10]; D3D_SHADER_MACRO macro_and_in[10]; AddDefinitionToMacro(macro, macro_and_out, macro_out); AddDefinitionToMacro(macro, macro_and_in, macro_in); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "removeAndIntegrateCS", "cs_5_0", &pBlob, macro_and_out)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashRemoveAndIntegrateOutFrustum)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "removeAndIntegrateCS", "cs_5_0", &pBlob, macro_and_in)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashRemoveAndIntegrateInFrustum)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "integrateFromOtherCS", "cs_5_0", &pBlob, macro)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashIntegrateFromOther)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRep.hlsl", "removeFromOtherCS", "cs_5_0", &pBlob, macro)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_VoxelHashRemoveFromOther)); SAFE_RELEASE(pBlob); return hr; }
void EffectHelper::init(ID3D11Device *pD3D11Device, std::string effectFile) { DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DCOMPILE_DEBUG; // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif #if D3D_COMPILER_VERSION >= 46 // Read the D3DX effect file HRESULT hr = S_OK; D3DX11CompileEffectFromFile(L"tutorial11.fx", nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, dwShaderFlags, 0, pD3D11Device, &m_pEffect, nullptr); #else ID3DBlob* pEffectBuffer = nullptr; V_RETURN(DXUTCompileFromFile(L"Tutorial11.fx", nullptr, "none", "fx_5_0", dwShaderFlags, 0, &pEffectBuffer)); hr = D3DX11CreateEffectFromMemory(pEffectBuffer->GetBufferPointer(), pEffectBuffer->GetBufferSize(), 0, pd3dDevice, &m_pEffect); SAFE_RELEASE(pEffectBuffer); if (FAILED(hr)) return hr; #endif m_pEffectTechnique = m_pEffect->GetTechniqueByName("Render"); std::vector<D3D11_INPUT_ELEMENT_DESC> vInputLayoutDesc; D3D11_INPUT_ELEMENT_DESC inputLayoutDesc; inputLayoutDesc.SemanticName = "POSITION"; inputLayoutDesc.SemanticIndex = 0; inputLayoutDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT; inputLayoutDesc.InputSlot = 0; inputLayoutDesc.AlignedByteOffset = 0; inputLayoutDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputLayoutDesc.InstanceDataStepRate = 0; vInputLayoutDesc.push_back(inputLayoutDesc); inputLayoutDesc.SemanticName = "NORMAL"; inputLayoutDesc.SemanticIndex = 0; inputLayoutDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT; inputLayoutDesc.InputSlot = 0; inputLayoutDesc.AlignedByteOffset = 12; inputLayoutDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputLayoutDesc.InstanceDataStepRate = 0; vInputLayoutDesc.push_back(inputLayoutDesc); inputLayoutDesc.SemanticName = "TEXCOORD"; inputLayoutDesc.SemanticIndex = 0; inputLayoutDesc.Format = DXGI_FORMAT_R32G32_FLOAT; inputLayoutDesc.InputSlot = 0; inputLayoutDesc.AlignedByteOffset = 24; inputLayoutDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputLayoutDesc.InstanceDataStepRate = 0; vInputLayoutDesc.push_back(inputLayoutDesc); // Create the input layout D3DX11_PASS_DESC PassDesc; auto numElements = vInputLayoutDesc.size(); m_pEffectTechnique->GetPassByIndex(0)->GetDesc(&PassDesc); pD3D11Device->CreateInputLayout(&vInputLayoutDesc[0], numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_pInputLayout); }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( g_D3DSettingsDlg.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( D3DX10CreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont10 ) ); V_RETURN( D3DX10CreateSprite( pd3dDevice, MAX_SPRITES, &g_pSprite10 ) ); V_RETURN( CDXUTDirectionWidget::StaticOnD3D10CreateDevice( pd3dDevice ) ); // Read the D3DX effect file WCHAR str[MAX_PATH]; DWORD dwShaderFlags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3D10_SHADER_DEBUG; #endif V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Exercise03.fx" ) ); V_RETURN( D3DX10CreateEffectFromFile( str, NULL, NULL, "fx_4_0", dwShaderFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect10, NULL, NULL ) ); // Obtain the technique handles g_pRenderTextured = g_pEffect10->GetTechniqueByName( "RenderTextured" ); g_pRenderPiece = g_pEffect10->GetTechniqueByName( "RenderPiece" ); // Obtain the parameter handles g_pmWorldViewProj = g_pEffect10->GetVariableByName( "g_mWorldViewProj" )->AsMatrix(); g_pmWorldView = g_pEffect10->GetVariableByName( "g_mWorldView" )->AsMatrix(); g_pmWorld = g_pEffect10->GetVariableByName( "g_mWorld" )->AsMatrix(); g_pmProj = g_pEffect10->GetVariableByName( "g_mProj" )->AsMatrix(); g_pViewSpaceLightDir = g_pEffect10->GetVariableByName( "g_ViewSpaceLightDir" )->AsVector(); g_pDiffuseTex = g_pEffect10->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); // Define our vertex data layout const D3D10_INPUT_ELEMENT_DESC layout[] = { { "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof( layout ) / sizeof( layout[0] ); D3D10_PASS_DESC PassDesc; g_pRenderPiece->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ) ); // Load our IB and VB with mesh data V_RETURN( LoadMesh( pd3dDevice ) ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -8.0f ); D3DXVECTOR3 vecAt( 0.0f,0.0f,0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); return S_OK; }
//-------------------------------------------------------------------------------------- // Helper to load a VB and IB from a mesh //-------------------------------------------------------------------------------------- HRESULT LoadMesh( ID3D10Device* pd3dDevice ) { static float treeVertexData[] = { 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, 0.762787f, 0.999001f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, 0.900607f, -0.999001f, 0.600000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, 0.926464f, -0.753354f, 0.600000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, 0.821507f, -0.753354f, 0.600000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.600000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.600000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.600000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.600000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.600000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.600000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, -0.073536f, -0.753354f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, 0.926464f, -0.753354f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, 0.821507f, -0.753354f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, -0.178493f, -0.753354f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, -0.178493f, -0.753354f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, -0.073536f, -0.753354f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, }; static DWORD treeIndexData[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113 }; HRESULT hr = S_OK; g_NumIndices = sizeof( treeIndexData ) / sizeof( DWORD ); g_VertStride = sizeof( TREE_VERTEX ); D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( treeVertexData ); bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; InitData.pSysMem = treeVertexData; V_RETURN( pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVB ) ); bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( treeIndexData ); bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; InitData.pSysMem = treeIndexData; V_RETURN( pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIB ) ); // Load the Texture WCHAR strPath[MAX_PATH] = {0}; DXUTFindDXSDKMediaFileCch( strPath, sizeof( strPath ) / sizeof( WCHAR ), L"bark_diff.dds" ); hr = D3DX10CreateShaderResourceViewFromFile( pd3dDevice, strPath, NULL, NULL, &g_pMeshTexRV, NULL ); return hr; }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { MessageBox(0, L"We aren't using DirectX9", L"We aren't using DirectX9", 0); exit(1); HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_D3DSettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont9 ) ); // Load the mesh V_RETURN( LoadMesh( pd3dDevice, L"tiny\\tiny.x", &g_pMesh9 ) ); D3DXVECTOR3* pData; D3DXVECTOR3 vCenter; FLOAT fObjectRadius; V( g_pMesh9->LockVertexBuffer( 0, ( LPVOID* )&pData ) ); V( D3DXComputeBoundingSphere( pData, g_pMesh9->GetNumVertices(), D3DXGetFVFVertexSize( g_pMesh9->GetFVF() ), &vCenter, &fObjectRadius ) ); V( g_pMesh9->UnlockVertexBuffer() ); D3DXMatrixTranslation( &g_mCenterMesh, -vCenter.x, -vCenter.y, -vCenter.z ); D3DXMATRIXA16 m; D3DXMatrixRotationY( &m, D3DX_PI ); g_mCenterMesh *= m; D3DXMatrixRotationX( &m, D3DX_PI / 2.0f ); g_mCenterMesh *= m; V_RETURN( CDXUTDirectionWidget::StaticOnD3D9CreateDevice( pd3dDevice ) ); g_LightControl.SetRadius( fObjectRadius ); // Read the D3DX effect file WCHAR str[MAX_PATH]; DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE | D3DXSHADER_NO_PRESHADER | D3DXFX_LARGEADDRESSAWARE; #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"BasicHLSL.fx" ) ); V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect9, NULL ) ); // Create the mesh texture from a file V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"tiny\\tiny_skin.dds" ) ); V_RETURN( D3DXCreateTextureFromFileEx( pd3dDevice, str, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &g_pMeshTexture9 ) ); // Set effect variables as needed D3DXCOLOR colorMtrlDiffuse( 1.0f, 1.0f, 1.0f, 1.0f ); D3DXCOLOR colorMtrlAmbient( 0.35f, 0.35f, 0.35f, 0 ); D3DXHANDLE hMaterialAmbientColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialAmbientColor" ); D3DXHANDLE hMaterialDiffuseColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialDiffuseColor" ); D3DXHANDLE hMeshTexture = g_pEffect9->GetParameterByName( NULL, "g_MeshTexture" ); V_RETURN( g_pEffect9->SetValue( hMaterialAmbientColor, &colorMtrlAmbient, sizeof( D3DXCOLOR ) ) ); V_RETURN( g_pEffect9->SetValue( hMaterialDiffuseColor, &colorMtrlDiffuse, sizeof( D3DXCOLOR ) ) ); V_RETURN( g_pEffect9->SetTexture( hMeshTexture, g_pMeshTexture9 ) ); g_hLightDir = g_pEffect9->GetParameterByName( NULL, "g_LightDir" ); g_hLightDiffuse = g_pEffect9->GetParameterByName( NULL, "g_LightDiffuse" ); g_hmWorldViewProjection = g_pEffect9->GetParameterByName( NULL, "g_mWorldViewProjection" ); g_hmWorld = g_pEffect9->GetParameterByName( NULL, "g_mWorld" ); g_hMaterialDiffuseColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialDiffuseColor" ); g_hfTime = g_pEffect9->GetParameterByName( NULL, "g_fTime" ); g_hnNumLights = g_pEffect9->GetParameterByName( NULL, "g_nNumLights" ); g_hRenderSceneWithTexture1Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture1Light" ); g_hRenderSceneWithTexture2Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture2Light" ); g_hRenderSceneWithTexture3Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture3Light" ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -15.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); g_Camera.SetRadius( fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f ); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependent on the back buffer //-------------------------------------------------------------------------------------- HRESULT PerlinFire::OnD3D11CreateDevice( ID3D11Device * pd3dDevice) { HRESULT hr; m_time = 0.0f; FirePosition = D3DXVECTOR3(0,0,0); const float lengthOfSide = 0.5f; //const float myparameter = 1.0f; SimpleVertex vertices[] = { { D3DXVECTOR3( -lengthOfSide, lengthOfSide, -lengthOfSide ) ,D3DXVECTOR3(0,0,0),D3DXVECTOR2(0,0)}, { D3DXVECTOR3( lengthOfSide, lengthOfSide, -lengthOfSide ) ,D3DXVECTOR3(0,0,0),D3DXVECTOR2(0,0)}, { D3DXVECTOR3( lengthOfSide, lengthOfSide, lengthOfSide ) ,D3DXVECTOR3(0,0,0),D3DXVECTOR2(0,0)}, { D3DXVECTOR3( -lengthOfSide, lengthOfSide, lengthOfSide ) ,D3DXVECTOR3(0,0,0),D3DXVECTOR2(0,0)}, { D3DXVECTOR3( -lengthOfSide, -lengthOfSide, -lengthOfSide ) ,D3DXVECTOR3(0,0,0),D3DXVECTOR2(0,0)}, { D3DXVECTOR3( lengthOfSide, -lengthOfSide, -lengthOfSide ) ,D3DXVECTOR3(0,0,0),D3DXVECTOR2(0,0)}, { D3DXVECTOR3( lengthOfSide, -lengthOfSide, lengthOfSide ) ,D3DXVECTOR3(0,0,0),D3DXVECTOR2(0,0)}, { D3DXVECTOR3( -lengthOfSide, -lengthOfSide, lengthOfSide ) ,D3DXVECTOR3(0,0,0),D3DXVECTOR2(0,0)}, }; D3D11_BUFFER_DESC bd; ZeroMemory( &bd, sizeof(bd) ); bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof( SimpleVertex ) * 8; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; D3D11_SUBRESOURCE_DATA InitData; ZeroMemory( &InitData, sizeof(InitData) ); InitData.pSysMem = vertices; V_RETURN(pd3dDevice->CreateBuffer( &bd, &InitData, &g_pBuffer )); WORD indices[] = { 3,1,0, 2,1,3, 0,5,4, 1,5,0, 3,4,7, 0,4,3, 1,6,5, 2,6,1, 2,7,6, 3,7,2, 6,4,5, 7,4,6, }; bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof( WORD ) * 36; // 36 vertices needed for 12 triangles in a triangle list bd.BindFlags = D3D11_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; InitData.pSysMem = indices; V_RETURN(pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIndex )); V_RETURN(LoadEffectFromFile(pd3dDevice,L"PerlinFire.hlsl",&g_pEffect)); // Obtain techniques g_pPerlinFire3D = g_pEffect->GetTechniqueByName( "PerlinFire3D" ); g_pPerlinFire3DFlow = g_pEffect->GetTechniqueByName( "PerlinFire3DFlow" ); g_pPerlinFire4D = g_pEffect->GetTechniqueByName( "PerlinFire4D" ); g_pGeometryTechnique = g_pEffect->GetTechniqueByName( "RenderGeometry" ); g_pGeometryTechniqueAux = g_pEffect->GetTechniqueByName( "RenderGeometryAux" ); g_pCurrentTechnique = g_pPerlinFire4D; // Obtain texture variables g_ptxScreenDepth = g_pEffect->GetVariableByName( "ScreenDepth" )->AsShaderResource(); g_ptxSceneTexture = g_pEffect->GetVariableByName( "SceneTexture" )->AsShaderResource(); g_ptxFireTex = g_pEffect->GetVariableByName( "FireShape" )->AsShaderResource(); g_ptxJitterTex = g_pEffect->GetVariableByName( "JitterTexture" )->AsShaderResource(); g_ptxPermTex = g_pEffect->GetVariableByName( "PermTexture" )->AsShaderResource(); g_pCubeMapTextureVariable = g_pEffect->GetVariableByName( "ShadowMap" )->AsShaderResource(); // Obtain miscellaneous variables g_pmCubeViewMatrixVariable = g_pEffect->GetVariableByName( "CubeViewMatrices" )->AsMatrix(); g_pmCubeProjMatrixVariable = g_pEffect->GetVariableByName( "CubeProjectionMatrix" )->AsMatrix(); g_pmWorldViewProj = g_pEffect->GetVariableByName( "WorldViewProj" )->AsMatrix(); g_pvEyePos = g_pEffect->GetVariableByName( "EyePos" )->AsVector(); g_pvLightPos = g_pEffect->GetVariableByName( "LightPos" )->AsVector(); g_pfLightIntensity = g_pEffect->GetVariableByName( "LightIntensity" )->AsScalar(); g_pfStepSize = g_pEffect->GetVariableByName( "StepSize" )->AsScalar(); g_pfTime = g_pEffect->GetVariableByName( "Time" )->AsScalar(); g_pfNoiseScale = g_pEffect->GetVariableByName( "NoiseScale" )->AsScalar(); g_pfRoughness = g_pEffect->GetVariableByName( "Roughness" )->AsScalar(); g_pfFrequencyWeights = g_pEffect->GetVariableByName( "FrequencyWeights" )->AsScalar(); g_pbJitter = g_pEffect->GetVariableByName( "bJitter" )->AsScalar(); g_piCubeMapFace = g_pEffect->GetVariableByName( "CubeMapFace" )->AsScalar(); // Set input layouts D3DX11_PASS_DESC PassDesc; D3D11_INPUT_ELEMENT_DESC geometryLayout [] = { { "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 } }; unsigned int numElements = sizeof (geometryLayout) / sizeof (geometryLayout [0]); g_pGeometryTechnique->GetPassByIndex(0)->GetDesc( & PassDesc ); V_RETURN(pd3dDevice->CreateInputLayout( geometryLayout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, & g_pGeometryVertexLayout )); // Load textures LoadTexture2D( pd3dDevice, L"Resource\\FireTexture\\Firetex.dds", & g_pFireTexture, & g_pFireTextureSRV ); // Create noise texture // Fill the texture with random numbers from 0 to 256 srand( GetTickCount() ); BYTE data[256 * 256]; for (int i = 0; i < 256 * 256; i++) { data[i] = (unsigned char) (rand () % 256); } D3D11_TEXTURE2D_DESC desc; desc.Width = 256; desc.Height = 256; desc.MipLevels = 1; desc.ArraySize = 1; desc.Format = DXGI_FORMAT_R8_TYPELESS; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Usage = D3D11_USAGE_IMMUTABLE; desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; desc.CPUAccessFlags = 0; desc.MiscFlags = 0; D3D11_SUBRESOURCE_DATA dataDesc; dataDesc.pSysMem = data; dataDesc.SysMemPitch = 256; V_RETURN(pd3dDevice->CreateTexture2D(&desc, &dataDesc, &g_pNoiseTexture)); // Create the shader resource view for jittering D3D11_SHADER_RESOURCE_VIEW_DESC descSRV; ZeroMemory( &descSRV, sizeof(descSRV) ); descSRV.Format = DXGI_FORMAT_R8_UNORM; descSRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; descSRV.Texture2D.MipLevels = 1; descSRV.Texture2D.MostDetailedMip = 0; V_RETURN(pd3dDevice->CreateShaderResourceView( g_pNoiseTexture, &descSRV, &g_pJitterTextureSRV )); // Create the shader resource view for permutation descSRV.Format = DXGI_FORMAT_R8_UINT; V_RETURN(pd3dDevice->CreateShaderResourceView( g_pNoiseTexture, &descSRV, &g_pPermTextureSRV )); V_RETURN(g_ptxFireTex -> SetResource (g_pFireTextureSRV)); V_RETURN(g_ptxJitterTex -> SetResource (g_pJitterTextureSRV)); V_RETURN(g_ptxPermTex -> SetResource (g_pPermTextureSRV)); //--------------------------------- //resize swap chain //--------------------------------- //---------------------------------- // frame move //---------------------------------- return S_OK; }
HRESULT CSkybox11::OnD3D11CreateDevice( ID3D11Device* pd3dDevice, float fSize, ID3D11Texture2D* pCubeTexture, ID3D11ShaderResourceView* pCubeRV ) { HRESULT hr; m_pd3dDevice11 = pd3dDevice; m_fSize = fSize; m_pEnvironmentMap11 = pCubeTexture; m_pEnvironmentRV11 = pCubeRV; ID3DBlob* pBlobVS = NULL; ID3DBlob* pBlobPS = NULL; // Create the shaders tstring filePath = GetFilePath::GetFilePath(_T("skybox.hlsl")); // V_RETURN( CompileShaderFromFile( L"skybox11.hlsl", "SkyboxVS", "vs_4_0", &pBlobVS ) ); // V_RETURN( CompileShaderFromFile( L"skybox11.hlsl", "SkyboxPS", "ps_4_0", &pBlobPS ) ); V_RETURN( CompileShaderFromFile( (WCHAR *)filePath.c_str(), "SkyboxVS", "vs_4_0", &pBlobVS ) ); V_RETURN( CompileShaderFromFile( (WCHAR *)filePath.c_str(), "SkyboxPS", "ps_4_0", &pBlobPS ) ); V_RETURN( pd3dDevice->CreateVertexShader( pBlobVS->GetBufferPointer(), pBlobVS->GetBufferSize(), NULL, &m_pVertexShader ) ); V_RETURN( pd3dDevice->CreatePixelShader( pBlobPS->GetBufferPointer(), pBlobPS->GetBufferSize(), NULL, &m_pPixelShader ) ); // Create an input layout V_RETURN( pd3dDevice->CreateInputLayout( g_aVertexLayout, 1, pBlobVS->GetBufferPointer(), pBlobVS->GetBufferSize(), &m_pVertexLayout11 ) ); SAFE_RELEASE( pBlobVS ); SAFE_RELEASE( pBlobPS ); // Query support for linear filtering on DXGI_FORMAT_R32G32B32A32 UINT FormatSupport = 0; V_RETURN( pd3dDevice->CheckFormatSupport( DXGI_FORMAT_R32G32B32A32_FLOAT, &FormatSupport ) ); // Setup linear or point sampler according to the format Query result D3D11_SAMPLER_DESC SamDesc; SamDesc.Filter = ( FormatSupport & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE ) > 0 ? D3D11_FILTER_MIN_MAG_MIP_LINEAR : D3D11_FILTER_MIN_MAG_MIP_POINT; SamDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.MipLODBias = 0.0f; SamDesc.MaxAnisotropy = 1; SamDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; SamDesc.BorderColor[0] = SamDesc.BorderColor[1] = SamDesc.BorderColor[2] = SamDesc.BorderColor[3] = 0; SamDesc.MinLOD = 0; SamDesc.MaxLOD = 0; V_RETURN( pd3dDevice->CreateSamplerState( &SamDesc, &m_pSam ) ); // Setup constant buffer D3D11_BUFFER_DESC Desc; Desc.Usage = D3D11_USAGE_DYNAMIC; Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; Desc.MiscFlags = 0; Desc.ByteWidth = sizeof( CB_VS_PER_OBJECT ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &m_pcbVSPerObject ) ); // Depth stencil state D3D11_DEPTH_STENCIL_DESC DSDesc; ZeroMemory( &DSDesc, sizeof( D3D11_DEPTH_STENCIL_DESC ) ); DSDesc.DepthEnable = FALSE; DSDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; DSDesc.DepthFunc = D3D11_COMPARISON_LESS; DSDesc.StencilEnable = FALSE; V_RETURN( pd3dDevice->CreateDepthStencilState( &DSDesc, &m_pDepthStencilState11 ) ); return S_OK; }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // reset, which will happen after a lost device scenario. This is the best location to // create D3DPOOL_DEFAULT resources since these resources need to be reloaded whenever // the device is lost. Resources created here should be released in the OnLostDevice // callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnResetDevice(IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext) { HRESULT hr; D3DVERTEXELEMENT9 elements[8]; elements[0].Stream = 0; elements[0].Offset = 0; elements[0].Type = D3DDECLTYPE_FLOAT3; elements[0].Method = D3DDECLMETHOD_DEFAULT; elements[0].Usage = D3DDECLUSAGE_POSITION; elements[0].UsageIndex = 0; elements[1].Stream = 0; elements[1].Offset = 12; elements[1].Type = D3DDECLTYPE_FLOAT3; elements[1].Method = D3DDECLMETHOD_DEFAULT; elements[1].Usage = D3DDECLUSAGE_NORMAL; elements[1].UsageIndex = 0; elements[2].Stream = 0; elements[2].Offset = 24; elements[2].Type = D3DDECLTYPE_FLOAT3; elements[2].Method = D3DDECLMETHOD_DEFAULT; elements[2].Usage = D3DDECLUSAGE_TANGENT; elements[2].UsageIndex = 0; elements[3].Stream = 0; elements[3].Offset = 36; elements[3].Type = D3DDECLTYPE_FLOAT3; elements[3].Method = D3DDECLMETHOD_DEFAULT; elements[3].Usage = D3DDECLUSAGE_BINORMAL; elements[3].UsageIndex = 0; elements[4].Stream = 1; elements[4].Offset = 0; elements[4].Type = D3DDECLTYPE_FLOAT2; elements[4].Method = D3DDECLMETHOD_DEFAULT; elements[4].Usage = D3DDECLUSAGE_TEXCOORD; elements[4].UsageIndex = 0; elements[5].Stream = 0xff; elements[5].Offset = 0; elements[5].Type = D3DDECLTYPE_UNUSED; elements[5].Method = 0; elements[5].Usage = 0; elements[5].UsageIndex = 0; V(pd3dDevice->CreateVertexDeclaration(elements, &g_pVdMesh)); elements[5].Stream = 2; elements[5].Offset = 0; elements[5].Type = D3DDECLTYPE_UBYTE4; elements[5].Method = D3DDECLMETHOD_DEFAULT; elements[5].Usage = D3DDECLUSAGE_BLENDINDICES; elements[5].UsageIndex = 0; elements[6].Stream = 2; elements[6].Offset = 4; elements[6].Type = D3DDECLTYPE_FLOAT4; elements[6].Method = D3DDECLMETHOD_DEFAULT; elements[6].Usage = D3DDECLUSAGE_BLENDWEIGHT; elements[6].UsageIndex = 0; elements[7].Stream = 0xff; elements[7].Offset = 0; elements[7].Type = D3DDECLTYPE_UNUSED; elements[7].Method = 0; elements[7].Usage = 0; elements[7].UsageIndex = 0; V(pd3dDevice->CreateVertexDeclaration(elements, &g_pVdMeshGpuSkinning)); elements[0].Stream = 0; elements[0].Offset = 0; elements[0].Type = D3DDECLTYPE_FLOAT3; elements[0].Method = D3DDECLMETHOD_DEFAULT; elements[0].Usage = D3DDECLUSAGE_POSITION; elements[0].UsageIndex = 0; elements[1].Stream = 0; elements[1].Offset = 12; elements[1].Type = D3DDECLTYPE_FLOAT2; elements[1].Method = D3DDECLMETHOD_DEFAULT; elements[1].Usage = D3DDECLUSAGE_TEXCOORD; elements[1].UsageIndex = 0; elements[2].Stream = 0xff; elements[2].Offset = 0; elements[2].Type = D3DDECLTYPE_UNUSED; elements[2].Method = 0; elements[2].Usage = 0; elements[2].UsageIndex = 0; V(pd3dDevice->CreateVertexDeclaration(elements, &g_pVdFloor)); //地板 V_RETURN(pd3dDevice->CreateVertexBuffer(6 * (sizeof(D3DXVECTOR3) + sizeof(D3DXVECTOR2)), D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &g_pVbFloor, NULL)); V_RETURN(g_DialogResourceManager.OnD3D9ResetDevice()); if(g_pFont) V_RETURN(g_pFont->OnResetDevice()); if(g_pEffect) V_RETURN(g_pEffect->OnResetDevice()); // Create a sprite to help batch calls when drawing many lines of text V_RETURN(D3DXCreateSprite(pd3dDevice, &g_pSprite)); V_RETURN(::D3DXCreateTextureFromFile(pd3dDevice, L"Test/floor.dds", &g_pTexFloor)); g_SampleUI.SetLocation(pBackBufferSurfaceDesc->Width-170, 0); g_SampleUI.SetSize(170, 300); float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; g_camera.setPerspectiveParams(D3DX_PI/3, fAspectRatio, 0.1f, 200.0f); return S_OK; }
HRESULT WEHDR::CreateViews() { HRESULT hr; if (!m_pd3dDevice) return S_FALSE; ReleaseViews(); int nSampleLen = 1; for(int i = 0; i < NUM_TONEMAP_TEXTURES; i++) { D3D11_TEXTURE2D_DESC tmdesc; ZeroMemory(&tmdesc, sizeof(D3D11_TEXTURE2D_DESC)); tmdesc.ArraySize = 1; tmdesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; tmdesc.Usage = D3D11_USAGE_DEFAULT; tmdesc.Format = DXGI_FORMAT_R32_FLOAT; tmdesc.Width = nSampleLen; tmdesc.Height = nSampleLen; tmdesc.MipLevels = 1; tmdesc.SampleDesc.Count = 1; V_RETURN(m_pd3dDevice->CreateTexture2D(&tmdesc, NULL, &m_pTexToneMap[i])); // Create the render target view D3D11_RENDER_TARGET_VIEW_DESC DescRT; DescRT.Format = tmdesc.Format; DescRT.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; DescRT.Texture2D.MipSlice = 0; V_RETURN(m_pd3dDevice->CreateRenderTargetView(m_pTexToneMap[i], &DescRT, &m_pTexToneMapRTV[i])); // Create the shader resource view D3D11_SHADER_RESOURCE_VIEW_DESC DescRV; DescRV.Format = tmdesc.Format; DescRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; DescRV.Texture2D.MipLevels = 1; DescRV.Texture2D.MostDetailedMip = 0; V_RETURN(m_pd3dDevice->CreateShaderResourceView(m_pTexToneMap[i], &DescRV, &m_pTexToneMapRV[i])); nSampleLen *= 3; } UINT width, height; WE::D3D().GetScreen(&width, &height); // Create the temporary blooming effect textures for PS path and buffers for CS path for( int i = 0; i < NUM_BLOOM_TEXTURES; i++ ) { // Texture for blooming effect in PS path D3D11_TEXTURE2D_DESC bmdesc; ZeroMemory( &bmdesc, sizeof( D3D11_TEXTURE2D_DESC ) ); bmdesc.ArraySize = 1; bmdesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; bmdesc.Usage = D3D11_USAGE_DEFAULT; bmdesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; bmdesc.Width = width / 8; bmdesc.Height = height / 8; bmdesc.MipLevels = 1; bmdesc.SampleDesc.Count = 1; V_RETURN( m_pd3dDevice->CreateTexture2D( &bmdesc, NULL, &m_pTexBloom[i] ) ); // Create the render target view D3D11_RENDER_TARGET_VIEW_DESC DescRT; DescRT.Format = bmdesc.Format; DescRT.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; DescRT.Texture2D.MipSlice = 0; V_RETURN( m_pd3dDevice->CreateRenderTargetView( m_pTexBloom[i], &DescRT, &m_pTexBloomRTV[i] ) ); // Create the shader resource view D3D11_SHADER_RESOURCE_VIEW_DESC DescRV; DescRV.Format = bmdesc.Format; DescRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; DescRV.Texture2D.MipLevels = 1; DescRV.Texture2D.MostDetailedMip = 0; V_RETURN( m_pd3dDevice->CreateShaderResourceView( m_pTexBloom[i], &DescRV, &m_pTexBloomRV[i] ) ); } // Create the final pass texture D3D11_TEXTURE2D_DESC Desc; ZeroMemory( &Desc, sizeof( D3D11_TEXTURE2D_DESC ) ); Desc.ArraySize = 1; Desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; Desc.Usage = D3D11_USAGE_DEFAULT; Desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; Desc.Width = width; Desc.Height = height; Desc.MipLevels = 1; Desc.SampleDesc.Count = 1; V_RETURN( m_pd3dDevice->CreateTexture2D( &Desc, NULL, &m_pTexFinal ) ); // Create the render target view D3D11_RENDER_TARGET_VIEW_DESC DescRT; DescRT.Format = Desc.Format; DescRT.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; DescRT.Texture2D.MipSlice = 0; V_RETURN( m_pd3dDevice->CreateRenderTargetView( m_pTexFinal, &DescRT, &m_pTexFinalRTV ) ); // Create the resource view D3D11_SHADER_RESOURCE_VIEW_DESC DescRV; DescRV.Format = Desc.Format; DescRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; DescRV.Texture2D.MipLevels = 1; DescRV.Texture2D.MostDetailedMip = 0; V_RETURN( m_pd3dDevice->CreateShaderResourceView( m_pTexFinal, &DescRV, &m_pTexFinalRV ) ); // Create the bright pass texture Desc.Width /= 8; Desc.Height /= 8; Desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; V_RETURN( m_pd3dDevice->CreateTexture2D( &Desc, NULL, &m_pTexBrightPass ) ); // Create the render target view DescRT.Format = Desc.Format; DescRT.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; DescRT.Texture2D.MipSlice = 0; V_RETURN( m_pd3dDevice->CreateRenderTargetView( m_pTexBrightPass, &DescRT, &m_pTexBrightPassRTV ) ); // Create the resource view DescRV.Format = Desc.Format; DescRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; DescRV.Texture2D.MipLevels = 1; DescRV.Texture2D.MostDetailedMip = 0; V_RETURN( m_pd3dDevice->CreateShaderResourceView( m_pTexBrightPass, &DescRV, &m_pTexBrightPassRV ) ); return S_OK; }
HRESULT ParticleRenderer::OnD3D11CreateDevice(ID3D11Device* pd3dDevice, ContentManager* pContentManager, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc) { HRESULT hr; // Load pixel shader PixelShaderOptions psOpts = { "PS_Particle", // const char* EntryPoint; NULL, // D3D_SHADER_MACRO* Defines; "Particle", // const char* DebugName; }; V_RETURN(pContentManager->LoadContent(pd3dDevice, L"Particle.hlsl", &psOpts, &_ps)); // Load geometry shader GeometryShaderOptions gsOpts = { "GS_Particle", // const char* EntryPoint; NULL, // D3D_SHADER_MACRO* Defines; "Particle", // const char* DebugName; }; V_RETURN(pContentManager->LoadContent(pd3dDevice, L"Particle.hlsl", &gsOpts, &_gs)); // Load vertex shader (using ParticleVertex) D3D11_INPUT_ELEMENT_DESC layout_particle[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "PREVPOSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "RADIUS", 0, DXGI_FORMAT_R32_FLOAT, 0, 40, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "PREVRADIUS", 0, DXGI_FORMAT_R32_FLOAT, 0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "ROTATION", 0, DXGI_FORMAT_R32_FLOAT, 0, 48, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "PREVROTATION", 0, DXGI_FORMAT_R32_FLOAT, 0, 52, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; VertexShaderOptions vsOpts = { "VS_Particle", // const char* EntryPoint; NULL, // D3D_SHADER_MACRO* Defines; layout_particle, // D3D11_INPUT_ELEMENT_DESC* InputElements; ARRAYSIZE(layout_particle), // UINT InputElementCount; "Particle" // const char* DebugName; }; V_RETURN(pContentManager->LoadContent(pd3dDevice, L"Particle.hlsl", &vsOpts, &_vs)); D3D11_BUFFER_DESC bufferDesc = { 0, //UINT ByteWidth; D3D11_USAGE_DYNAMIC, //D3D11_USAGE Usage; D3D11_BIND_CONSTANT_BUFFER, //UINT BindFlags; D3D11_CPU_ACCESS_WRITE, //UINT CPUAccessFlags; 0, //UINT MiscFlags; 0, //UINT StructureByteStride; }; bufferDesc.ByteWidth = sizeof(CB_PARTICLE_PROPERTIES); V_RETURN(pd3dDevice->CreateBuffer(&bufferDesc, NULL, &_particleCB)); V_RETURN(SetDXDebugName(_particleCB, "Particle Renderer particle CB")); bufferDesc.ByteWidth = sizeof(CB_CAMERA_PROPERTIES); V_RETURN(pd3dDevice->CreateBuffer(&bufferDesc, NULL, &_cameraCB)); V_RETURN(SetDXDebugName(_particleCB, "Particle Renderer camera CB")); D3D11_BLEND_DESC blendDesc; blendDesc.AlphaToCoverageEnable = false; blendDesc.IndependentBlendEnable = true; blendDesc.RenderTarget[0].BlendEnable = true; blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; for (UINT i = 1; i < 8; i++) { blendDesc.RenderTarget[i].BlendEnable = true; blendDesc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD; blendDesc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD; blendDesc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; blendDesc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; blendDesc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; blendDesc.RenderTarget[i].SrcBlend = D3D11_BLEND_SRC_ALPHA; blendDesc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE; } V_RETURN(pd3dDevice->CreateBlendState(&blendDesc, &_particleBlend)); V_RETURN(_dsStates.OnD3D11CreateDevice(pd3dDevice, pContentManager, pBackBufferSurfaceDesc)); V_RETURN(_samplerStates.OnD3D11CreateDevice(pd3dDevice, pContentManager, pBackBufferSurfaceDesc)); V_RETURN(_blendStates.OnD3D11CreateDevice(pd3dDevice, pContentManager, pBackBufferSurfaceDesc)); V_RETURN(_rasterStates.OnD3D11CreateDevice(pd3dDevice, pContentManager, pBackBufferSurfaceDesc)); return S_OK; }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // created, which will happen during application initialization and windowed/full screen // toggles. This is the best location to create D3DPOOL_MANAGED resources since these // resources need to be reloaded whenever the device is destroyed. Resources created // here should be released in the OnDestroyDevice callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; WCHAR str[MAX_PATH]; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); // Create the mesh and load it with data already gathered from a file V_RETURN( g_MeshLoader.Create( pd3dDevice, L"media\\cup.obj" ) ); // Add the identified material subsets to the UI CDXUTComboBox* pComboBox = g_SampleUI.GetComboBox( IDC_SUBSET ); pComboBox->RemoveAllItems(); pComboBox->AddItem( L"All", ( void* )( INT_PTR )-1 ); for( UINT i = 0; i < g_MeshLoader.GetNumMaterials(); i++ ) { Material* pMaterial = g_MeshLoader.GetMaterial( i ); pComboBox->AddItem( pMaterial->strName, ( void* )( INT_PTR )i ); } // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the // shader debugger. Debugging vertex shaders requires either REF or software vertex // processing, and debugging pixel shaders requires REF. The // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the // shader debugger. It enables source level debugging, prevents instruction // reordering, prevents dead code elimination, and forces the compiler to compile // against the next higher available software target, which ensures that the // unoptimized shaders do not exceed the shader model limitations. Setting these // flags will cause slower rendering since the shaders will be unoptimized and // forced into software. See the DirectX documentation for more information about // using the shader debugger. DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DXSHADER_DEBUG; #endif #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif // Read the D3DX effect file V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"MeshFromOBJ.fx" ) ); // If this fails, there should be debug output as to // they the .fx file failed to compile V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) ); // Cache the effect handles g_hAmbient = g_pEffect->GetParameterBySemantic( 0, "Ambient" ); g_hDiffuse = g_pEffect->GetParameterBySemantic( 0, "Diffuse" ); g_hSpecular = g_pEffect->GetParameterBySemantic( 0, "Specular" ); g_hOpacity = g_pEffect->GetParameterBySemantic( 0, "Opacity" ); g_hSpecularPower = g_pEffect->GetParameterBySemantic( 0, "SpecularPower" ); g_hLightColor = g_pEffect->GetParameterBySemantic( 0, "LightColor" ); g_hLightPosition = g_pEffect->GetParameterBySemantic( 0, "LightPosition" ); g_hCameraPosition = g_pEffect->GetParameterBySemantic( 0, "CameraPosition" ); g_hTexture = g_pEffect->GetParameterBySemantic( 0, "Texture" ); g_hTime = g_pEffect->GetParameterBySemantic( 0, "Time" ); g_hWorld = g_pEffect->GetParameterBySemantic( 0, "World" ); g_hWorldViewProjection = g_pEffect->GetParameterBySemantic( 0, "WorldViewProjection" ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 2.0f, 1.0f, 0.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); return S_OK; }
HRESULT ParticleRenderer::RenderParticles(ID3D11DeviceContext* pd3dDeviceContext, vector<ParticleSystemInstance*>* instances, Camera* camera, GBuffer* gBuffer) { UINT partCount = instances->size(); if (partCount > 0) { HRESULT hr; // Sort systems by the emitter's depth from the camera XMFLOAT3 camForward = camera->GetForward(); std::vector<PARTICLE_SYSTEM_INFO> depthVec = std::vector<PARTICLE_SYSTEM_INFO>(partCount); for (UINT i = 0; i < partCount; i++) { XMFLOAT3 systemPos = instances->at(i)->GetPosition(); float depth = systemPos.x * camForward.x + systemPos.y * camForward.y + systemPos.z * camForward.z; depthVec[i] = PARTICLE_SYSTEM_INFO(i, depth); } std::sort(depthVec.begin(), depthVec.end(), depthCompare); pd3dDeviceContext->VSSetShader(_vs->VertexShader, NULL, 0); pd3dDeviceContext->GSSetShader(_gs->GeometryShader, NULL, 0); pd3dDeviceContext->PSSetShader(_ps->PixelShader, NULL, 0); pd3dDeviceContext->IASetInputLayout(_vs->InputLayout); float blendFactor[4] = {1, 1, 1, 1}; pd3dDeviceContext->OMSetBlendState(_particleBlend, blendFactor, 0xFFFFFFFF); ID3D11SamplerState* samplers[2] = { _samplerStates.GetLinearClamp(), _samplerStates.GetPointClamp(), }; pd3dDeviceContext->PSSetSamplers(0, 2, samplers); pd3dDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); pd3dDeviceContext->OMSetDepthStencilState(_dsStates.GetDepthEnabled(), 0); XMFLOAT4X4 fViewProj = camera->GetViewProjection(); XMMATRIX viewProj = XMLoadFloat4x4(&fViewProj); XMFLOAT4X4 fInvView = camera->GetWorld(); XMMATRIX invView = XMLoadFloat4x4(&fInvView); XMFLOAT4X4 fPrevViewProj = camera->GetPreviousViewProjection(); XMMATRIX prevViewProj = XMLoadFloat4x4(&fPrevViewProj); XMFLOAT4X4 fPrevInvView = camera->GetPreviousWorld(); XMMATRIX prevInvView = XMLoadFloat4x4(&fPrevInvView); D3D11_MAPPED_SUBRESOURCE mappedResource; // Map Geometry shader perameters V_RETURN(pd3dDeviceContext->Map(_particleCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)); CB_PARTICLE_PROPERTIES* particleProperties = (CB_PARTICLE_PROPERTIES*)mappedResource.pData; XMStoreFloat4x4(&particleProperties->ViewProjection, XMMatrixTranspose(viewProj)); XMStoreFloat4x4(&particleProperties->InverseView, XMMatrixTranspose(invView)); XMStoreFloat4x4(&particleProperties->PrevViewProjection, XMMatrixTranspose(prevViewProj)); XMStoreFloat4x4(&particleProperties->PrevInverseView, XMMatrixTranspose(prevInvView)); pd3dDeviceContext->Unmap(_particleCB, 0); pd3dDeviceContext->GSSetConstantBuffers(0, 1, &_particleCB); // Map pixel shader parameters V_RETURN(pd3dDeviceContext->Map(_cameraCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)); CB_CAMERA_PROPERTIES* cameraProperties = (CB_CAMERA_PROPERTIES*)mappedResource.pData; cameraProperties->CameraNearClip = camera->GetNearClip(); cameraProperties->CameraFarClip = camera->GetFarClip(); cameraProperties->FadeDistance = _fadeDistance; cameraProperties->Forward = camera->GetForward(); cameraProperties->Up = camera->GetUp(); cameraProperties->Right = camera->GetRight(); pd3dDeviceContext->Unmap(_cameraCB, 0); pd3dDeviceContext->PSSetConstantBuffers(0, 1, &_cameraCB); ID3D11ShaderResourceView* depthSRV = gBuffer->GetDepthSRV(); pd3dDeviceContext->PSSetShaderResources(2, 1, &depthSRV); for (UINT i = 0; i < partCount; i++) { ParticleSystemInstance* system = instances->at(depthVec[i].System); ID3D11ShaderResourceView* srvs[2] = { system->GetDiffuseSRV(), system->GetNormalSRV() }; pd3dDeviceContext->PSSetShaderResources(0, 2, srvs); ID3D11Buffer* vertexBuffers[1] = { system->GetParticleVertexBuffer(pd3dDeviceContext, camera) }; UINT strides[1] = { system->GetVertexStride() }; UINT offsets[1] = { 0 }; pd3dDeviceContext->IASetVertexBuffers(0, 1, vertexBuffers, strides, offsets); pd3dDeviceContext->Draw(system->GetParticleCount(), 0); } ID3D11ShaderResourceView* nullSrvs[2] = { NULL, NULL }; pd3dDeviceContext->PSSetShaderResources(0, 2, nullSrvs); ID3D11Buffer* nullBuf = NULL; pd3dDeviceContext->GSSetConstantBuffers(0, 1, &nullBuf); pd3dDeviceContext->PSSetConstantBuffers(1, 1, &nullBuf); pd3dDeviceContext->VSSetShader(NULL, NULL, 0); pd3dDeviceContext->GSSetShader(NULL, NULL, 0); pd3dDeviceContext->PSSetShader(NULL, NULL, 0); } return S_OK; }
//-------------------------------------------------------------------------------------- // This function loads a new technique and all device objects it requires. //-------------------------------------------------------------------------------------- HRESULT LoadTechniqueObjects( const char* szMedia ) { HRESULT hr = S_OK; if( NULL == g_pEffect ) return D3DERR_INVALIDCALL; IDirect3DTexture9* pTexture = NULL; IDirect3DCubeTexture9* pCubeTexture = NULL; IDirect3DDevice9* pDevice = DXUTGetD3D9Device(); WCHAR strFileName[MAX_PATH+1] = {0}; WCHAR strPath[MAX_PATH+1] = {0}; char strTechnique[MAX_PATH] = {0}; // Make sure the technique works char* strComboTech = ( char* )g_SampleUI.GetComboBox( IDC_TECHNIQUE )->GetSelectedData(); strcpy_s( strTechnique, MAX_PATH, strComboTech ); bool bLDPRT = ( strTechnique && ( 0 == strcmp( strTechnique, "LDPRT" ) ) ); // If we're not a signed format, make sure we use a technnique that will unbias if( D3DFMT_Q16W16V16U16 != g_fmtTexture && D3DFMT_Q8W8V8U8 != g_fmtTexture ) strcat_s( strTechnique, MAX_PATH, "_Unbias" ); D3DXHANDLE hTechnique = g_pEffect->GetTechniqueByName( strTechnique ); V_RETURN( g_pEffect->SetTechnique( hTechnique ) ); // Enable/disable LDPRT-only items g_SampleUI.GetStatic( IDC_ENV_LABEL )->SetEnabled( bLDPRT ); g_SampleUI.GetSlider( IDC_ENV_SLIDER )->SetEnabled( bLDPRT ); g_SampleUI.GetSlider( IDC_RED_TRANSMIT_SLIDER )->SetEnabled( bLDPRT ); g_SampleUI.GetSlider( IDC_GREEN_TRANSMIT_SLIDER )->SetEnabled( bLDPRT ); g_SampleUI.GetSlider( IDC_BLUE_TRANSMIT_SLIDER )->SetEnabled( bLDPRT ); g_SampleUI.GetStatic( IDC_RED_TRANSMIT_LABEL )->SetEnabled( bLDPRT ); g_SampleUI.GetStatic( IDC_GREEN_TRANSMIT_LABEL )->SetEnabled( bLDPRT ); g_SampleUI.GetStatic( IDC_BLUE_TRANSMIT_LABEL )->SetEnabled( bLDPRT ); // Load the mesh swprintf_s( strFileName, MAX_PATH, TEXT( "media\\%S" ), szMedia ); V_RETURN( LoadLDPRTData( pDevice, strFileName ) ); // Albedo texture swprintf_s( strFileName, MAX_PATH, TEXT( "media\\%SAlbedo.dds" ), szMedia ); DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, strFileName ); V( D3DXCreateTextureFromFile( pDevice, strPath, &pTexture ) ); g_pEffect->SetTexture( "Albedo", pTexture ); SAFE_RELEASE( pTexture ); // Normal map swprintf_s( strFileName, MAX_PATH, TEXT( "media\\%SNormalMap.dds" ), szMedia ); DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, strFileName ); V( D3DXCreateTextureFromFileEx( pDevice, strPath, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, g_fmtTexture, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pTexture ) ); g_pEffect->SetTexture( "NormalMap", pTexture ); SAFE_RELEASE( pTexture ); // Spherical harmonic basic functions char* pNames[4] = {"YlmCoeff0","YlmCoeff4","YlmCoeff8","YlmCoeff12"}; for( int i = 0; i < 4; i++ ) { D3DXCreateCubeTexture( pDevice, 32, 1, 0, g_fmtCubeMap, D3DPOOL_MANAGED, &pCubeTexture ); D3DXFillCubeTexture( pCubeTexture, myFillBF, ( LPVOID )( INT_PTR )( i * 4 ) ); g_pEffect->SetTexture( pNames[i], pCubeTexture ); SAFE_RELEASE( pCubeTexture ); } return S_OK; }
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; }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that depend on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) ); V_RETURN( g_D3DSettingsDlg.OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) ); // Setup the camera's projection parameters width = pBackBufferSurfaceDesc->Width; height = pBackBufferSurfaceDesc->Height; float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 2.0f, 100000.0f ); //g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); //g_Camera.SetButtonMasks( MOUSE_MIDDLE_BUTTON, MOUSE_WHEEL, MOUSE_LEFT_BUTTON ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 300 ); g_SampleUI.SetSize( 170, 300 ); //deboard.OnD3D11ResizedSwapChain(pd3dDevice,pSwapChain,pBackBufferSurfaceDesc,pUserContext); DXGI_FORMAT format; D3D11_TEXTURE2D_DESC info ; ZeroMemory (& info , sizeof ( info )); info.Width = pBackBufferSurfaceDesc->Width; info.Height = pBackBufferSurfaceDesc->Height; info.MipLevels = 1; info.ArraySize = 1; info.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; info.SampleDesc . Count = 1; info.Usage = D3D11_USAGE_DEFAULT; info.BindFlags = D3D11_BIND_RENDER_TARGET|D3D11_BIND_SHADER_RESOURCE ; info.CPUAccessFlags = 0; info.MiscFlags = 0; hr = pd3dDevice->CreateTexture2D(&info,nullptr,&original_texture); hr = pd3dDevice->CreateTexture2D(&info,nullptr,&texture1); hr = pd3dDevice->CreateTexture2D(&info,nullptr,&texture2); format = info.Format; D3D11_SAMPLER_DESC sampDesc; ZeroMemory( &sampDesc, sizeof(sampDesc) ); sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP ; sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP ; sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP ; sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; sampDesc.MinLOD = 0; sampDesc.MaxLOD = 0; hr = pd3dDevice->CreateSamplerState( &sampDesc, &g_pSamLinear ); // render target view D3D11_RENDER_TARGET_VIEW_DESC target_info; target_info.Format = format; target_info.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; target_info.Texture2D.MipSlice = 0; hr = pd3dDevice->CreateRenderTargetView(original_texture, &target_info ,&rtv_render_to_texture_ori); hr = pd3dDevice->CreateRenderTargetView(texture1, &target_info ,&rtv_render_to_texture_1); hr = pd3dDevice->CreateRenderTargetView(texture2, &target_info ,&rtv_render_to_texture_2); D3D11_SHADER_RESOURCE_VIEW_DESC shader_resource_info; shader_resource_info.Format = format; shader_resource_info.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; shader_resource_info.Texture2D . MostDetailedMip = 0; shader_resource_info.Texture2D . MipLevels = 1; hr = pd3dDevice->CreateShaderResourceView(original_texture, &shader_resource_info ,&sr_texture_original); hr = pd3dDevice->CreateShaderResourceView(texture1, &shader_resource_info ,&sr_texture1); hr = pd3dDevice->CreateShaderResourceView(texture2, &shader_resource_info ,&sr_texture2); return S_OK; }
void Hill::init_shader(ID3D11Device *pD3D11Device, HWND hWnd) { DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DCOMPILE_DEBUG; // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif #if D3D_COMPILER_VERSION >= 46 // Read the D3DX effect file HRESULT hr = S_OK; D3DX11CompileEffectFromFile(L"wave.fx", nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, dwShaderFlags, 0, pD3D11Device, &m_pEffect, nullptr); #else ID3DBlob* pEffectBuffer = nullptr; V_RETURN(DXUTCompileFromFile(L"Tutorial11.fx", nullptr, "none", "fx_5_0", dwShaderFlags, 0, &pEffectBuffer)); hr = D3DX11CreateEffectFromMemory(pEffectBuffer->GetBufferPointer(), pEffectBuffer->GetBufferSize(), 0, pd3dDevice, &m_pEffect); SAFE_RELEASE(pEffectBuffer); if (FAILED(hr)) return hr; #endif m_pEffectTechnique = m_pEffect->GetTechniqueByName("ModelTech"); m_pFxWorld = m_pEffect->GetVariableByName("g_World")->AsMatrix(); m_pFxView = m_pEffect->GetVariableByName("g_View")->AsMatrix(); m_pFxProj = m_pEffect->GetVariableByName("g_Proj")->AsMatrix(); m_pFxEyePos = m_pEffect->GetVariableByName("g_EyePos")->AsVector(); m_pFxDirLight = m_pEffect->GetVariableByName("g_DirLight"); m_pFxPointLight = m_pEffect->GetVariableByName("g_PointLight"); m_pFxSpotLight = m_pEffect->GetVariableByName("g_SpotLight"); m_pFxMaterial = m_pEffect->GetVariableByName("g_Mat"); m_pFxDiffuseMap = m_pEffect->GetVariableByName("g_DiffuseMap")->AsShaderResource(); // Done with compiled shader. 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 } }; D3DX11_PASS_DESC passDesc; m_pEffectTechnique->GetPassByIndex(0)->GetDesc(&passDesc); pD3D11Device->CreateInputLayout(vertexDesc, 3, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &m_pInputLayout); }
// // Alpha_D3D11VertexPosColorNormalShader::OnRestore() // HRESULT Alpha_D3D11VertexPosColorNormalShader::OnRestore(Scene *pScene) { HRESULT hr = S_OK; SAFE_RELEASE(m_pVertexLayout); SAFE_RELEASE(m_pVertexShader); SAFE_RELEASE(m_pcbVSMatrices); SAFE_RELEASE(m_pcbVSLighting); SAFE_RELEASE(m_pcbVSMaterial); shared_ptr<D3DRenderer11> d3dRenderer11 = static_pointer_cast<D3DRenderer11>(pScene->GetRenderer()); //======================================================== // Set up the vertex shader and related constant buffers ID3DBlob* pVertexShaderBuffer = NULL; // Load the file via the Resource controller std::string hlslFileName = "Effects\\Alpha_D3D11VertexPosColorNormal.fx"; Resource resource(hlslFileName.c_str()); shared_ptr<ResHandle> pResourceHandle = g_pApp->m_ResCache->GetHandle(&resource); // Compile the shader if(FAILED(d3dRenderer11->CompileShader(pResourceHandle->Buffer(), pResourceHandle->Size(), hlslFileName.c_str(), "Alpha_VSMaterial", "vs_5_0", &pVertexShaderBuffer))) { SAFE_RELEASE (pVertexShaderBuffer); return hr; } // Create the shader if(FAILED(g_pDX11W->GetD3D11Device()->CreateVertexShader(pVertexShaderBuffer->GetBufferPointer(),pVertexShaderBuffer->GetBufferSize(), NULL, &m_pVertexShader))) { SAFE_RELEASE (pVertexShaderBuffer); return hr; } //Create the Input Layout if (SUCCEEDED(g_pDX11W->GetD3D11Device()->CreateInputLayout(D3D11VertexPosColorNormalLayout, D3D11VertexPosColorNormalLayoutSize, pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), &m_pVertexLayout))) { // Setup constant buffers D3D11_BUFFER_DESC constantBufferDesc; ZeroMemory(&constantBufferDesc, sizeof(constantBufferDesc)); constantBufferDesc.Usage = D3D11_USAGE_DYNAMIC; constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; constantBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; constantBufferDesc.MiscFlags = 0; // Create matrix constant buffer constantBufferDesc.ByteWidth = sizeof(ConstantBuffer_Matrices); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&constantBufferDesc, NULL, &m_pcbVSMatrices)); // Create lighting constant buffer constantBufferDesc.ByteWidth = sizeof(ConstantBuffer_Lighting); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&constantBufferDesc, NULL, &m_pcbVSLighting)); // Create material constant buffer constantBufferDesc.ByteWidth = sizeof(ConstantBuffer_Material); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&constantBufferDesc, NULL, &m_pcbVSMaterial)); } SAFE_RELEASE (pVertexShaderBuffer); return hr; }
HRESULT DX11SceneRepHashSDF::OnD3D11CreateDevice( ID3D11Device* pd3dDevice ) { HRESULT hr = S_OK; char SDFBLOCKSIZE[5]; sprintf_s(SDFBLOCKSIZE, "%d", SDF_BLOCK_SIZE); char HANDLECOLLISIONS[5]; sprintf_s(HANDLECOLLISIONS, "%d", GlobalAppState::getInstance().s_HANDLE_COLLISIONS); char blockSize[5]; sprintf_s(blockSize, "%d", THREAD_GROUP_SIZE_SCENE_REP); D3D_SHADER_MACRO macro[] = {{"groupthreads", blockSize}, { "SDF_BLOCK_SIZE", SDFBLOCKSIZE }, {"HANDLE_COLLISIONS", HANDLECOLLISIONS }, {0}}; D3D_SHADER_MACRO macro2[] = {{"groupthreads", blockSize}, { "SDF_BLOCK_SIZE", SDFBLOCKSIZE }, {0}}; D3D_SHADER_MACRO* validDefines = macro; if(GlobalAppState::getInstance().s_HANDLE_COLLISIONS == 0) { validDefines = macro2; } ID3DBlob* pBlob = NULL; V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "integrateCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_SDFVoxelHashIntegrate)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "resetHeapCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_SDFVoxelHashResetHeap)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "resetHashCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_SDFVoxelHashResetHash)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "starveVoxelsCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_SDFVoxelStarve)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "allocCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_SDFVoxelHashAlloc)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "decisionArrayFillerCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_HashDecisionArrayFiller)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "compactifyHashCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_HashCompactify)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "garbadgeCollectIdentifyCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_GarbageCollectIdentify)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "garbadgeCollectIdentifyOldCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_GarbageCollectIdentifyOld)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "garbadgeCollectFreeCS", "cs_5_0", &pBlob, validDefines)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_GarbageCollectFree)); D3D_SHADER_MACRO macro_out = {"MOVE_OUT_FRUSTUM", "1"}; D3D_SHADER_MACRO macro_in = {"MOVE_IN_FRUSTUM", "1"}; D3D_SHADER_MACRO macro_and_out[10]; D3D_SHADER_MACRO macro_and_in[10]; AddDefinitionToMacro(validDefines, macro_and_out, macro_out); AddDefinitionToMacro(validDefines, macro_and_in, macro_in); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "removeAndIntegrateCS", "cs_5_0", &pBlob, macro_and_out)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_SDFVoxelHashRemoveAndIntegrateOutFrustum)); V_RETURN(CompileShaderFromFile(L"Shaders\\SceneRepSDF.hlsl", "removeAndIntegrateCS", "cs_5_0", &pBlob, macro_and_in)); V_RETURN(pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &s_SDFVoxelHashRemoveAndIntegrateInFrustum)); SAFE_RELEASE(pBlob); V_RETURN(s_PrefixSumScan.OnD3D11CreateDevice(pd3dDevice)) return hr; }
// // Alpha_Hlsl_VertexShader::OnRestore - Chapter 15, page 509 // HRESULT Alpha_Hlsl_VertexShader::OnRestore(Scene *pScene) { HRESULT hr = S_OK; SAFE_RELEASE(m_pVertexLayout11); SAFE_RELEASE(m_pVertexShader); SAFE_RELEASE(m_pcbVSMatrices); SAFE_RELEASE(m_pcbVSLighting ); SAFE_RELEASE(m_pcbVSMaterial); shared_ptr<D3DRenderer11> d3dRenderer11 = static_pointer_cast<D3DRenderer11>(pScene->GetRenderer()); //======================================================== // Set up the vertex shader and related constant buffers // Compile the vertex shader using the lowest possible profile for broadest feature level support ID3DBlob* pVertexShaderBuffer = NULL; std::string hlslFileName = "Effects\\Alpha_VS.hlsl"; Resource resource(hlslFileName.c_str()); shared_ptr<ResHandle> pResourceHandle = g_pApp->m_ResCache->GetHandle(&resource); // this actually loads the HLSL file from the zip file **** did i code this yet? if(FAILED(d3dRenderer11->CompileShader(pResourceHandle->Buffer(), pResourceHandle->Size(), hlslFileName.c_str(), "Alpha_VSMain", "vs_4_0_level_9_1", &pVertexShaderBuffer))) { SAFE_RELEASE (pVertexShaderBuffer); return hr; } if(FAILED(g_pDX11W->GetD3D11Device()->CreateVertexShader(pVertexShaderBuffer->GetBufferPointer(),pVertexShaderBuffer->GetBufferSize(), NULL, &m_pVertexShader))) { SAFE_RELEASE (pVertexShaderBuffer); return hr; } //DXUT_SetDebugName( m_pVertexShader, "GameCode4_VSMain" );//??? need to write this into my wrapper if (SUCCEEDED(g_pDX11W->GetD3D11Device()->CreateInputLayout(D3D11VertexLayout_UnlitTextured, ARRAYSIZE(D3D11VertexLayout_UnlitTextured), pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), &m_pVertexLayout11))) { ///DXUT_SetDebugName( m_pVertexLayout11, "Primary" ); need to write this into my wrapper // Setup constant buffers D3D11_BUFFER_DESC Desc; Desc.Usage = D3D11_USAGE_DYNAMIC; Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; Desc.MiscFlags = 0; Desc.ByteWidth = sizeof(ConstantBuffer_Matrices); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&Desc, NULL, &m_pcbVSMatrices)); //DXUT_SetDebugName( m_pcbVSMatrices, "ConstantBuffer_Matrices" ); Desc.ByteWidth = sizeof(ConstantBuffer_Lighting); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&Desc, NULL, &m_pcbVSLighting)); //DXUT_SetDebugName( m_pcbPSLighting, "ConstantBuffer_Lighting" ); Desc.ByteWidth = sizeof(ConstantBuffer_Material); V_RETURN(g_pDX11W->GetD3D11Device()->CreateBuffer(&Desc, NULL, &m_pcbVSMaterial)); //DXUT_SetDebugName( m_pcbVSMaterial, "ConstantBuffer_Material" ); } SAFE_RELEASE(pVertexShaderBuffer); return S_OK; }
HRESULT DX11SceneRepHashSDF::CreateBuffers( ID3D11Device* pd3dDevice ) { HRESULT hr = S_OK; m_LastRigidTransform.setIdentity(); D3D11_BUFFER_DESC bDesc; ZeroMemory(&bDesc, sizeof(D3D11_BUFFER_DESC)); bDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bDesc.Usage = D3D11_USAGE_DYNAMIC; bDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bDesc.MiscFlags = 0; bDesc.ByteWidth = sizeof(CB_VOXEL_HASH_SDF); V_RETURN(pd3dDevice->CreateBuffer(&bDesc, NULL, &m_SDFVoxelHashCB)); D3D11_BUFFER_DESC descBUF; D3D11_SHADER_RESOURCE_VIEW_DESC descSRV; D3D11_UNORDERED_ACCESS_VIEW_DESC descUAV; //create hash buffers/uav/srv (each element is a int3 -> (short3, short, int)) ZeroMemory(&descBUF, sizeof(D3D11_BUFFER_DESC)); descBUF.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; descBUF.Usage = D3D11_USAGE_DEFAULT; descBUF.CPUAccessFlags = 0; descBUF.MiscFlags = 0; descBUF.ByteWidth = sizeof(int) * 3 * m_HashBucketSize * m_HashNumBuckets; descBUF.StructureByteStride = sizeof(int); ZeroMemory( &descSRV, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC) ); descSRV.Format = DXGI_FORMAT_R32_SINT; descSRV.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; descSRV.Buffer.FirstElement = 0; descSRV.Buffer.NumElements = 3 * m_HashBucketSize * m_HashNumBuckets; ZeroMemory( &descUAV, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC) ); descUAV.Format = DXGI_FORMAT_R32_SINT; descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; descUAV.Buffer.FirstElement = 0; descUAV.Buffer.NumElements = 3 * m_HashBucketSize * m_HashNumBuckets; D3D11_SUBRESOURCE_DATA InitData; ZeroMemory( &InitData, sizeof(D3D11_SUBRESOURCE_DATA) ); int* cpuNull = new int[3 * m_HashBucketSize * m_HashNumBuckets]; for (unsigned int i = 0; i < m_HashBucketSize * m_HashNumBuckets; i++) { cpuNull[3*i+0] = 0; cpuNull[3*i+1] = 0; cpuNull[3*i+2] = -2; //marks free memory } InitData.pSysMem = cpuNull; V_RETURN(pd3dDevice->CreateBuffer(&descBUF, &InitData, &m_Hash)); V_RETURN(pd3dDevice->CreateBuffer(&descBUF, &InitData, &m_HashCompactified)); SAFE_DELETE_ARRAY(cpuNull); V_RETURN(pd3dDevice->CreateShaderResourceView(m_Hash, &descSRV, &m_HashSRV)); V_RETURN(pd3dDevice->CreateShaderResourceView(m_HashCompactified, &descSRV, &m_HashCompactifiedSRV)); V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_Hash, &descUAV, &m_HashUAV)); V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_HashCompactified, &descUAV, &m_HashCompactifiedUAV)); if (!m_JustHashAndNoSDFBlocks) { //create hash mutex ZeroMemory(&descBUF, sizeof(D3D11_BUFFER_DESC)); descBUF.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; descBUF.Usage = D3D11_USAGE_DEFAULT; descBUF.CPUAccessFlags = 0; descBUF.MiscFlags = 0; descBUF.ByteWidth = sizeof(unsigned int) * m_HashNumBuckets; descBUF.StructureByteStride = sizeof(unsigned int); ZeroMemory( &descSRV, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC) ); descSRV.Format = DXGI_FORMAT_R32_UINT; descSRV.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; descSRV.Buffer.FirstElement = 0; descSRV.Buffer.NumElements = m_HashNumBuckets; ZeroMemory( &descUAV, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC) ); descUAV.Format = DXGI_FORMAT_R32_UINT; descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; descUAV.Buffer.FirstElement = 0; descUAV.Buffer.NumElements = m_HashNumBuckets; ZeroMemory( &InitData, sizeof(D3D11_SUBRESOURCE_DATA) ); cpuNull = new int[m_HashNumBuckets]; for (unsigned int i = 0; i < m_HashNumBuckets; i++) { cpuNull[i] = 0; } InitData.pSysMem = cpuNull; V_RETURN(pd3dDevice->CreateBuffer(&descBUF, &InitData, &m_HashBucketMutex)); SAFE_DELETE_ARRAY(cpuNull); V_RETURN(pd3dDevice->CreateShaderResourceView(m_HashBucketMutex, &descSRV, &m_HashBucketMutexSRV)); V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_HashBucketMutex, &descUAV, &m_HashBucketMutexUAV)); //create heap (unsigned int -> ptr to data blocks) ZeroMemory(&descBUF, sizeof(D3D11_BUFFER_DESC)); descBUF.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; descBUF.Usage = D3D11_USAGE_DEFAULT; descBUF.CPUAccessFlags = 0; descBUF.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; descBUF.ByteWidth = sizeof(unsigned int) * m_SDFNumBlocks; descBUF.StructureByteStride = sizeof(unsigned int); ZeroMemory (&descSRV, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC)); descSRV.Format = DXGI_FORMAT_UNKNOWN; descSRV.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; descSRV.Buffer.FirstElement = 0; descSRV.Buffer.NumElements = m_SDFNumBlocks; ZeroMemory( &descUAV, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC) ); descUAV.Format = DXGI_FORMAT_UNKNOWN; descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; descUAV.Buffer.FirstElement = 0; descUAV.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_APPEND; descUAV.Buffer.NumElements = m_SDFNumBlocks; cpuNull = new int[m_SDFNumBlocks]; for (unsigned int i = 0; i < m_SDFNumBlocks; i++) { cpuNull[i] = m_SDFNumBlocks - i - 1; } InitData.pSysMem = cpuNull; V_RETURN(pd3dDevice->CreateBuffer(&descBUF, &InitData, &m_Heap)); SAFE_DELETE_ARRAY(cpuNull); V_RETURN(pd3dDevice->CreateShaderResourceView(m_Heap, &descSRV, &m_HeapSRV)); V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_Heap, &descUAV, &m_HeapUAV)); ZeroMemory( &descUAV, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC) ); descUAV.Format = DXGI_FORMAT_UNKNOWN; descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; descUAV.Buffer.FirstElement = 0; descUAV.Buffer.Flags = 0; descUAV.Buffer.NumElements = m_SDFNumBlocks; V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_Heap, &descUAV, &m_HeapStaticUAV)) ZeroMemory(&descBUF, sizeof(D3D11_BUFFER_DESC)); descBUF.BindFlags = 0; descBUF.Usage = D3D11_USAGE_STAGING; descBUF.CPUAccessFlags = D3D11_CPU_ACCESS_READ; descBUF.MiscFlags = 0; descBUF.ByteWidth = sizeof(int); V_RETURN(pd3dDevice->CreateBuffer(&descBUF, NULL, &m_HeapFreeCount)); unsigned int initalCount = m_SDFNumBlocks; DXUTGetD3D11DeviceContext()->CSSetUnorderedAccessViews(0, 1, &m_HeapUAV, &initalCount); ID3D11UnorderedAccessView* nullUAV[] = {NULL}; DXUTGetD3D11DeviceContext()->CSSetUnorderedAccessViews(0, 1, nullUAV, NULL); //create sdf blocks (8x8x8 -> 8 byte per voxel: SDF part) ZeroMemory(&descBUF, sizeof(D3D11_BUFFER_DESC)); descBUF.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; descBUF.Usage = D3D11_USAGE_DEFAULT; descBUF.CPUAccessFlags = 0; descBUF.MiscFlags = 0; descBUF.ByteWidth = (sizeof(float)) * m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks; descBUF.StructureByteStride = sizeof(int); ZeroMemory( &descSRV, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC) ); descSRV.Format = DXGI_FORMAT_R32_FLOAT; descSRV.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; descSRV.Buffer.FirstElement = 0; descSRV.Buffer.NumElements = m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks; ZeroMemory( &descUAV, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC) ); descUAV.Format = DXGI_FORMAT_R32_FLOAT; descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; descUAV.Buffer.FirstElement = 0; descUAV.Buffer.NumElements = m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks; ZeroMemory( &InitData, sizeof(D3D11_SUBRESOURCE_DATA) ); cpuNull = new int[m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks]; ZeroMemory(cpuNull, sizeof(int) * m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks); InitData.pSysMem = cpuNull; V_RETURN(pd3dDevice->CreateBuffer(&descBUF, &InitData, &m_SDFBlocksSDF)); SAFE_DELETE_ARRAY(cpuNull); V_RETURN(pd3dDevice->CreateShaderResourceView(m_SDFBlocksSDF, &descSRV, &m_SDFBlocksSDFSRV)); V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_SDFBlocksSDF, &descUAV, &m_SDFBlocksSDFUAV)); //create sdf blocks (8x8x8 -> 8 byte per voxel: RGBW part) ZeroMemory(&descBUF, sizeof(D3D11_BUFFER_DESC)); descBUF.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; descBUF.Usage = D3D11_USAGE_DEFAULT; descBUF.CPUAccessFlags = 0; descBUF.MiscFlags = 0; descBUF.ByteWidth = sizeof(int) * m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks; descBUF.StructureByteStride = sizeof(int); ZeroMemory( &descSRV, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC) ); descSRV.Format = DXGI_FORMAT_R32_SINT; descSRV.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; descSRV.Buffer.FirstElement = 0; descSRV.Buffer.NumElements = m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks; ZeroMemory( &descUAV, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC) ); descUAV.Format = DXGI_FORMAT_R32_SINT;; descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; descUAV.Buffer.FirstElement = 0; descUAV.Buffer.NumElements = m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks; ZeroMemory( &InitData, sizeof(D3D11_SUBRESOURCE_DATA) ); cpuNull = new int[m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks]; ZeroMemory(cpuNull, sizeof(int) * m_SDFBlockSize * m_SDFBlockSize * m_SDFBlockSize * m_SDFNumBlocks); InitData.pSysMem = cpuNull; V_RETURN(pd3dDevice->CreateBuffer(&descBUF, &InitData, &m_SDFBlocksRGBW)); SAFE_DELETE_ARRAY(cpuNull); V_RETURN(pd3dDevice->CreateShaderResourceView(m_SDFBlocksRGBW, &descSRV, &m_SDFBlocksRGBWSRV)); V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_SDFBlocksRGBW, &descUAV, &m_SDFBlocksRGBWUAV)); } //////////////// // Compactify // //////////////// //create hash buffers/uav/srv (each element is a int3 -> (short3, short, int)) ZeroMemory(&descBUF, sizeof(D3D11_BUFFER_DESC)); descBUF.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; descBUF.Usage = D3D11_USAGE_DEFAULT; descBUF.CPUAccessFlags = 0; descBUF.MiscFlags = 0; descBUF.ByteWidth = sizeof(int) * m_HashBucketSize * m_HashNumBuckets; descBUF.StructureByteStride = sizeof(int); ZeroMemory( &descSRV, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC) ); descSRV.Format = DXGI_FORMAT_R32_SINT; descSRV.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; descSRV.Buffer.FirstElement = 0; descSRV.Buffer.NumElements = m_HashBucketSize * m_HashNumBuckets; ZeroMemory( &descUAV, sizeof(D3D11_UNORDERED_ACCESS_VIEW_DESC) ); descUAV.Format = DXGI_FORMAT_R32_SINT; descUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; descUAV.Buffer.FirstElement = 0; descUAV.Buffer.NumElements = m_HashBucketSize * m_HashNumBuckets; ZeroMemory( &InitData, sizeof(D3D11_SUBRESOURCE_DATA) ); cpuNull = new int[m_HashBucketSize * m_HashNumBuckets]; for (unsigned int i = 0; i < m_HashBucketSize * m_HashNumBuckets; i++) { cpuNull[i] = 0; } InitData.pSysMem = cpuNull; V_RETURN(pd3dDevice->CreateBuffer(&descBUF, &InitData, &m_HashIntegrateDecision)); V_RETURN(pd3dDevice->CreateBuffer(&descBUF, &InitData, &m_HashIntegrateDecisionPrefix)); SAFE_DELETE_ARRAY(cpuNull); V_RETURN(pd3dDevice->CreateShaderResourceView(m_HashIntegrateDecision, &descSRV, &m_HashIntegrateDecisionSRV)); V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_HashIntegrateDecision, &descUAV, &m_HashIntegrateDecisionUAV)); V_RETURN(pd3dDevice->CreateShaderResourceView(m_HashIntegrateDecisionPrefix, &descSRV, &m_HashIntegrateDecisionPrefixSRV)); V_RETURN(pd3dDevice->CreateUnorderedAccessView(m_HashIntegrateDecisionPrefix, &descUAV, &m_HashIntegrateDecisionPrefixUAV)); return hr; }
HRESULT DX11BuildLinearSystem::OnD3D11CreateDevice( ID3D11Device* pd3dDevice ) { HRESULT hr = S_OK; ///////////////////////////////////////////////////// // Build Linear System ///////////////////////////////////////////////////// m_pComputeShaderBL = new ID3D11ComputeShader*[GlobalCameraTrackingState::getInstance().s_maxLevels]; char BLOCK_SIZE_BL[5]; sprintf_s(BLOCK_SIZE_BL, "%d", m_blockSizeBL); for(int level = GlobalCameraTrackingState::getInstance().s_maxLevels-1; level >= 0; level--) { char LOCALWINDOWSIZE_BL[5]; sprintf_s(LOCALWINDOWSIZE_BL, "%d", GlobalCameraTrackingState::getInstance().s_localWindowSize[level]); // Build Linearized System char ARRAYSIZE_BL[5]; sprintf_s(ARRAYSIZE_BL, "%d", 30); D3D_SHADER_MACRO shaderDefinesBL[] = { { "groupthreads", BLOCK_SIZE_BL }, { "LOCALWINDOWSIZE", LOCALWINDOWSIZE_BL }, {"ARRAYSIZE", ARRAYSIZE_BL }, { 0 } }; ID3DBlob* pBlob = NULL; hr = CompileShaderFromFile(L"Shaders\\BuildLinearSystem.hlsl", "scanScanElementsCS", "cs_5_0", &pBlob, shaderDefinesBL); if(FAILED(hr)) return hr; hr = pd3dDevice->CreateComputeShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &m_pComputeShaderBL[level]); if(FAILED(hr)) return hr; SAFE_RELEASE(pBlob); } D3D11_BUFFER_DESC bDesc; bDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bDesc.Usage = D3D11_USAGE_DYNAMIC; bDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bDesc.MiscFlags = 0; bDesc.ByteWidth = sizeof(CBufferBL); hr = pd3dDevice->CreateBuffer(&bDesc, NULL, &m_constantBufferBL); if(FAILED(hr)) return hr; unsigned int dimX = (unsigned int)ceil(((float)GlobalAppState::getInstance().s_windowWidth*GlobalAppState::getInstance().s_windowHeight)/(GlobalCameraTrackingState::getInstance().s_localWindowSize[0]*m_blockSizeBL)); // Create Output Buffer D3D11_BUFFER_DESC Desc; Desc.Usage = D3D11_USAGE_DEFAULT; Desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS; Desc.CPUAccessFlags = 0; Desc.MiscFlags = 0; Desc.ByteWidth = 30*sizeof(float)*dimX; // Buffer is reused for all levels -> maximal dimX*dimY==400 elements on each level V_RETURN(pd3dDevice->CreateBuffer(&Desc, NULL, &m_pOutputFloat)); // Create Output Buffer CPU Desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; Desc.BindFlags = 0; Desc.Usage = D3D11_USAGE_STAGING; V_RETURN(pd3dDevice->CreateBuffer(&Desc, NULL, &m_pOutputFloatCPU)); // Create Output UAV D3D11_UNORDERED_ACCESS_VIEW_DESC DescUAV; ZeroMemory( &DescUAV, sizeof(DescUAV)); DescUAV.Format = DXGI_FORMAT_R32_FLOAT; DescUAV.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; DescUAV.Buffer.FirstElement = 0; DescUAV.Buffer.NumElements = 30*dimX; V_RETURN( pd3dDevice->CreateUnorderedAccessView(m_pOutputFloat, &DescUAV, &m_pOutputFloatUAV)); return hr; }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // created, which will happen during application initialization and windowed/full screen // toggles. This is the best location to create D3DPOOL_MANAGED resources since these // resources need to be reloaded whenever the device is destroyed. Resources created // here should be released in the OnDestroyDevice callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice(IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext) { #ifdef _PERFORMANCE ::SetThreadAffinityMask(::GetCurrentThread(), 1); PerfManager::createTheOne(); grp::setProfiler(PerfManager::getTheOne()); #endif HRESULT hr; V_RETURN(g_DialogResourceManager.OnD3D9CreateDevice(pd3dDevice)); // Initialize the font V_RETURN(D3DXCreateFont(pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont)); // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the // shader debugger. Debugging vertex shaders requires either REF or software vertex // processing, and debugging pixel shaders requires REF. The // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the // shader debugger. It enables source level debugging, prevents instruction // reordering, prevents dead code elimination, and forces the compiler to compile // against the next higher available software target, which ensures that the // unoptimized shaders do not exceed the shader model limitations. Setting these // flags will cause slower rendering since the shaders will be unoptimized and // forced into software. See the DirectX documentation for more information about // using the shader debugger. DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #if defined(DEBUG) || defined(_DEBUG) // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DXSHADER_DEBUG; #endif #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, L"Test/Demo.fx")); // If this fails, there should be debug output as to // why the .fx file failed to compile LPD3DXBUFFER pError = NULL; D3DXCreateEffectFromFile(pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, &pError); if (NULL != pError) { char* szErr = (char*)(pError->GetBufferPointer()); assert(false); } V(g_pEffect->SetFloat("g_fAmbient", g_fAmbient)); V(g_pEffect->SetFloat("g_fDiffuse", g_fDiffuse)); // Setup the camera's view parameters g_camera.setControlMode(DemoCamera::THIRD_PERSON); g_camera.setViewDistance(3.0f); g_camera.setYaw(3.1415926f); //g_camera.setRoll(3.14159f/4); #if (DEMO_RIGHT_HAND_COORD) g_camera.setViewParams(grp::Vector3(-2.0f, -2.0f, 1.0f), grp::Vector3(0.0f, 0.0f, 1.0f), grp::Vector3(0.0f, 0.0f, 1.0f)); g_camera.setCoordinateSystem(DemoCamera::RIGHT_HAND); g_camera.setUpAxis(DemoCamera::Z_UP); #else g_camera.setViewParams(grp::Vector3(-2.0f, 1.0f, -2.0f), grp::Vector3(0.0f, 1.0f, 0.0f), grp::Vector3(0.0f, 1.0f, 0.0f)); #endif // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; g_camera.setPerspectiveParams(D3DX_PI/4, fAspectRatio, 0.001f, 200.0f); g_fileLoader = new MultithreadFileLoader(pd3dDevice); g_fileLoader->enableMultithread(false); g_resourceManager = new MultithreadResManager(g_fileLoader); grp::initialize(NULL, g_fileLoader, NULL, g_resourceManager); g_character = new DemoCharacter(L"Test/warrior.gmd", pd3dDevice); //g_character->setGpuSkinning(true); g_model = g_character->getModel(); if (g_model == NULL) { return E_FAIL; } //g_model->enableMeshLod(true); //g_model->enableWeightLod(true); //g_model->enableSkeletonLod(true); //g_model->setLodTolerance(0.1f); #if (DEMO_RIGHT_HAND_COORD) grp::Matrix coordTransform(grp::Matrix::IDENTITY); #else grp::Matrix coordTransform(grp::Matrix::ZERO); coordTransform._11 = 1.0f; coordTransform._23 = 1.0f; coordTransform._32 = 1.0f; coordTransform._44 = 1.0f; #endif g_model->setTransform(coordTransform);// * translateMatrix); g_model->playAnimation(L"fight", grp::ANIMATION_LOOP, 0, 0); g_model->playAnimation(L"walk", grp::ANIMATION_LOOP, 0, 0); g_model->playAnimation(L"run", grp::ANIMATION_LOOP, 0, 0); setAnimationWeight(); return S_OK; }