diff --git a/panda/src/dxgsg9/wdxGraphicsBuffer9.cxx b/panda/src/dxgsg9/wdxGraphicsBuffer9.cxx index 4fcb0941fb..92ec6ba225 100644 --- a/panda/src/dxgsg9/wdxGraphicsBuffer9.cxx +++ b/panda/src/dxgsg9/wdxGraphicsBuffer9.cxx @@ -92,7 +92,7 @@ begin_frame(FrameMode mode, Thread *current_thread) { if (_dxgsg -> _d3d_device == 0) { return false; } - + if (mode == FM_render) { if (!save_bitplanes()) { return false; @@ -140,7 +140,7 @@ end_frame(FrameMode mode, Thread *current_thread) { //////////////////////////////////////////////////////////////////// // Function: wglGraphicsStateGuardian::save_bitplanes // Access: Public -// Description: After rendering, d3d_device will need to be restored +// Description: After rendering, d3d_device will need to be restored // to its initial state. This function saves the state. //////////////////////////////////////////////////////////////////// bool wdxGraphicsBuffer9:: @@ -163,7 +163,7 @@ save_bitplanes() { //////////////////////////////////////////////////////////////////// // Function: wglGraphicsStateGuardian::restore_bitplanes // Access: Public -// Description: After rendering, d3d_device will need to be restored +// Description: After rendering, d3d_device will need to be restored // to its initial state. This function restores the state. //////////////////////////////////////////////////////////////////// void wdxGraphicsBuffer9:: @@ -172,7 +172,7 @@ restore_bitplanes() { DCAST_INTO_V(dxgsg, _gsg); HRESULT hr; - + hr = dxgsg -> _d3d_device -> SetRenderTarget (0, _saved_color_buffer); if (!SUCCEEDED (hr)) { @@ -182,7 +182,7 @@ restore_bitplanes() { if (!SUCCEEDED (hr)) { dxgsg9_cat.error ( ) << "SetDepthStencilSurface " << D3DERRORSTRING(hr) FL; } - + _saved_color_buffer->Release(); _saved_depth_buffer->Release(); _saved_color_buffer = NULL; @@ -193,7 +193,7 @@ restore_bitplanes() { //////////////////////////////////////////////////////////////////// // Function: wglGraphicsStateGuardian::rebuild_bitplanes // Access: Public -// Description: If necessary, reallocates (or allocates) the +// Description: If necessary, reallocates (or allocates) the // bitplanes for the buffer. //////////////////////////////////////////////////////////////////// bool wdxGraphicsBuffer9:: @@ -221,11 +221,11 @@ rebuild_bitplanes() { } // Find the color and depth textures. Either may be present, - // or neither. + // or neither. // // NOTE: Currently, depth-stencil textures are not implemented, // but since it's coming soon, we're structuring for it. - + int color_tex_index = -1; int depth_tex_index = -1; for (int i=0; iset_x_size(bitplane_x); color_tex->set_y_size(bitplane_y); color_tex->set_format(Texture::F_rgba); - color_ctx = + color_ctx = DCAST(DXTextureContext9, color_tex->prepare_now(_gsg->get_prepared_objects(), _gsg)); if (color_tex->get_texture_type() == Texture::TT_2d_texture) { @@ -280,9 +280,12 @@ rebuild_bitplanes() { } else { color_cube = color_ctx->_d3d_cube_texture; nassertr(color_cube != 0, false); - hr = color_cube -> GetCubeMapSurface ((D3DCUBEMAP_FACES) _cube_map_index, 0, &color_surf); - if (!SUCCEEDED(hr)) { - dxgsg9_cat.error ( ) << "GetCubeMapSurface " << D3DERRORSTRING(hr) FL; + + if (_cube_map_index >= 0 && _cube_map_index < 6) { + hr = color_cube -> GetCubeMapSurface ((D3DCUBEMAP_FACES) _cube_map_index, 0, &color_surf); + if (!SUCCEEDED(hr)) { + dxgsg9_cat.error ( ) << "GetCubeMapSurface " << D3DERRORSTRING(hr) FL; + } } } } @@ -296,7 +299,7 @@ rebuild_bitplanes() { } if (!_depth_backing_store) { hr = _dxgsg -> _d3d_device -> - CreateDepthStencilSurface (bitplane_x, bitplane_y, _saved_depth_desc.Format, + CreateDepthStencilSurface (bitplane_x, bitplane_y, _saved_depth_desc.Format, _saved_depth_desc.MultiSampleType, _saved_depth_desc.MultiSampleQuality, false, &_depth_backing_store, NULL); if (!SUCCEEDED(hr)) { @@ -314,7 +317,7 @@ rebuild_bitplanes() { depth_tex->set_x_size(bitplane_x); depth_tex->set_y_size(bitplane_y); depth_tex->set_format(Texture::F_depth_component); // Should say depth_stencil - depth_ctx = + depth_ctx = DCAST(DXTextureContext9, depth_tex->prepare_now(_gsg->get_prepared_objects(), _gsg)); if (depth_tex->get_texture_type() == Texture::TT_2d_texture) { @@ -338,17 +341,21 @@ rebuild_bitplanes() { _backing_sizey = bitplane_y; // Load up the bitplanes. - - hr = _dxgsg -> _d3d_device -> SetRenderTarget (0, color_surf); - if (!SUCCEEDED (hr)) { - dxgsg9_cat.error ( ) << "SetRenderTarget " << D3DERRORSTRING(hr) FL; + + if (color_surf) { + hr = _dxgsg -> _d3d_device -> SetRenderTarget (0, color_surf); + if (!SUCCEEDED (hr)) { + dxgsg9_cat.error ( ) << "SetRenderTarget " << D3DERRORSTRING(hr) FL; + } } - - hr = _dxgsg -> _d3d_device -> SetDepthStencilSurface (depth_surf); - if (!SUCCEEDED (hr)) { - dxgsg9_cat.error ( ) << "SetDepthStencilSurface " << D3DERRORSTRING(hr) FL; + + if (depth_surf) { + hr = _dxgsg -> _d3d_device -> SetDepthStencilSurface (depth_surf); + if (!SUCCEEDED (hr)) { + dxgsg9_cat.error ( ) << "SetDepthStencilSurface " << D3DERRORSTRING(hr) FL; + } } - + // Decrement the reference counts on these surfaces. The refcounts // were incremented earlier when we called GetSurfaceLevel. @@ -372,7 +379,51 @@ rebuild_bitplanes() { //////////////////////////////////////////////////////////////////// void wdxGraphicsBuffer9:: select_cube_map(int cube_map_index) { + _cube_map_index = cube_map_index; + + HRESULT hr; + Texture *color_tex = 0; + DXTextureContext9 *color_ctx = 0; + IDirect3DCubeTexture9 *color_cube = 0; + IDirect3DSurface9 *color_surf = 0; + int color_tex_index = -1; + + for (int i=0; iget_format() != Texture::F_depth_component)&& + (get_texture(i)->get_format() != Texture::F_stencil_index)&& + (color_tex_index < 0)) { + color_tex_index = i; + } else { + _textures[i]._rtm_mode = RTM_copy_texture; + } + } + } + + color_tex = get_texture(color_tex_index); + if (color_tex) { + color_ctx = + DCAST(DXTextureContext9, + color_tex->prepare_now(_gsg->get_prepared_objects(), _gsg)); + + color_cube = color_ctx->_d3d_cube_texture; + + if (color_cube && _cube_map_index >= 0 && _cube_map_index < 6) { + hr = color_cube -> GetCubeMapSurface ((D3DCUBEMAP_FACES) _cube_map_index, 0, &color_surf); + if (!SUCCEEDED(hr)) { + dxgsg9_cat.error ( ) << "GetCubeMapSurface " << D3DERRORSTRING(hr) FL; + } + + hr = _dxgsg -> _d3d_device -> SetRenderTarget (0, color_surf); + if (!SUCCEEDED (hr)) { + dxgsg9_cat.error ( ) << "SetRenderTarget " << D3DERRORSTRING(hr) FL; + } + else { + color_surf->Release(); + } + } + } } //////////////////////////////////////////////////////////////////// @@ -411,7 +462,7 @@ close_buffer() { if (_gsg != (GraphicsStateGuardian *)NULL) { _gsg.clear(); } - + if (_color_backing_store) { _color_backing_store->Release(); _color_backing_store = NULL; @@ -443,7 +494,7 @@ open_buffer() { } else { DCAST_INTO_R(_dxgsg, _gsg, false); } - + if (!save_bitplanes()) { return false; } @@ -465,13 +516,13 @@ open_buffer() { _saved_depth_desc.MultiSampleType, _saved_depth_desc.MultiSampleQuality); _fb_properties.set_force_hardware(1); // Wild guess. - + if (!rebuild_bitplanes()) { restore_bitplanes(); return false; } - + restore_bitplanes(); return true; }