protected override void Destroy() { for (int i = 0; i < VertexBufferCount; i++) { vertexBuffers[i].Dispose(); } activeVertexBufferIndex = -1; mappedVertexBufferPointer = IntPtr.Zero; if (indexBuffer != null) { indexBuffer.Dispose(); indexBuffer = null; } indexBufferBinding = null; pipelineState = null; if (simpleEffect != null) { simpleEffect.Dispose(); simpleEffect = null; } for (int i = 0; i < VertexBufferCount; i++) { inputElementDescriptions[i] = null; } charsToRenderCount = -1; base.Destroy(); }
/// <summary> /// Initializes a new instance of the <see cref="PrimitiveQuad" /> class. /// </summary> /// <param name="graphicsDevice">The graphics device.</param> /// <param name="effect">The effect.</param> public PrimitiveQuad(GraphicsDevice graphicsDevice) { GraphicsDevice = graphicsDevice; sharedData = GraphicsDevice.GetOrCreateSharedData(GraphicsDeviceSharedDataType.PerDevice, "PrimitiveQuad::VertexBuffer", d => new SharedData(GraphicsDevice)); simpleEffect = new EffectInstance(new Effect(GraphicsDevice, SpriteEffect.Bytecode)); simpleEffect.Parameters.Set(SpriteBaseKeys.MatrixTransform, Matrix.Identity); simpleEffect.UpdateEffect(graphicsDevice); pipelineState = new MutablePipelineState(GraphicsDevice); pipelineState.State.SetDefaults(); pipelineState.State.InputElements = VertexDeclaration.CreateInputElements(); pipelineState.State.PrimitiveType = PrimitiveType; }
public ComputeEffectShader(RenderContext context) : base(context, null) { pipelineState = new MutablePipelineState(context.GraphicsDevice); // Setup the effect compiler EffectInstance = new DynamicEffectInstance("ComputeEffectShader", Parameters); EffectInstance.Initialize(context.Services); // We give ComputeEffectShader a higher priority, since they are usually executed serially and blocking EffectInstance.EffectCompilerParameters.TaskPriority = -1; ThreadNumbers = new Int3(1); ThreadGroupCounts = new Int3(1); SetDefaultParameters(); }
/// <summary> /// Initializes a new instance of the <see cref="XenkoRenderer"/> class. /// </summary> /// <param name="graphicsDeviceManager">The graphics device manager.</param> public XenkoRenderer(GraphicsDeviceManager graphicsDeviceManager, EffectSystem effectSystem) : base() { manager = graphicsDeviceManager; this.effectSystem = effectSystem; spriteBatch = new SpriteBatch(manager.GraphicsDevice); clipRectanges = new Stack<Rectangle>(); activeEffects = new Stack<EffectInstance>(); scissorRasterizerStateDescription = RasterizerStates.CullNone; scissorRasterizerStateDescription.ScissorTestEnable = true; // enables the scissor test geometryRasterizerStateDescription = RasterizerStates.CullNone; //geometryRasterizerStateDescription.FillMode = FillMode.Wireframe; geometryPipelineState = new MutablePipelineState(manager.GraphicsDevice); geometryPipelineState.State.DepthStencilState = DepthStencilStates.None; }
protected BatchBase(GraphicsDevice device, EffectBytecode defaultEffectByteCode, EffectBytecode defaultEffectByteCodeSRgb, ResourceBufferInfo resourceBufferInfo, VertexDeclaration vertexDeclaration, int indexSize = sizeof(short)) { if (defaultEffectByteCode == null) { throw new ArgumentNullException(nameof(defaultEffectByteCode)); } if (defaultEffectByteCodeSRgb == null) { throw new ArgumentNullException(nameof(defaultEffectByteCodeSRgb)); } if (resourceBufferInfo == null) { throw new ArgumentNullException("resourceBufferInfo"); } if (vertexDeclaration == null) { throw new ArgumentNullException("vertexDeclaration"); } GraphicsDevice = device; MutablePipeline = new MutablePipelineState(device); // TODO GRAPHICS REFACTOR Should we initialize FX lazily? DefaultEffect = new EffectInstance(new Effect(device, defaultEffectByteCode) { Name = "BatchDefaultEffect" }); DefaultEffectSRgb = new EffectInstance(new Effect(device, defaultEffectByteCodeSRgb) { Name = "BatchDefaultEffectSRgb" }); drawsQueue = new ElementInfo[resourceBufferInfo.BatchCapacity]; drawTextures = new Texture[resourceBufferInfo.BatchCapacity]; TextureComparer = new TextureIdComparer(); BackToFrontComparer = new SpriteBackToFrontComparer(); FrontToBackComparer = new SpriteFrontToBackComparer(); // set the vertex layout and size indexStructSize = indexSize; vertexStructSize = vertexDeclaration.CalculateSize(); // Creates the vertex buffer (shared by within a device context). ResourceContext = GraphicsDevice.GetOrCreateSharedData(GraphicsDeviceSharedDataType.PerContext, resourceBufferInfo.ResourceKey, d => new DeviceResourceContext(GraphicsDevice, vertexDeclaration, resourceBufferInfo)); }
/// <inheritdoc/> protected override void InitializeCore() { base.InitializeCore(); pipelineState = new MutablePipelineState(Context.GraphicsDevice); pipelineState.State.SetDefaults(); pipelineState.State.InputElements = PrimitiveQuad.VertexDeclaration.CreateInputElements(); pipelineState.State.PrimitiveType = PrimitiveQuad.PrimitiveType; if (EffectName == null) throw new ArgumentNullException("No EffectName specified"); // Setup the effect compiler EffectInstance.Initialize(Context.Services); // We give ImageEffectShader a higher priority, since they are usually executed serially and blocking EffectInstance.EffectCompilerParameters.TaskPriority = -1; SetDefaultParameters(); }
public PrepareThreadContext(RenderContext renderContext) { MutablePipelineState = new MutablePipelineState(renderContext.GraphicsDevice); Context = renderContext.GetThreadContext(); }
/// <summary> /// Initializes a FastTextRendering instance (create and build required ressources, ...). /// </summary> /// <param name="graphicsContext">The current GraphicsContext.</param> public unsafe FastTextRenderer Initialize([NotNull] GraphicsContext graphicsContext) { var indexBufferSize = MaxCharactersPerLine * MaxCharactersLines * 6 * sizeof(int); var indexBufferLength = indexBufferSize / IndexStride; // Map and build the indice buffer indexBuffer = graphicsContext.Allocator.GetTemporaryBuffer(new BufferDescription(indexBufferSize, BufferFlags.IndexBuffer, GraphicsResourceUsage.Dynamic)); var mappedIndices = graphicsContext.CommandList.MapSubresource(indexBuffer, 0, MapMode.WriteNoOverwrite, false, 0, indexBufferSize); var indexPointer = mappedIndices.DataBox.DataPointer; var i = 0; for (var c = 0; c < MaxCharactersPerLine * MaxCharactersLines; c++) { *(int *)(indexPointer + IndexStride * i++) = c * 4 + 0; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 1; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 2; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 1; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 3; *(int *)(indexPointer + IndexStride * i++) = c * 4 + 2; } graphicsContext.CommandList.UnmapSubresource(mappedIndices); indexBufferBinding = new IndexBufferBinding(Buffer.Index.New(graphicsContext.CommandList.GraphicsDevice, new DataPointer(indexPointer, indexBufferSize)), true, indexBufferLength); // Create vertex buffers vertexBuffers = new Buffer[VertexBufferCount]; for (int j = 0; j < VertexBufferCount; j++) { vertexBuffers[j] = Buffer.Vertex.New(graphicsContext.CommandList.GraphicsDevice, new VertexPositionNormalTexture[VertexBufferLength], GraphicsResourceUsage.Dynamic); } vertexBuffersBinding = new VertexBufferBinding[VertexBufferCount]; for (int j = 0; j < VertexBufferCount; j++) { vertexBuffersBinding[j] = new VertexBufferBinding(vertexBuffers[j], VertexPositionNormalTexture.Layout, 0); } inputElementDescriptions = new InputElementDescription[VertexBufferCount][]; for (int j = 0; j < VertexBufferCount; j++) { inputElementDescriptions[j] = vertexBuffersBinding[j].Declaration.CreateInputElements(); } // Create the pipeline state object pipelineState = new MutablePipelineState(graphicsContext.CommandList.GraphicsDevice); pipelineState.State.SetDefaults(); pipelineState.State.InputElements = inputElementDescriptions[0]; pipelineState.State.PrimitiveType = PrimitiveType.TriangleList; // Create the effect simpleEffect = new EffectInstance(new Effect(graphicsContext.CommandList.GraphicsDevice, SpriteEffect.Bytecode)); simpleEffect.Parameters.Set(SpriteBaseKeys.MatrixTransform, Matrix.Identity); simpleEffect.Parameters.Set(TexturingKeys.Texture0, DebugSpriteFont); simpleEffect.Parameters.Set(TexturingKeys.Sampler, graphicsContext.CommandList.GraphicsDevice.SamplerStates.LinearClamp); simpleEffect.Parameters.Set(SpriteEffectKeys.Color, TextColor); simpleEffect.UpdateEffect(graphicsContext.CommandList.GraphicsDevice); return(this); }
/// <inheritdoc/> protected override void InitializeCore() { base.InitializeCore(); MutablePipeline = new MutablePipelineState(Context.GraphicsDevice); // Create RenderEffectKey RenderEffectKey = RenderData.CreateStaticObjectKey<RenderEffect>(null, EffectPermutationSlotCount); // TODO: Assign weights so that PerDraw is always last? (we usually most custom user ones to be between PerView and PerDraw) perFrameDescriptorSetSlot = GetOrCreateEffectDescriptorSetSlot("PerFrame"); perViewDescriptorSetSlot = GetOrCreateEffectDescriptorSetSlot("PerView"); perDrawDescriptorSetSlot = GetOrCreateEffectDescriptorSetSlot("PerDraw"); RenderSystem.RenderStages.CollectionChanged += RenderStages_CollectionChanged; // Create effect slots Array.Resize(ref effectSlots, RenderSystem.RenderStages.Count); for (int index = 0; index < RenderSystem.RenderStages.Count; index++) { var renderStage = RenderSystem.RenderStages[index]; effectSlots[index] = CreateEffectPermutationSlot(renderStage.EffectSlotName); } }