1 #ifndef DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <dali/public-api/shader-effects/sampler.h>
21 #include <dali/integration-api/resource-declarations.h>
22 #include <dali/internal/event/common/event-thread-services.h>
23 #include <dali/internal/update/common/double-buffered.h>
24 #include <dali/internal/update/common/double-buffered-property.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/resources/bitmap-metadata.h>
27 #include <dali/internal/render/data-providers/sampler-data-provider.h>
37 class SceneController;
39 class Sampler : public PropertyOwner, public SamplerDataProvider
42 typedef Dali::Sampler::FilterMode FilterMode;
43 typedef Dali::Sampler::WrapMode WrapMode;
48 Sampler( const std::string& samplerName );
56 * Set the uniform name of this sampler. This allows the shader to find the
57 * GL index of this sampler.
59 void SetUnitName( const std::string& unitName );
62 * Set the texture identity of this sampler (needs to double buffer this value because
63 * it can be read through the data provider interface in the render thread )
64 * @param[in] bufferIndex The buffer index to use
65 * @param[in] textureId The identity of the texture
67 void SetTexture( BufferIndex bufferIndex, Integration::ResourceId textureId );
70 * Set the filter modes for minify and magnify filters
71 * @param[in] bufferIndex The buffer index to use
72 * @param[in] minFilter The minify filter
73 * @param[in] magFilter The magnify filter
75 void SetFilterMode( BufferIndex bufferIndex, FilterMode minFilter, FilterMode magFilter );
78 * @param[in] bufferIndex The buffer index to use
80 void SetWrapMode( BufferIndex bufferIndex, WrapMode uWrap, WrapMode vWrap );
83 * @param[in] bufferIndex The buffer index to use
84 * @return true if this sampler affects transparency of the material
85 * @note this should only be called from Update thread
87 bool AffectsTransparency( BufferIndex bufferIndex ) const;
90 * @param[in] bufferIndex The buffer index to use
91 * @return true if the texture is fully opaque
92 * @note this should only be called from Update thread
94 bool IsFullyOpaque( BufferIndex bufferIndex ) const;
97 public: // SamplerDataProvider interface - called from RenderThread
99 * Get the texture unit uniform name
100 * @return the name of the texture unit uniform
102 virtual const std::string& GetUnitName() const;
106 * @param[in] bufferIndex The buffer index to use
107 * @return the identity of the associated texture
109 virtual Integration::ResourceId GetTextureId(BufferIndex buffer) const;
112 * Get the filter mode
113 * @param[in] bufferIndex The buffer index to use
114 * @return The minify filter mode
116 virtual FilterMode GetMinifyFilterMode( BufferIndex bufferIndex ) const;
119 * Get the filter mode
120 * @param[in] bufferIndex The buffer index to use
121 * @return The magnify filter mode
123 virtual FilterMode GetMagnifyFilterMode( BufferIndex bufferIndex ) const;
126 * Get the horizontal wrap mode
127 * @param[in] bufferIndex The buffer index to use
128 * @return The horizontal wrap mode
130 virtual WrapMode GetUWrapMode( BufferIndex bufferIndex ) const;
133 * Get the vertical wrap mode
134 * @param[in] bufferIndex The buffer index to use
135 * @return The vertical wrap mode
137 virtual WrapMode GetVWrapMode( BufferIndex bufferIndex ) const;
140 * Connect the object to the scene graph
142 * @param[in] sceneController The scene controller - used for sending messages to render thread
143 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
145 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
148 * Disconnect the object from the scene graph
149 * @param[in] sceneController The scene controller - used for sending messages to render thread
150 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
152 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
155 std::string mUnitName; ///< The name of the uniform of the texture unit
157 // @todo MESH_REWORK Need these to automatically copy
158 // new value into old value on frame change
160 ResourceId mTextureId[2]; ///< The identity of the associated texture for this frame (Can be read from RenderThread)
162 BitmapMetadata mBitmapMetadata[2]; /// The meta data of the associated texture for this frame (Not needed in RenderThread)
164 DoubleBufferedProperty<int> mMinFilter; ///< The minify filter
165 DoubleBufferedProperty<int> mMagFilter; ///< The magnify filter
166 DoubleBufferedProperty<int> mUWrapMode; ///< The horizontal wrap mode
167 DoubleBufferedProperty<int> mVWrapMode; ///< The vertical wrap mode
169 // Note, this is only called from UpdateThread
170 DoubleBufferedProperty<bool> mAffectsTransparency; ///< If this sampler affects renderer transparency
173 } // namespace SceneGraph
175 inline void SetUnitNameMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, const std::string& name )
177 typedef MessageValue1< SceneGraph::Sampler, std::string > LocalType;
179 // Reserve some memory inside the message queue
180 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
182 // Construct message in the message queue memory; note that delete should not be called on the return value
183 new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetUnitName, name );
187 inline void SetTextureMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, unsigned int resourceId )
189 typedef MessageDoubleBuffered1< SceneGraph::Sampler, unsigned int > LocalType;
191 // Reserve some memory inside the message queue
192 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
194 // Construct message in the message queue memory; note that delete should not be called on the return value
195 new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetTexture, resourceId );
198 // Declare enum as a message parameter type outside the SceneGraph namespace
199 template <> struct ParameterType< SceneGraph::Sampler::FilterMode > : public BasicType< SceneGraph::Sampler::FilterMode > {};
202 inline void SetFilterModeMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, SceneGraph::Sampler::FilterMode minFilter, SceneGraph::Sampler::FilterMode magFilter )
204 typedef MessageDoubleBuffered2< SceneGraph::Sampler, SceneGraph::Sampler::FilterMode, SceneGraph::Sampler::FilterMode > LocalType;
206 // Reserve some memory inside the message queue
207 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
209 // Construct message in the message queue memory; note that delete should not be called on the return value
210 new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetFilterMode, minFilter, magFilter );
213 // Declare enum as a message parameter type
214 template <> struct ParameterType< SceneGraph::Sampler::WrapMode > : public BasicType< SceneGraph::Sampler::WrapMode > {};
217 inline void SetWrapModeMessage( EventThreadServices& eventThreadServices, const SceneGraph::Sampler& sampler, SceneGraph::Sampler::WrapMode horizontalWrap, SceneGraph::Sampler::WrapMode verticalWrap )
219 typedef MessageDoubleBuffered2< SceneGraph::Sampler, SceneGraph::Sampler::WrapMode, SceneGraph::Sampler::WrapMode > LocalType;
221 // Reserve some memory inside the message queue
222 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
224 // Construct message in the message queue memory; note that delete should not be called on the return value
225 new (slot) LocalType( &sampler, &SceneGraph::Sampler::SetWrapMode, horizontalWrap, verticalWrap );
228 } // namespace Internal
232 #endif // DALI_INTERNAL_SCENE_GRAPH_SAMPLER_H