All files / common rage.mjs

0% Statements 0/293
0% Branches 0/1
0% Functions 0/1
0% Lines 0/293

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
import {log} from './rage_modules/logger.mjs'
import {redTriangleSample} from './rage_modules/shader-samples.mjs'

import * as constants from './rage_modules/rage_constants.mjs'

import {ResourceManager} from './rage_modules/resource-manager.mjs'
import {Matrix} from './rage_modules/matrix.mjs'
import {Vector2, Vector3, Vector4} from './rage_modules/vector.mjs'
import {Material, Texture} from './rage_modules/material.mjs'
import {Vertex2D, Vertex3D} from './rage_modules/vertex.mjs'
import {Mesh} from './rage_modules/mesh.mjs'
import {Model} from './rage_modules/model.mjs'


class RAGE {
    constructor(){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        this.def = constants
        this.constants = constants
        this.vertexType = false
        this.viewMode = false
        this.shaderMode = false
        this.canvas = false
        this.context = false
        
        this.modules = new Array()
        this.pipelines = new Array()
        this.renderPassDescriptors = new Array()
        this.currentRenderPass = false
        this.currentEncoder = false
        this.adapter = false
        this.device = false
        this.presentationFormat = false
        
        this.materials = new Array()

        this.resourceManager = false
    }

    async isSupported(){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        const adapter = await navigator.gpu?.requestAdapter()
        const device = await adapter?.requestDevice()   
        if(adapter && device) return true
        return false
    }

    log(text){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        log(text)
    }

    async init(canvas){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        log('Checking for Rage Support')
        if(!this.isSupported()){
            log('Rage is not supported on this browser!')
            return false
        }
        log('Rage is supported')
        log('Setting canvas with Id: ' + canvas.id)
        this.canvas = canvas
        log('Initialising Rage')
        
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        log(canvas)
        this.canvas = canvas
        
        this.adapter = await navigator.gpu?.requestAdapter()
        log(this.adaptor)
        this.device = await this.adapter?.requestDevice()   
        log(this.device)
        if(!this.adapter || !this.device){
            log('WebGPU Not Supported in this browser')
            return false
        }

        this.context = canvas.getContext('webgpu')
        log(this.context)
        this.presentationFormat = await navigator.gpu.getPreferredCanvasFormat()
        log(this.presentationFormat)
        this.context.configure({
            device: this.device,
            format: this.presentationFormat,
        })

        
        this.resourceManager = new ResourceManager()
        
        log('Rage Init() Completed!')
        return this
    }

    createMaterial(name){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        let material = new Material()
        material.name = name
        let id = this.materials.length
        material.id = id
        this.materials.push(material)   
        return id
    }

    setVertexType(type){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        if(this.def.defExists(type))
            this.vertexType = type
        return type
    }

    setViewMode(mode){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        this.viewMode = mode
    }

    setShaderMode(mode){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        this.shaderMode = mode
    }

    createMatrix() {
        // CODE: COMPLETE
        // UNIT: TRUE
        // DOCS: FALSE
        return new Matrix()
    }

    createVector2d(x,y){
        // CODE: COMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        return new Vector2(x,y)
    }
    createVector3d(x,y,z){
        // CODE: COMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        return new Vector3(x,y,z)
    }
    createVector4d(x,y,z,a){
        // CODE: COMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        return new Vector4(x,y,z,a)
    }

    

    getPipeline(id) { 
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        return this.pipelines[id]
    }
    getRenderPassDescriptor(id) { 
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        return this.renderPassDescriptors[id]
    }
    getModule(id) { 
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        return this.modules[id]
    }

    async createShaderModule(shaderSrc){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        log('Creating Shader Module')
        let newModule = await this.device.createShaderModule(shaderSrc)
        let id = this.modules.length
        this.modules.push(newModule)
        return id
    }


    async createPipeline(label, moduleId, vsEntry, fsEntry){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        log('creating pipeline')
        const pipeline = await this.device.createRenderPipeline({
            label: label,
            layout: 'auto',
            vertex: {
            entryPoint: vsEntry,
            module: this.modules[moduleId],
            },
            fragment: {
            entryPoint: fsEntry,
            module: this.modules[moduleId],
            targets: [{ format: this.presentationFormat }],
            },
        })
        let id = this.pipelines.length
        this.pipelines.push(pipeline)
        return id
    }

    async createRenderPassDescriptor(label, clearColor4f){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        log('Creating renderPassDescriptor')
        const renderPassDescriptor = {
            label: label,
            colorAttachments: [
                {
                // view: <- to be filled out when we render
                clearValue: clearColor4f,
                loadOp: 'clear',
                storeOp: 'store',
                },
            ],
        }
        let id = this.renderPassDescriptors.length
        this.renderPassDescriptors.push(renderPassDescriptor)
        return id

    }


    createRenderPass(renderPassDescriptorId){
        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        if(this.currentRenderPass){
            log('renderPass already exists - using existing Render Pass')
            return this.currentRenderPass
        }

        this.renderPassDescriptors[renderPassDescriptorId].colorAttachments[0].view =
            this.context.getCurrentTexture().createView()
        
        this.currentEncoder = this.device.createCommandEncoder({ label: 'our encoder' })
        this.currentRenderPass = this.currentEncoder.beginRenderPass(this.renderPassDescriptors[renderPassDescriptorId])
        return this.currentRenderPass


    }

    endRenderPass() {

        // CODE: INCOMPLETE
        // UNIT: FALSE
        // DOCS: FALSE
        this.currentRenderPass.end()
    
        const commandBuffer = this.currentEncoder.finish()
        this.device.queue.submit([commandBuffer])

        this.currentRenderPass = false
    }
}



export {
    RAGE, 
    log, 
    Matrix, 
    Vector2, 
    Vector3, 
    Vector4,
    Material,
    Texture, 
    Vertex2D,
    Vertex3D,
    Mesh,
    Model,
    redTriangleSample
}