Example #1
0
        /// <summary>
        /// Create or update resources defined by this IGraphicsState, based on the associated <see cref="ShaderProgram"/>.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> used for allocating resources.
        /// </param>
        /// <param name="shaderProgram">
        /// A <see cref="ShaderProgram"/> that will be used in conjunction with this IGraphicsState.
        /// </param>
        public override void Create(GraphicsContext ctx, ShaderProgram shaderProgram)
        {
            // Create IGraphicsResource uniforms (i.e. textures)
            Dictionary <string, UniformStateMember> uniformState = UniformState;

            foreach (KeyValuePair <string, UniformStateMember> pair in uniformState)
            {
                if (pair.Value.GetUniformType().GetInterface("IGraphicsResource") == null)
                {
                    continue;
                }

                IGraphicsResource graphicsResource = pair.Value.GetUniformValue(this) as IGraphicsResource;
                if (graphicsResource == null)
                {
                    continue;
                }

                // Create the IGraphicsResource associated with the uniform state variable
                graphicsResource.Create(ctx);
            }

            // Create uniform buffer, if supported
            if (UniformBlockTag != null && shaderProgram != null && shaderProgram.IsActiveUniformBlock(UniformBlockTag) && UniformBuffer == null)
            {
                _UniformBuffer = shaderProgram.CreateUniformBlock(UniformBlockTag, MapBufferUsageMask.MapWriteBit);
                _UniformBuffer.Create(ctx);
            }

            // Base implementation
            base.Create(ctx, shaderProgram);
        }
Example #2
0
        /// <summary>
        /// Performs a deep copy of this <see cref="IGraphicsState"/>.
        /// </summary>
        /// <returns>
        /// It returns the equivalent of this <see cref="IGraphicsState"/>, but all objects referenced
        /// are not referred by both instances.
        /// </returns>
        public override IGraphicsState Push()
        {
            ShaderUniformStateBase copiedState = (ShaderUniformStateBase)base.Push();

            if (copiedState._UniformBuffer != null)
            {
                copiedState._UniformBuffer.IncRef();
            }

#if ENABLE_REFS_ON_COPY
            foreach (KeyValuePair <string, UniformStateMember> pair in copiedState.UniformState)
            {
                //if (pair.Value.GetUniformType().GetInterface("IGraphicsResource") == null)
                //	continue;

                IGraphicsResource graphicsResource = pair.Value.GetUniformValue(this) as IGraphicsResource;
                if (graphicsResource == null)
                {
                    continue;
                }

                // Copied share references
                graphicsResource.IncRef();
            }
#endif

            return(copiedState);
        }
Example #3
0
		protected internal static void CheckThatExistence(GraphicsContext ctx, IGraphicsResource resource)
		{
			if (resource == null)
				throw new ArgumentNullException("resource");
			if (resource.Exists(ctx) == false)
				throw new InvalidOperationException("not existing");
		}
Example #4
0
        /// <summary>
        /// Create or update resources defined by this IGraphicsState, based on the associated <see cref="ShaderProgram"/>.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> used for allocating resources.
        /// </param>
        /// <param name="shaderProgram">
        /// A <see cref="ShaderProgram"/> that will be used in conjunction with this IGraphicsState.
        /// </param>
        public override void CreateState(GraphicsContext ctx, ShaderProgram shaderProgram)
        {
            // Create IGraphicsResource uniforms (i.e. textures)
            Dictionary <string, UniformStateMember> uniformState = UniformState;

            foreach (KeyValuePair <string, UniformStateMember> pair in uniformState)
            {
                if (pair.Value.GetUniformType().GetInterface("IGraphicsResource") == null)
                {
                    continue;
                }

                IGraphicsResource graphicsResource = pair.Value.GetUniformValue(this) as IGraphicsResource;
                if (graphicsResource == null)
                {
                    continue;
                }

                // Create the IGraphicsResource associated with the uniform state variable
                graphicsResource.Create(ctx);
            }

            // Uniform buffer is created depending on program
            // It is assumed that the uniform buffer layout is shared across all shader programs, otherwise
            // we need a way to identify the specific uniform buffer layout and map them based on ShaderProgram
            // instances
            CreateUniformBuffer(ctx, shaderProgram);
        }
		/// <summary>
		/// Collect rendering object for associated resource deletion.
		/// </summary>
		/// <param name="garbage">
		/// A <see cref="IGraphicsResource"/> which need to be released. It has to be a created object.
		/// </param>
		/// <remarks>
		/// <para>
		/// This routine collect a <see cref="IGraphicsResource"/> instance for later resource deletion. <paramref name="garbage"/>
		/// resources will be effectively released on <see cref="DeleteGarbage"/> method call with the approriate arguments.
		/// </para>
		/// <para>
		/// Note that <paramref name="garbage"/> won't be finalized by .NET runtime garbage collector until it is effectively
		/// released.
		/// </para>
		/// </remarks>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="garbage"/> is null.
		/// </exception>
		internal void CollectGarbage(IGraphicsResource garbage)
		{
			if (garbage == null)
				throw new ArgumentNullException("garbage");

			lock (mGarbageItemsLock) {
				mGarbageItems.Add(garbage);
			}
		}
Example #6
0
        /// <summary>
        /// Collect rendering object for associated resource deletion.
        /// </summary>
        /// <param name="garbage">
        /// A <see cref="IGraphicsResource"/> which need to be released. It has to be a created object.
        /// </param>
        /// <remarks>
        /// <para>
        /// This routine collect a <see cref="IGraphicsResource"/> instance for later resource deletion. <paramref name="garbage"/>
        /// resources will be effectively released on <see cref="DeleteGarbage"/> method call with the approriate arguments.
        /// </para>
        /// <para>
        /// Note that <paramref name="garbage"/> won't be finalized by .NET runtime garbage collector until it is effectively
        /// released.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="garbage"/> is null.
        /// </exception>
        internal void CollectGarbage(IGraphicsResource garbage)
        {
            if (garbage == null)
            {
                throw new ArgumentNullException("garbage");
            }

            lock (mGarbageItemsLock) {
                mGarbageItems.Add(garbage);
            }
        }
Example #7
0
            /// <summary>
            /// Actually create resources associated to the type.
            /// </summary>
            /// <param name="instance">
            /// A <see cref="Object"/> that specifies the underlying instance.
            /// </param>
            /// <param name="ctx">
            /// A <see cref="GraphicsContext"/> used for creating the resources.
            /// </param>
            public virtual void Create(object instance, GraphicsContext ctx)
            {
                IGraphicsResource graphicsResource = instance as IGraphicsResource;

                if (graphicsResource == null)
                {
                    throw new InvalidOperationException("not implementing IGraphicsResource");
                }

                graphicsResource.Create(ctx);
            }
Example #8
0
		/// <summary>
		/// Link a resource used by this UserGraphicsResource.
		/// </summary>
		/// <param name="graphicsResource">
		/// The <see cref="IGraphicsResource"/> that will be linked by this UserGraphicsResource. It will be referenced till
		/// this instance disposition. You should not manually reference this instance for the UserGraphicsResource lifetime.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="graphicsResource"/> is null.
		/// </exception>
		protected void LinkResource(IGraphicsResource graphicsResource)
		{
			if (graphicsResource == null)
				throw new ArgumentNullException("graphicsResource");
			if (ObjectNamespace != Guid.Empty && graphicsResource.ObjectNamespace != Guid.Empty && ObjectNamespace != graphicsResource.ObjectNamespace)
				throw new ArgumentException("namespace mismatch", "graphicsResource");

			// Reference resources
			graphicsResource.IncRef();
			// Unreference at disposition
			_GpuResources.Add(graphicsResource);
		}
Example #9
0
		/// <summary>
		/// Unlink a resource used by this UserGraphicsResource.
		/// </summary>
		/// <param name="graphicsResource">
		/// The <see cref="IGraphicsResource"/> that will be unlinked from this UserGraphicsResource. It will be unreferenced.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="graphicsResource"/> is null.
		/// </exception>
		protected void UnlinkResource(IGraphicsResource graphicsResource)
		{
			if (graphicsResource == null)
				throw new ArgumentNullException("graphicsResource");
			if (ObjectNamespace != Guid.Empty && graphicsResource.ObjectNamespace != Guid.Empty && ObjectNamespace != graphicsResource.ObjectNamespace)
				throw new ArgumentException("namespace mismatch", "graphicsResource");

			// Unreference at disposition
			bool res = _GpuResources.Remove(graphicsResource);
			Debug.Assert(res);
			// No more referenced
			graphicsResource.DecRef();
		}
        /// <summary>
        /// Link a resource associated with this SceneGraphObject.
        /// </summary>
        /// <param name="graphicsResource">
        /// A <see cref="IGraphicsResource"/> that will be created and disposed along with this SceneGraphObject instance.
        /// </param>
        /// <remarks>
        /// The <paramref name="graphicsResource"/> can be shared across multiple SceneGraphObject instances.
        /// </remarks>
        protected void LinkResource(IGraphicsResource graphicsResource)
        {
            if (graphicsResource == null)
            {
                throw new ArgumentNullException("graphicsResource");
            }

            // Formally a resource can be linked multiple times, but probably it is caused by a copy & paste error
            Debug.Assert(!_Resources.Contains(graphicsResource));

            // By default, threat resources as shared ones
            graphicsResource.IncRef();
            // Let create/dispose it
            _Resources.Add(graphicsResource);
        }
        /// <summary>
        /// Link a resource used by this UserGraphicsResource.
        /// </summary>
        /// <param name="graphicsResource">
        /// The <see cref="GraphicsResource"/> that will be linked by this UserGraphicsResource. It will be referenced till
        /// this instance disposition. You should not manually reference this instance for the UserGraphicsResource lifetime.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="graphicsResource"/> is null.
        /// </exception>
        protected void LinkResource(IGraphicsResource graphicsResource)
        {
            if (graphicsResource == null)
            {
                throw new ArgumentNullException("graphicsResource");
            }
            if (ObjectNamespace != Guid.Empty && graphicsResource.ObjectNamespace != Guid.Empty && ObjectNamespace != graphicsResource.ObjectNamespace)
            {
                throw new ArgumentException("namespace mismatch", "graphicsResource");
            }

            // Reference resources
            graphicsResource.IncRef();
            // Unreference at disposition
            Debug.Assert(!_UserResources.Contains(graphicsResource));
            _UserResources.Add(graphicsResource);
        }
Example #12
0
        /// <summary>
        /// Actually create this GraphicsResource resources.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> used for allocating resources.
        /// </param>
        protected override void CreateObject(GraphicsContext ctx)
        {
            Dictionary <string, UniformStateMember> uniformState = UniformState;

            foreach (KeyValuePair <string, UniformStateMember> pair in uniformState)
            {
                if (pair.Value.GetUniformType().GetInterface("IGraphicsResource") == null)
                {
                    continue;
                }

                IGraphicsResource graphicsResource = pair.Value.GetUniformValue(this) as IGraphicsResource;

                // Create the IGraphicsResource associated with the uniform state variable
                Debug.Assert(graphicsResource != null);
                graphicsResource.Create(ctx);
            }
        }
Example #13
0
        public void TestDeleteOnDispose(Type resourceType)
        {
            if (resourceType == null)
            {
                throw new ArgumentNullException("resourceType");
            }

            IGraphicsTypeSupport graphicsTypeSupport = GetGraphicsTypeSupport(resourceType);

            if (graphicsTypeSupport == null)
            {
                Assert.Inconclusive(String.Format("no type support for {0}", resourceType.Name));
            }

            // Assert currency of context
            Assert.IsTrue(_Context.IsCurrent);

            using (IGraphicsResource resource = graphicsTypeSupport.AllocateSpy <IGraphicsResource>(_Context)) {
                if (resource == null)
                {
                    Assert.Inconclusive(String.Format("unable to allocate an instance of {0}", resourceType.Name));
                }

                // Create the resource
                Assert.DoesNotThrow(delegate() { graphicsTypeSupport.Create(resource, _Context); });

                Assert.AreNotEqual(0, resource.ObjectName);
                Assert.AreEqual(_Context.ObjectNameSpace, resource.ObjectNamespace);
                Assert.IsTrue(resource.Exists(_Context));

                // Delete the resource
                resource.Dispose();
                // Dispose(GraphicsContext) method shall call the Delete(GraphicsContext) method
                resource.Received().Delete(_Context);

                Assert.AreEqual(0, resource.ObjectName);
                Assert.AreEqual(Guid.Empty, resource.ObjectNamespace);
                Assert.DoesNotThrow(delegate() { resource.Exists(_Context); });
                Assert.IsFalse(resource.Exists(_Context));
            }
        }
Example #14
0
        /// <summary>
        /// Set uniform variable state.
        /// </summary>
        /// <param name="uniformName">
        ///
        /// </param>
        /// <param name="value">
        ///
        /// </param>
        public void SetUniformState(string uniformName, object value)
        {
            if (uniformName == null)
            {
                throw new ArgumentNullException("uniformName");
            }

            UniformStateMember uniformStateMember;

            if (_UniformProperties.TryGetValue(uniformName, out uniformStateMember))
            {
                UniformStateVariable uniformStateVariable = (UniformStateVariable)uniformStateMember;

                IGraphicsResource prevResource = uniformStateVariable.UniformValue as IGraphicsResource;
                if (prevResource != null)
                {
                    prevResource.DecRef();
                }

                IGraphicsResource currResource = value as IGraphicsResource;
                if (currResource != null)
                {
                    currResource.IncRef();
                }

                uniformStateVariable.UniformValue = value;
            }
            else
            {
                IGraphicsResource currResource = value as IGraphicsResource;
                if (currResource != null)
                {
                    currResource.IncRef();
                }

                _UniformProperties.Add(uniformName, new UniformStateVariable(uniformName, value));
            }
        }
Example #15
0
        /// <summary>
        /// Dispose resources allocated by <see cref="Create(GraphicsContext, ShaderProgram)"/>.
        /// </summary>
        public override void Delete()
        {
            if (_UniformBuffer != null)
            {
                _UniformBuffer.Dispose();
                _UniformBuffer = null;
            }

            Dictionary <string, UniformStateMember> uniformState = UniformState;

            foreach (KeyValuePair <string, UniformStateMember> pair in uniformState)
            {
                if (pair.Value.GetUniformType().GetInterface("IGraphicsResource") == null)
                {
                    continue;
                }

                IGraphicsResource graphicsResource = pair.Value.GetUniformValue(this) as IGraphicsResource;
                if (graphicsResource == null)
                {
                    continue;
                }
            }
        }
Example #16
0
        /// <summary>
        /// Dispose resources hold by this GraphicsState.
        /// </summary>
        public override void Dispose()
        {
            if (_UniformBuffer != null)
            {
                _UniformBuffer.DecRef();
            }

#if ENABLE_REFS_ON_COPY
            foreach (KeyValuePair <string, UniformStateMember> pair in UniformState)
            {
                if (pair.Value.GetUniformType().GetInterface("IGraphicsResource") == null)
                {
                    continue;
                }

                IGraphicsResource graphicsResource = pair.Value.GetUniformValue(this) as IGraphicsResource;
                if (graphicsResource == null)
                {
                    continue;
                }
                graphicsResource.DecRef();
            }
#endif
        }
 /// <summary>
 /// Create the <see cref="IGraphicsResource"/> asynchrnously on the specified thread.
 /// </summary>
 /// <param name="graphicsResource"></param>
 /// <param name="ctx">
 /// The <see cref="GraphicsContext"/> which will create the resource.
 /// </param>
 public static void CreateAsync(this IGraphicsResource graphicsResource, GraphicsContext ctx)
 {
     ctx.CreateAsync(graphicsResource);
 }
Example #18
0
 /// <summary>
 /// Adds an IGraphicsObject to the internal list of graphics objects.
 /// </summary>
 public void AddGraphicsObject( IGraphicsResource graphicsObject )
 {
     graphicsObjects.Add( graphicsObject );
 }