#include "stdafx.h"
#include "Pipeline.h"
#include "Core/Exception.h"
#include "Utils/Bitwise.h"

namespace gui {

	Pipeline::Pipeline(Array<Nat> *bufferSizes, Nat textureCount) {
		buffers = runtime::allocArray<CBuffer *>(engine(), &pointerArrayType, bufferSizes->count());
		for (Nat i = 0; i < buffers->count(); i++) {
			// Round up to 16 bytes, since GPUs tend to work with 4-float vectors.
			Nat sz = roundUp(bufferSizes->at(i), Nat(16));
			buffers->v[i] = runtime::allocArray<Byte>(engine(), &byteArrayType, sz);
		}

		if (textureCount == 0) {
			textures = null;
		} else {
			textures = runtime::allocArray<Texture *>(engine(), &pointerArrayType, textureCount);
			for (Nat i = 0; i < textures->count(); i++) {
				textures->v[i] = new (this) Texture();
			}
		}
	}

	void Pipeline::throwBufferOutOfBounds(Nat i) const {
		throw new (this) ArrayError(i, Nat(buffers->count()));
	}

	void Pipeline::throwTextureOutOfBounds(Nat i) const {
		throw new (this) ArrayError(i, Nat(textures->count()));
	}

}
