HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BRAY_PixelFilter.h
Go to the documentation of this file.
1 
2 /*
3  * PROPRIETARY INFORMATION. This software is proprietary to
4  * Side Effects Software Inc., and is not to be reproduced,
5  * transmitted, or disclosed in any way without written permission.
6  *
7  * NAME: BRAY_PixelFilter.h (RAY Library, C++)
8  *
9  * COMMENTS:
10  */
11 
12 #ifndef __BRAY_PixelFilter__
13 #define __BRAY_PixelFilter__
14 
15 #include "BRAY_API.h"
16 #include "BRAY_SampleFilter.h"
17 #include "BRAY_Interface.h"
18 #include "BRAY_Raster.h"
19 #include <UT/UT_UniquePtr.h>
20 #include <UT/UT_IntArray.h>
21 
22 class UT_Options;
23 class BRAY_PixelState;
24 class BRAY_FilterInit;
25 
26 extern "C" {
27  SYS_VISIBILITY_EXPORT extern void newBRAYPixelFilter(void *);
28 }
29 
30 /// Pixel filters take raw samples as generated by the renderer and can adjust
31 /// the sample data values prior to sending it to pixel filtering. A pixel
32 /// filter might average all the samples for a given pixel, or it might just
33 /// select a single sample for the pixel value.
35 {
36 public:
38  {
39  public:
40  virtual ~Factory();
41 
42  /// Create an instance of the BRAY_PixelFilter
43  virtual UT_UniquePtr<BRAY_PixelFilter> instance(const UT_Options *o) const = 0;
44 
45  /// Test if the filter is deprecated
46  virtual bool isDeprecated() const { return false; }
47  };
49 
50  /// @{
51  /// Access to factory
52  static void listFactories(UT_StringArray &result,
53  bool include_deprecated=false);
54 
55  static void registerPlugin(const UT_StringHolder &name, FactoryPtr factory);
56 
57  static UT_UniquePtr<BRAY_PixelFilter> instancePlugin(
58  const UT_StringRef &style,
59  const UT_Options *options=nullptr);
60  static void releasePlugin(UT_UniquePtr<BRAY_PixelFilter> p);
61  static void freePluginCache();
62  /// @}
63 
64  // c-tor
65  BRAY_PixelFilter(const UT_Options *options);
66 
67  // d-tor
68  virtual ~BRAY_PixelFilter();
69 
70  /// Class name for the pixel filter
71  virtual const char *className() const = 0;
72 
73  /// Sample data passed to the pixel filter is the same as the data
74  /// processed by sample filters.
76 
77  /// Process the sample data. Each AOV plane of the @c data will point to a
78  /// contiguous array of sample data which contains at least @c npixels of
79  /// data. The filter should be applied to each pixel in the sample data.
80  ///
81  /// The @c sample_locations are stored as: @code
82  /// v[0] := pixel_x location
83  /// v[1] := pixel_y location
84  /// v[2] := jitter_x location (0 to 1 in pixel)
85  /// v[3] := jitter_y location (0 to 1 in pixel)
86  /// @endcode
87  ///
88  /// Note that with Filtered Importance Sampling, the jitter values may be
89  /// outside the given pixel.
90  ///
91  /// The @c BRAY_PixelState object is an opaque object that should be passed
92  /// to @c writePixel()
93  ///
94  /// The typical filtering code looks like: @code
95  /// for (size_t sample = 0; sample < nsamples; ++sample)
96  /// {
97  /// // Optionally only write data if the sample hit an object
98  /// // if (sampleHitOrMiss(pstate)) {}
99  /// writePixel(pstate, data, sample_locations, i);
100  /// }
101  /// @endcode
102  virtual void filter(BRAY_PixelState &pstate,
103  const SampleData &data,
104  const UT_Vector4 *sample_locations,
105  size_t nsamples) = 0;
106 
107  /// The checkpoint function is called to save the state of the pixel filter
108  /// to a checkpoint file.
109  virtual bool checkpoint(UT_JSONWriter &w) const = 0;
110 
111  /// @c loadCheckpoint is called to restore the state of the pixel filter.
112  /// In theory, all the settings for the pixel filter should match. Only
113  /// per-pixel data should be different.
114  virtual bool loadCheckpoint(UT_JSONParser &p);
115 
116  /// Test whether the sample hit or missed intersecting an object
117  bool sampleHitOrMiss(const BRAY_PixelState &ps, int sidx) const;
118 
119  /// Write sample data to a pixel. The @c weight is the filter weight for
120  /// the given sample. The sample data is passed directly from the
121  /// BRAY_SampleFilters.
122  void writePixel(BRAY_PixelState &ps,
123  const SampleData &data,
124  const UT_Vector4 *sample_locations,
125  int sidx);
126 
127  /// Overwrite the color in the pixel with the given sample data. This
128  /// doesn't filter data, but writes the data directly to the AOVs,
129  /// overwriting the data that was there previously.
130  void overwritePixel(BRAY_PixelState &ps,
131  const SampleData &data,
132  const UT_Vector4 *sample_locations,
133  int sidx);
134 
135  /// Add to existing data without any filtering.
136  void accumulatePixel(BRAY_PixelState &ps,
137  const SampleData &data,
138  const UT_Vector4 *sample_locations,
139  int sidx);
140 
141  /// Test to see if data has been written to a given sample
142  bool hasWrittenPixel(const BRAY_PixelState &ps,
143  const UT_Vector4 *sample_locations,
144  int sidx) const;
145 
146  /// Return the AOVs that this pixel filter is applied to. This may not be
147  /// a subset of all the AOVs. The filter should only write to these planes
148  /// and no others.
149  const UT_IntArray &aovs() const { return myAOVs; }
150 
151  /// @{
152  /// @private This method is used to create the weight buffer.
153  void initializeWeight(BRAY_FilterInit &init,
154  int xres, int yres,
155  const UT_StringArray &aovnames);
156  void setAOVs(const UT_IntArray &aovs) { myAOVs = aovs; }
157  SYS_HashType optionsHash() const { return myOptionsHash; }
158  /// @}
159 
160  /// By default(0) one pixel filter instance can be applied to any number of
161  /// AOVs. Some filters (e.g. minmax under "min" or "max" mode) shouldn't be
162  /// shared between multiple AOVs, in which case this may return 1.
163  virtual int aovLimit() const { return 0; }
164 
165  /// @{
166  /// Save and restore state
167  bool saveState(UT_JSONWriter &w) const;
168  bool loadState(UT_JSONParser &p);
169  /// @}
170 
171 protected:
172  /// This method can be used to perform any setup required prior to
173  /// filtering. This method may be called when re-rendering. That is, this
174  /// method may be called on an existing object.
175  ///
176  /// The AOV names passed in will be in the same order as the sample data
177  /// passed in.
178  virtual void setResolution(BRAY_FilterInit &init,
179  int xres, int yres,
180  const UT_StringArray &aovnames);
181 
182  /// If the filter needs an additional AOV, you can call this method during
183  /// setResolution(). The function returns the index of the new AOV (or -1
184  /// on failure). If there are errors, the messages will be printed out to
185  /// UT_ErrorLog.
186  ///
187  /// Data from this AOV will be available in the SampleData at the returned
188  /// index.
189  ///
190  /// If options are required, they can be initialized by planeProperties()
191  /// Passing in an empty OptionSet is legal.
192  int addAOV(BRAY_FilterInit &init,
193  const UT_StringHolder &name,
194  const UT_StringHolder &var,
195  int tuple_size,
196  const BRAY::OptionSet &options);
197 
198  /// Create an option set before adding creating an extra AOV
199  BRAY::OptionSet planeProperties(BRAY_FilterInit &init) const;
200 
201 private:
202  UT_IntArray myAOVs;
203  UT_UniquePtr<BRAY_Raster> myWriteCount;
204  SYS_HashType myOptionsHash;
205 };
206 
208 
209 #endif
UT_UniquePtr< Factory > FactoryPtr
#define SYS_VISIBILITY_EXPORT
void setAOVs(const UT_IntArray &aovs)
virtual int aovLimit() const
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
**But if you need a result
Definition: thread.h:613
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
Sample data for processing.
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual bool isDeprecated() const
Test if the filter is deprecated.
A map of string to various well defined value types.
Definition: UT_Options.h:84
#define BRAY_API
Definition: BRAY_API.h:12
SYS_VISIBILITY_EXPORT void newBRAYPixelFilter(void *)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
const UT_IntArray & aovs() const
Definition: format.h:895
UT_UniquePtr< BRAY_PixelFilter > BRAY_PixelFilterPtr
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297
SYS_HashType optionsHash() const