|
| 1 | +//! OpenGL-specific validation rules. |
| 2 | +//! |
| 3 | +//! This module validates OpenGL-specific SPIR-V requirements including: |
| 4 | +//! |
| 5 | +//! - ARB_gl_spirv binding requirements for uniform/storage block variables |
| 6 | +
|
| 7 | +use rspirv::spirv::{Decoration, Op, StorageClass}; |
| 8 | + |
| 9 | +use crate::validation::context::{ValidationContext, ValidationRule}; |
| 10 | +use crate::validation::error::ValidationError; |
| 11 | +use crate::validation::helpers::is_opengl_env; |
| 12 | +use crate::validation::rules::vulkan::{ |
| 13 | + get_array_element_struct, get_variable_pointee_type, has_decoration, |
| 14 | +}; |
| 15 | +use crate::validation::types::ResultId; |
| 16 | +use crate::validation::ValidationResult; |
| 17 | + |
| 18 | +// ============================================================================ |
| 19 | +// OpenGL Buffer Binding Rule |
| 20 | +// ============================================================================ |
| 21 | + |
| 22 | +/// Validates that uniform and shader storage block variables have Binding decorations in OpenGL. |
| 23 | +/// |
| 24 | +/// From ARB_gl_spirv extension: |
| 25 | +/// Uniform and shader storage block variables must also be decorated with a *Binding*. |
| 26 | +pub struct OpenGlBufferBindingRule; |
| 27 | + |
| 28 | +impl ValidationRule for OpenGlBufferBindingRule { |
| 29 | + fn name(&self) -> &'static str { |
| 30 | + "opengl-buffer-binding" |
| 31 | + } |
| 32 | + |
| 33 | + fn should_skip(&self, ctx: &ValidationContext<'_>) -> bool { |
| 34 | + !is_opengl_env(ctx.env) |
| 35 | + } |
| 36 | + |
| 37 | + fn validate(&self, ctx: &ValidationContext<'_>) -> ValidationResult { |
| 38 | + // Build a set of variable IDs referenced by any entry point |
| 39 | + let mut entry_point_vars: std::collections::HashSet<u32> = |
| 40 | + std::collections::HashSet::new(); |
| 41 | + for ep in &ctx.module.entry_points { |
| 42 | + let mut operands = ep.operands.iter(); |
| 43 | + // Skip ExecutionModel |
| 44 | + let _ = operands.next(); |
| 45 | + // Skip entry point function ID |
| 46 | + let _ = operands.next(); |
| 47 | + // Skip name (LiteralString) |
| 48 | + let _ = operands.next(); |
| 49 | + // Remaining operands are interface variable IDs |
| 50 | + for operand in operands { |
| 51 | + if let rspirv::dr::Operand::IdRef(id) = operand { |
| 52 | + entry_point_vars.insert(*id); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + for inst in &ctx.module.types_global_values { |
| 58 | + if inst.class.opcode != Op::Variable && inst.class.opcode != Op::UntypedVariableKHR { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + let storage_class = match inst.operands.first() { |
| 63 | + Some(rspirv::dr::Operand::StorageClass(sc)) => *sc, |
| 64 | + _ => continue, |
| 65 | + }; |
| 66 | + |
| 67 | + let var_id = match inst.result_id { |
| 68 | + Some(id) => id, |
| 69 | + None => continue, |
| 70 | + }; |
| 71 | + |
| 72 | + let uniform = storage_class == StorageClass::Uniform |
| 73 | + || storage_class == StorageClass::UniformConstant; |
| 74 | + let storage_buffer = storage_class == StorageClass::StorageBuffer; |
| 75 | + |
| 76 | + if !uniform && !storage_buffer { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // Get the pointee struct type |
| 81 | + let pointee_type_id = get_variable_pointee_type(inst, ctx); |
| 82 | + let Some(struct_id) = pointee_type_id else { |
| 83 | + continue; |
| 84 | + }; |
| 85 | + |
| 86 | + // Check if pointee is a struct (possibly through arrays) |
| 87 | + let pointee_opcode = ResultId::try_from(struct_id) |
| 88 | + .ok() |
| 89 | + .and_then(|rid| ctx.opcodes.get(&rid)) |
| 90 | + .copied(); |
| 91 | + |
| 92 | + let final_struct_id = if matches!( |
| 93 | + pointee_opcode, |
| 94 | + Some(Op::TypeArray) | Some(Op::TypeRuntimeArray) |
| 95 | + ) { |
| 96 | + get_array_element_struct(struct_id, ctx) |
| 97 | + } else if pointee_opcode == Some(Op::TypeStruct) { |
| 98 | + Some(struct_id) |
| 99 | + } else { |
| 100 | + None |
| 101 | + }; |
| 102 | + |
| 103 | + let Some(struct_id) = final_struct_id else { |
| 104 | + continue; |
| 105 | + }; |
| 106 | + |
| 107 | + let has_block = has_decoration(ctx, struct_id, Decoration::Block); |
| 108 | + let has_buffer_block = has_decoration(ctx, struct_id, Decoration::BufferBlock); |
| 109 | + |
| 110 | + // OpenGL requires Binding on: |
| 111 | + // - Uniform variables with Block or BufferBlock decoration |
| 112 | + // - StorageBuffer variables with Block decoration |
| 113 | + if (uniform && (has_block || has_buffer_block)) |
| 114 | + || (storage_buffer && has_block) |
| 115 | + { |
| 116 | + // Only check variables referenced by entry points |
| 117 | + if !entry_point_vars.is_empty() |
| 118 | + && entry_point_vars.contains(&var_id) |
| 119 | + && !has_decoration(ctx, var_id, Decoration::Binding) |
| 120 | + { |
| 121 | + return Err(ValidationError::OpenGlBufferMissingBindingDecoration { |
| 122 | + storage_class, |
| 123 | + variable_id: var_id, |
| 124 | + } |
| 125 | + .into()); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Ok(()) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// ============================================================================ |
| 135 | +// All OpenGL rules |
| 136 | +// ============================================================================ |
| 137 | + |
| 138 | +/// Returns all OpenGL-specific validation rules. |
| 139 | +pub fn all_opengl_rules() -> Vec<&'static dyn ValidationRule> { |
| 140 | + vec![&OpenGlBufferBindingRule] |
| 141 | +} |
0 commit comments