Panda3D

Loading...
Searching...
No Matches
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 */
29 ComputeNode::
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 */
40 ComputeNode::
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 */
52PandaNode *ComputeNode::
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::
85 add_for_draw(CullTraverser *trav, CullTraverserData &data) {
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 */
115ComputeNode::Dispatcher::
116Dispatcher() {
117}
118
119/**
120 *
121 */
122ComputeNode::Dispatcher::
123Dispatcher(const Dispatcher &copy) :
124 _cycler(copy._cycler)
125{
126}
127
128/**
129 * Asks the GSG to dispatch the compute shader.
130 */
131 void ComputeNode::Dispatcher::
132 do_callback(CallbackData *cbdata) {
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::
150 register_with_read_factory() {
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::
159 write_datagram(BamWriter *manager, Datagram &dg) {
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::
170make_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::
186fillin(DatagramIterator &scan, BamReader *manager) {
187 PandaNode::fillin(scan, manager);
188 manager->read_cdata(scan, _dispatcher->_cycler);
189}
190
191/**
192 *
193 */
194CycleData *ComputeNode::Dispatcher::CData::
195make_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 */
203 void ComputeNode::Dispatcher::CData::
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 */
217 void ComputeNode::Dispatcher::CData::
218 fillin(DatagramIterator &scan, BamReader *manager) {
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}
parse_params
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
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
BamReader::read_cdata
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
Definition bamReader.cxx:695
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
BamWriter::write_cdata
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
Definition bamWriter.cxx:425
CallbackData
This is a generic data block that is passed along to a CallbackObject when a callback is made.
Definition callbackData.h:29
ComputeNode::Dispatcher::CData::fillin
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...
Definition computeNode.cxx:218
ComputeNode::Dispatcher::CData::write_datagram
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.
Definition computeNode.cxx:204
ComputeNode::Dispatcher
Definition computeNode.h:56
ComputeNode::Dispatcher::do_callback
virtual void do_callback(CallbackData *cbdata)
Asks the GSG to dispatch the compute shader.
Definition computeNode.cxx:132
ComputeNode
A special node, the sole purpose of which is to invoke a dispatch operation on the assigned compute s...
Definition computeNode.h:27
ComputeNode::add_for_draw
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
ComputeNode::make_copy
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition computeNode.cxx:53
ComputeNode::register_with_read_factory
static void register_with_read_factory()
Tells the BamReader how to create objects of type ComputeNode.
Definition computeNode.cxx:150
ComputeNode::ComputeNode
ComputeNode(const std::string &name)
Creates a ComputeNode with the given name.
Definition computeNode.cxx:30
ComputeNode::is_renderable
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
ComputeNode::safe_to_combine
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
ComputeNode::output
virtual void output(std::ostream &out) const
Writes a brief description of the node to the indicated output stream.
Definition computeNode.cxx:108
ComputeNode::write_datagram
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 computeNode.cxx:159
CullHandler::record_object
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
CullTraverser
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
Definition cullTraverser.h:45
CullTraverser::get_cull_handler
CullHandler * get_cull_handler() const
Returns the object that will receive the culled Geoms.
Definition cullTraverser.I:152
CullableObject
The smallest atom of cull.
Definition cullableObject.h:41
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition datagramIterator.h:30
DatagramIterator::get_uint16
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
Definition datagramIterator.I:145
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
Datagram::add_uint16
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition datagram.I:85
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition factoryParams.h:36
Factory::register_factory
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
GeomDrawCallbackData
This specialization on CallbackData is passed when the callback is initiated from deep within the dra...
Definition geomDrawCallbackData.h:28
GraphicsStateGuardianBase
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
Definition graphicsStateGuardianBase.h:110
GraphicsStateGuardianBase::get_gsg
get_gsg
Returns the nth GSG in the universe.
Definition graphicsStateGuardianBase.h:247
OmniBoundingVolume
This is a special kind of GeometricBoundingVolume that fills all of space.
Definition omniBoundingVolume.h:24
PandaNode
A basic node of the scene graph or data graph.
Definition pandaNode.h:65
PandaNode::write_datagram
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:3602
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition typedWritable.h:35
computeNode.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
config_pgraph.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
cullHandler.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
cullTraverser.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
cullableObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geomDrawCallbackData.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
omniBoundingVolume.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.