Panda3D
computeNode.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file computeNode.cxx
10  * @author rdb
11  * @date 2014-06-19
12  */
13 
14 #include "pandabase.h"
15 #include "computeNode.h"
16 #include "cullTraverser.h"
17 #include "cullableObject.h"
18 #include "cullHandler.h"
19 #include "geomDrawCallbackData.h"
20 #include "omniBoundingVolume.h"
21 #include "config_pgraph.h"
22 
23 TypeHandle ComputeNode::_type_handle;
24 
25 /**
26  * Creates a ComputeNode with the given name. Use add_dispatch and also
27  * assign a shader using a ShaderAttrib.
28  */
30 ComputeNode(const std::string &name) :
31  PandaNode(name),
32  _dispatcher(new ComputeNode::Dispatcher)
33 {
34  set_internal_bounds(new OmniBoundingVolume);
35 }
36 
37 /**
38  *
39  */
41 ComputeNode(const ComputeNode &copy) :
42  PandaNode(copy),
43  _dispatcher(new ComputeNode::Dispatcher(*copy._dispatcher))
44 {
45 }
46 
47 /**
48  * Returns a newly-allocated Node that is a shallow copy of this one. It will
49  * be a different Node pointer, but its internal data may or may not be shared
50  * with that of the original Node.
51  */
53 make_copy() const {
54  return new ComputeNode(*this);
55 }
56 
57 /**
58  * Returns true if it is generally safe to combine this particular kind of
59  * PandaNode with other kinds of PandaNodes of compatible type, adding
60  * children or whatever. For instance, an LODNode should not be combined with
61  * any other PandaNode, because its set of children is meaningful.
62  */
63 bool ComputeNode::
64 safe_to_combine() const {
65  return false;
66 }
67 
68 /**
69  * Returns true if there is some value to visiting this particular node during
70  * the cull traversal for any camera, false otherwise. This will be used to
71  * optimize the result of get_net_draw_show_mask(), so that any subtrees that
72  * contain only nodes for which is_renderable() is false need not be visited.
73  */
74 bool ComputeNode::
75 is_renderable() const {
76  return true;
77 }
78 
79 /**
80  * Adds the node's contents to the CullResult we are building up during the
81  * cull traversal, so that it will be drawn at render time. For most nodes
82  * other than GeomNodes, this is a do-nothing operation.
83  */
84 void ComputeNode::
86  if (pgraph_cat.is_spam()) {
87  pgraph_cat.spam()
88  << "Found " << *this << " in state " << *data._state
89  << " draw_mask = " << data._draw_mask << "\n";
90  }
91 
92  // OK, render this node. Rendering this node means creating a
93  // CullableObject for the Dispatcher. We don't need to pass any Geoms,
94  // however.
95  CullableObject *object =
96  new CullableObject(nullptr, data._state,
97  data.get_internal_transform(trav));
98  object->set_draw_callback(_dispatcher);
99  trav->get_cull_handler()->record_object(object, trav);
100 }
101 
102 /**
103  * Writes a brief description of the node to the indicated output stream.
104  * This is invoked by the << operator. It may be overridden in derived
105  * classes to include some information relevant to the class.
106  */
107 void ComputeNode::
108 output(std::ostream &out) const {
109  PandaNode::output(out);
110 }
111 
112 /**
113  *
114  */
115 ComputeNode::Dispatcher::
116 Dispatcher() {
117 }
118 
119 /**
120  *
121  */
122 ComputeNode::Dispatcher::
123 Dispatcher(const Dispatcher &copy) :
124  _cycler(copy._cycler)
125 {
126 }
127 
128 /**
129  * Asks the GSG to dispatch the compute shader.
130  */
133  GeomDrawCallbackData *data = (GeomDrawCallbackData *)cbdata;
134  GraphicsStateGuardianBase *gsg = data->get_gsg();
135 
136  CDReader cdata(_cycler);
137 
138  Dispatches::const_iterator it;
139  for (it = cdata->_dispatches.begin(); it != cdata->_dispatches.end(); ++it) {
140  gsg->dispatch_compute(it->get_x(), it->get_y(), it->get_z());
141  }
142 
143  // No need to upcall; we don't have any geometry, after all.
144 }
145 
146 /**
147  * Tells the BamReader how to create objects of type ComputeNode.
148  */
149 void ComputeNode::
151  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
152 }
153 
154 /**
155  * Writes the contents of this object to the datagram for shipping out to a
156  * Bam file.
157  */
158 void ComputeNode::
160  PandaNode::write_datagram(manager, dg);
161  manager->write_cdata(dg, _dispatcher->_cycler);
162 }
163 
164 /**
165  * This function is called by the BamReader's factory when a new object of
166  * type ComputeNode is encountered in the Bam file. It should create the
167  * ComputeNode and extract its information from the file.
168  */
169 TypedWritable *ComputeNode::
170 make_from_bam(const FactoryParams &params) {
171  ComputeNode *node = new ComputeNode("");
172  DatagramIterator scan;
173  BamReader *manager;
174 
175  parse_params(params, scan, manager);
176  node->fillin(scan, manager);
177 
178  return node;
179 }
180 
181 /**
182  * This internal function is called by make_from_bam to read in all of the
183  * relevant data from the BamFile for the new ComputeNode.
184  */
185 void ComputeNode::
186 fillin(DatagramIterator &scan, BamReader *manager) {
187  PandaNode::fillin(scan, manager);
188  manager->read_cdata(scan, _dispatcher->_cycler);
189 }
190 
191 /**
192  *
193  */
194 CycleData *ComputeNode::Dispatcher::CData::
195 make_copy() const {
196  return new CData(*this);
197 }
198 
199 /**
200  * Writes the contents of this object to the datagram for shipping out to a
201  * Bam file.
202  */
204 write_datagram(BamWriter *manager, Datagram &dg) const {
205  dg.add_uint16(_dispatches.size());
206 
207  Dispatches::const_iterator it;
208  for (it = _dispatches.begin(); it != _dispatches.end(); ++it) {
209  generic_write_datagram(dg, *it);
210  }
211 }
212 
213 /**
214  * This internal function is called by make_from_bam to read in all of the
215  * relevant data from the BamFile for the new ComputeNode.
216  */
219  int num_dispatches = scan.get_uint16();
220  _dispatches.resize(num_dispatches);
221 
222  for (int i = 0; i < num_dispatches; ++i) {
223  generic_read_datagram(_dispatches[i], scan);
224  }
225 }
This specialization on CallbackData is passed when the callback is initiated from deep within the dra...
virtual void output(std::ostream &out) const
Writes a brief description of the node to the indicated output stream.
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
CullHandler * get_cull_handler() const
Returns the object that will receive the culled Geoms.
ComputeNode(const std::string &name)
Creates a ComputeNode with the given name.
Definition: computeNode.cxx:30
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: pandaNode.cxx:3589
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition: bamReader.cxx:695
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:47
virtual bool is_renderable() const
Returns true if there is some value to visiting this particular node during the cull traversal for an...
Definition: computeNode.cxx:75
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition: bamWriter.cxx:425
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This collects together the pieces of data that are accumulated for each node while walking the scene ...
static void register_with_read_factory()
Tells the BamReader how to create objects of type ComputeNode.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
This is a generic data block that is passed along to a CallbackObject when a callback is made.
Definition: callbackData.h:29
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition: computeNode.cxx:53
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The smallest atom of cull.
virtual void record_object(CullableObject *object, const CullTraverser *traverser)
This callback function is intended to be overridden by a derived class.
Definition: cullHandler.cxx:43
virtual void do_callback(CallbackData *cbdata)
Asks the GSG to dispatch the compute shader.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
virtual void add_for_draw(CullTraverser *trav, CullTraverserData &data)
Adds the node's contents to the CullResult we are building up during the cull traversal,...
Definition: computeNode.cxx:85
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A special node, the sole purpose of which is to invoke a dispatch operation on the assigned compute s...
Definition: computeNode.h:27
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is called by make_from_bam to read in all of the relevant data from the BamFil...
virtual void write_datagram(BamWriter *manager, Datagram &dg) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
void set_draw_callback(CallbackObject *draw_callback)
Specifies a CallbackObject that will be responsible for drawing this object.
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a special kind of GeometricBoundingVolume that fills all of space.
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
Definition: cullTraverser.h:45
virtual bool safe_to_combine() const
Returns true if it is generally safe to combine this particular kind of PandaNode with other kinds of...
Definition: computeNode.cxx:64
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.