public static void Declare(CodeGenContext context, StructuredProgramInfo info) { context.AppendLine("#version 430 core"); context.AppendLine("#extension GL_ARB_gpu_shader_int64 : enable"); context.AppendLine("#extension GL_ARB_shader_ballot : enable"); context.AppendLine("#extension GL_ARB_shader_group_vote : enable"); if (context.Config.Stage == ShaderStage.Compute) { context.AppendLine("#extension GL_ARB_compute_shader : enable"); } context.AppendLine("#pragma optionNV(fastmath off)"); context.AppendLine(); context.AppendLine($"const int {DefaultNames.UndefinedName} = 0;"); context.AppendLine(); if (context.Config.Stage == ShaderStage.Geometry) { string inPrimitive = context.Config.GpuAccessor.QueryPrimitiveTopology().ToGlslString(); context.AppendLine($"layout ({inPrimitive}) in;"); string outPrimitive = context.Config.OutputTopology.ToGlslString(); int maxOutputVertices = context.Config.MaxOutputVertices; context.AppendLine($"layout ({outPrimitive}, max_vertices = {maxOutputVertices}) out;"); context.AppendLine(); } if (context.Config.Stage == ShaderStage.Compute) { int localMemorySize = BitUtils.DivRoundUp(context.Config.GpuAccessor.QueryComputeLocalMemorySize(), 4); if (localMemorySize != 0) { string localMemorySizeStr = NumberFormatter.FormatInt(localMemorySize); context.AppendLine($"uint {DefaultNames.LocalMemoryName}[{localMemorySizeStr}];"); context.AppendLine(); } int sharedMemorySize = BitUtils.DivRoundUp(context.Config.GpuAccessor.QueryComputeSharedMemorySize(), 4); if (sharedMemorySize != 0) { string sharedMemorySizeStr = NumberFormatter.FormatInt(sharedMemorySize); context.AppendLine($"shared uint {DefaultNames.SharedMemoryName}[{sharedMemorySizeStr}];"); context.AppendLine(); } } else if (context.Config.LocalMemorySize != 0) { int localMemorySize = BitUtils.DivRoundUp(context.Config.LocalMemorySize, 4); string localMemorySizeStr = NumberFormatter.FormatInt(localMemorySize); context.AppendLine($"uint {DefaultNames.LocalMemoryName}[{localMemorySizeStr}];"); context.AppendLine(); } if (info.CBuffers.Count != 0) { DeclareUniforms(context, info); context.AppendLine(); } if (info.SBuffers.Count != 0) { DeclareStorages(context, info); context.AppendLine(); } if (info.Samplers.Count != 0) { DeclareSamplers(context, info); context.AppendLine(); } if (info.Images.Count != 0) { DeclareImages(context, info); context.AppendLine(); } if (context.Config.Stage != ShaderStage.Compute) { if (info.IAttributes.Count != 0) { DeclareInputAttributes(context, info); context.AppendLine(); } if (info.OAttributes.Count != 0 || context.Config.Stage != ShaderStage.Fragment) { DeclareOutputAttributes(context, info); context.AppendLine(); } } else { string localSizeX = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeX()); string localSizeY = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeY()); string localSizeZ = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeZ()); context.AppendLine( "layout (" + $"local_size_x = {localSizeX}, " + $"local_size_y = {localSizeY}, " + $"local_size_z = {localSizeZ}) in;"); context.AppendLine(); } if ((info.HelperFunctionsMask & HelperFunctionsMask.MultiplyHighS32) != 0) { AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/MultiplyHighS32.glsl"); } if ((info.HelperFunctionsMask & HelperFunctionsMask.MultiplyHighU32) != 0) { AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/MultiplyHighU32.glsl"); } if ((info.HelperFunctionsMask & HelperFunctionsMask.Shuffle) != 0) { AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/Shuffle.glsl"); } if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleDown) != 0) { AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl"); } if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleUp) != 0) { AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl"); } if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleXor) != 0) { AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl"); } if ((info.HelperFunctionsMask & HelperFunctionsMask.SwizzleAdd) != 0) { AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/SwizzleAdd.glsl"); } }
private static void DeclareSamplers(CodeGenContext context, StructuredProgramInfo info) { Dictionary <string, AstTextureOperation> samplers = new Dictionary <string, AstTextureOperation>(); // Texture instructions other than TextureSample (like TextureSize) // may have incomplete sampler type information. In those cases, // we prefer instead the more accurate information from the // TextureSample instruction, if both are available. foreach (AstTextureOperation texOp in info.Samplers.OrderBy(x => x.Handle * 2 + (x.Inst == Instruction.TextureSample ? 0 : 1))) { string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize); string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); if (!samplers.TryAdd(samplerName, texOp)) { continue; } string samplerTypeName = GetSamplerTypeName(texOp.Type); context.AppendLine("uniform " + samplerTypeName + " " + samplerName + ";"); } foreach (KeyValuePair <string, AstTextureOperation> kv in samplers) { string samplerName = kv.Key; AstTextureOperation texOp = kv.Value; TextureDescriptor desc; if ((texOp.Flags & TextureFlags.Bindless) != 0) { AstOperand operand = texOp.GetSource(0) as AstOperand; desc = new TextureDescriptor(samplerName, texOp.Type, operand.CbufSlot, operand.CbufOffset); context.TextureDescriptors.Add(desc); } else if ((texOp.Type & SamplerType.Indexed) != 0) { for (int index = 0; index < texOp.ArraySize; index++) { string indexExpr = NumberFormatter.FormatInt(index); string indexedSamplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr); desc = new TextureDescriptor(indexedSamplerName, texOp.Type, texOp.Handle + index * 2); context.TextureDescriptors.Add(desc); } } else { desc = new TextureDescriptor(samplerName, texOp.Type, texOp.Handle); context.TextureDescriptors.Add(desc); } } }