static HRESULT CreateShaders(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dContext) { HRESULT hr; CHAR path[MAX_PATH] = "Data/Shaders/TerrainTessellation/TerrainTessellation.fx"; ID3DXBuffer* pShader = NULL; const DWORD flags = 0; ID3DXBuffer* pErrors = NULL; /*if (D3DXCompileShaderFromFile(path, NULL, NULL, NULL, "fx_5_0", flags, &pShader, &pErrors, NULL) != S_OK) { const char* pTxt = (char *)pErrors->GetBufferPointer(); MessageBoxA(NULL, pTxt, "Compilation errors", MB_OK); return hr; } if (D3DX11CreateEffectFromMemory(pShader->GetBufferPointer(), pShader->GetBufferSize(), 0, pd3dDevice, &g_pTerrainEffect) != S_OK) { MessageBoxA(NULL, "Failed to create terrain effect", "Effect load error", MB_OK); return hr; }*/ if (FAILED(LoadEffectFromFile(pd3dDevice, path, &g_pTerrainEffect))) { MessageBoxA(NULL, "Failed to create terrain effect", "Effect load error", MB_OK); return hr; } // Obtain techniques g_pTesselationTechnique = g_pTerrainEffect->GetTechniqueByName("TesselationTechnique"); // Obtain miscellaneous variables g_HeightMapVar = g_pTerrainEffect->GetVariableByName("g_CoarseHeightMap")->AsShaderResource(); g_GradientMapVar = g_pTerrainEffect->GetVariableByName("g_CoarseGradientMap")->AsShaderResource(); g_pEyePosVar = g_pTerrainEffect->GetVariableByName("g_EyePos")->AsVector(); g_pViewDirVar = g_pTerrainEffect->GetVariableByName("g_ViewDir")->AsVector(); g_pFractalOctavesTVar = g_pTerrainEffect->GetVariableByName("g_FractalOctaves")->AsVector(); g_pUVOffsetTVar = g_pTerrainEffect->GetVariableByName("g_TextureWorldOffset")->AsVector(); g_pViewportVar = g_pTerrainEffect->GetVariableByName( "Viewport" )->AsVector(); g_pTriSizeVar = g_pTerrainEffect->GetVariableByName("g_tessellatedTriWidth")->AsScalar(); g_pTileSizeVar = g_pTerrainEffect->GetVariableByName("g_tileSize")->AsScalar(); g_DebugShowPatchesVar = g_pTerrainEffect->GetVariableByName("g_DebugShowPatches")->AsScalar(); g_DebugShowTilesVar = g_pTerrainEffect->GetVariableByName("g_DebugShowTiles")->AsScalar(); g_WireAlphaVar = g_pTerrainEffect->GetVariableByName("g_WireAlpha")->AsScalar(); g_WireWidthVar = g_pTerrainEffect->GetVariableByName("g_WireWidth")->AsScalar(); g_DetailNoiseVar = g_pTerrainEffect->GetVariableByName("g_DetailNoiseScale")->AsScalar(); g_DetailUVVar = g_pTerrainEffect->GetVariableByName("g_DetailUVScale")->AsVector(); g_SampleSpacingVar = g_pTerrainEffect->GetVariableByName("g_CoarseSampleSpacing")->AsScalar(); loadTextureFromFile("Data/TerrainTessellation/LunarSurface1.dds", "g_TerrainColourTexture1", pd3dDevice, g_pTerrainEffect); loadTextureFromFile("Data/TerrainTessellation/LunarMicroDetail1.dds", "g_TerrainColourTexture2", pd3dDevice, g_pTerrainEffect); loadTextureFromFile("Data/TerrainTessellation/GaussianNoise256.jpg", "g_NoiseTexture", pd3dDevice, g_pTerrainEffect); loadTextureFromFile("Data/TerrainTessellation/fBm5Octaves.dds", "g_DetailNoiseTexture", pd3dDevice, g_pTerrainEffect); loadTextureFromFile("Data/TerrainTessellation/fBm5OctavesGrad.dds", "g_DetailNoiseGradTexture", pd3dDevice, g_pTerrainEffect); return S_OK; }
static HRESULT CreateDeformEffect(ID3D11Device* pd3dDevice) { HRESULT hr; CHAR path[MAX_PATH] = "Data/Shaders/TerrainTessellation/Deformation.fx"; ID3DXBuffer* pShader = NULL; //if (FAILED (hr = DXUTFindDXSDKMediaFileCch(path, MAX_PATH, L"Deformation.fx"))) { // MessageBox (NULL, L"Deformation.fx cannot be found.", L"Error", MB_OK); // return hr; //} ID3DXBuffer* pErrors = NULL; ////if (D3DXCompileShaderFromFile(path, NULL, NULL, NULL, "fx_5_0", 0, &pShader, &pErrors, NULL) != S_OK) //{ // MessageBoxA(NULL, (char *)pErrors->GetBufferPointer(), "Compilation errors", MB_OK); // return hr; //} //if (D3DX11CreateEffectFromMemory(pShader->GetBufferPointer(), pShader->GetBufferSize(), 0, pd3dDevice, &g_pDeformEffect) != S_OK) //{ // MessageBoxA(NULL, "Failed to create deformation effect", "Effect load error", MB_OK); // return hr; //} LoadEffectFromFile(pd3dDevice, path, &g_pDeformEffect); // Obtain techniques g_pInitializationTechnique = g_pDeformEffect->GetTechniqueByName("InitializationTechnique"); g_pGradientTechnique = g_pDeformEffect->GetTechniqueByName("GradientTechnique"); // Obtain miscellaneous variables g_pFractalOctavesDVar = g_pDeformEffect->GetVariableByName("g_FractalOctaves")->AsVector(); g_pUVOffsetDVar = g_pDeformEffect->GetVariableByName("g_TextureWorldOffset")->AsVector(); g_InputTexVar = g_pDeformEffect->GetVariableByName("g_InputTexture")->AsShaderResource(); loadTextureFromFile("Data/TerrainTessellation/GaussianNoise256.jpg", "g_NoiseTexture", pd3dDevice, g_pDeformEffect); return S_OK; }
void ProcTexTerrainRender::CreateShaders() { LoadEffectFromFile(D3D11Dev(), "data/shaders/terrascape.fx", &TerrainEffect); loadTextureFromFile("data/ambient.jpg", "occlusionTexture", D3D11Dev(), TerrainEffect); loadTextureFromFile("data/shadow.jpg", "shadowTexture", D3D11Dev(), TerrainEffect); loadTextureFromFile("data/noise16.png", "texNoiseMap0", D3D11Dev(), TerrainEffect); loadTextureFromFile("data/displace16.png", "texNoiseMap0", D3D11Dev(), TerrainEffect); HeightMapTex16 = new D3D11Texture2D; HeightMapTex16->Initialize(D3D11Dev(), heightMapXFull-1, heightMapXFull-1, DXGI_FORMAT_R16G16_UNORM); WORD *heightMapDouble = new WORD[ (heightMapXFull-1)*(heightMapXFull-1)*2 ]; int i,j; for ( j = 0; j < (heightMapXFull-1); j++ ) for ( i = 0; i < (heightMapXFull-1); i++ ) { WORD h = HeightMap16Full[ i + (heightMapXFull-2-j) * heightMapXFull ]; WORD h2 = HeightMap16Full[ i + 1 + (heightMapXFull-2-j) * heightMapXFull ]; heightMapDouble[ (i + j * (heightMapXFull-1)) * 2 + 0 ] = h; heightMapDouble[ (i + j * (heightMapXFull-1)) * 2 + 1 ] = h2; } uint32 pitch; WORD*data = (WORD*)HeightMapTex16->Map(D3D11Context(), 0, pitch); memcpy(data, heightMapDouble, (heightMapXFull-1)*(heightMapXFull-1)*2 * sizeof(WORD)); HeightMapTex16->Unmap(D3D11Context(), 0); delete heightMapDouble; // interpolation look-up textures if ( generateBiCubicWeights( D3D11Dev() ) ) return; // create chunk lod quadtree createQLOD( D3D11Dev(), heightMapX, HeightMap16, HeightMapTex16 ); }
//-------------------------------------------------------------------------------------- // 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; }
void Dx11TextHelper::InitFont() { ID3DBlob* pVSBlob = NULL; ID3DBlob* pPSBlob = NULL; char filename[MAX_PATH] = "UI/Font.fx"; LoadEffectFromFile( m_pd3d11Device, filename, &g_pFontEffect); //ID3DBlob* pErrorBlob = NULL; //DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; //hr = D3DX11CreateEffectFromMemory(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), 0, m_pd3d11Device, &g_pFontEffect); g_pFontTec = g_pFontEffect->GetTechniqueByName( "RenderUI" ); // Create input layout const D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; D3DX11_PASS_DESC PassDesc; g_pFontTec->GetPassByIndex( 0 )->GetDesc( &PassDesc ) ; m_pd3d11Device->CreateInputLayout( layout, ARRAYSIZE( layout ), PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &m_pInputLayout11 ); // Release the blobs SAFE_RELEASE( pVSBlob ); //SAFE_RELEASE( pPSBlob ); //SAFE_RELEASE( pPSUntexBlob ); 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; m_pd3d11Device->CreateDepthStencilState( &DSDesc, &m_pDepthStencilStateUI11 ); D3D11_RASTERIZER_DESC RSDesc; RSDesc.AntialiasedLineEnable = FALSE; RSDesc.CullMode = D3D11_CULL_BACK; RSDesc.DepthBias = 0; RSDesc.DepthBiasClamp = 0.0f; RSDesc.DepthClipEnable = TRUE; RSDesc.FillMode = D3D11_FILL_SOLID; RSDesc.FrontCounterClockwise = FALSE; RSDesc.MultisampleEnable = TRUE; RSDesc.ScissorEnable = FALSE; RSDesc.SlopeScaledDepthBias = 0.0f; m_pd3d11Device->CreateRasterizerState( &RSDesc, &m_pRasterizerStateUI11 ); D3D11_BLEND_DESC BSDesc; ZeroMemory( &BSDesc, sizeof( D3D11_BLEND_DESC ) ); BSDesc.RenderTarget[0].BlendEnable = TRUE; BSDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; BSDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; BSDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; BSDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; BSDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; BSDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; BSDesc.RenderTarget[0].RenderTargetWriteMask = 0x0F; m_pd3d11Device->CreateBlendState( &BSDesc, &m_pBlendStateUI11 ); D3D11_SAMPLER_DESC SSDesc; ZeroMemory( &SSDesc, sizeof( D3D11_SAMPLER_DESC ) ); SSDesc.Filter = D3D11_FILTER_ANISOTROPIC ; SSDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; SSDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; SSDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; SSDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; SSDesc.MaxAnisotropy = 16; SSDesc.MinLOD = 0; SSDesc.MaxLOD = D3D11_FLOAT32_MAX; if ( m_pd3d11Device->GetFeatureLevel() < D3D_FEATURE_LEVEL_9_3 ) { SSDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; SSDesc.MaxAnisotropy = 0; } m_pd3d11Device->CreateSamplerState( &SSDesc, &m_pSamplerStateUI11 ); m_pFontTex = g_pFontEffect->GetVariableByName( "g_Texture" )->AsShaderResource(); InitFont11( m_pd3d11Device, m_pInputLayout11 ); }
ID3DX11Effect* LoadEffect( ID3D11Device* device, const char* filename ) { ID3DX11Effect* effect; DXCall(LoadEffectFromFile(device, filename,&effect)); return effect; }
//-------------------------------------------------------------------------------------- // 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 GaussianMain::OnD3D11CreateDevice( ID3D11Device* pd3dDevice) { //HRESULT hr; ID3D11DeviceContext* pd3dImmediateContext; pd3dDevice->GetImmediateContext(&pd3dImmediateContext); // Load effect LoadEffectFromFile(pd3dDevice, L"gaussian_vs_ps.hlsl", &g_pFX_Render); // Layout for full screen quad rendering const D3D11_INPUT_ELEMENT_DESC quad_layout[] = { {"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0}, }; D3DX11_PASS_DESC pass_desc; g_pFX_Render->GetTechniqueByName("Tech_ShowImage")->GetPassByName("Pass_ShowMonoImage")->GetDesc(&pass_desc); pd3dDevice->CreateInputLayout(quad_layout, 2, pass_desc.pIAInputSignature, pass_desc.IAInputSignatureSize, &g_pQuadLayout); // Vertex buffer for full screen quad rendering SCREEN_VERTEX svQuad[4]; svQuad[0].pos = D3DXVECTOR4( -1.0f, 1.0f, 0.5f, 1.0f ); svQuad[0].tex = D3DXVECTOR2( 0.0f, 0.0f ); svQuad[1].pos = D3DXVECTOR4( 1.0f, 1.0f, 0.5f, 1.0f ); svQuad[1].tex = D3DXVECTOR2( 1.0f, 0.0f ); svQuad[2].pos = D3DXVECTOR4( -1.0f, -1.0f, 0.5f, 1.0f ); svQuad[2].tex = D3DXVECTOR2( 0.0f, 1.0f ); svQuad[3].pos = D3DXVECTOR4( 1.0f, -1.0f, 0.5f, 1.0f ); svQuad[3].tex = D3DXVECTOR2( 1.0f, 1.0f ); D3D11_BUFFER_DESC vbdesc = { 4 * sizeof( SCREEN_VERTEX ), D3D11_USAGE_IMMUTABLE, D3D11_BIND_VERTEX_BUFFER, 0, 0 }; D3D11_SUBRESOURCE_DATA InitData; InitData.pSysMem = svQuad; InitData.SysMemPitch = 0; InitData.SysMemSlicePitch = 0; pd3dDevice->CreateBuffer( &vbdesc, &InitData, &g_pScreenQuadVB ); //--------------------------------- //resize swap chain //--------------------------------- CreateResources(pd3dDevice, &GetDXGIBackBufferSurfaceDesc()); // Save render target and viewport pd3dImmediateContext->OMGetRenderTargets(1, &g_pRTV_Default, &g_pDSV_Default); UINT nViewPorts = 1; pd3dImmediateContext->RSGetViewports(&nViewPorts, g_VP_Default); SAFE_RELEASE(pd3dImmediateContext); //---------------------------------- // frame move //---------------------------------- // Compile the shaders if haven't. SAFE_RELEASE(g_pFX_GaussianCol); SAFE_RELEASE(g_pFX_GaussianRow); CompileGaussianFilterEffects(pd3dDevice, &GetDXGIBackBufferSurfaceDesc()); return S_OK; }
void M2EffectRender::LoadShaders() { if (ExistParticle_) { /*D3D11_SAMPLER_DESC sd; ZeroMemory(&sd, sizeof(sd)); sd.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; sd.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; sd.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; sd.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; sd.MipLODBias = 0.0f; sd.MaxAnisotropy = 1; sd.ComparisonFunc = D3D11_COMPARISON_ALWAYS; sd.BorderColor[0] = 0.0f; sd.BorderColor[1] = 0.0f; sd.BorderColor[2] = 0.0f; sd.BorderColor[3] = 0.0f; sd.MinLOD = 0.0f; sd.MaxLOD = D3D11_FLOAT32_MAX; ID3D11SamplerState* pSampleState; GetApp()->GetDevice()->CreateSamplerState(&sd, &pSampleState);*/ M2ParticleSystem* ps; ID3D11ShaderResourceView* particleMapSRV; for (size_t i =0; i < Owner_->header.nParticleEmitters; ++i) { ps =&(Owner_->particleSystems[i]); std::string texname = Owner_->GetTextureName(ps->texture); size_t len = strlen(texname.c_str()); texname[len - 3] = 'd'; texname[len - 2] = 'd'; texname[len - 1] = 's'; if (!wxFile::Exists(texname.c_str())) MessageBox(NULL, texname.c_str(), "Texture", 0); D3DX11CreateShaderResourceViewFromFile( GetApp()->GetDevice(), texname.c_str(), NULL, NULL, &particleMapSRV, NULL ); ParticleData_.ParticleTextureSRVMaps.insert(std::make_pair(ps->texture, particleMapSRV)); } ParticleData_.gNumActiveParticles.resize(Owner_->header.nParticleEmitters); LoadEffectFromFile(GetApp()->GetDevice(), "Data/Shaders/Particle.fx", &Effect_); ParticleData_.gpEffect10 = Effect_; ParticleData_.gpRenderParticles = ParticleData_.gpEffect10->GetTechniqueByName( "RenderParticles" ); ParticleData_.gptxDiffuse = ParticleData_.gpEffect10->GetVariableByName( "g_txMeshTexture" )->AsShaderResource(); ParticleData_.gpmWorld = ParticleData_.gpEffect10->GetVariableByName( "g_mWorld" )->AsMatrix(); //gpfTime = gpEffect10->GetVariableByName( "g_fTime" )->AsScalar(); ParticleData_.gpvEyePt = ParticleData_.gpEffect10->GetVariableByName( "g_vEyePt" )->AsVector(); //gpvRight = gpEffect10->GetVariableByName( "g_vRight" )->AsVector(); //gpvUp = gpEffect10->GetVariableByName( "g_vUp" )->AsVector(); //gpvForward = gpEffect10->GetVariableByName( "g_vForward" )->AsVector(); //gpmViewProj = gpEffect10->GetVariableByName( "gmViewProj" )->AsMatrix(); ParticleData_.gpmWorldViewProjection = ParticleData_.gpEffect10->GetVariableByName( "g_mWorldViewProjection" )->AsMatrix(); } if (ExistRibbon_) { M2RibbonEmitter* ribbon; ID3D11ShaderResourceView* particleMapSRV; for (size_t i =0; i < Owner_->header.nRibbonEmitters; ++i) { ribbon = &(Owner_->ribbons[i]); std::string texname = Owner_->GetTextureName(ribbon->texture); size_t len = strlen(texname.c_str()); texname[len - 3] = 'd'; texname[len - 2] = 'd'; texname[len - 1] = 's'; if (!wxFile::Exists(texname.c_str())) MessageBox(NULL, texname.c_str(), "Texture", 0); D3DX11CreateShaderResourceViewFromFile( GetApp()->GetDevice(), texname.c_str(), NULL, NULL, &particleMapSRV, NULL ); RibbonData_.ParticleTextureSRVMaps.insert(std::make_pair(ribbon->texture, particleMapSRV)); } RibbonData_.gNumActiveParticles.resize(Owner_->header.nRibbonEmitters); //LoadEffectFromFile(GetApp()->GetDevice(), "Data/Shaders/Particle.fx", &RibbonData_.gpEffect10); RibbonData_.gpEffect10 = Effect_; RibbonData_.gpRenderParticles = RibbonData_.gpEffect10->GetTechniqueByName( "RenderParticles" ); RibbonData_.gptxDiffuse = RibbonData_.gpEffect10->GetVariableByName( "g_txMeshTexture" )->AsShaderResource(); RibbonData_.gpmWorld = RibbonData_.gpEffect10->GetVariableByName( "g_mWorld" )->AsMatrix(); //gpfTime = gpEffect10->GetVariableByName( "g_fTime" )->AsScalar(); RibbonData_.gpvEyePt = RibbonData_.gpEffect10->GetVariableByName( "g_vEyePt" )->AsVector(); //gpvRight = gpEffect10->GetVariableByName( "g_vRight" )->AsVector(); //gpvUp = gpEffect10->GetVariableByName( "g_vUp" )->AsVector(); //gpvForward = gpEffect10->GetVariableByName( "g_vForward" )->AsVector(); //gpmViewProj = gpEffect10->GetVariableByName( "gmViewProj" )->AsMatrix(); RibbonData_.gpmWorldViewProjection = RibbonData_.gpEffect10->GetVariableByName( "g_mWorldViewProjection" )->AsMatrix(); } }