📄 direct3d.c
字号:
LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys; p_pic->p_sys = NULL; if( p_d3dsurf ) { IDirect3DSurface9_Release(p_d3dsurf); } } } msg_Dbg( p_vout, "%u Direct3D pictures released.", c); I_OUTPUTPICTURES = 0;}/***************************************************************************** * Direct3DVoutLockSurface: Lock surface and get picture data pointer ***************************************************************************** * This function locks a surface and get the surface descriptor which amongst * other things has the pointer to the picture data. *****************************************************************************/static int Direct3DVoutLockSurface( vout_thread_t *p_vout, picture_t *p_pic ){ HRESULT hr; D3DLOCKED_RECT d3drect; LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys; if( NULL == p_d3dsurf ) return VLC_EGENERIC; /* Lock the surface to get a valid pointer to the picture buffer */ hr = IDirect3DSurface9_LockRect(p_d3dsurf, &d3drect, NULL, 0); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return VLC_EGENERIC; } /* fill in buffer info in first plane */ p_pic->p->p_pixels = d3drect.pBits; p_pic->p->i_pitch = d3drect.Pitch; return VLC_SUCCESS;}/***************************************************************************** * Direct3DVoutUnlockSurface: Unlock a surface locked by Direct3DLockSurface(). *****************************************************************************/static int Direct3DVoutUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic ){ HRESULT hr; LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys; if( NULL == p_d3dsurf ) return VLC_EGENERIC; /* Unlock the Surface */ hr = IDirect3DSurface9_UnlockRect(p_d3dsurf); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return VLC_EGENERIC; } return VLC_SUCCESS;}/***************************************************************************** * Direct3DVoutCreateScene: allocate and initialize a 3D scene ***************************************************************************** * for advanced blending/filtering a texture needs be used in a 3D scene. *****************************************************************************/static int Direct3DVoutCreateScene( vout_thread_t *p_vout ){ LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev; LPDIRECT3DTEXTURE9 p_d3dtex; LPDIRECT3DVERTEXBUFFER9 p_d3dvtc; HRESULT hr; /* ** Create a texture for use when rendering a scene ** for performance reason, texture format is identical to backbuffer ** which would usually be a RGB format */ hr = IDirect3DDevice9_CreateTexture(p_d3ddev, p_vout->render.i_width, p_vout->render.i_height, 1, D3DUSAGE_RENDERTARGET, p_vout->p_sys->bbFormat, D3DPOOL_DEFAULT, &p_d3dtex, NULL); if( FAILED(hr)) { msg_Err(p_vout, "Failed to create texture. (hr=0x%lx)", hr); return VLC_EGENERIC; } /* ** Create a vertex buffer for use when rendering scene */ hr = IDirect3DDevice9_CreateVertexBuffer(p_d3ddev, sizeof(CUSTOMVERTEX)*4, D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &p_d3dvtc, NULL); if( FAILED(hr) ) { msg_Err(p_vout, "Failed to create vertex buffer. (hr=0x%lx)", hr); IDirect3DTexture9_Release(p_d3dtex); return VLC_EGENERIC; } p_vout->p_sys->p_d3dtex = p_d3dtex; p_vout->p_sys->p_d3dvtc = p_d3dvtc; // Texture coordinates outside the range [0.0, 1.0] are set // to the texture color at 0.0 or 1.0, respectively. IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); // Set linear filtering quality IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); // set maximum ambient light IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255)); // Turn off culling IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_CULLMODE, D3DCULL_NONE); // Turn off the zbuffer IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ZENABLE, D3DZB_FALSE); // Turn off lights IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_LIGHTING, FALSE); // Enable dithering IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DITHERENABLE, TRUE); // disable stencil IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_STENCILENABLE, FALSE); // manage blending IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHABLENDENABLE, TRUE); IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHATESTENABLE,TRUE); IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAREF, 0x10); IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAFUNC,D3DCMP_GREATER); // Set texture states IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLOROP,D3DTOP_MODULATE); IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG1,D3DTA_TEXTURE); IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG2,D3DTA_DIFFUSE); // turn off alpha operation IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE); msg_Dbg( p_vout, "Direct3D scene created successfully"); return VLC_SUCCESS;}/***************************************************************************** * Direct3DVoutReleaseScene *****************************************************************************/static void Direct3DVoutReleaseScene( vout_thread_t *p_vout ){ LPDIRECT3DTEXTURE9 p_d3dtex = p_vout->p_sys->p_d3dtex; LPDIRECT3DVERTEXBUFFER9 p_d3dvtc = p_vout->p_sys->p_d3dvtc; if( p_d3dvtc ) { IDirect3DVertexBuffer9_Release(p_d3dvtc); p_vout->p_sys->p_d3dvtc = NULL; } if( p_d3dtex ) { IDirect3DTexture9_Release(p_d3dtex); p_vout->p_sys->p_d3dtex = NULL; } msg_Dbg( p_vout, "Direct3D scene released successfully");}/***************************************************************************** * Render: copy picture surface into a texture and render into a scene ***************************************************************************** * This function is intented for higher end 3D cards, with pixel shader support * and at least 64 MB of video RAM. *****************************************************************************/static void Direct3DVoutRenderScene( vout_thread_t *p_vout, picture_t *p_pic ){ LPDIRECT3DDEVICE9 p_d3ddev = p_vout->p_sys->p_d3ddev; LPDIRECT3DTEXTURE9 p_d3dtex; LPDIRECT3DVERTEXBUFFER9 p_d3dvtc; LPDIRECT3DSURFACE9 p_d3dsrc, p_d3ddest; CUSTOMVERTEX *p_vertices; HRESULT hr; float f_width, f_height; // check if device is still available hr = IDirect3DDevice9_TestCooperativeLevel(p_d3ddev); if( FAILED(hr) ) { if( (D3DERR_DEVICENOTRESET != hr) || (VLC_SUCCESS != Direct3DVoutResetDevice(p_vout)) ) { // device is not usable at present (lost device, out of video mem ?) return; } } p_d3dtex = p_vout->p_sys->p_d3dtex; p_d3dvtc = p_vout->p_sys->p_d3dvtc; /* Clear the backbuffer and the zbuffer */ hr = IDirect3DDevice9_Clear( p_d3ddev, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 ); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return; } /* retrieve picture surface */ p_d3dsrc = (LPDIRECT3DSURFACE9)p_pic->p_sys; if( NULL == p_d3dsrc ) { msg_Dbg( p_vout, "no surface to render ?"); return; } /* retrieve texture top-level surface */ hr = IDirect3DTexture9_GetSurfaceLevel(p_d3dtex, 0, &p_d3ddest); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return; } /* Copy picture surface into texture surface, color space conversion happens here */ hr = IDirect3DDevice9_StretchRect(p_d3ddev, p_d3dsrc, NULL, p_d3ddest, NULL, D3DTEXF_NONE); IDirect3DSurface9_Release(p_d3ddest); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return; } /* Update the vertex buffer */ hr = IDirect3DVertexBuffer9_Lock(p_d3dvtc, 0, 0, (VOID **)(&p_vertices), D3DLOCK_DISCARD); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return; } /* Setup vertices */ f_width = (float)(p_vout->output.i_width) + 1; f_height = (float)(p_vout->output.i_height) + 1; p_vertices[0].x = 0.0f; // left p_vertices[0].y = 0.0f; // top p_vertices[0].z = 0.0f; p_vertices[0].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255); p_vertices[0].rhw = 1.0f; p_vertices[0].tu = 0.0f; p_vertices[0].tv = 0.0f; p_vertices[1].x = f_width; // right p_vertices[1].y = 0.0f; // top p_vertices[1].z = 0.0f; p_vertices[1].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255); p_vertices[1].rhw = 1.0f; p_vertices[1].tu = 1.0f; p_vertices[1].tv = 0.0f; p_vertices[2].x = f_width; // right p_vertices[2].y = f_height; // bottom p_vertices[2].z = 0.0f; p_vertices[2].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255); p_vertices[2].rhw = 1.0f; p_vertices[2].tu = 1.0f; p_vertices[2].tv = 1.0f; p_vertices[3].x = 0.0f; // left p_vertices[3].y = f_height; // bottom p_vertices[3].z = 0.0f; p_vertices[3].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255); p_vertices[3].rhw = 1.0f; p_vertices[3].tu = 0.0f; p_vertices[3].tv = 1.0f; hr= IDirect3DVertexBuffer9_Unlock(p_d3dvtc); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return; } // Begin the scene hr = IDirect3DDevice9_BeginScene(p_d3ddev); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return; } // Setup our texture. Using textures introduces the texture stage states, // which govern how textures get blended together (in the case of multiple // textures) and lighting information. In this case, we are modulating // (blending) our texture with the diffuse color of the vertices. hr = IDirect3DDevice9_SetTexture(p_d3ddev, 0, (LPDIRECT3DBASETEXTURE9)p_d3dtex); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); IDirect3DDevice9_EndScene(p_d3ddev); return; } // Render the vertex buffer contents hr = IDirect3DDevice9_SetStreamSource(p_d3ddev, 0, p_d3dvtc, 0, sizeof(CUSTOMVERTEX)); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); IDirect3DDevice9_EndScene(p_d3ddev); return; } // we use FVF instead of vertex shader hr = IDirect3DDevice9_SetVertexShader(p_d3ddev, NULL); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); IDirect3DDevice9_EndScene(p_d3ddev); return; } hr = IDirect3DDevice9_SetFVF(p_d3ddev, D3DFVF_CUSTOMVERTEX); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); IDirect3DDevice9_EndScene(p_d3ddev); return; } // draw rectangle hr = IDirect3DDevice9_DrawPrimitive(p_d3ddev, D3DPT_TRIANGLEFAN, 0, 2); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); IDirect3DDevice9_EndScene(p_d3ddev); return; } // End the scene hr = IDirect3DDevice9_EndScene(p_d3ddev); if( FAILED(hr) ) { msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr); return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -