20 #define FL << "\n" << __FILE__ << " " << __LINE__ << "\n" 33 const std::string &name,
39 GraphicsBuffer(engine, pipe, name, fb_prop, win_prop, flags, gsg, host)
43 _saved_color_buffer =
nullptr;
44 _saved_depth_buffer =
nullptr;
45 _color_backing_store =
nullptr;
46 _depth_backing_store =
nullptr;
50 _screenshot_buffer_type = _draw_buffer_type;
52 _shared_depth_buffer = 0;
57 cout <<
"+++++ wdxGraphicsBuffer9 constructor " <<
this <<
" " <<
this -> get_name ( ) <<
"\n";
67 dxgsg -> _graphics_buffer_list.push_back(_this);
75 ~wdxGraphicsBuffer9() {
78 cout <<
"----- wdxGraphicsBuffer9 destructor " <<
this <<
" " <<
this ->
get_name ( ) <<
"\n";
87 dxgsg -> _graphics_buffer_list.remove(_this);
100 std::list <wdxGraphicsBuffer9 *>::iterator graphics_buffer_iterator;
102 graphics_buffer_iterator = _shared_depth_buffer_list.begin( );
103 while (graphics_buffer_iterator != _shared_depth_buffer_list.end( )) {
104 graphics_buffer = (*graphics_buffer_iterator);
105 if (graphics_buffer) {
109 graphics_buffer_iterator = _shared_depth_buffer_list.begin( );
113 this -> close_buffer ( );
125 begin_frame_spam(mode);
126 if (_gsg ==
nullptr) {
129 if (_dxgsg -> _d3d_device == 0) {
133 if (mode == FM_render) {
134 if (!save_bitplanes()) {
137 if (!rebuild_bitplanes()) {
141 clear_cube_map_selection();
145 return _gsg->begin_frame(current_thread);
156 end_frame_spam(mode);
157 nassertv(_gsg !=
nullptr);
159 if (mode == FM_render) {
163 _gsg->end_frame(current_thread);
165 if (mode == FM_render) {
167 clear_cube_map_selection();
176 bool wdxGraphicsBuffer9::
179 DWORD render_target_index;
181 render_target_index = 0;
183 hr = _dxgsg -> _d3d_device -> GetRenderTarget (render_target_index, &_saved_color_buffer);
184 if (!SUCCEEDED (hr)) {
185 dxgsg9_cat.error ( ) <<
"GetRenderTarget " << D3DERRORSTRING(hr) FL;
189 _saved_depth_buffer = 0;
190 hr = _dxgsg -> _d3d_device -> GetDepthStencilSurface (&_saved_depth_buffer);
191 if (hr == D3DERR_NOTFOUND) {
195 if (!SUCCEEDED (hr)) {
196 dxgsg9_cat.error ( ) <<
"GetDepthStencilSurface " << D3DERRORSTRING(hr) FL;
207 void wdxGraphicsBuffer9::
208 restore_bitplanes() {
210 DCAST_INTO_V(dxgsg, _gsg);
213 DWORD render_target_index;
215 render_target_index = 0;
217 hr = dxgsg -> _d3d_device ->
218 SetRenderTarget (render_target_index, _saved_color_buffer);
219 if (!SUCCEEDED (hr)) {
220 dxgsg9_cat.error ( ) <<
"SetRenderTarget " << D3DERRORSTRING(hr) FL;
222 if (_saved_depth_buffer) {
223 hr = _dxgsg -> _d3d_device -> SetDepthStencilSurface (_saved_depth_buffer);
224 if (!SUCCEEDED (hr)) {
225 dxgsg9_cat.error ( ) <<
"SetDepthStencilSurface " << D3DERRORSTRING(hr) FL;
231 hr = _dxgsg -> _d3d_device -> SetRenderTarget (i,
nullptr);
232 if (!SUCCEEDED (hr)) {
233 dxgsg9_cat.error ( ) <<
"SetRenderTarget " << i <<
" " << D3DERRORSTRING(hr) FL;
237 _saved_color_buffer->Release();
238 if (_saved_depth_buffer) {
239 _saved_depth_buffer->Release();
241 _saved_color_buffer =
nullptr;
242 _saved_depth_buffer =
nullptr;
250 bool wdxGraphicsBuffer9::
251 rebuild_bitplanes() {
257 IDirect3DTexture9 *color_d3d_tex = 0;
258 IDirect3DTexture9 *depth_d3d_tex = 0;
259 IDirect3DCubeTexture9 *color_cube = 0;
260 IDirect3DCubeTexture9 *depth_cube = 0;
261 IDirect3DSurface9 *color_surf = 0;
262 IDirect3DSurface9 *depth_surf = 0;
263 DWORD render_target_index;
265 render_target_index = 0;
269 if ((_host != 0)&&(_creation_flags & GraphicsPipe::BF_size_track_host)) {
270 if (_host->get_size() != _size) {
272 _host->get_y_size());
284 int color_tex_index = -1;
285 int depth_tex_index = -1;
287 CDLockedReader cdata(_cycler);
288 for (
size_t i = 0; i != cdata->_textures.size(); ++i) {
289 const RenderTexture &rt = cdata->_textures[i];
290 RenderTextureMode rtm_mode = rt._rtm_mode;
291 if (rtm_mode == RTM_bind_or_copy) {
292 RenderTexturePlane plane = rt._plane;
300 case RTP_depth_stencil:
308 case RTP_aux_hrgba_0:
309 case RTP_aux_hrgba_1:
310 case RTP_aux_hrgba_2:
311 case RTP_aux_hrgba_3:
312 case RTP_aux_float_0:
313 case RTP_aux_float_1:
314 case RTP_aux_float_2:
315 case RTP_aux_float_3:
317 CDWriter cdataw(_cycler, cdata,
false);
318 cdataw->_textures[i]._rtm_mode = RTM_none;
321 cdata = CDLockedReader(_cycler);
325 CDWriter cdataw(_cycler, cdata,
false);
326 cdataw->_textures[i]._rtm_mode = RTM_copy_texture;
329 cdata = CDLockedReader(_cycler);
336 if (color_tex_index < 0) {
338 if ((_color_backing_store)&&
339 ((bitplane_x != _backing_sizex)||(bitplane_y != _backing_sizey))) {
340 _color_backing_store->Release();
341 _color_backing_store =
nullptr;
343 if (!_color_backing_store) {
344 hr = _dxgsg->_d3d_device->CreateRenderTarget(bitplane_x, bitplane_y,
345 _saved_color_desc.Format,
346 _saved_color_desc.MultiSampleType,
347 _saved_color_desc.MultiSampleQuality,
349 &_color_backing_store,
351 if (!SUCCEEDED(hr)) {
352 dxgsg9_cat.error ( ) <<
"CreateRenderTarget " << D3DERRORSTRING(hr) FL;
355 color_surf = _color_backing_store;
358 if (_color_backing_store) {
359 _color_backing_store->Release();
360 _color_backing_store =
nullptr;
367 color_tex->
prepare_now(0, _gsg->get_prepared_objects(), _gsg));
372 <<
"Unable to re-create texture " << *color_ctx->
get_texture() << endl;
376 color_d3d_tex = color_ctx->_d3d_2d_texture;
377 nassertr(color_d3d_tex != 0,
false);
378 hr = color_d3d_tex -> GetSurfaceLevel(0, &color_surf);
379 if (!SUCCEEDED(hr)) {
380 dxgsg9_cat.error ( ) <<
"GetSurfaceLevel " << D3DERRORSTRING(hr) FL;
384 color_cube = color_ctx->_d3d_cube_texture;
385 nassertr(color_cube != 0,
false);
387 if (_cube_map_index >= 0 && _cube_map_index < 6) {
388 hr = color_cube -> GetCubeMapSurface ((D3DCUBEMAP_FACES) _cube_map_index, 0, &color_surf);
389 if (!SUCCEEDED(hr)) {
390 dxgsg9_cat.error ( ) <<
"GetCubeMapSurface " << D3DERRORSTRING(hr) FL;
399 release_depth =
true;
400 if (depth_tex_index < 0) {
401 if (_shared_depth_buffer) {
402 if (_shared_depth_buffer -> _depth_backing_store) {
404 printf (
"SHARE DEPTH BUFFER\n");
406 depth_surf = _shared_depth_buffer -> _depth_backing_store;
407 release_depth =
false;
410 if (depth_surf == 0) {
412 if ((_depth_backing_store)&&
413 ((bitplane_x != _backing_sizex)||(bitplane_y != _backing_sizey))) {
414 _depth_backing_store->Release();
415 _depth_backing_store =
nullptr;
417 if (!_depth_backing_store) {
418 hr = _dxgsg -> _d3d_device ->
419 CreateDepthStencilSurface (bitplane_x, bitplane_y, _saved_depth_desc.Format,
420 _saved_depth_desc.MultiSampleType, _saved_depth_desc.MultiSampleQuality,
421 false, &_depth_backing_store,
nullptr);
422 if (!SUCCEEDED(hr)) {
423 dxgsg9_cat.error ( ) <<
"CreateDepthStencilSurface " << D3DERRORSTRING(hr) FL;
426 depth_surf = _depth_backing_store;
430 if (_depth_backing_store) {
431 _depth_backing_store->Release();
432 _depth_backing_store =
nullptr;
435 if (_shared_depth_buffer) {
436 depth_tex = _shared_depth_buffer ->
get_texture(depth_tex_index);
438 if (depth_tex == 0) {
443 depth_tex->
set_format(Texture::F_depth_stencil);
446 depth_tex->
prepare_now(0, _gsg->get_prepared_objects(), _gsg));
450 <<
"Unable to re-create texture " << *depth_ctx->
get_texture() << endl;
454 depth_d3d_tex = depth_ctx->_d3d_2d_texture;
455 nassertr(depth_d3d_tex != 0,
false);
456 hr = depth_d3d_tex -> GetSurfaceLevel(0, &depth_surf);
457 if (!SUCCEEDED(hr)) {
458 dxgsg9_cat.error ( ) <<
"GetSurfaceLevel " << D3DERRORSTRING(hr) FL;
462 depth_cube = depth_ctx->_d3d_cube_texture;
463 nassertr(depth_cube != 0,
false);
464 hr = depth_cube -> GetCubeMapSurface ((D3DCUBEMAP_FACES) _cube_map_index, 0, &depth_surf);
465 if (!SUCCEEDED(hr)) {
466 dxgsg9_cat.error ( ) <<
"GetCubeMapSurface " << D3DERRORSTRING(hr) FL;
472 _backing_sizex = bitplane_x;
473 _backing_sizey = bitplane_y;
477 hr = _dxgsg -> _d3d_device -> SetRenderTarget (render_target_index, color_surf);
478 if (!SUCCEEDED (hr)) {
479 dxgsg9_cat.error ( ) <<
"SetRenderTarget " << D3DERRORSTRING(hr) FL;
484 hr = _dxgsg -> _d3d_device -> SetDepthStencilSurface (depth_surf);
485 if (!SUCCEEDED (hr)) {
486 dxgsg9_cat.error ( ) <<
"SetDepthStencilSurface " << D3DERRORSTRING(hr) FL;
490 render_target_index = 1;
507 case RTP_aux_hrgba_0:
508 case RTP_aux_hrgba_1:
509 case RTP_aux_hrgba_2:
510 case RTP_aux_hrgba_3:
511 case RTP_aux_float_0:
512 case RTP_aux_float_1:
513 case RTP_aux_float_2:
514 case RTP_aux_float_3:
517 IDirect3DTexture9 *color_d3d_tex = 0;
518 IDirect3DSurface9 *color_surf = 0;
519 IDirect3DCubeTexture9 *color_cube = 0;
525 <<
"Unable to re-create texture " << *color_ctx->
get_texture() << endl;
529 color_d3d_tex = color_ctx->_d3d_2d_texture;
530 nassertr(color_d3d_tex != 0,
false);
532 hr = color_d3d_tex -> GetSurfaceLevel(0, &color_surf);
533 if (!SUCCEEDED(hr)) {
534 dxgsg9_cat.error ( ) <<
"GetSurfaceLevel " << D3DERRORSTRING(hr) FL;
537 hr = _dxgsg -> _d3d_device -> SetRenderTarget (render_target_index, color_surf);
538 if (SUCCEEDED (hr)) {
539 render_target_index++;
541 dxgsg9_cat.error ( ) <<
"SetRenderTarget " << render_target_index <<
" " << D3DERRORSTRING(hr) FL;
543 color_surf->Release();
558 if ((color_surf != 0)&&(color_surf != _color_backing_store)) {
559 color_surf->Release();
563 if ((depth_surf != 0)&&(depth_surf != _depth_backing_store)) {
564 depth_surf->Release();
579 DWORD render_target_index;
581 render_target_index = 0;
583 _cube_map_index = page;
588 IDirect3DCubeTexture9 *color_cube = 0;
589 IDirect3DSurface9 *color_surf = 0;
590 int color_tex_index = -1;
594 for (
size_t i = 0; i != cdata->_textures.size(); ++i) {
595 const RenderTexture &rt = cdata->_textures[i];
596 RenderTextureMode rtm_mode = rt._rtm_mode;
597 if (rtm_mode == RTM_bind_or_copy) {
599 if ((tex->
get_format() != Texture::F_depth_stencil)&&
600 (tex->
get_format() != Texture::F_depth_component)&&
601 (color_tex_index < 0)) {
604 CDWriter cdataw(_cycler, cdata,
false);
605 nassertv(cdata->_textures.size() == cdataw->_textures.size());
606 cdataw->_textures[i]._rtm_mode = RTM_copy_texture;
616 color_tex->
prepare_now(0, _gsg->get_prepared_objects(), _gsg));
617 color_cube = color_ctx->_d3d_cube_texture;
618 if (color_cube && _cube_map_index >= 0 && _cube_map_index < 6) {
619 hr = color_cube -> GetCubeMapSurface ((D3DCUBEMAP_FACES) _cube_map_index, 0, &color_surf);
620 if (!SUCCEEDED(hr)) {
621 dxgsg9_cat.error ( ) <<
"GetCubeMapSurface " << D3DERRORSTRING(hr) FL;
624 hr = _dxgsg -> _d3d_device -> SetRenderTarget (render_target_index, color_surf);
625 if (!SUCCEEDED (hr)) {
626 dxgsg9_cat.error ( ) <<
"SetRenderTarget " << D3DERRORSTRING(hr) FL;
629 color_surf->Release();
634 render_target_index = 1;
647 case RTP_aux_hrgba_0:
648 case RTP_aux_hrgba_1:
649 case RTP_aux_hrgba_2:
650 case RTP_aux_hrgba_3:
651 case RTP_aux_float_0:
652 case RTP_aux_float_1:
653 case RTP_aux_float_2:
654 case RTP_aux_float_3:
657 IDirect3DSurface9 *color_surf = 0;
658 IDirect3DCubeTexture9 *color_cube = 0;
665 printf (
"CUBEMAP i = %d, RenderTexturePlane = %d, _cube_map_index %d \n", i, plane, _cube_map_index);
668 color_cube = color_ctx->_d3d_cube_texture;
669 if (color_cube && _cube_map_index >= 0 && _cube_map_index < 6) {
670 hr = color_cube -> GetCubeMapSurface ((D3DCUBEMAP_FACES) _cube_map_index, 0, &color_surf);
671 if (!SUCCEEDED(hr)) {
672 dxgsg9_cat.error ( ) <<
"GetCubeMapSurface " << D3DERRORSTRING(hr) FL;
675 hr = _dxgsg -> _d3d_device -> SetRenderTarget (render_target_index, color_surf);
676 if (SUCCEEDED (hr)) {
677 render_target_index++;
679 dxgsg9_cat.error ( ) <<
"cube map SetRenderTarget " << render_target_index <<
" " << D3DERRORSTRING(hr) FL;
681 color_surf->Release();
710 while (PeekMessage(&msg,
nullptr, 0, 0, PM_NOREMOVE)) {
718 void wdxGraphicsBuffer9::
721 if (_color_backing_store) {
722 _color_backing_store->Release();
723 _color_backing_store =
nullptr;
725 if (_depth_backing_store) {
726 _depth_backing_store->Release();
727 _depth_backing_store =
nullptr;
730 _cube_map_index = -1;
738 bool wdxGraphicsBuffer9::
749 DCAST_INTO_R(_dxgsg, _gsg,
false);
751 if (!save_bitplanes()) {
756 hr = _saved_color_buffer -> GetDesc (&_saved_color_desc);
757 if (!SUCCEEDED (hr)) {
758 dxgsg9_cat.error ( ) <<
"GetDesc " << D3DERRORSTRING(hr) FL;
761 hr = _saved_depth_buffer -> GetDesc (&_saved_depth_desc);
762 if (!SUCCEEDED (hr)) {
763 dxgsg9_cat.error ( ) <<
"GetDesc " << D3DERRORSTRING(hr) FL;
766 _fb_properties = _dxgsg->
767 calc_fb_properties(_saved_color_desc.Format,
768 _saved_depth_desc.Format,
769 _saved_depth_desc.MultiSampleType,
770 _saved_depth_desc.MultiSampleQuality);
771 _fb_properties.set_force_hardware(1);
774 if (!rebuild_bitplanes()) {
786 void wdxGraphicsBuffer9::
790 if (!GetMessage(&msg,
nullptr, 0, 0)) {
797 TranslateMessage(&msg);
799 DispatchMessage(&msg);
817 if (
this != input_graphics_output && input_graphics_output) {
823 printf (
"share_depth_buffer\n");
829 printf (
"ERROR: share_depth_buffer: non matching width \n");
836 printf (
"ERROR: share_depth_buffer: non matching height \n");
845 _shared_depth_buffer = input_graphics_output;
858 if (_shared_depth_buffer) {
860 printf (
"wdxGraphicsBuffer9 unshare_depth_buffer \n");
866 _shared_depth_buffer = 0;
878 if (input_graphics_output) {
880 _shared_depth_buffer_list.push_back(input_graphics_output);
892 if (input_graphics_output) {
894 _shared_depth_buffer_list.remove(input_graphics_output);
void unregister_shared_depth_buffer(GraphicsOutput *graphics_output)
Unregister who is sharing the depth buffer.
A GraphicsStateGuardian for rendering into DirectX9 contexts.
Texture * get_texture() const
Returns the pointer to the associated Texture object.
void set_size_and_recalc(int x, int y)
Changes the x_size and y_size, then recalculates structures that depend on size.
virtual bool share_depth_buffer(GraphicsOutput *graphics_output)
Will attempt to use the depth buffer of the input graphics_output.
virtual Texture * get_texture(int i=0) const
Returns the nth texture into which the GraphicsOutput renders.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
int get_y_size() const
Returns the visible height of the window or buffer, if it is known.
get_texture_type
Returns the overall interpretation of the texture.
bool create_texture(DXScreenData &scrn)
Use panda texture's pixelbuffer to create a texture for the specified device.
virtual void select_target_tex_page(int page)
Called internally when the window is in render-to-a-texture mode and we are in the process of renderi...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void end_frame(FrameMode mode, Thread *current_thread)
This function will be called within the draw thread after rendering is completed for a given frame.
virtual void process_events()
Honor any requests recently made via request_open() or request_close().
void set_size_padded(int x=1, int y=1, int z=1)
Changes the size of the texture, padding if necessary, and setting the pad region as well.
static AutoTextureScale get_textures_power_2()
This flag returns ATS_none, ATS_up, or ATS_down and controls the scaling of textures in general.
get_format
Returns the format of the texture, which represents both the semantic meaning of the texels and,...
A container for the various kinds of properties we might ask to have on a graphics window before we o...
virtual void clear(Thread *current_thread)
Clears the entire framebuffer before rendering, according to the settings of get_color_clear_active()...
An offscreen render buffer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An offscreen buffer for rendering into.
set_format
Changes the format value for the texture components.
An object to create GraphicsOutputs that share a particular 3-D API.
virtual void process_events()
Do whatever processing is necessary to ensure that the window responds to user events.
This template class calls PipelineCycler::read() in the constructor and PipelineCycler::release_read(...
int count_textures() const
If the GraphicsOutput is set to render into a texture, returns the number of textures that are being ...
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
This is a base class for the various different classes that represent the result of a frame of render...
int get_x_size() const
Returns the visible width of the window or buffer, if it is known.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TextureContext * prepare_now(int view, PreparedGraphicsObjects *prepared_objects, GraphicsStateGuardianBase *gsg)
Creates a context for the texture on the particular GSG, if it does not already exist.
virtual bool begin_frame(FrameMode mode, Thread *current_thread)
This function will be called within the draw thread before beginning rendering for a given frame.
static int up_to_power_2(int value)
Returns the smallest power of 2 greater than or equal to value.
get_name
Returns the name that was passed to the GraphicsOutput constructor.
A thread; that is, a lightweight process.
Encapsulates all the communication with a particular instance of a given rendering backend.
virtual void unshare_depth_buffer()
Discontinue sharing the depth buffer.
This class is the main interface to controlling the render process.
TypeHandle is the identifier used to differentiate C++ class types.
A container for the various kinds of properties we might ask to have on a graphics frameBuffer before...
RenderTexturePlane get_texture_plane(int i=0) const
Returns the RenderTexturePlane associated with the nth render-texture.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
const FrameBufferProperties & get_fb_properties() const
Returns the framebuffer properties of the window.
void register_shared_depth_buffer(GraphicsOutput *graphics_output)
Register/save who is sharing the depth buffer.