{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://vgpu.sh/schemas/native/tint-compiler/v1/request.schema.json",
    "title": "vGPU Native Tint Compiler Request v1",
    "description": "One resolved WGSL entry-point translation request. The one-shot process transport carries one UTF-8 JSON value terminated by EOF. The Node caller validates the schema and semantics, and the C++ worker independently decodes the complete typed request except for the documented caller-side NFC precondition.",
    "type": "object",
    "additionalProperties": false,
    "required": [
        "schemaVersion",
        "contractId",
        "source",
        "originMap",
        "entryPoint",
        "semanticInterface",
        "overrides",
        "languageFeatures",
        "metal"
    ],
    "properties": {
        "schemaVersion": {
            "const": 1
        },
        "contractId": {
            "const": "vgpu-native-tint-compiler/v1"
        },
        "source": {
            "$ref": "#/$defs/source"
        },
        "originMap": {
            "$ref": "https://vgpu.sh/schemas/native/tint-compiler/v1/origin-map.schema.json"
        },
        "entryPoint": {
            "$ref": "#/$defs/entryPoint"
        },
        "semanticInterface": {
            "description": "The exact, flattened, backend-neutral interface expected for the selected entry point. Artifact type IDs and diagnostic source names are resolved away before this boundary.",
            "$ref": "#/$defs/semanticInterface"
        },
        "overrides": {
            "description": "Canonical declaration-name order. This post-materialization request contains exactly every override active in the selected entry point, including typed values selected from shader defaults upstream.",
            "type": "array",
            "uniqueItems": true,
            "items": {
                "$ref": "#/$defs/override"
            }
        },
        "languageFeatures": {
            "type": "array",
            "uniqueItems": true,
            "items": {
                "$ref": "#/$defs/languageFeature"
            }
        },
        "metal": {
            "description": "The v1 writer profile always supplies one candidate immediate-data reservation at buffer(30), the fixed immediate-data layout model, and its stage-specific storage-size region offset. Their presence configures Tint but does not imply that either is effective in the response.",
            "$ref": "#/$defs/metal"
        }
    },
    "allOf": [
        {
            "if": {
                "properties": {
                    "entryPoint": {
                        "type": "object",
                        "required": ["stage"],
                        "properties": {
                            "stage": {
                                "const": "fragment"
                            }
                        }
                    }
                },
                "required": ["entryPoint"]
            },
            "then": {
                "properties": {
                    "metal": {
                        "type": "object",
                        "required": ["storageBufferSizes"],
                        "properties": {
                            "storageBufferSizes": {
                                "type": "object",
                                "required": ["immediateDataByteOffset"],
                                "properties": {
                                    "immediateDataByteOffset": {
                                        "const": 12
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "else": {
                "properties": {
                    "metal": {
                        "type": "object",
                        "required": ["storageBufferSizes"],
                        "properties": {
                            "storageBufferSizes": {
                                "type": "object",
                                "required": ["immediateDataByteOffset"],
                                "properties": {
                                    "immediateDataByteOffset": {
                                        "const": 4
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    ],
    "$defs": {
        "nonEmptyString": {
            "type": "string",
            "minLength": 1,
            "maxLength": 1024
        },
        "identifier": {
            "type": "string",
            "pattern": "^[A-Za-z_][A-Za-z0-9_]*$",
            "maxLength": 256
        },
        "emittedIdentifier": {
            "description": "A vgpu-owned Metal identifier. The prefix keeps caller-controlled names out of Metal's reserved-word domain before Tint runs.",
            "type": "string",
            "pattern": "^vgpu_[A-Za-z0-9_]+$",
            "maxLength": 256
        },
        "sha256": {
            "type": "string",
            "pattern": "^[a-f0-9]{64}$"
        },
        "relativePath": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4096,
            "pattern": "^(?!/)(?![A-Za-z]:)(?![A-Za-z][A-Za-z0-9+.-]*:)(?!.*\\\\)(?!.*[\\u0000-\\u001f\\u007f])(?!.*(?:^|/)\\.\\.?(?:/|$))(?!.*//).+$"
        },
        "uint32": {
            "type": "integer",
            "minimum": 0,
            "maximum": 4294967295
        },
        "stage": {
            "enum": ["vertex", "fragment", "compute"]
        },
        "source": {
            "type": "object",
            "additionalProperties": false,
            "required": ["virtualPath", "sha256", "text"],
            "properties": {
                "virtualPath": {
                    "$ref": "#/$defs/relativePath"
                },
                "sha256": {
                    "$ref": "#/$defs/sha256"
                },
                "text": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 16777216
                }
            }
        },
        "entryPoint": {
            "type": "object",
            "additionalProperties": false,
            "required": ["stage", "wgsl", "metal"],
            "properties": {
                "stage": {
                    "$ref": "#/$defs/stage"
                },
                "wgsl": {
                    "$ref": "#/$defs/identifier"
                },
                "metal": {
                    "$ref": "#/$defs/emittedIdentifier"
                }
            }
        },
        "interfaceType": {
            "type": "object",
            "additionalProperties": false,
            "required": ["scalar", "width"],
            "properties": {
                "scalar": {
                    "enum": ["bool", "f16", "f32", "i32", "u32"]
                },
                "width": {
                    "enum": [1, 2, 3, 4]
                }
            }
        },
        "numericInterfaceType": {
            "type": "object",
            "additionalProperties": false,
            "required": ["scalar", "width"],
            "properties": {
                "scalar": {
                    "enum": ["f16", "f32", "i32", "u32"]
                },
                "width": {
                    "enum": [1, 2, 3, 4]
                }
            }
        },
        "interpolation": {
            "type": "object",
            "additionalProperties": false,
            "required": ["type", "sampling"],
            "properties": {
                "type": {
                    "enum": ["perspective", "linear", "flat"]
                },
                "sampling": {
                    "enum": ["center", "centroid", "sample", "first", "either"]
                }
            },
            "oneOf": [
                {
                    "properties": {
                        "type": {
                            "enum": ["perspective", "linear"]
                        },
                        "sampling": {
                            "enum": ["center", "centroid", "sample"]
                        }
                    }
                },
                {
                    "properties": {
                        "type": {
                            "const": "flat"
                        },
                        "sampling": {
                            "enum": ["first", "either"]
                        }
                    }
                }
            ]
        },
        "interfaceValue": {
            "type": "object",
            "additionalProperties": false,
            "required": ["type", "invariant"],
            "properties": {
                "type": {
                    "$ref": "#/$defs/interfaceType"
                },
                "invariant": {
                    "type": "boolean"
                },
                "location": {
                    "$ref": "#/$defs/uint32"
                },
                "builtin": {
                    "type": "string",
                    "pattern": "^[a-z][a-z0-9_]*$",
                    "maxLength": 256
                },
                "interpolation": {
                    "$ref": "#/$defs/interpolation"
                },
                "blendSource": {
                    "enum": [0, 1]
                }
            },
            "oneOf": [
                {
                    "required": ["location"],
                    "properties": {
                        "location": true,
                        "builtin": false
                    }
                },
                {
                    "required": ["builtin"],
                    "properties": {
                        "location": false,
                        "builtin": true,
                        "interpolation": false,
                        "blendSource": false
                    }
                }
            ]
        },
        "vertexInputValue": {
            "type": "object",
            "allOf": [
                {
                    "$ref": "#/$defs/interfaceValue"
                },
                {
                    "properties": {
                        "invariant": {
                            "const": false
                        },
                        "builtin": {
                            "enum": ["vertex_index", "instance_index"]
                        },
                        "interpolation": false,
                        "blendSource": false
                    }
                },
                {
                    "if": {
                        "required": ["location"],
                        "properties": {
                            "location": true
                        }
                    },
                    "then": {
                        "properties": {
                            "type": {
                                "$ref": "#/$defs/numericInterfaceType"
                            }
                        }
                    }
                }
            ]
        },
        "vertexOutputValue": {
            "type": "object",
            "allOf": [
                {
                    "$ref": "#/$defs/interfaceValue"
                },
                {
                    "properties": {
                        "builtin": {
                            "const": "position"
                        },
                        "blendSource": false
                    }
                },
                {
                    "if": {
                        "required": ["location"],
                        "properties": {
                            "location": true
                        }
                    },
                    "then": {
                        "required": ["interpolation"],
                        "properties": {
                            "type": {
                                "$ref": "#/$defs/numericInterfaceType"
                            },
                            "invariant": {
                                "const": false
                            },
                            "interpolation": true
                        }
                    },
                    "else": {
                        "properties": {
                            "interpolation": false
                        }
                    }
                }
            ]
        },
        "fragmentInputValue": {
            "type": "object",
            "allOf": [
                {
                    "$ref": "#/$defs/interfaceValue"
                },
                {
                    "properties": {
                        "invariant": {
                            "const": false
                        },
                        "builtin": {
                            "enum": [
                                "position",
                                "front_facing",
                                "sample_index",
                                "sample_mask"
                            ]
                        },
                        "blendSource": false
                    }
                },
                {
                    "if": {
                        "required": ["location"],
                        "properties": {
                            "location": true
                        }
                    },
                    "then": {
                        "required": ["interpolation"],
                        "properties": {
                            "type": {
                                "$ref": "#/$defs/numericInterfaceType"
                            },
                            "interpolation": true
                        }
                    },
                    "else": {
                        "properties": {
                            "interpolation": false
                        }
                    }
                }
            ]
        },
        "fragmentOutputValue": {
            "type": "object",
            "allOf": [
                {
                    "$ref": "#/$defs/interfaceValue"
                },
                {
                    "properties": {
                        "invariant": {
                            "const": false
                        },
                        "builtin": {
                            "enum": ["frag_depth", "sample_mask"]
                        },
                        "interpolation": false
                    }
                },
                {
                    "if": {
                        "required": ["location"],
                        "properties": {
                            "location": true
                        }
                    },
                    "then": {
                        "properties": {
                            "type": {
                                "$ref": "#/$defs/numericInterfaceType"
                            }
                        }
                    }
                },
                {
                    "if": {
                        "required": ["blendSource"],
                        "properties": {
                            "blendSource": true
                        }
                    },
                    "then": {
                        "required": ["location"],
                        "properties": {
                            "location": {
                                "const": 0
                            }
                        }
                    }
                }
            ]
        },
        "computeInputValue": {
            "type": "object",
            "allOf": [
                {
                    "$ref": "#/$defs/interfaceValue"
                },
                {
                    "properties": {
                        "invariant": {
                            "const": false
                        },
                        "location": false,
                        "builtin": {
                            "enum": [
                                "local_invocation_id",
                                "local_invocation_index",
                                "global_invocation_id",
                                "workgroup_id",
                                "num_workgroups"
                            ]
                        },
                        "interpolation": false,
                        "blendSource": false
                    }
                }
            ]
        },
        "semanticInterface": {
            "oneOf": [
                {
                    "type": "object",
                    "additionalProperties": false,
                    "required": ["kind", "inputs", "outputs"],
                    "properties": {
                        "kind": {
                            "const": "vertex"
                        },
                        "inputs": {
                            "type": "array",
                            "maxItems": 64,
                            "items": {
                                "$ref": "#/$defs/vertexInputValue"
                            }
                        },
                        "outputs": {
                            "type": "array",
                            "maxItems": 64,
                            "items": {
                                "$ref": "#/$defs/vertexOutputValue"
                            },
                            "contains": {
                                "type": "object",
                                "required": ["builtin"],
                                "properties": {
                                    "builtin": {
                                        "const": "position"
                                    }
                                }
                            },
                            "minContains": 1,
                            "maxContains": 1
                        }
                    }
                },
                {
                    "type": "object",
                    "additionalProperties": false,
                    "required": ["kind", "inputs", "outputs"],
                    "properties": {
                        "kind": {
                            "const": "fragment"
                        },
                        "inputs": {
                            "type": "array",
                            "maxItems": 64,
                            "items": {
                                "$ref": "#/$defs/fragmentInputValue"
                            }
                        },
                        "outputs": {
                            "type": "array",
                            "maxItems": 64,
                            "items": {
                                "$ref": "#/$defs/fragmentOutputValue"
                            }
                        }
                    }
                },
                {
                    "type": "object",
                    "additionalProperties": false,
                    "required": ["kind", "inputs", "outputs"],
                    "properties": {
                        "kind": {
                            "const": "compute"
                        },
                        "inputs": {
                            "type": "array",
                            "maxItems": 5,
                            "items": {
                                "$ref": "#/$defs/computeInputValue"
                            }
                        },
                        "outputs": {
                            "type": "array",
                            "maxItems": 0
                        }
                    }
                }
            ]
        },
        "boolValue": {
            "type": "object",
            "additionalProperties": false,
            "required": ["type", "value"],
            "properties": {
                "type": {
                    "const": "bool"
                },
                "value": {
                    "type": "boolean"
                }
            }
        },
        "i32Value": {
            "type": "object",
            "additionalProperties": false,
            "required": ["type", "value"],
            "properties": {
                "type": {
                    "const": "i32"
                },
                "value": {
                    "type": "integer",
                    "minimum": -2147483648,
                    "maximum": 2147483647
                }
            }
        },
        "u32Value": {
            "type": "object",
            "additionalProperties": false,
            "required": ["type", "value"],
            "properties": {
                "type": {
                    "const": "u32"
                },
                "value": {
                    "$ref": "#/$defs/uint32"
                }
            }
        },
        "f16Value": {
            "type": "object",
            "additionalProperties": false,
            "required": ["type", "bits"],
            "properties": {
                "type": {
                    "const": "f16"
                },
                "bits": {
                    "description": "Finite IEEE 754 binary16 bits in canonical lowercase hexadecimal. NaN and infinity encodings are outside protocol v1.",
                    "type": "string",
                    "pattern": "^(?![7f][c-f][a-f0-9]{2}$)[a-f0-9]{4}$"
                }
            }
        },
        "f32Value": {
            "type": "object",
            "additionalProperties": false,
            "required": ["type", "bits"],
            "properties": {
                "type": {
                    "const": "f32"
                },
                "bits": {
                    "description": "Finite IEEE 754 binary32 bits in canonical lowercase hexadecimal. NaN and infinity encodings are outside protocol v1.",
                    "type": "string",
                    "pattern": "^(?![7f]f[89a-f][a-f0-9]{5}$)[a-f0-9]{8}$"
                }
            }
        },
        "overrideValue": {
            "oneOf": [
                {
                    "$ref": "#/$defs/boolValue"
                },
                {
                    "$ref": "#/$defs/i32Value"
                },
                {
                    "$ref": "#/$defs/u32Value"
                },
                {
                    "$ref": "#/$defs/f16Value"
                },
                {
                    "$ref": "#/$defs/f32Value"
                }
            ]
        },
        "override": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name", "value"],
            "properties": {
                "name": {
                    "$ref": "#/$defs/identifier"
                },
                "value": {
                    "$ref": "#/$defs/overrideValue"
                }
            }
        },
        "languageFeature": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9_]*$",
            "maxLength": 256
        },
        "directSlot": {
            "type": "object",
            "additionalProperties": false,
            "required": ["mode", "resourceClass", "component", "index", "count"],
            "properties": {
                "mode": {
                    "const": "direct"
                },
                "resourceClass": {
                    "enum": ["buffer", "texture", "sampler"]
                },
                "component": {
                    "type": "string",
                    "pattern": "^[a-z][a-z0-9-]*$",
                    "maxLength": 256
                },
                "index": {
                    "$ref": "#/$defs/uint32"
                },
                "count": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 4294967295
                }
            },
            "allOf": [
                {
                    "if": {
                        "properties": { "resourceClass": { "const": "buffer" } },
                        "required": ["resourceClass"]
                    },
                    "then": { "properties": { "component": { "const": "buffer" } } }
                },
                {
                    "if": {
                        "properties": { "resourceClass": { "const": "texture" } },
                        "required": ["resourceClass"]
                    },
                    "then": { "properties": { "component": { "const": "texture" } } }
                },
                {
                    "if": {
                        "properties": { "resourceClass": { "const": "sampler" } },
                        "required": ["resourceClass"]
                    },
                    "then": { "properties": { "component": { "const": "sampler" } } }
                }
            ]
        },
        "binding": {
            "description": "The selected entry's WGSL resource kind is derived and checked through Tint Inspector. The request carries only the direct Metal component intervals: uniform and storage buffers use the buffer class, while sampled and storage textures use the texture class.",
            "type": "object",
            "additionalProperties": false,
            "required": ["group", "binding", "slots"],
            "properties": {
                "group": {
                    "$ref": "#/$defs/uint32"
                },
                "binding": {
                    "$ref": "#/$defs/uint32"
                },
                "slots": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 1,
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/$defs/directSlot"
                    }
                }
            }
        },
        "internalReservation": {
            "description": "The one candidate shared immediate-data binding in compiler protocol v1.",
            "type": "object",
            "additionalProperties": false,
            "required": ["role", "slots"],
            "properties": {
                "role": {
                    "const": "immediate-data"
                },
                "slots": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 1,
                    "items": {
                        "$ref": "#/$defs/immediateDataSlot"
                    }
                }
            }
        },
        "immediateDataSlot": {
            "type": "object",
            "additionalProperties": false,
            "required": ["mode", "resourceClass", "component", "index", "count"],
            "properties": {
                "mode": {
                    "const": "direct"
                },
                "resourceClass": {
                    "const": "buffer"
                },
                "component": {
                    "const": "buffer"
                },
                "index": {
                    "const": 30
                },
                "count": {
                    "const": 1
                }
            }
        },
        "storageBufferSizes": {
            "description": "Candidate configuration supplied before generation. Only the writer result can make a size region effective.",
            "type": "object",
            "additionalProperties": false,
            "required": ["model", "immediateDataByteOffset"],
            "properties": {
                "model": {
                    "const": "vgpu-metal-slot-indexed-storage-buffer-byte-sizes-v1"
                },
                "immediateDataByteOffset": {
                    "enum": [4, 12]
                }
            }
        },
        "metal": {
            "type": "object",
            "additionalProperties": false,
            "required": [
                "bindingModel",
                "immediateDataLayoutModel",
                "bindings",
                "internalReservations",
                "storageBufferSizes"
            ],
            "properties": {
                "bindingModel": {
                    "const": "vgpu-metal-binding-slots-v1"
                },
                "immediateDataLayoutModel": {
                    "description": "Versioned positions of internal roles inside the shared stage-local immediate-data block.",
                    "const": "vgpu-metal-immediate-data-layout-v1"
                },
                "bindings": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/$defs/binding"
                    }
                },
                "internalReservations": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 1,
                    "items": {
                        "$ref": "#/$defs/internalReservation"
                    }
                },
                "storageBufferSizes": {
                    "$ref": "#/$defs/storageBufferSizes"
                }
            }
        }
    }
}
