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);
        }
 protected override void DoInitialize()
 {
     {
         // particleSimulator-fountain.comp is also OK.
         var shaderCode = new ShaderCode(File.ReadAllText(@"shaders\ParticleSimulatorRenderer\particleSimulator.comp"), ShaderType.ComputeShader);
         this.computeProgram = shaderCode.CreateProgram();
     }
     {
         BufferPtr bufferPtr = this.positionBufferPtr;
         Texture   texture   = bufferPtr.DumpBufferTexture(OpenGL.GL_RGBA32F, autoDispose: false);
         texture.Initialize();
         this.positionTexture = texture;
     }
     {
         BufferPtr bufferPtr = this.velocityBufferPtr;
         Texture   texture   = bufferPtr.DumpBufferTexture(OpenGL.GL_RGBA32F, autoDispose: false);
         texture.Initialize();
         this.velocityTexture = texture;
     }
     {
         IndependentBufferPtr bufferPtr = null;
         using (var buffer = new UniformBuffer <vec4>(BufferUsage.DynamicCopy, noDataCopyed: true))
         {
             buffer.Create(elementCount: 64);
             bufferPtr = buffer.GetBufferPtr();
         }
         bufferPtr.Bind();
         OpenGL.BindBufferBase((BindBufferBaseTarget)BufferTarget.UniformBuffer, 0, bufferPtr.BufferId);
         this.attractorBufferPtr = bufferPtr;
         bufferPtr.Unbind();
     }
 }
        protected override void DoInitialize()
        {
            {
                // particleSimulator-fountain.comp is also OK.
                var shaderCode = new ShaderCode(File.ReadAllText(@"shaders\ParticleSimulatorRenderer\particleSimulator.comp"), ShaderType.ComputeShader);
                this.computeProgram = shaderCode.CreateProgram();
            }
            {
                GLBuffer buffer  = this.positionBuffer;
                Texture  texture = buffer.DumpBufferTexture(OpenGL.GL_RGBA32F, autoDispose: false);
                texture.Initialize();
                this.positionTexture = texture;
            }
            {
                GLBuffer buffer  = this.velocityBuffer;
                Texture  texture = buffer.DumpBufferTexture(OpenGL.GL_RGBA32F, autoDispose: false);
                texture.Initialize();
                this.velocityTexture = texture;
            }
            {
                const int     length = 64;
                UniformBuffer buffer = UniformBuffer.Create(typeof(vec4), length, BufferUsage.DynamicCopy);
                buffer.Bind();
                OpenGL.BindBufferBase((BindBufferBaseTarget)BufferTarget.UniformBuffer, 0, buffer.BufferId);
                buffer.Unbind();
                this.attractorBuffer = buffer;

                OpenGL.CheckError();
            }
        }
Example #4
0
 /// <summary>
 /// Apply this depth test render state.
 /// </summary>
 /// <param name="ctx">
 /// A <see cref="GraphicsContext"/> which has defined the shader program <paramref name="shaderProgram"/>.
 /// </param>
 /// <param name="shaderProgram">
 /// The <see cref="ShaderProgram"/> which has the state set.
 /// </param>
 public override void Apply(GraphicsContext ctx, ShaderProgram shaderProgram)
 {
     if (UniformBlockTag != null && shaderProgram != null && shaderProgram.IsActiveUniformBlock(UniformBlockTag))
     {
         // Ensure uniform buffer existing
         if (UniformBuffer == null)
         {
             _UniformBuffer = shaderProgram.CreateUniformBlock(UniformBlockTag, MapBufferUsageMask.MapWriteBit);
             _UniformBuffer.Create(ctx);
         }
         // Apply uniforms to uniform buffer
         ApplyState(ctx, shaderProgram, String.Empty);
         // Set uniform block
         shaderProgram.SetUniformBlock(ctx, UniformBlockTag, _UniformBuffer);
     }
     else
     {
         // Apply uniforms to program
         ApplyState(ctx, shaderProgram, String.Empty);
     }
 }