internal static unsafe void Init() { FrameConstantsStereoLeftEye = MyHwBuffers.CreateConstantsBuffer(sizeof(MyFrameConstantsLayout), "FrameConstantsStereoLeftEye"); FrameConstantsStereoRightEye = MyHwBuffers.CreateConstantsBuffer(sizeof(MyFrameConstantsLayout), "FrameConstantsStereoRightEye"); FrameConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(MyFrameConstantsLayout), "FrameConstants"); ProjectionConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(Matrix), "ProjectionConstants"); ObjectConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(Matrix), "ObjectConstants"); FoliageConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(Vector4), "FoliageConstants"); MaterialFoliageTableConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(Vector4) * 256, "MaterialFoliageTableConstants"); OutlineConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(OutlineConstantsLayout), "OutlineConstants"); AlphamaskViewsConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(Matrix) * 181, "AlphamaskViewsConstants"); UpdateAlphamaskViewsConstants(); }
internal void OnDeviceReset() { if (m_indirectionBuffer != StructuredBufferId.NULL) { MyHwBuffers.Destroy(m_indirectionBuffer); MyHwBuffers.Destroy(m_instanceBuffer); } m_indirectionBuffer = StructuredBufferId.NULL; m_instanceBuffer = StructuredBufferId.NULL; Array.Clear(m_SRVs, 0, m_SRVs.Length); m_tablesDirty = true; m_instancesDataDirty = true; }
internal unsafe static void Init() { m_vs = MyShaders.CreateVs("sprite.hlsl"); m_ps = MyShaders.CreatePs("sprite.hlsl"); m_inputLayout = MyShaders.CreateIL(m_vs.BytecodeId, MyVertexLayouts.GetLayout( new MyVertexInputComponent(MyVertexInputComponentType.CUSTOM_HALF4_0, MyVertexInputComponentFreq.PER_INSTANCE), new MyVertexInputComponent(MyVertexInputComponentType.CUSTOM_HALF4_1, MyVertexInputComponentFreq.PER_INSTANCE), new MyVertexInputComponent(MyVertexInputComponentType.CUSTOM_HALF4_2, MyVertexInputComponentFreq.PER_INSTANCE), new MyVertexInputComponent(MyVertexInputComponentType.COLOR4, MyVertexInputComponentFreq.PER_INSTANCE) )); m_currentBufferSize = 100000; m_VB = MyHwBuffers.CreateVertexBuffer(m_currentBufferSize, sizeof(MyVertexFormatSpritePositionTextureRotationColor), BindFlags.VertexBuffer, ResourceUsage.Dynamic); m_contextsStack.Add(new MySpritesContext()); }
internal void ReleaseBuffers() { if (IB != IndexBufferId.NULL) { MyHwBuffers.Destroy(IB); IB = IndexBufferId.NULL; } if (VB != null) { foreach (var vb in VB) { //vb.Dispose(); MyHwBuffers.Destroy(vb); } VB = null; } }
public static void InitializeConstantBuffer(int?randomSeed = null) { MyRandom random; if (randomSeed.HasValue) { random = new MyRandom(randomSeed.Value); } else { random = new MyRandom(); } const int JITTERSIZE = 4 * 4; var jitters = new Vector4[JITTERSIZE]; for (int i = 0; i < JITTERSIZE; i++) { float angle = 2.0f * (float)Math.PI * random.NextFloat() / NUM_DIRECTIONS; jitters[i].X = (float)Math.Cos(angle); jitters[i].Y = (float)Math.Sin(angle); jitters[i].Z = random.NextFloat(); jitters[i].W = random.NextFloat(); } PerPassConstantBuffer data; for (uint sliceIndex = 0; sliceIndex < NUM_SLICES; ++sliceIndex) { data.Offset.X = (float)(sliceIndex % 4) + 0.5f; data.Offset.Y = (float)(sliceIndex / 4) + 0.5f; data.Jitter = jitters[sliceIndex]; data.SliceIndexFloat = (float)sliceIndex; data.SliceIndexInt = sliceIndex; var buffer = m_perPassCBs[sliceIndex]; if (buffer == ConstantsBufferId.NULL) { buffer = MyHwBuffers.CreateConstantsBuffer(PERPASSCONSTANTBUFFERSIZE, "MyHBAO::passCB " + sliceIndex); m_perPassCBs[sliceIndex] = buffer; } var mapping = MyMapping.MapDiscard(buffer); mapping.WriteAndPosition(ref data); mapping.Unmap(); } }
internal static void Init() { m_linearizeDepthPS = MyShaders.CreatePs("Postprocess/HBAO/LinearizeDepth.hlsl"); m_deinterleaveDepthPS = MyShaders.CreatePs("Postprocess/HBAO/DeinterleaveDepth.hlsl"); m_reinterleaveAOPS = MyShaders.CreatePs("Postprocess/HBAO/ReinterleaveAO.hlsl"); m_reinterleaveAOPS_PreBlur = MyShaders.CreatePs("Postprocess/HBAO/ReinterleaveAO.hlsl", new SharpDX.Direct3D.ShaderMacro[] { new SharpDX.Direct3D.ShaderMacro("ENABLE_BLUR", 1) }); m_copyPS = MyShaders.CreatePs("Postprocess/HBAO/Copy.hlsl"); m_dataCB = MyHwBuffers.CreateConstantsBuffer(GLOBALCONSTANTBUFFERSIZE, "MyHBAO::dataCB"); for (int it = 0; it < NUM_SLICES; it++) { m_perPassCBs[it] = ConstantsBufferId.NULL; } InitializeConstantBuffer(); }
internal static unsafe void Init() { //MyRender11.RegisterSettingsChangedListener(new OnSettingsChangedDelegate(RecreateShadersForSettings)); DirectionalEnvironmentLight_Pixel = MyShaders.CreatePs("Lighting/LightDir.hlsl"); DirectionalEnvironmentLight_Sample = MyShaders.CreatePs("Lighting/LightDir.hlsl", MyRender11.ShaderSampleFrequencyDefine()); PointlightsTiled_Pixel = MyShaders.CreatePs("Lighting/LightPoint.hlsl"); PointlightsTiled_Sample = MyShaders.CreatePs("Lighting/LightPoint.hlsl", MyRender11.ShaderSampleFrequencyDefine()); m_preparePointLights = MyShaders.CreateCs("Lighting/PrepareLights.hlsl", new[] { new ShaderMacro("NUMTHREADS", TILE_SIZE) }); SpotlightProxyVs = MyShaders.CreateVs("Lighting/LightSpot.hlsl"); SpotlightPs_Pixel = MyShaders.CreatePs("Lighting/LightSpot.hlsl"); SpotlightPs_Sample = MyShaders.CreatePs("Lighting/LightSpot.hlsl", MyRender11.ShaderSampleFrequencyDefine()); SpotlightProxyIL = MyShaders.CreateIL(SpotlightProxyVs.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION_PACKED)); m_pointlightCullHwBuffer = MyHwBuffers.CreateStructuredBuffer(MyRender11Constants.MAX_POINT_LIGHTS, sizeof(MyPointlightConstants), true, null, "MyLightRendering"); }
internal unsafe void UpdateCube(List <MyCubeInstanceData> instanceData, int capacity) { Debug.Assert(m_type == MyRenderInstanceBufferType.Cube); var instancesNum = instanceData.Count; if (m_capacity < instancesNum && VB != VertexBufferId.NULL) { MyHwBuffers.Destroy(VB); VB = VertexBufferId.NULL; } if (m_capacity < instancesNum) { m_capacity = Math.Max(instancesNum, capacity); VB = MyHwBuffers.CreateVertexBuffer(m_capacity, sizeof(MyVertexFormatCubeInstance), null, m_debugName + " instances buffer"); } var rawBuffer = new MyVertexFormatCubeInstance[m_capacity]; for (int i = 0; i < instancesNum; i++) { fixed(byte *pSource = instanceData[i].RawBones(), pTarget = rawBuffer[i].bones) { for (int j = 0; j < MyRender11Constants.CUBE_INSTANCE_BONES_NUM * 4; j++) { pTarget[j] = pSource[j]; } } rawBuffer[i].translationRotation = new HalfVector4(instanceData[i].m_translationAndRot); rawBuffer[i].colorMaskHSV = new HalfVector4(instanceData[i].ColorMaskHSV); } fixed(MyVertexFormatCubeInstance *dataPtr = rawBuffer) { DataBox srcBox = new DataBox(new IntPtr(dataPtr)); ResourceRegion dstRegion = new ResourceRegion(0, 0, 0, sizeof(MyVertexFormatCubeInstance) * instancesNum, 1, 1); MyRender11.ImmediateContext.UpdateSubresource(srcBox, VB.Buffer, 0, dstRegion); } BumpRenderable(); }
internal unsafe static void Init() { m_vs = MyShaders.CreateVs("billboard.hlsl", "vs"); m_ps = MyShaders.CreatePs("billboard.hlsl", "ps"); m_vsLit = MyShaders.CreateVs("billboard.hlsl", "vs", MyShaderHelpers.FormatMacros("LIT_PARTICLE")); m_psLit = MyShaders.CreatePs("billboard.hlsl", "ps", MyShaderHelpers.FormatMacros("LIT_PARTICLE")); m_inputLayout = MyShaders.CreateIL(m_vs.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION3, MyVertexInputComponentType.TEXCOORD0_H)); //MyCallbacks.RegisterDeviceResetListener(new OnDeviceResetDelegate(OnDeviceRestart)); InitBillboardsIndexBuffer(MaxBillboards); m_VB = MyHwBuffers.CreateVertexBuffer(MaxBillboards * 4, sizeof(MyVertexFormatPositionTextureH), BindFlags.VertexBuffer, ResourceUsage.Dynamic); var stride = sizeof(MyBillboardData); m_SB = MyHwBuffers.CreateStructuredBuffer(MaxBillboards, stride, true); MyTextureAtlas.ParseAtlasDescription("Textures\\Particles\\", "Textures\\Particles\\ParticlesAtlas.tai", m_atlasedTextures); }
internal unsafe void Update(MyRenderMessageDebugDrawMesh message) { edges = !message.Shaded; depth = message.DepthRead; if (message.VertexCount > vbuffer.Capacity) { MyHwBuffers.ResizeVertexBuffer(vbuffer, message.VertexCount); } var mapping = MyMapping.MapDiscard(MyPrimitivesRenderer.RC.DeviceContext, vbuffer.Buffer); for (int i = 0; i < message.VertexCount; i++) { MyVertexFormatPositionColor vert = new MyVertexFormatPositionColor(Vector3.Transform(message.Vertices[i].Position, message.WorldMatrix), message.Vertices[i].Color); mapping.WriteAndPosition(ref vert); } mapping.Unmap(); message.Vertices.Clear(); }
internal static void OnDeviceReset() { MyRenderContext.OnDeviceReset(); MyRenderContextPool.OnDeviceReset(); MyHwBuffers.OnDeviceReset(); MyShaders.OnDeviceReset(); MyMaterialShaders.OnDeviceReset(); MyPipelineStates.OnDeviceReset(); MyTextures.OnDeviceReset(); MyRwTextures.OnDeviceReset(); SamplerStates.OnDeviceReset(); MyTransparentRendering.OnDeviceReset(); ResetShadows(Settings.ShadowCascadeCount, RenderSettings.ShadowQuality.ShadowCascadeResolution()); MyBillboardRenderer.OnDeviceRestart(); MyScreenDecals.OnDeviceReset(); MyMeshMaterials1.OnDeviceReset(); MyVoxelMaterials1.OnDeviceReset(); MyRenderableComponent.MarkAllDirty(); foreach (var f in MyComponentFactory <MyFoliageComponent> .GetAll()) { f.Dispose(); } foreach (var c in MyComponentFactory <MyGroupRootComponent> .GetAll()) { c.OnDeviceReset(); } MyBigMeshTable.Table.OnDeviceReset(); MySceneMaterials.OnDeviceReset(); MyMeshes.OnDeviceReset(); MyInstancing.OnDeviceReset(); MyScreenDecals.OnDeviceReset(); }
private unsafe void InitIndexBuffer() { DestroyIndexBuffer(); const int indexCount = 36; ushort * indices = stackalloc ushort[indexCount]; indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 0; indices[4] = 2; indices[5] = 3; indices[6] = 1; indices[7] = 5; indices[8] = 6; indices[9] = 1; indices[10] = 6; indices[11] = 2; indices[12] = 5; indices[13] = 4; indices[14] = 7; indices[15] = 5; indices[16] = 7; indices[17] = 6; indices[18] = 4; indices[19] = 0; indices[20] = 3; indices[21] = 4; indices[22] = 3; indices[23] = 7; indices[24] = 3; indices[25] = 2; indices[26] = 6; indices[27] = 3; indices[28] = 6; indices[29] = 7; indices[30] = 1; indices[31] = 0; indices[32] = 4; indices[33] = 1; indices[34] = 4; indices[35] = 5; m_cubeIB = MyHwBuffers.CreateIndexBuffer(indexCount, Format.R16_UInt, BindFlags.IndexBuffer, ResourceUsage.Immutable, new IntPtr(indices), "MyScreenDecals"); }
internal unsafe void UpdateGeneric(List <MyInstanceData> instanceData, int capacity) { var instancesNum = instanceData.Count; if (m_capacity < instancesNum && VertexBuffer != VertexBufferId.NULL) { MyHwBuffers.Destroy(VertexBuffer); VertexBuffer = VertexBufferId.NULL; } if (m_capacity < instancesNum) { m_capacity = Math.Max(instancesNum, capacity); VertexBuffer = MyHwBuffers.CreateVertexBuffer(m_capacity, sizeof(MyVertexFormatGenericInstance), null, m_debugName + " instances buffer"); } fixed(MyInstanceData *dataPtr = instanceData.ToArray()) { DataBox srcBox = new DataBox(new IntPtr(dataPtr)); ResourceRegion dstRegion = new ResourceRegion(0, 0, 0, sizeof(MyVertexFormatGenericInstance) * instancesNum, 1, 1); MyRender11.ImmediateContext.UpdateSubresource(srcBox, VertexBuffer.Buffer, 0, dstRegion); } }
internal unsafe static void InitBillboardsIndexBuffer(int billboardsLimit) { if (m_IB != IndexBufferId.NULL) { MyHwBuffers.Destroy(m_IB); } uint[] indices = new uint[billboardsLimit * 6]; for (int i = 0; i < billboardsLimit; i++) { indices[i * 6 + 0] = (uint)(i * 4 + 0); indices[i * 6 + 1] = (uint)(i * 4 + 1); indices[i * 6 + 2] = (uint)(i * 4 + 2); indices[i * 6 + 3] = (uint)(i * 4 + 0); indices[i * 6 + 4] = (uint)(i * 4 + 2); indices[i * 6 + 5] = (uint)(i * 4 + 3); } fixed(uint *ptr = indices) { m_IB = MyHwBuffers.CreateIndexBuffer(MaxBillboards * 6, SharpDX.DXGI.Format.R32_UInt, BindFlags.IndexBuffer, ResourceUsage.Immutable, new IntPtr(ptr)); } }
unsafe static void InitBillboardsIndexBuffer() { if (m_IB != IndexBufferId.NULL) { MyHwBuffers.Destroy(m_IB); } uint[] indices = new uint[MAX_BILLBOARDS_SIZE * 6]; for (int i = 0; i < MAX_BILLBOARDS_SIZE; i++) { indices[i * 6 + 0] = (uint)(i * 4 + 0); indices[i * 6 + 1] = (uint)(i * 4 + 1); indices[i * 6 + 2] = (uint)(i * 4 + 2); indices[i * 6 + 3] = (uint)(i * 4 + 0); indices[i * 6 + 4] = (uint)(i * 4 + 2); indices[i * 6 + 5] = (uint)(i * 4 + 3); } fixed(uint *ptr = indices) { m_IB = MyHwBuffers.CreateIndexBuffer(MAX_BILLBOARDS_SIZE * 6, SharpDX.DXGI.Format.R32_UInt, BindFlags.IndexBuffer, ResourceUsage.Immutable, new IntPtr(ptr), "MyBillboardRenderer"); } }
internal static void Init() { //MyRender11.RegisterSettingsChangedListener(new OnSettingsChangedDelegate(RecreateShadersForSettings)); m_baseColorShader = MyShaders.CreatePs("debug.hlsl", "base_color"); m_baseColorLinearShader = MyShaders.CreatePs("debug.hlsl", "base_color_linear"); m_normalShader = MyShaders.CreatePs("debug.hlsl", "normal"); m_glossinessShader = MyShaders.CreatePs("debug.hlsl", "glossiness"); m_metalnessShader = MyShaders.CreatePs("debug.hlsl", "metalness"); m_matIDShader = MyShaders.CreatePs("debug.hlsl", "mat_id"); m_aoShader = MyShaders.CreatePs("debug.hlsl", "ambient_occlusion"); m_emissiveShader = MyShaders.CreatePs("debug.hlsl", "emissive"); m_ambientDiffuseShader = MyShaders.CreatePs("debug.hlsl", "debug_ambient_diffuse"); m_ambientSpecularShader = MyShaders.CreatePs("debug.hlsl", "debug_ambient_specular"); m_edgeDebugShader = MyShaders.CreatePs("debug.hlsl", "debug_edge"); m_shadowsDebugShader = MyShaders.CreatePs("debug.hlsl", "cascades_shadow"); m_screenVertexShader = MyShaders.CreateVs("debug.hlsl", "screenVertex"); m_blitTextureShader = MyShaders.CreatePs("debug.hlsl", "blitTexture"); m_inputLayout = MyShaders.CreateIL(m_screenVertexShader.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION2, MyVertexInputComponentType.TEXCOORD0)); m_quadBuffer = MyHwBuffers.CreateVertexBuffer(6, MyVertexFormatPosition2Texcoord.STRIDE, BindFlags.VertexBuffer, ResourceUsage.Dynamic); }
internal unsafe static void Init() { m_vs = MyShaders.CreateVs("billboard.hlsl"); m_ps = MyShaders.CreatePs("billboard.hlsl"); m_vsLit = MyShaders.CreateVs("billboard.hlsl", new [] { new ShaderMacro("LIT_PARTICLE", null) }); m_psLit = MyShaders.CreatePs("billboard.hlsl", new[] { new ShaderMacro("LIT_PARTICLE", null) }); m_psAlphaCutout = MyShaders.CreatePs("billboard.hlsl", new[] { new ShaderMacro("ALPHA_CUTOUT", null) }); m_psAlphaCutoutAndLit = MyShaders.CreatePs("billboard.hlsl", new[] { new ShaderMacro("ALPHA_CUTOUT", null), new ShaderMacro("LIT_PARTICLE", null) }); m_inputLayout = MyShaders.CreateIL(m_vs.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION3, MyVertexInputComponentType.TEXCOORD0_H)); //MyCallbacks.RegisterDeviceResetListener(new OnDeviceResetDelegate(OnDeviceRestart)); InitBillboardsIndexBuffer(MaxBillboards); m_VB = MyHwBuffers.CreateVertexBuffer(MaxBillboards * 4, sizeof(MyVertexFormatPositionTextureH), BindFlags.VertexBuffer, ResourceUsage.Dynamic, null, "MyBillboardRenderer"); var stride = sizeof(MyBillboardData); m_SB = MyHwBuffers.CreateStructuredBuffer(MaxBillboards, stride, true, null, "MyBillboardRenderer"); m_atlas = new MyTextureAtlas("Textures\\Particles\\", "Textures\\Particles\\ParticlesAtlas.tai"); }
internal static unsafe void Init() { //MyRender11.RegisterSettingsChangedListener(new OnSettingsChangedDelegate(RecreateShadersForSettings)); DirectionalEnvironmentLight_Pixel = MyShaders.CreatePs("light.hlsl", "directional_environment"); DirectionalEnvironmentLight_Sample = MyShaders.CreatePs("light.hlsl", "directional_environment", MyShaderHelpers.FormatMacros(MyRender11.ShaderSampleFrequencyDefine())); PointlightsTiled_Pixel = MyShaders.CreatePs("light.hlsl", "pointlights_tiled"); PointlightsTiled_Sample = MyShaders.CreatePs("light.hlsl", "pointlights_tiled", MyShaderHelpers.FormatMacros(MyRender11.ShaderSampleFrequencyDefine())); m_preparePointLights = MyShaders.CreateCs("prepare_lights.hlsl", "prepare_lights", MyShaderHelpers.FormatMacros("NUMTHREADS " + TILE_SIZE)); SpotlightProxyVs = MyShaders.CreateVs("light.hlsl", "spotlightVs"); SpotlightPs = MyShaders.CreatePs("light.hlsl", "spotlightFromProxy"); SpotlightProxyIL = MyShaders.CreateIL(SpotlightProxyVs.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION_PACKED)); var stride = sizeof(MyPointlightConstants); m_pointlightCullHwBuffer = MyHwBuffers.CreateStructuredBuffer(MyRender11Constants.MAX_POINT_LIGHTS, stride, true); m_pointlightsConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(MyPointlightInfo) * MyRender11Constants.MAX_POINT_LIGHTS); m_spotlightsConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(MySpotlightConstants) * MyRender11Constants.MAX_SPOTLIGHTS); m_sunlightConstants = MyHwBuffers.CreateConstantsBuffer(sizeof(MySunlightConstantsLayout)); }
static unsafe void InitIB() { ushort[] indices = new ushort[] { // 0 1 2 3 0, 1, 2, 0, 2, 3, // 1 5 6 2 1, 5, 6, 1, 6, 2, // 5 4 7 6 5, 4, 7, 5, 7, 6, // 4 0 3 7 4, 0, 3, 4, 3, 7, // 3 2 6 7 3, 2, 6, 3, 6, 7, // 1 0 4 5 1, 0, 4, 1, 4, 5 }; ushort[] indicesData = new ushort[DECAL_BATCH_SIZE * indices.Length]; var instanceLen = indices.Length; for (int i = 0; i < DECAL_BATCH_SIZE; i++) { for (int j = 0; j < instanceLen; j++) { indicesData[i * instanceLen + j] = (ushort)(indices[j] + 8 * i); } } if (m_IB == IndexBufferId.NULL) { fixed(ushort *I = indicesData) { m_IB = MyHwBuffers.CreateIndexBuffer(indicesData.Length, Format.R16_UInt, BindFlags.IndexBuffer, ResourceUsage.Immutable, new IntPtr(I), "MyScreenDecals"); } } }
private static void DoneDevice() { MyHwBuffers.Destroy(ref m_ib); MyHwBuffers.Destroy(ref m_activeListConstantBuffer); if (m_indirectDrawArgsBuffer != null) { m_indirectDrawArgsBuffer.Release(); m_indirectDrawArgsBuffer = null; } if (m_debugCounterBuffers[0] != null) { m_debugCounterBuffers[0].Release(); m_debugCounterBuffers[0] = null; } if (m_debugCounterBuffers[1] != null) { m_debugCounterBuffers[1].Release(); m_debugCounterBuffers[1] = null; } if (m_aliveIndexBuffer != null) { m_aliveIndexBuffer.Release(); m_aliveIndexBuffer = null; } if (m_deadListBuffer != null) { m_deadListBuffer.Release(); m_deadListBuffer = null; } if (m_skippedParticleCountBuffer != null) { m_skippedParticleCountBuffer.Release(); m_skippedParticleCountBuffer = null; } if (m_particleBuffer != null) { m_particleBuffer.Release(); m_particleBuffer = null; } MyHwBuffers.Destroy(ref m_emitterConstantBuffer); MyHwBuffers.Destroy(ref m_emitterStructuredBuffer); }
internal unsafe void MoveToGPU() { if (m_dirty) { Release(); fixed(void *ptr = m_vertexStream0) { m_VB_positions = MyHwBuffers.CreateStructuredBuffer(m_vertices, Stride0, false, new IntPtr(ptr), "MyMergeInstancing positions"); } fixed(void *ptr = m_vertexStream1) { m_VB_rest = MyHwBuffers.CreateStructuredBuffer(m_vertices, Stride1, false, new IntPtr(ptr), "MyMergeInstancing rest"); } fixed(void *ptr = m_indexStream) { m_IB = MyHwBuffers.CreateStructuredBuffer(m_indices, IndexStride, false, new IntPtr(ptr), "MyMergeInstancing"); } m_dirty = false; } }
internal unsafe static void Init() { ResetCascades(); m_csmConstants = MyHwBuffers.CreateConstantsBuffer((sizeof(Matrix) + sizeof(Vector2)) * 8 + 2 * sizeof(Vector4)); InitIB(); m_cornersCS = new Vector3D[8] { new Vector3D(-1, -1, 0), new Vector3D(-1, 1, 0), new Vector3D(1, 1, 0), new Vector3D(1, -1, 0), new Vector3D(-1, -1, 1), new Vector3D(-1, 1, 1), new Vector3D(1, 1, 1), new Vector3D(1, -1, 1) }; m_markVS = MyShaders.CreateVs("shape.hlsl", "vs"); m_markPS = MyShaders.CreatePs("shape.hlsl", "ps_dummy"); m_inputLayout = MyShaders.CreateIL(m_markVS.BytecodeId, MyVertexLayouts.GetLayout(MyVertexInputComponentType.POSITION3)); }
internal unsafe void MoveToGPU() { var context = MyImmediateRC.RC.Context; if (m_tablesDirty) { var array = m_instancingTable.Data; fixed(void *ptr = array) { var intPtr = new IntPtr(ptr); if (m_indirectionBuffer != StructuredBufferId.NULL && m_indirectionBuffer.Capacity < array.Length) { MyHwBuffers.Destroy(m_indirectionBuffer); m_indirectionBuffer = StructuredBufferId.NULL; m_SRVs[0] = null; } if (m_indirectionBuffer == StructuredBufferId.NULL) { m_indirectionBuffer = MyHwBuffers.CreateStructuredBuffer(array.Length, sizeof(MyInstancingTableEntry), false, intPtr); m_SRVs[0] = m_indirectionBuffer.Srv; } else { context.UpdateSubresource(new DataBox(intPtr, array.Length * sizeof(MyInstancingTableEntry), 0), m_indirectionBuffer.Buffer); } } m_tablesDirty = false; } if (m_instancesDataDirty) { var array = m_perInstance.Data; fixed(void *ptr = array) { var intPtr = new IntPtr(ptr); if (m_instanceBuffer != StructuredBufferId.NULL && m_instanceBuffer.Capacity < array.Length) { MyHwBuffers.Destroy(m_instanceBuffer); m_instanceBuffer = StructuredBufferId.NULL; m_SRVs[1] = null; } if (m_instanceBuffer == StructuredBufferId.NULL) { m_instanceBuffer = MyHwBuffers.CreateStructuredBuffer(array.Length, sizeof(MyPerInstanceData), true, intPtr); m_SRVs[1] = m_instanceBuffer.Srv; } else { var mapping = MyMapping.MapDiscard(context, m_instanceBuffer.Buffer); mapping.stream.Write(intPtr, 0, array.Length * sizeof(MyPerInstanceData)); mapping.Unmap(); } } m_instancesDataDirty = false; } }
MyRenderMeshInfo LoadMesh(string assetName, out MyLODDescriptor[] LodDescriptors) { //Debug.Assert(assetName.EndsWith(".mwm")); #region Temporary for mwm endings if (!assetName.EndsWith(".mwm")) { assetName += ".mwm"; } #endregion var meshVertexInput = MyVertexInputLayout.Empty; LodDescriptors = null; MyRenderMeshInfo result = new MyRenderMeshInfo(); var importer = new MyModelImporter(); var fsPath = Path.IsPathRooted(assetName) ? assetName : Path.Combine(MyFileSystem.ContentPath, assetName); if (!MyFileSystem.FileExists(fsPath)) { System.Diagnostics.Debug.Fail("Model " + assetName + " does not exists!"); return(MyAssetsLoader.GetDebugMesh().LODs[0].m_meshInfo); } string contentPath = null; if (Path.IsPathRooted(assetName) && assetName.ToLower().Contains("models")) { contentPath = assetName.Substring(0, assetName.ToLower().IndexOf("models")); } try { importer.ImportData(fsPath, new string[] { MyImporterConstants.TAG_VERTICES, MyImporterConstants.TAG_BLENDINDICES, MyImporterConstants.TAG_BLENDWEIGHTS, MyImporterConstants.TAG_NORMALS, MyImporterConstants.TAG_TEXCOORDS0, MyImporterConstants.TAG_TANGENTS, MyImporterConstants.TAG_BINORMALS, MyImporterConstants.TAG_BONES, MyImporterConstants.TAG_MESH_PARTS, MyImporterConstants.TAG_BOUNDING_BOX, MyImporterConstants.TAG_BOUNDING_SPHERE, MyImporterConstants.TAG_LODS, }); Dictionary <string, object> tagData = importer.GetTagData(); // extract data var positions = (HalfVector4[])tagData[MyImporterConstants.TAG_VERTICES]; System.Diagnostics.Debug.Assert(positions.Length > 0); var verticesNum = positions.Length; var boneIndices = (Vector4I[])tagData[MyImporterConstants.TAG_BLENDINDICES]; var boneWeights = (Vector4[])tagData[MyImporterConstants.TAG_BLENDWEIGHTS]; var normals = (Byte4[])tagData[MyImporterConstants.TAG_NORMALS]; var texcoords = (HalfVector2[])tagData[MyImporterConstants.TAG_TEXCOORDS0]; var tangents = (Byte4[])tagData[MyImporterConstants.TAG_TANGENTS]; var bintangents = (Byte4[])tagData[MyImporterConstants.TAG_BINORMALS]; var tangentBitanSgn = new Byte4[verticesNum]; for (int i = 0; i < verticesNum; i++) { var N = VF_Packer.UnpackNormal(normals[i].PackedValue); var T = VF_Packer.UnpackNormal(tangents[i].PackedValue); var B = VF_Packer.UnpackNormal(bintangents[i].PackedValue); var tanW = new Vector4(T.X, T.Y, T.Z, 0); tanW.W = T.Cross(N).Dot(B) < 0 ? -1 : 1; tangentBitanSgn[i] = VF_Packer.PackTangentSignB4(ref tanW); } bool hasBonesInfo = false; if (boneIndices.Length > 0 && boneWeights.Length > 0) { hasBonesInfo = true; } var bones = (MyModelBone[])tagData[MyImporterConstants.TAG_BONES]; // var vertexBuffers = new List <VertexBufferId>(); IndexBufferId indexBuffer = IndexBufferId.NULL; var submeshes = new Dictionary <string, List <MyDrawSubmesh> >(); var submeshes2 = new Dictionary <string, List <MySubmeshInfo> >(); var submeshesMeta = new List <MySubmeshInfo>(); int indicesNum = 0; bool missingMaterial = false; if (tagData.ContainsKey(MyImporterConstants.TAG_MESH_PARTS)) { var indices = new List <uint>(positions.Length); uint maxIndex = 0; var meshParts = tagData[MyImporterConstants.TAG_MESH_PARTS] as List <MyMeshPartInfo>; foreach (MyMeshPartInfo meshPart in meshParts) { # region Bones indirection int[] bonesRemapping = null; if (boneIndices.Length > 0 && bones.Length > MyRender11Constants.SHADER_MAX_BONES) { Dictionary <int, int> vertexChanged = new Dictionary <int, int>(); Dictionary <int, int> bonesUsed = new Dictionary <int, int>(); int trianglesNum = meshPart.m_indices.Count / 3; for (int i = 0; i < trianglesNum; i++) { for (int j = 0; j < 3; j++) { int index = meshPart.m_indices[i * 3 + j]; if (boneWeights[index].X > 0) { bonesUsed[boneIndices[index].X] = 1; } if (boneWeights[index].Y > 0) { bonesUsed[boneIndices[index].Y] = 1; } if (boneWeights[index].Z > 0) { bonesUsed[boneIndices[index].Z] = 1; } if (boneWeights[index].W > 0) { bonesUsed[boneIndices[index].W] = 1; } } } if (bonesUsed.Count > MyRender11Constants.SHADER_MAX_BONES) { Debug.Assert(bonesUsed.Count <= MyRender11Constants.SHADER_MAX_BONES, "Model \"" + assetName + "\"'s part uses more than 60 bones, please split model on more parts"); } var partBones = new List <int>(bonesUsed.Keys); partBones.Sort(); if (partBones.Count > 0 && partBones[partBones.Count - 1] >= MyRender11Constants.SHADER_MAX_BONES) { for (int i = 0; i < partBones.Count; i++) { bonesUsed[partBones[i]] = i; } Dictionary <int, int> vertexTouched = new Dictionary <int, int>(); for (int i = 0; i < trianglesNum; i++) { for (int j = 0; j < 3; j++) { int index = meshPart.m_indices[i * 3 + j]; if (!vertexTouched.ContainsKey(index)) { if (boneWeights[index].X > 0) { boneIndices[index].X = bonesUsed[boneIndices[index].X]; } if (boneWeights[index].Y > 0) { boneIndices[index].Y = bonesUsed[boneIndices[index].Y]; } if (boneWeights[index].Z > 0) { boneIndices[index].Z = bonesUsed[boneIndices[index].Z]; } if (boneWeights[index].W > 0) { boneIndices[index].W = bonesUsed[boneIndices[index].W]; } vertexTouched[index] = 1; int changes = 0; vertexChanged.TryGetValue(index, out changes); vertexChanged[index] = changes + 1; } } } bonesRemapping = partBones.ToArray(); } if (vertexChanged.Values.Count > 0) { Debug.Assert(vertexChanged.Values.Max() < 2, "Vertex shared between model parts, will likely result in wrong skinning"); } } #endregion int startIndex = indices.Count; int indexCount = meshPart.m_indices.Count; uint minIndex = (uint)meshPart.m_indices[0]; foreach (var i in meshPart.m_indices) { indices.Add((uint)i); minIndex = Math.Min(minIndex, (uint)i); } uint baseVertex = minIndex; for (int i = startIndex; i < startIndex + indexCount; i++) { indices[i] -= minIndex; maxIndex = Math.Max(maxIndex, indices[i]); } #region Material var materialDesc = meshPart.m_MaterialDesc; var matId = MyMeshMaterials1.GetMaterialId(materialDesc, contentPath); var partKey = MyMeshMaterials1.Table[matId.Index].Technique; var materialName = MyMeshMaterials1.Table[matId.Index].Name; var list = submeshes.SetDefault(partKey, new List <MyDrawSubmesh>()); list.Add(new MyDrawSubmesh(indexCount, startIndex, (int)baseVertex, MyMeshMaterials1.GetProxyId(matId), bonesRemapping)); submeshesMeta.Add(new MySubmeshInfo { IndexCount = indexCount, StartIndex = startIndex, BaseVertex = (int)baseVertex, BonesMapping = bonesRemapping, Material = materialName.ToString(), Technique = partKey }); var list2 = submeshes2.SetDefault(partKey, new List <MySubmeshInfo>()); list2.Add(submeshesMeta[submeshesMeta.Count - 1]); #endregion } indicesNum = indices.Count; #region Fill gpu buffes unsafe { if (maxIndex <= ushort.MaxValue) { // create 16 bit indices var indices16 = new ushort[indices.Count]; for (int i = 0; i < indices.Count; i++) { indices16[i] = (ushort)indices[i]; } result.Indices = indices16; fixed(ushort *I = indices16) { indexBuffer = MyHwBuffers.CreateIndexBuffer(indices16.Length, Format.R16_UInt, BindFlags.IndexBuffer, ResourceUsage.Immutable, new IntPtr(I), assetName + " index buffer"); } } else { var indicesArray = indices.ToArray(); fixed(uint *I = indicesArray) { indexBuffer = MyHwBuffers.CreateIndexBuffer(indices.Count, Format.R32_UInt, BindFlags.IndexBuffer, ResourceUsage.Immutable, new IntPtr(I), assetName + " index buffer"); } } } unsafe { if (!hasBonesInfo) { var vertices = new MyVertexFormatPositionH4[verticesNum]; for (int i = 0; i < verticesNum; i++) { vertices[i] = new MyVertexFormatPositionH4(positions[i]); } meshVertexInput = meshVertexInput.Append(MyVertexInputComponentType.POSITION_PACKED); result.VertexPositions = vertices; fixed(MyVertexFormatPositionH4 *V = vertices) { vertexBuffers.Add(MyHwBuffers.CreateVertexBuffer(verticesNum, sizeof(MyVertexFormatPositionH4), BindFlags.VertexBuffer, ResourceUsage.Immutable, new IntPtr(V), assetName + " vertex buffer " + vertexBuffers.Count)); } } else { var vertices = new MyVertexFormatPositionSkinning[verticesNum]; for (int i = 0; i < verticesNum; i++) { vertices[i] = new MyVertexFormatPositionSkinning( positions[i], new Byte4(boneIndices[i].X, boneIndices[i].Y, boneIndices[i].Z, boneIndices[i].W), boneWeights[i]); } meshVertexInput = meshVertexInput.Append(MyVertexInputComponentType.POSITION_PACKED) .Append(MyVertexInputComponentType.BLEND_WEIGHTS).Append(MyVertexInputComponentType.BLEND_INDICES); fixed(MyVertexFormatPositionSkinning *V = vertices) { vertexBuffers.Add(MyHwBuffers.CreateVertexBuffer(verticesNum, sizeof(MyVertexFormatPositionSkinning), BindFlags.VertexBuffer, ResourceUsage.Immutable, new IntPtr(V), assetName + " vertex buffer " + vertexBuffers.Count)); } } // add second stream { var vertices = new MyVertexFormatTexcoordNormalTangent[verticesNum]; for (int i = 0; i < verticesNum; i++) { vertices[i] = new MyVertexFormatTexcoordNormalTangent(texcoords[i], normals[i], tangentBitanSgn[i]); } fixed(MyVertexFormatTexcoordNormalTangent *V = vertices) { vertexBuffers.Add(MyHwBuffers.CreateVertexBuffer(verticesNum, sizeof(MyVertexFormatTexcoordNormalTangent), BindFlags.VertexBuffer, ResourceUsage.Immutable, new IntPtr(V), assetName + " vertex buffer " + vertexBuffers.Count)); } result.VertexExtendedData = vertices; meshVertexInput = meshVertexInput .Append(MyVertexInputComponentType.NORMAL, 1) .Append(MyVertexInputComponentType.TANGENT_SIGN_OF_BITANGENT, 1) .Append(MyVertexInputComponentType.TEXCOORD0_H, 1); } } #endregion } #region Extract lods if (tagData.ContainsKey(MyImporterConstants.TAG_LODS)) { var tagLODs = tagData[MyImporterConstants.TAG_LODS]; if (((MyLODDescriptor[])tagLODs).Length > 0) { } LodDescriptors = (MyLODDescriptor[])((MyLODDescriptor[])tagLODs).Clone(); } #endregion if (missingMaterial) { Debug.WriteLine(String.Format("Mesh {0} has missing material", assetName)); } //indexBuffer.SetDebugName(assetName + " index buffer"); int c = 0; //vertexBuffers.ForEach(x => x.SetDebugName(assetName + " vertex buffer " + c++)); // result.BoundingBox = (BoundingBox)tagData[MyImporterConstants.TAG_BOUNDING_BOX]; result.BoundingSphere = (BoundingSphere)tagData[MyImporterConstants.TAG_BOUNDING_SPHERE]; result.VerticesNum = verticesNum; result.IndicesNum = indicesNum; result.VertexLayout = meshVertexInput; result.IB = indexBuffer; result.VB = vertexBuffers.ToArray(); result.IsAnimated = hasBonesInfo; result.Parts = submeshes.ToDictionary(x => x.Key, x => x.Value.ToArray()); result.PartsMetadata = submeshes2.ToDictionary(x => x.Key, x => x.Value.ToArray()); result.m_submeshes = submeshesMeta; IsAnimated |= result.IsAnimated; importer.Clear(); return(result); }
internal static void Init() { m_objectConstants = MyHwBuffers.CreateConstantsBuffer(64); }
public unsafe MyDebugMesh(MyRenderMessageDebugDrawMesh message) { vbuffer = MyHwBuffers.CreateVertexBuffer(message.VertexCount, sizeof(MyVertexFormatPositionColor), BindFlags.VertexBuffer, ResourceUsage.Dynamic, null, "MyDebugMesh"); Update(message); }
internal void Close() { MyHwBuffers.Destroy(vbuffer); }
internal static unsafe void InitSubsystems() { MyManagers.OnDeviceInit(); //MyRwTextures.Init(); MyHwBuffers.Init(); ResetShadows(MyShadowCascades.Settings.NewData.CascadesCount, RenderSettings.ShadowQuality.ShadowCascadeResolution()); MyRender11.Init(); MyCommon.Init(); MyVertexLayouts.Init(); MyShaders.Init(); MyMeshes.Init(); MyMeshTableSrv.Init(); MyLightRendering.Init(); MyLinesRenderer.Init(); MySpritesRenderer.Init(); MyPrimitivesRenderer.Init(); MyOutline.Init(); MyBlur.Init(); MyTransparentRendering.Init(); MyFoliageComponents.Init(); MyBillboardRenderer.Init(); // hardcoded limits MyDebugRenderer.Init(); MyScreenDecals.Init(); MyEnvProbeProcessing.Init(); MyAtmosphereRenderer.Init(); MyCloudRenderer.Init(); MyAAEdgeMarking.Init(); MyScreenPass.Init(); MyCopyToRT.Init(); MyBlendTargets.Init(); MyFXAA.Init(); MyDepthResolve.Init(); MyBloom.Init(); MyLuminanceAverage.Init(); MyToneMapping.Init(); MySSAO.Init(); MyHdrDebugTools.Init(); MySceneMaterials.Init(); MyMaterials1.Init(); MyVoxelMaterials1.Init(); MyMeshMaterials1.Init(); MyHBAO.Init(); try { if (m_settings.UseStereoRendering) { var openVR = new MyOpenVR(); MyStereoStencilMask.InitUsingOpenVR(); } } catch (System.Exception e) { if (!VRage.MyCompilationSymbols.DX11ForceStereo) { throw; } MyStereoStencilMask.InitUsingUndefinedMask(); } }
internal unsafe static void Init() { m_buffer = MyHwBuffers.CreateStructuredBuffer(4096, sizeof(MyPerMaterialData), true, null, "MySceneMaterials"); }
private unsafe void InitVertexBuffer(int numberOfCascades) { DestroyVertexBuffer(); m_cascadesBoundingsVertices = MyHwBuffers.CreateVertexBuffer(8 * numberOfCascades, sizeof(Vector3), BindFlags.VertexBuffer, ResourceUsage.Dynamic, null, "MyShadowCascadesPostProcess"); }